Debian Codegraph @

gcc/tree-vect-stmts.cc

raw
1 /* Statement Analysis and Transformation for Vectorization
2 Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <[email protected]>
4 and Ira Rosen <[email protected]>
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 stmt_vectype (class _stmt_vec_info *stmt_info)
69 {
70 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 stmt_in_inner_loop_p (vec_info *vinfo, class _stmt_vec_info *stmt_info)
77 {
78 gimple *stmt = STMT_VINFO_STMT (stmt_info);
79 basic_block bb = gimple_bb (stmt);
80 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
81 class loop* loop;
82
83 if (!loop_vinfo)
84 return false;
85
86 loop = LOOP_VINFO_LOOP (loop_vinfo);
87
88 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 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 if ((kind == vector_load || kind == unaligned_load)
103 && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
104 kind = vector_gather_load;
105 if ((kind == vector_store || kind == unaligned_store)
106 && (stmt_info && STMT_VINFO_GATHER_SCATTER_P (stmt_info)))
107 kind = vector_scatter_store;
108
109 stmt_info_for_cost si
110 = { count, kind, where, stmt_info, node, vectype, misalign };
111 body_cost_vec->safe_push (si);
112
113 return (unsigned)
114 (builtin_vectorization_cost (kind, vectype, misalign) * count);
115 }
116
117 unsigned
118 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 return record_stmt_cost (body_cost_vec, count, kind, stmt_info, NULL,
124 vectype, misalign, where);
125 }
126
127 unsigned
128 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 return record_stmt_cost (body_cost_vec, count, kind, NULL, node,
134 vectype, misalign, where);
135 }
136
137 unsigned
138 record_stmt_cost (stmt_vector_for_cost *body_cost_vec, int count,
139 enum vect_cost_for_stmt kind,
140 enum vect_cost_model_location where)
141 {
142 gcc_assert (kind == cond_branch_taken || kind == cond_branch_not_taken
143 || kind == scalar_stmt);
144 return record_stmt_cost (body_cost_vec, count, kind, NULL, NULL,
145 NULL_TREE, 0, where);
146 }
147
148 /* Return a variable of type ELEM_TYPE[NELEMS]. */
149
150 static tree
151 create_vector_array (tree elem_type, unsigned HOST_WIDE_INT nelems)
152 {
153 return create_tmp_var (build_array_type_nelts (elem_type, nelems),
154 "vect_array");
155 }
156
157 /* ARRAY is an array of vectors created by create_vector_array.
158 Return an SSA_NAME for the vector in index N. The reference
159 is part of the vectorization of STMT_INFO and the vector is associated
160 with scalar destination SCALAR_DEST.
161 If we need to ensure that inactive elements are set to zero,
162 NEED_ZEROING is true, MASK contains the loop mask to be used. */
163
164 static tree
165 read_vector_array (vec_info *vinfo,
166 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
167 tree scalar_dest, tree array, unsigned HOST_WIDE_INT n,
168 bool need_zeroing, tree mask)
169 {
170 tree vect_type, vect, vect_name, tmp, tmp_name, array_ref;
171 gimple *new_stmt;
172
173 gcc_assert (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE);
174 vect_type = TREE_TYPE (TREE_TYPE (array));
175 tmp = vect_create_destination_var (scalar_dest, vect_type);
176 vect = vect_create_destination_var (scalar_dest, vect_type);
177 array_ref = build4 (ARRAY_REF, vect_type, array,
178 build_int_cst (size_type_node, n),
179 NULL_TREE, NULL_TREE);
180
181 new_stmt = gimple_build_assign (tmp, array_ref);
182 tmp_name = make_ssa_name (vect, new_stmt);
183 gimple_assign_set_lhs (new_stmt, tmp_name);
184 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
185
186 if (need_zeroing)
187 {
188 tree vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
189 vect_type);
190 vect_name = make_ssa_name (vect, new_stmt);
191 new_stmt
192 = gimple_build_assign (vect_name, VEC_COND_EXPR,
193 mask, tmp_name, vec_els);
194 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
195 }
196 else
197 vect_name = tmp_name;
198
199 return vect_name;
200 }
201
202 /* ARRAY is an array of vectors created by create_vector_array.
203 Emit code to store SSA_NAME VECT in index N of the array.
204 The store is part of the vectorization of STMT_INFO. */
205
206 static void
207 write_vector_array (vec_info *vinfo,
208 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
209 tree vect, tree array, unsigned HOST_WIDE_INT n)
210 {
211 tree array_ref;
212 gimple *new_stmt;
213
214 array_ref = build4 (ARRAY_REF, TREE_TYPE (vect), array,
215 build_int_cst (size_type_node, n),
216 NULL_TREE, NULL_TREE);
217
218 new_stmt = gimple_build_assign (array_ref, vect);
219 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
220 }
221
222 /* PTR is a pointer to an array of type TYPE. Return a representation
223 of *PTR. The memory reference replaces those in FIRST_DR
224 (and its group). */
225
226 static tree
227 create_array_ref (tree type, tree ptr, tree alias_ptr_type)
228 {
229 tree mem_ref;
230
231 mem_ref = build2 (MEM_REF, type, ptr, build_int_cst (alias_ptr_type, 0));
232 /* Arrays have the same alignment as their type. */
233 set_ptr_info_alignment (get_ptr_info (ptr), TYPE_ALIGN_UNIT (type), 0);
234 return mem_ref;
235 }
236
237 /* Add a clobber of variable VAR to the vectorization of STMT_INFO.
238 Emit the clobber before *GSI. */
239
240 static void
241 vect_clobber_variable (vec_info *vinfo, stmt_vec_info stmt_info,
242 gimple_stmt_iterator *gsi, tree var)
243 {
244 tree clobber = build_clobber (TREE_TYPE (var));
245 gimple *new_stmt = gimple_build_assign (var, clobber);
246 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
247 }
248
249 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
250
251 /* Function vect_mark_relevant.
252
253 Mark STMT_INFO as "relevant for vectorization" and add it to WORKLIST. */
254
255 static void
256 vect_mark_relevant (vec<stmt_vec_info> *worklist, stmt_vec_info stmt_info,
257 enum vect_relevant relevant, bool live_p)
258 {
259 enum vect_relevant save_relevant = STMT_VINFO_RELEVANT (stmt_info);
260 bool save_live_p = STMT_VINFO_LIVE_P (stmt_info);
261
262 if (dump_enabled_p ())
263 dump_printf_loc (MSG_NOTE, vect_location,
264 "mark relevant %d, live %d: %G", relevant, live_p,
265 stmt_info->stmt);
266
267 /* If this stmt is an original stmt in a pattern, we might need to mark its
268 related pattern stmt instead of the original stmt. However, such stmts
269 may have their own uses that are not in any pattern, in such cases the
270 stmt itself should be marked. */
271 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
272 {
273 /* This is the last stmt in a sequence that was detected as a
274 pattern that can potentially be vectorized. Don't mark the stmt
275 as relevant/live because it's not going to be vectorized.
276 Instead mark the pattern-stmt that replaces it. */
277
278 if (dump_enabled_p ())
279 dump_printf_loc (MSG_NOTE, vect_location,
280 "last stmt in pattern. don't mark"
281 " relevant/live.\n");
282
283 stmt_vec_info old_stmt_info = stmt_info;
284 stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
285 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_info) == old_stmt_info);
286 save_relevant = STMT_VINFO_RELEVANT (stmt_info);
287 save_live_p = STMT_VINFO_LIVE_P (stmt_info);
288
289 if (live_p && relevant == vect_unused_in_scope)
290 {
291 if (dump_enabled_p ())
292 dump_printf_loc (MSG_NOTE, vect_location,
293 "vec_stmt_relevant_p: forcing live pattern stmt "
294 "relevant.\n");
295 relevant = vect_used_only_live;
296 }
297
298 if (dump_enabled_p ())
299 dump_printf_loc (MSG_NOTE, vect_location,
300 "mark relevant %d, live %d: %G", relevant, live_p,
301 stmt_info->stmt);
302 }
303
304 STMT_VINFO_LIVE_P (stmt_info) |= live_p;
305 if (relevant > STMT_VINFO_RELEVANT (stmt_info))
306 STMT_VINFO_RELEVANT (stmt_info) = relevant;
307
308 if (STMT_VINFO_RELEVANT (stmt_info) == save_relevant
309 && STMT_VINFO_LIVE_P (stmt_info) == save_live_p)
310 {
311 if (dump_enabled_p ())
312 dump_printf_loc (MSG_NOTE, vect_location,
313 "already marked relevant/live.\n");
314 return;
315 }
316
317 worklist->safe_push (stmt_info);
318 }
319
320
321 /* Function is_simple_and_all_uses_invariant
322
323 Return true if STMT_INFO is simple and all uses of it are invariant. */
324
325 bool
326 is_simple_and_all_uses_invariant (stmt_vec_info stmt_info,
327 loop_vec_info loop_vinfo)
328 {
329 tree op;
330 ssa_op_iter iter;
331
332 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
333 if (!stmt)
334 return false;
335
336 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
337 {
338 enum vect_def_type dt = vect_uninitialized_def;
339
340 if (!vect_is_simple_use (op, loop_vinfo, &dt))
341 {
342 if (dump_enabled_p ())
343 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
344 "use not simple.\n");
345 return false;
346 }
347
348 if (dt != vect_external_def && dt != vect_constant_def)
349 return false;
350 }
351 return true;
352 }
353
354 /* Function vect_stmt_relevant_p.
355
356 Return true if STMT_INFO, in the loop that is represented by LOOP_VINFO,
357 is "relevant for vectorization".
358
359 A stmt is considered "relevant for vectorization" if:
360 - it has uses outside the loop.
361 - it has vdefs (it alters memory).
362 - control stmts in the loop (except for the exit condition).
363 - it is an induction and we have multiple exits.
364
365 CHECKME: what other side effects would the vectorizer allow? */
366
367 static bool
368 vect_stmt_relevant_p (stmt_vec_info stmt_info, loop_vec_info loop_vinfo,
369 enum vect_relevant *relevant, bool *live_p)
370 {
371 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
372 ssa_op_iter op_iter;
373 imm_use_iterator imm_iter;
374 use_operand_p use_p;
375 def_operand_p def_p;
376
377 *relevant = vect_unused_in_scope;
378 *live_p = false;
379
380 /* cond stmt other than loop exit cond. */
381 gimple *stmt = STMT_VINFO_STMT (stmt_info);
382 if (is_ctrl_stmt (stmt)
383 && LOOP_VINFO_LOOP_IV_COND (loop_vinfo) != stmt
384 && (!loop->inner || gimple_bb (stmt)->loop_father == loop))
385 *relevant = vect_used_in_scope;
386
387 /* changing memory. */
388 if (gimple_code (stmt_info->stmt) != GIMPLE_PHI)
389 if (gimple_vdef (stmt_info->stmt)
390 && !gimple_clobber_p (stmt_info->stmt))
391 {
392 if (dump_enabled_p ())
393 dump_printf_loc (MSG_NOTE, vect_location,
394 "vec_stmt_relevant_p: stmt has vdefs.\n");
395 *relevant = vect_used_in_scope;
396 }
397
398 /* uses outside the loop. */
399 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt_info->stmt, op_iter, SSA_OP_DEF)
400 {
401 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, DEF_FROM_PTR (def_p))
402 {
403 basic_block bb = gimple_bb (USE_STMT (use_p));
404 if (!flow_bb_inside_loop_p (loop, bb))
405 {
406 if (is_gimple_debug (USE_STMT (use_p)))
407 continue;
408
409 if (dump_enabled_p ())
410 dump_printf_loc (MSG_NOTE, vect_location,
411 "vec_stmt_relevant_p: used out of loop.\n");
412
413 /* We expect all such uses to be in the loop exit phis
414 (because of loop closed form) */
415 gcc_assert (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI);
416
417 *live_p = true;
418 }
419 }
420 }
421
422 /* Check if it's a not live PHI and multiple exits. In this case
423 there will be a usage later on after peeling which is needed for the
424 alternate exit. */
425 if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
426 && is_a <gphi *> (stmt)
427 && gimple_bb (stmt) == LOOP_VINFO_LOOP (loop_vinfo)->header
428 && ((! VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info))
429 && ! *live_p)
430 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def))
431 {
432 if (dump_enabled_p ())
433 dump_printf_loc (MSG_NOTE, vect_location,
434 "vec_stmt_relevant_p: PHI forced live for "
435 "early break.\n");
436 LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo).safe_push (stmt_info);
437 *live_p = true;
438 }
439
440 if (*live_p && *relevant == vect_unused_in_scope
441 && !is_simple_and_all_uses_invariant (stmt_info, loop_vinfo))
442 {
443 if (dump_enabled_p ())
444 dump_printf_loc (MSG_NOTE, vect_location,
445 "vec_stmt_relevant_p: stmt live but not relevant.\n");
446 *relevant = vect_used_only_live;
447 }
448
449 return (*live_p || *relevant);
450 }
451
452
453 /* Function exist_non_indexing_operands_for_use_p
454
455 USE is one of the uses attached to STMT_INFO. Check if USE is
456 used in STMT_INFO for anything other than indexing an array. */
457
458 static bool
459 exist_non_indexing_operands_for_use_p (tree use, stmt_vec_info stmt_info)
460 {
461 tree operand;
462
463 /* USE corresponds to some operand in STMT. If there is no data
464 reference in STMT, then any operand that corresponds to USE
465 is not indexing an array. */
466 if (!STMT_VINFO_DATA_REF (stmt_info))
467 return true;
468
469 /* STMT has a data_ref. FORNOW this means that its of one of
470 the following forms:
471 -1- ARRAY_REF = var
472 -2- var = ARRAY_REF
473 (This should have been verified in analyze_data_refs).
474
475 'var' in the second case corresponds to a def, not a use,
476 so USE cannot correspond to any operands that are not used
477 for array indexing.
478
479 Therefore, all we need to check is if STMT falls into the
480 first case, and whether var corresponds to USE. */
481
482 gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
483 if (!assign || !gimple_assign_copy_p (assign))
484 {
485 gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
486 if (call && gimple_call_internal_p (call))
487 {
488 internal_fn ifn = gimple_call_internal_fn (call);
489 int mask_index = internal_fn_mask_index (ifn);
490 if (mask_index >= 0
491 && use == gimple_call_arg (call, mask_index))
492 return true;
493 int els_index = internal_fn_else_index (ifn);
494 if (els_index >= 0
495 && use == gimple_call_arg (call, els_index))
496 return true;
497 int stored_value_index = internal_fn_stored_value_index (ifn);
498 if (stored_value_index >= 0
499 && use == gimple_call_arg (call, stored_value_index))
500 return true;
501 if (internal_gather_scatter_fn_p (ifn)
502 && use == gimple_call_arg (call, 1))
503 return true;
504 }
505 return false;
506 }
507
508 if (TREE_CODE (gimple_assign_lhs (assign)) == SSA_NAME)
509 return false;
510 operand = gimple_assign_rhs1 (assign);
511 if (TREE_CODE (operand) != SSA_NAME)
512 return false;
513
514 if (operand == use)
515 return true;
516
517 return false;
518 }
519
520
521 /*
522 Function process_use.
523
524 Inputs:
525 - a USE in STMT_VINFO in a loop represented by LOOP_VINFO
526 - RELEVANT - enum value to be set in the STMT_VINFO of the stmt
527 that defined USE. This is done by calling mark_relevant and passing it
528 the WORKLIST (to add DEF_STMT to the WORKLIST in case it is relevant).
529 - FORCE is true if exist_non_indexing_operands_for_use_p check shouldn't
530 be performed.
531
532 Outputs:
533 Generally, LIVE_P and RELEVANT are used to define the liveness and
534 relevance info of the DEF_STMT of this USE:
535 STMT_VINFO_LIVE_P (DEF_stmt_vinfo) <-- live_p
536 STMT_VINFO_RELEVANT (DEF_stmt_vinfo) <-- relevant
537 Exceptions:
538 - case 1: If USE is used only for address computations (e.g. array indexing),
539 which does not need to be directly vectorized, then the liveness/relevance
540 of the respective DEF_STMT is left unchanged.
541 - case 2: If STMT_VINFO is a reduction phi and DEF_STMT is a reduction stmt,
542 we skip DEF_STMT cause it had already been processed.
543 - case 3: If DEF_STMT and STMT_VINFO are in different nests, then
544 "relevant" will be modified accordingly.
545
546 Return true if everything is as expected. Return false otherwise. */
547
548 static opt_result
549 process_use (stmt_vec_info stmt_vinfo, tree use, loop_vec_info loop_vinfo,
550 enum vect_relevant relevant, vec<stmt_vec_info> *worklist,
551 bool force)
552 {
553 stmt_vec_info dstmt_vinfo;
554 enum vect_def_type dt;
555
556 /* case 1: we are only interested in uses that need to be vectorized. Uses
557 that are used for address computation are not considered relevant. */
558 if (!force && !exist_non_indexing_operands_for_use_p (use, stmt_vinfo))
559 return opt_result::success ();
560
561 if (!vect_is_simple_use (use, loop_vinfo, &dt, &dstmt_vinfo))
562 return opt_result::failure_at (stmt_vinfo->stmt,
563 "not vectorized:"
564 " unsupported use in stmt.\n");
565
566 if (!dstmt_vinfo)
567 return opt_result::success ();
568
569 basic_block def_bb = gimple_bb (dstmt_vinfo->stmt);
570 basic_block bb = gimple_bb (stmt_vinfo->stmt);
571
572 /* case 2: A reduction phi (STMT) defined by a reduction stmt (DSTMT_VINFO).
573 We have to force the stmt live since the epilogue loop needs it to
574 continue computing the reduction. */
575 if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
576 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
577 && gimple_code (dstmt_vinfo->stmt) != GIMPLE_PHI
578 && STMT_VINFO_DEF_TYPE (dstmt_vinfo) == vect_reduction_def
579 && bb->loop_father == def_bb->loop_father)
580 {
581 if (dump_enabled_p ())
582 dump_printf_loc (MSG_NOTE, vect_location,
583 "reduc-stmt defining reduc-phi in the same nest.\n");
584 vect_mark_relevant (worklist, dstmt_vinfo, relevant, true);
585 return opt_result::success ();
586 }
587
588 /* case 3a: outer-loop stmt defining an inner-loop stmt:
589 outer-loop-header-bb:
590 d = dstmt_vinfo
591 inner-loop:
592 stmt # use (d)
593 outer-loop-tail-bb:
594 ... */
595 if (flow_loop_nested_p (def_bb->loop_father, bb->loop_father))
596 {
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_NOTE, vect_location,
599 "outer-loop def-stmt defining inner-loop stmt.\n");
600
601 switch (relevant)
602 {
603 case vect_unused_in_scope:
604 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_nested_cycle) ?
605 vect_used_in_scope : vect_unused_in_scope;
606 break;
607
608 case vect_used_in_outer_by_reduction:
609 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
610 relevant = vect_used_by_reduction;
611 break;
612
613 case vect_used_in_outer:
614 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) != vect_reduction_def);
615 relevant = vect_used_in_scope;
616 break;
617
618 case vect_used_in_scope:
619 break;
620
621 default:
622 gcc_unreachable ();
623 }
624 }
625
626 /* case 3b: inner-loop stmt defining an outer-loop stmt:
627 outer-loop-header-bb:
628 ...
629 inner-loop:
630 d = dstmt_vinfo
631 outer-loop-tail-bb (or outer-loop-exit-bb in double reduction):
632 stmt # use (d) */
633 else if (flow_loop_nested_p (bb->loop_father, def_bb->loop_father))
634 {
635 if (dump_enabled_p ())
636 dump_printf_loc (MSG_NOTE, vect_location,
637 "inner-loop def-stmt defining outer-loop stmt.\n");
638
639 switch (relevant)
640 {
641 case vect_unused_in_scope:
642 relevant = (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
643 || STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_double_reduction_def) ?
644 vect_used_in_outer_by_reduction : vect_unused_in_scope;
645 break;
646
647 case vect_used_by_reduction:
648 case vect_used_only_live:
649 relevant = vect_used_in_outer_by_reduction;
650 break;
651
652 case vect_used_in_scope:
653 relevant = vect_used_in_outer;
654 break;
655
656 default:
657 gcc_unreachable ();
658 }
659 }
660 /* We are also not interested in uses on loop PHI backedges that are
661 inductions. Otherwise we'll needlessly vectorize the IV increment
662 and cause hybrid SLP for SLP inductions. Unless the PHI is live
663 of course. */
664 else if (gimple_code (stmt_vinfo->stmt) == GIMPLE_PHI
665 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_induction_def
666 && ! STMT_VINFO_LIVE_P (stmt_vinfo)
667 && (PHI_ARG_DEF_FROM_EDGE (stmt_vinfo->stmt,
668 loop_latch_edge (bb->loop_father))
669 == use))
670 {
671 if (dump_enabled_p ())
672 dump_printf_loc (MSG_NOTE, vect_location,
673 "induction value on backedge.\n");
674 return opt_result::success ();
675 }
676
677
678 vect_mark_relevant (worklist, dstmt_vinfo, relevant, false);
679 return opt_result::success ();
680 }
681
682
683 /* Function vect_mark_stmts_to_be_vectorized.
684
685 Not all stmts in the loop need to be vectorized. For example:
686
687 for i...
688 for j...
689 1. T0 = i + j
690 2. T1 = a[T0]
691
692 3. j = j + 1
693
694 Stmt 1 and 3 do not need to be vectorized, because loop control and
695 addressing of vectorized data-refs are handled differently.
696
697 This pass detects such stmts. */
698
699 opt_result
700 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo, bool *fatal)
701 {
702 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
703 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
704 unsigned int nbbs = loop->num_nodes;
705 gimple_stmt_iterator si;
706 unsigned int i;
707 basic_block bb;
708 bool live_p;
709 enum vect_relevant relevant;
710
711 DUMP_VECT_SCOPE ("vect_mark_stmts_to_be_vectorized");
712
713 auto_vec<stmt_vec_info, 64> worklist;
714
715 /* 1. Init worklist. */
716 for (i = 0; i < nbbs; i++)
717 {
718 bb = bbs[i];
719 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
720 {
721 if (virtual_operand_p (gimple_phi_result (gsi_stmt (si))))
722 continue;
723 stmt_vec_info phi_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
724 if (dump_enabled_p ())
725 dump_printf_loc (MSG_NOTE, vect_location, "init: phi relevant? %G",
726 phi_info->stmt);
727
728 if (vect_stmt_relevant_p (phi_info, loop_vinfo, &relevant, &live_p))
729 vect_mark_relevant (&worklist, phi_info, relevant, live_p);
730 }
731 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
732 {
733 if (is_gimple_debug (gsi_stmt (si)))
734 continue;
735 stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (gsi_stmt (si));
736 if (dump_enabled_p ())
737 dump_printf_loc (MSG_NOTE, vect_location,
738 "init: stmt relevant? %G", stmt_info->stmt);
739
740 if (vect_stmt_relevant_p (stmt_info, loop_vinfo, &relevant, &live_p))
741 vect_mark_relevant (&worklist, stmt_info, relevant, live_p);
742 }
743 }
744
745 /* 2. Process_worklist */
746 while (worklist.length () > 0)
747 {
748 use_operand_p use_p;
749 ssa_op_iter iter;
750
751 stmt_vec_info stmt_vinfo = worklist.pop ();
752 if (dump_enabled_p ())
753 dump_printf_loc (MSG_NOTE, vect_location,
754 "worklist: examine stmt: %G", stmt_vinfo->stmt);
755
756 /* Examine the USEs of STMT. For each USE, mark the stmt that defines it
757 (DEF_STMT) as relevant/irrelevant according to the relevance property
758 of STMT. */
759 relevant = STMT_VINFO_RELEVANT (stmt_vinfo);
760
761 /* Generally, the relevance property of STMT (in STMT_VINFO_RELEVANT) is
762 propagated as is to the DEF_STMTs of its USEs.
763
764 One exception is when STMT has been identified as defining a reduction
765 variable; in this case we set the relevance to vect_used_by_reduction.
766 This is because we distinguish between two kinds of relevant stmts -
767 those that are used by a reduction computation, and those that are
768 (also) used by a regular computation. This allows us later on to
769 identify stmts that are used solely by a reduction, and therefore the
770 order of the results that they produce does not have to be kept. */
771
772 switch (STMT_VINFO_DEF_TYPE (stmt_vinfo))
773 {
774 case vect_reduction_def:
775 gcc_assert (relevant != vect_unused_in_scope);
776 if (relevant != vect_unused_in_scope
777 && relevant != vect_used_in_scope
778 && relevant != vect_used_by_reduction
779 && relevant != vect_used_only_live)
780 return opt_result::failure_at
781 (stmt_vinfo->stmt, "unsupported use of reduction.\n");
782 break;
783
784 case vect_nested_cycle:
785 if (relevant != vect_unused_in_scope
786 && relevant != vect_used_in_outer_by_reduction
787 && relevant != vect_used_in_outer)
788 return opt_result::failure_at
789 (stmt_vinfo->stmt, "unsupported use of nested cycle.\n");
790 break;
791
792 case vect_double_reduction_def:
793 if (relevant != vect_unused_in_scope
794 && relevant != vect_used_by_reduction
795 && relevant != vect_used_only_live)
796 return opt_result::failure_at
797 (stmt_vinfo->stmt, "unsupported use of double reduction.\n");
798 break;
799
800 default:
801 break;
802 }
803
804 if (is_pattern_stmt_p (stmt_vinfo))
805 {
806 /* Pattern statements are not inserted into the code, so
807 FOR_EACH_PHI_OR_STMT_USE optimizes their operands out, and we
808 have to scan the RHS or function arguments instead. */
809 if (gassign *assign = dyn_cast <gassign *> (stmt_vinfo->stmt))
810 {
811 enum tree_code rhs_code = gimple_assign_rhs_code (assign);
812 tree op = gimple_assign_rhs1 (assign);
813
814 i = 1;
815 if (rhs_code == COND_EXPR && COMPARISON_CLASS_P (op))
816 {
817 opt_result res
818 = process_use (stmt_vinfo, TREE_OPERAND (op, 0),
819 loop_vinfo, relevant, &worklist, false);
820 if (!res)
821 return res;
822 res = process_use (stmt_vinfo, TREE_OPERAND (op, 1),
823 loop_vinfo, relevant, &worklist, false);
824 if (!res)
825 return res;
826 i = 2;
827 }
828 for (; i < gimple_num_ops (assign); i++)
829 {
830 op = gimple_op (assign, i);
831 if (TREE_CODE (op) == SSA_NAME)
832 {
833 opt_result res
834 = process_use (stmt_vinfo, op, loop_vinfo, relevant,
835 &worklist, false);
836 if (!res)
837 return res;
838 }
839 }
840 }
841 else if (gcond *cond = dyn_cast <gcond *> (stmt_vinfo->stmt))
842 {
843 tree_code rhs_code = gimple_cond_code (cond);
844 gcc_assert (TREE_CODE_CLASS (rhs_code) == tcc_comparison);
845 opt_result res
846 = process_use (stmt_vinfo, gimple_cond_lhs (cond),
847 loop_vinfo, relevant, &worklist, false);
848 if (!res)
849 return res;
850 res = process_use (stmt_vinfo, gimple_cond_rhs (cond),
851 loop_vinfo, relevant, &worklist, false);
852 if (!res)
853 return res;
854 }
855 else if (gcall *call = dyn_cast <gcall *> (stmt_vinfo->stmt))
856 {
857 for (i = 0; i < gimple_call_num_args (call); i++)
858 {
859 tree arg = gimple_call_arg (call, i);
860 opt_result res
861 = process_use (stmt_vinfo, arg, loop_vinfo, relevant,
862 &worklist, false);
863 if (!res)
864 return res;
865 }
866 }
867 else
868 gcc_unreachable ();
869 }
870 else
871 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt_vinfo->stmt, iter, SSA_OP_USE)
872 {
873 tree op = USE_FROM_PTR (use_p);
874 opt_result res
875 = process_use (stmt_vinfo, op, loop_vinfo, relevant,
876 &worklist, false);
877 if (!res)
878 return res;
879 }
880
881 if (STMT_VINFO_GATHER_SCATTER_P (stmt_vinfo))
882 {
883 gather_scatter_info gs_info;
884 if (!vect_check_gather_scatter (stmt_vinfo, loop_vinfo, &gs_info))
885 gcc_unreachable ();
886 opt_result res
887 = process_use (stmt_vinfo, gs_info.offset, loop_vinfo, relevant,
888 &worklist, true);
889 if (!res)
890 {
891 if (fatal)
892 *fatal = false;
893 return res;
894 }
895 }
896 } /* while worklist */
897
898 return opt_result::success ();
899 }
900
901 /* Function vect_model_simple_cost.
902
903 Models cost for simple operations, i.e. those that only emit ncopies of a
904 single op. Right now, this does not account for multiple insns that could
905 be generated for the single vector op. We will handle that shortly. */
906
907 static void
908 vect_model_simple_cost (vec_info *,
909 stmt_vec_info stmt_info, int ncopies,
910 enum vect_def_type *dt,
911 int ndts,
912 slp_tree node,
913 stmt_vector_for_cost *cost_vec,
914 vect_cost_for_stmt kind = vector_stmt)
915 {
916 int inside_cost = 0, prologue_cost = 0;
917
918 gcc_assert (cost_vec != NULL);
919
920 /* ??? Somehow we need to fix this at the callers. */
921 if (node)
922 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (node);
923
924 if (!node)
925 /* Cost the "broadcast" of a scalar operand in to a vector operand.
926 Use scalar_to_vec to cost the broadcast, as elsewhere in the vector
927 cost model. */
928 for (int i = 0; i < ndts; i++)
929 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
930 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
931 stmt_info, 0, vect_prologue);
932
933 /* Pass the inside-of-loop statements to the target-specific cost model. */
934 inside_cost += record_stmt_cost (cost_vec, ncopies, kind,
935 stmt_info, 0, vect_body);
936
937 if (dump_enabled_p ())
938 dump_printf_loc (MSG_NOTE, vect_location,
939 "vect_model_simple_cost: inside_cost = %d, "
940 "prologue_cost = %d .\n", inside_cost, prologue_cost);
941 }
942
943
944 /* Model cost for type demotion and promotion operations. PWR is
945 normally zero for single-step promotions and demotions. It will be
946 one if two-step promotion/demotion is required, and so on. NCOPIES
947 is the number of vector results (and thus number of instructions)
948 for the narrowest end of the operation chain. Each additional
949 step doubles the number of instructions required. If WIDEN_ARITH
950 is true the stmt is doing widening arithmetic. */
951
952 static void
953 vect_model_promotion_demotion_cost (stmt_vec_info stmt_info,
954 enum vect_def_type *dt,
955 unsigned int ncopies, int pwr,
956 stmt_vector_for_cost *cost_vec,
957 bool widen_arith)
958 {
959 int i;
960 int inside_cost = 0, prologue_cost = 0;
961
962 for (i = 0; i < pwr + 1; i++)
963 {
964 inside_cost += record_stmt_cost (cost_vec, ncopies,
965 widen_arith
966 ? vector_stmt : vec_promote_demote,
967 stmt_info, 0, vect_body);
968 ncopies *= 2;
969 }
970
971 /* FORNOW: Assuming maximum 2 args per stmts. */
972 for (i = 0; i < 2; i++)
973 if (dt[i] == vect_constant_def || dt[i] == vect_external_def)
974 prologue_cost += record_stmt_cost (cost_vec, 1, vector_stmt,
975 stmt_info, 0, vect_prologue);
976
977 if (dump_enabled_p ())
978 dump_printf_loc (MSG_NOTE, vect_location,
979 "vect_model_promotion_demotion_cost: inside_cost = %d, "
980 "prologue_cost = %d .\n", inside_cost, prologue_cost);
981 }
982
983 /* Returns true if the current function returns DECL. */
984
985 static bool
986 cfun_returns (tree decl)
987 {
988 edge_iterator ei;
989 edge e;
990 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
991 {
992 greturn *ret = safe_dyn_cast <greturn *> (*gsi_last_bb (e->src));
993 if (!ret)
994 continue;
995 if (gimple_return_retval (ret) == decl)
996 return true;
997 /* We often end up with an aggregate copy to the result decl,
998 handle that case as well. First skip intermediate clobbers
999 though. */
1000 gimple *def = ret;
1001 do
1002 {
1003 def = SSA_NAME_DEF_STMT (gimple_vuse (def));
1004 }
1005 while (gimple_clobber_p (def));
1006 if (is_a <gassign *> (def)
1007 && gimple_assign_lhs (def) == gimple_return_retval (ret)
1008 && gimple_assign_rhs1 (def) == decl)
1009 return true;
1010 }
1011 return false;
1012 }
1013
1014 /* Calculate cost of DR's memory access. */
1015 void
1016 vect_get_store_cost (vec_info *, stmt_vec_info stmt_info, slp_tree slp_node,
1017 int ncopies, dr_alignment_support alignment_support_scheme,
1018 int misalignment,
1019 unsigned int *inside_cost,
1020 stmt_vector_for_cost *body_cost_vec)
1021 {
1022 switch (alignment_support_scheme)
1023 {
1024 case dr_aligned:
1025 {
1026 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1027 vector_store, stmt_info, slp_node, 0,
1028 vect_body);
1029
1030 if (dump_enabled_p ())
1031 dump_printf_loc (MSG_NOTE, vect_location,
1032 "vect_model_store_cost: aligned.\n");
1033 break;
1034 }
1035
1036 case dr_unaligned_supported:
1037 {
1038 /* Here, we assign an additional cost for the unaligned store. */
1039 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1040 unaligned_store, stmt_info, slp_node,
1041 misalignment, vect_body);
1042 if (dump_enabled_p ())
1043 dump_printf_loc (MSG_NOTE, vect_location,
1044 "vect_model_store_cost: unaligned supported by "
1045 "hardware.\n");
1046 break;
1047 }
1048
1049 case dr_unaligned_unsupported:
1050 {
1051 *inside_cost = VECT_MAX_COST;
1052
1053 if (dump_enabled_p ())
1054 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1055 "vect_model_store_cost: unsupported access.\n");
1056 break;
1057 }
1058
1059 default:
1060 gcc_unreachable ();
1061 }
1062 }
1063
1064 /* Calculate cost of DR's memory access. */
1065 void
1066 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 switch (alignment_support_scheme)
1076 {
1077 case dr_aligned:
1078 {
1079 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1080 stmt_info, slp_node, 0, vect_body);
1081
1082 if (dump_enabled_p ())
1083 dump_printf_loc (MSG_NOTE, vect_location,
1084 "vect_model_load_cost: aligned.\n");
1085
1086 break;
1087 }
1088 case dr_unaligned_supported:
1089 {
1090 /* Here, we assign an additional cost for the unaligned load. */
1091 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1092 unaligned_load, stmt_info, slp_node,
1093 misalignment, vect_body);
1094
1095 if (dump_enabled_p ())
1096 dump_printf_loc (MSG_NOTE, vect_location,
1097 "vect_model_load_cost: unaligned supported by "
1098 "hardware.\n");
1099
1100 break;
1101 }
1102 case dr_explicit_realign:
1103 {
1104 *inside_cost += record_stmt_cost (body_cost_vec, ncopies * 2,
1105 vector_load, stmt_info, slp_node, 0,
1106 vect_body);
1107 *inside_cost += record_stmt_cost (body_cost_vec, ncopies,
1108 vec_perm, stmt_info, slp_node, 0,
1109 vect_body);
1110
1111 /* FIXME: If the misalignment remains fixed across the iterations of
1112 the containing loop, the following cost should be added to the
1113 prologue costs. */
1114 if (targetm.vectorize.builtin_mask_for_load)
1115 *inside_cost += record_stmt_cost (body_cost_vec, 1, vector_stmt,
1116 stmt_info, slp_node, 0, vect_body);
1117
1118 if (dump_enabled_p ())
1119 dump_printf_loc (MSG_NOTE, vect_location,
1120 "vect_model_load_cost: explicit realign\n");
1121
1122 break;
1123 }
1124 case dr_explicit_realign_optimized:
1125 {
1126 if (dump_enabled_p ())
1127 dump_printf_loc (MSG_NOTE, vect_location,
1128 "vect_model_load_cost: unaligned software "
1129 "pipelined.\n");
1130
1131 /* Unaligned software pipeline has a load of an address, an initial
1132 load, and possibly a mask operation to "prime" the loop. However,
1133 if this is an access in a group of loads, which provide grouped
1134 access, then the above cost should only be considered for one
1135 access in the group. Inside the loop, there is a load op
1136 and a realignment op. */
1137
1138 if (add_realign_cost && record_prologue_costs)
1139 {
1140 *prologue_cost += record_stmt_cost (prologue_cost_vec, 2,
1141 vector_stmt, stmt_info,
1142 slp_node, 0, vect_prologue);
1143 if (targetm.vectorize.builtin_mask_for_load)
1144 *prologue_cost += record_stmt_cost (prologue_cost_vec, 1,
1145 vector_stmt, stmt_info,
1146 slp_node, 0, vect_prologue);
1147 }
1148
1149 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vector_load,
1150 stmt_info, slp_node, 0, vect_body);
1151 *inside_cost += record_stmt_cost (body_cost_vec, ncopies, vec_perm,
1152 stmt_info, slp_node, 0, vect_body);
1153
1154 if (dump_enabled_p ())
1155 dump_printf_loc (MSG_NOTE, vect_location,
1156 "vect_model_load_cost: explicit realign optimized"
1157 "\n");
1158
1159 break;
1160 }
1161
1162 case dr_unaligned_unsupported:
1163 {
1164 *inside_cost = VECT_MAX_COST;
1165
1166 if (dump_enabled_p ())
1167 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1168 "vect_model_load_cost: unsupported access.\n");
1169 break;
1170 }
1171
1172 default:
1173 gcc_unreachable ();
1174 }
1175 }
1176
1177 /* Insert the new stmt NEW_STMT at *GSI or at the appropriate place in
1178 the loop preheader for the vectorized stmt STMT_VINFO. */
1179
1180 static void
1181 vect_init_vector_1 (vec_info *vinfo, stmt_vec_info stmt_vinfo, gimple *new_stmt,
1182 gimple_stmt_iterator *gsi)
1183 {
1184 if (gsi)
1185 vect_finish_stmt_generation (vinfo, stmt_vinfo, new_stmt, gsi);
1186 else
1187 vinfo->insert_on_entry (stmt_vinfo, new_stmt);
1188
1189 if (dump_enabled_p ())
1190 dump_printf_loc (MSG_NOTE, vect_location,
1191 "created new init_stmt: %G", new_stmt);
1192 }
1193
1194 /* Function vect_init_vector.
1195
1196 Insert a new stmt (INIT_STMT) that initializes a new variable of type
1197 TYPE with the value VAL. If TYPE is a vector type and VAL does not have
1198 vector type a vector with all elements equal to VAL is created first.
1199 Place the initialization at GSI if it is not NULL. Otherwise, place the
1200 initialization at the loop preheader.
1201 Return the DEF of INIT_STMT.
1202 It will be used in the vectorization of STMT_INFO. */
1203
1204 tree
1205 vect_init_vector (vec_info *vinfo, stmt_vec_info stmt_info, tree val, tree type,
1206 gimple_stmt_iterator *gsi)
1207 {
1208 gimple *init_stmt;
1209 tree new_temp;
1210
1211 /* We abuse this function to push sth to a SSA name with initial 'val'. */
1212 if (! useless_type_conversion_p (type, TREE_TYPE (val)))
1213 {
1214 gcc_assert (VECTOR_TYPE_P (type));
1215 if (! types_compatible_p (TREE_TYPE (type), TREE_TYPE (val)))
1216 {
1217 /* Scalar boolean value should be transformed into
1218 all zeros or all ones value before building a vector. */
1219 if (VECTOR_BOOLEAN_TYPE_P (type))
1220 {
1221 tree true_val = build_all_ones_cst (TREE_TYPE (type));
1222 tree false_val = build_zero_cst (TREE_TYPE (type));
1223
1224 if (CONSTANT_CLASS_P (val))
1225 val = integer_zerop (val) ? false_val : true_val;
1226 else
1227 {
1228 new_temp = make_ssa_name (TREE_TYPE (type));
1229 init_stmt = gimple_build_assign (new_temp, COND_EXPR,
1230 val, true_val, false_val);
1231 vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1232 val = new_temp;
1233 }
1234 }
1235 else
1236 {
1237 gimple_seq stmts = NULL;
1238 if (! INTEGRAL_TYPE_P (TREE_TYPE (val)))
1239 val = gimple_build (&stmts, VIEW_CONVERT_EXPR,
1240 TREE_TYPE (type), val);
1241 else
1242 /* ??? Condition vectorization expects us to do
1243 promotion of invariant/external defs. */
1244 val = gimple_convert (&stmts, TREE_TYPE (type), val);
1245 for (gimple_stmt_iterator gsi2 = gsi_start (stmts);
1246 !gsi_end_p (gsi2); )
1247 {
1248 init_stmt = gsi_stmt (gsi2);
1249 gsi_remove (&gsi2, false);
1250 vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1251 }
1252 }
1253 }
1254 val = build_vector_from_val (type, val);
1255 }
1256
1257 new_temp = vect_get_new_ssa_name (type, vect_simple_var, "cst_");
1258 init_stmt = gimple_build_assign (new_temp, val);
1259 vect_init_vector_1 (vinfo, stmt_info, init_stmt, gsi);
1260 return new_temp;
1261 }
1262
1263
1264 /* Function vect_get_vec_defs_for_operand.
1265
1266 OP is an operand in STMT_VINFO. This function returns a vector of
1267 NCOPIES defs that will be used in the vectorized stmts for STMT_VINFO.
1268
1269 In the case that OP is an SSA_NAME which is defined in the loop, then
1270 STMT_VINFO_VEC_STMTS of the defining stmt holds the relevant defs.
1271
1272 In case OP is an invariant or constant, a new stmt that creates a vector def
1273 needs to be introduced. VECTYPE may be used to specify a required type for
1274 vector invariant. */
1275
1276 void
1277 vect_get_vec_defs_for_operand (vec_info *vinfo, stmt_vec_info stmt_vinfo,
1278 unsigned ncopies,
1279 tree op, vec<tree> *vec_oprnds, tree vectype)
1280 {
1281 gimple *def_stmt;
1282 enum vect_def_type dt;
1283 bool is_simple_use;
1284 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
1285
1286 if (dump_enabled_p ())
1287 dump_printf_loc (MSG_NOTE, vect_location,
1288 "vect_get_vec_defs_for_operand: %T\n", op);
1289
1290 stmt_vec_info def_stmt_info;
1291 is_simple_use = vect_is_simple_use (op, loop_vinfo, &dt,
1292 &def_stmt_info, &def_stmt);
1293 gcc_assert (is_simple_use);
1294 if (def_stmt && dump_enabled_p ())
1295 dump_printf_loc (MSG_NOTE, vect_location, " def_stmt = %G", def_stmt);
1296
1297 vec_oprnds->create (ncopies);
1298 if (dt == vect_constant_def || dt == vect_external_def)
1299 {
1300 tree stmt_vectype = STMT_VINFO_VECTYPE (stmt_vinfo);
1301 tree vector_type;
1302
1303 if (vectype)
1304 vector_type = vectype;
1305 else if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op))
1306 && VECTOR_BOOLEAN_TYPE_P (stmt_vectype))
1307 vector_type = truth_type_for (stmt_vectype);
1308 else
1309 vector_type = get_vectype_for_scalar_type (loop_vinfo, TREE_TYPE (op));
1310
1311 gcc_assert (vector_type);
1312 /* A masked load can have a default SSA definition as else operand.
1313 We should "vectorize" this instead of creating a duplicate from the
1314 scalar default. */
1315 tree vop;
1316 if (TREE_CODE (op) == SSA_NAME
1317 && SSA_NAME_IS_DEFAULT_DEF (op)
1318 && VAR_P (SSA_NAME_VAR (op)))
1319 vop = get_or_create_ssa_default_def (cfun,
1320 create_tmp_var (vector_type));
1321 else
1322 vop = vect_init_vector (vinfo, stmt_vinfo, op, vector_type, NULL);
1323 while (ncopies--)
1324 vec_oprnds->quick_push (vop);
1325 }
1326 else
1327 {
1328 def_stmt_info = vect_stmt_to_vectorize (def_stmt_info);
1329 gcc_assert (STMT_VINFO_VEC_STMTS (def_stmt_info).length () == ncopies);
1330 for (unsigned i = 0; i < ncopies; ++i)
1331 vec_oprnds->quick_push (gimple_get_lhs
1332 (STMT_VINFO_VEC_STMTS (def_stmt_info)[i]));
1333 }
1334 }
1335
1336
1337 /* Get vectorized definitions for OP0 and OP1. */
1338
1339 void
1340 vect_get_vec_defs (vec_info *vinfo, stmt_vec_info stmt_info, slp_tree slp_node,
1341 unsigned ncopies,
1342 tree op0, tree vectype0, vec<tree> *vec_oprnds0,
1343 tree op1, tree vectype1, vec<tree> *vec_oprnds1,
1344 tree op2, tree vectype2, vec<tree> *vec_oprnds2,
1345 tree op3, tree vectype3, vec<tree> *vec_oprnds3)
1346 {
1347 if (slp_node)
1348 {
1349 if (op0)
1350 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_oprnds0);
1351 if (op1)
1352 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[1], vec_oprnds1);
1353 if (op2)
1354 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[2], vec_oprnds2);
1355 if (op3)
1356 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[3], vec_oprnds3);
1357 }
1358 else
1359 {
1360 if (op0)
1361 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1362 op0, vec_oprnds0, vectype0);
1363 if (op1)
1364 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1365 op1, vec_oprnds1, vectype1);
1366 if (op2)
1367 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1368 op2, vec_oprnds2, vectype2);
1369 if (op3)
1370 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
1371 op3, vec_oprnds3, vectype3);
1372 }
1373 }
1374
1375 void
1376 vect_get_vec_defs (vec_info *vinfo, stmt_vec_info stmt_info, slp_tree slp_node,
1377 unsigned ncopies,
1378 tree op0, vec<tree> *vec_oprnds0,
1379 tree op1, vec<tree> *vec_oprnds1,
1380 tree op2, vec<tree> *vec_oprnds2,
1381 tree op3, vec<tree> *vec_oprnds3)
1382 {
1383 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
1384 op0, NULL_TREE, vec_oprnds0,
1385 op1, NULL_TREE, vec_oprnds1,
1386 op2, NULL_TREE, vec_oprnds2,
1387 op3, NULL_TREE, vec_oprnds3);
1388 }
1389
1390 /* Helper function called by vect_finish_replace_stmt and
1391 vect_finish_stmt_generation. Set the location of the new
1392 statement and create and return a stmt_vec_info for it. */
1393
1394 static void
1395 vect_finish_stmt_generation_1 (vec_info *,
1396 stmt_vec_info stmt_info, gimple *vec_stmt)
1397 {
1398 if (dump_enabled_p ())
1399 dump_printf_loc (MSG_NOTE, vect_location, "add new stmt: %G", vec_stmt);
1400
1401 if (stmt_info)
1402 {
1403 gimple_set_location (vec_stmt, gimple_location (stmt_info->stmt));
1404
1405 /* While EH edges will generally prevent vectorization, stmt might
1406 e.g. be in a must-not-throw region. Ensure newly created stmts
1407 that could throw are part of the same region. */
1408 int lp_nr = lookup_stmt_eh_lp (stmt_info->stmt);
1409 if (lp_nr != 0 && stmt_could_throw_p (cfun, vec_stmt))
1410 add_stmt_to_eh_lp (vec_stmt, lp_nr);
1411 }
1412 else
1413 gcc_assert (!stmt_could_throw_p (cfun, vec_stmt));
1414 }
1415
1416 /* Replace the scalar statement STMT_INFO with a new vector statement VEC_STMT,
1417 which sets the same scalar result as STMT_INFO did. Create and return a
1418 stmt_vec_info for VEC_STMT. */
1419
1420 void
1421 vect_finish_replace_stmt (vec_info *vinfo,
1422 stmt_vec_info stmt_info, gimple *vec_stmt)
1423 {
1424 gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt;
1425 gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt));
1426
1427 gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt);
1428 gsi_replace (&gsi, vec_stmt, true);
1429
1430 vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1431 }
1432
1433 /* Add VEC_STMT to the vectorized implementation of STMT_INFO and insert it
1434 before *GSI. Create and return a stmt_vec_info for VEC_STMT. */
1435
1436 void
1437 vect_finish_stmt_generation (vec_info *vinfo,
1438 stmt_vec_info stmt_info, gimple *vec_stmt,
1439 gimple_stmt_iterator *gsi)
1440 {
1441 gcc_assert (!stmt_info || gimple_code (stmt_info->stmt) != GIMPLE_LABEL);
1442
1443 if (!gsi_end_p (*gsi)
1444 && gimple_has_mem_ops (vec_stmt))
1445 {
1446 gimple *at_stmt = gsi_stmt (*gsi);
1447 tree vuse = gimple_vuse (at_stmt);
1448 if (vuse && TREE_CODE (vuse) == SSA_NAME)
1449 {
1450 tree vdef = gimple_vdef (at_stmt);
1451 gimple_set_vuse (vec_stmt, gimple_vuse (at_stmt));
1452 gimple_set_modified (vec_stmt, true);
1453 /* If we have an SSA vuse and insert a store, update virtual
1454 SSA form to avoid triggering the renamer. Do so only
1455 if we can easily see all uses - which is what almost always
1456 happens with the way vectorized stmts are inserted. */
1457 if ((vdef && TREE_CODE (vdef) == SSA_NAME)
1458 && ((is_gimple_assign (vec_stmt)
1459 && !is_gimple_reg (gimple_assign_lhs (vec_stmt)))
1460 || (is_gimple_call (vec_stmt)
1461 && (!(gimple_call_flags (vec_stmt)
1462 & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
1463 || (gimple_call_lhs (vec_stmt)
1464 && !is_gimple_reg (gimple_call_lhs (vec_stmt)))))))
1465 {
1466 tree new_vdef = copy_ssa_name (vuse, vec_stmt);
1467 gimple_set_vdef (vec_stmt, new_vdef);
1468 SET_USE (gimple_vuse_op (at_stmt), new_vdef);
1469 }
1470 }
1471 }
1472 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
1473 vect_finish_stmt_generation_1 (vinfo, stmt_info, vec_stmt);
1474 }
1475
1476 /* We want to vectorize a call to combined function CFN with function
1477 decl FNDECL, using VECTYPE_OUT as the type of the output and VECTYPE_IN
1478 as the types of all inputs. Check whether this is possible using
1479 an internal function, returning its code if so or IFN_LAST if not. */
1480
1481 static internal_fn
1482 vectorizable_internal_function (combined_fn cfn, tree fndecl,
1483 tree vectype_out, tree vectype_in)
1484 {
1485 internal_fn ifn;
1486 if (internal_fn_p (cfn))
1487 ifn = as_internal_fn (cfn);
1488 else
1489 ifn = associated_internal_fn (fndecl);
1490 if (ifn != IFN_LAST && direct_internal_fn_p (ifn))
1491 {
1492 const direct_internal_fn_info &info = direct_internal_fn (ifn);
1493 if (info.vectorizable)
1494 {
1495 bool same_size_p = TYPE_SIZE (vectype_in) == TYPE_SIZE (vectype_out);
1496 tree type0 = (info.type0 < 0 ? vectype_out : vectype_in);
1497 tree type1 = (info.type1 < 0 ? vectype_out : vectype_in);
1498
1499 /* The type size of both the vectype_in and vectype_out should be
1500 exactly the same when vectype_out isn't participating the optab.
1501 While there is no restriction for type size when vectype_out
1502 is part of the optab query. */
1503 if (type0 != vectype_out && type1 != vectype_out && !same_size_p)
1504 return IFN_LAST;
1505
1506 if (direct_internal_fn_supported_p (ifn, tree_pair (type0, type1),
1507 OPTIMIZE_FOR_SPEED))
1508 return ifn;
1509 }
1510 }
1511 return IFN_LAST;
1512 }
1513
1514
1515 static tree permute_vec_elements (vec_info *, tree, tree, tree, stmt_vec_info,
1516 gimple_stmt_iterator *);
1517
1518 /* Check whether a load or store statement in the loop described by
1519 LOOP_VINFO is possible in a loop using partial vectors. This is
1520 testing whether the vectorizer pass has the appropriate support,
1521 as well as whether the target does.
1522
1523 VLS_TYPE says whether the statement is a load or store and VECTYPE
1524 is the type of the vector being loaded or stored. SLP_NODE is the SLP
1525 node that contains the statement, or null if none. MEMORY_ACCESS_TYPE
1526 says how the load or store is going to be implemented and GROUP_SIZE
1527 is the number of load or store statements in the containing group.
1528 If the access is a gather load or scatter store, GS_INFO describes
1529 its arguments. If the load or store is conditional, SCALAR_MASK is the
1530 condition under which it occurs.
1531
1532 Clear LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P if a loop using partial
1533 vectors is not supported, otherwise record the required rgroup control
1534 types.
1535
1536 If partial vectors can be used and ELSVALS is nonzero the supported
1537 else values will be added to the vector ELSVALS points to. */
1538
1539 static void
1540 check_load_store_for_partial_vectors (loop_vec_info loop_vinfo, tree vectype,
1541 slp_tree slp_node,
1542 vec_load_store_type vls_type,
1543 int group_size,
1544 vect_memory_access_type
1545 memory_access_type,
1546 gather_scatter_info *gs_info,
1547 tree scalar_mask,
1548 vec<int> *elsvals = nullptr)
1549 {
1550 /* Invariant loads need no special support. */
1551 if (memory_access_type == VMAT_INVARIANT)
1552 return;
1553
1554 unsigned int nvectors = vect_get_num_copies (loop_vinfo, slp_node, vectype);
1555 vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
1556 vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
1557 machine_mode vecmode = TYPE_MODE (vectype);
1558 bool is_load = (vls_type == VLS_LOAD);
1559 if (memory_access_type == VMAT_LOAD_STORE_LANES)
1560 {
1561 if (slp_node)
1562 nvectors /= group_size;
1563 internal_fn ifn
1564 = (is_load ? vect_load_lanes_supported (vectype, group_size, true,
1565 elsvals)
1566 : vect_store_lanes_supported (vectype, group_size, true));
1567 if (ifn == IFN_MASK_LEN_LOAD_LANES || ifn == IFN_MASK_LEN_STORE_LANES)
1568 vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1569 else if (ifn == IFN_MASK_LOAD_LANES || ifn == IFN_MASK_STORE_LANES)
1570 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1571 scalar_mask);
1572 else
1573 {
1574 if (dump_enabled_p ())
1575 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1576 "can't operate on partial vectors because"
1577 " the target doesn't have an appropriate"
1578 " load/store-lanes instruction.\n");
1579 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1580 }
1581 return;
1582 }
1583
1584 if (memory_access_type == VMAT_GATHER_SCATTER)
1585 {
1586 internal_fn ifn = (is_load
1587 ? IFN_MASK_GATHER_LOAD
1588 : IFN_MASK_SCATTER_STORE);
1589 internal_fn len_ifn = (is_load
1590 ? IFN_MASK_LEN_GATHER_LOAD
1591 : IFN_MASK_LEN_SCATTER_STORE);
1592 if (internal_gather_scatter_fn_supported_p (len_ifn, vectype,
1593 gs_info->memory_type,
1594 gs_info->offset_vectype,
1595 gs_info->scale,
1596 elsvals))
1597 vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, 1);
1598 else if (internal_gather_scatter_fn_supported_p (ifn, vectype,
1599 gs_info->memory_type,
1600 gs_info->offset_vectype,
1601 gs_info->scale,
1602 elsvals))
1603 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype,
1604 scalar_mask);
1605 else
1606 {
1607 if (dump_enabled_p ())
1608 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1609 "can't operate on partial vectors because"
1610 " the target doesn't have an appropriate"
1611 " gather load or scatter store instruction.\n");
1612 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1613 }
1614 return;
1615 }
1616
1617 if (memory_access_type != VMAT_CONTIGUOUS
1618 && memory_access_type != VMAT_CONTIGUOUS_PERMUTE)
1619 {
1620 /* Element X of the data must come from iteration i * VF + X of the
1621 scalar loop. We need more work to support other mappings. */
1622 if (dump_enabled_p ())
1623 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1624 "can't operate on partial vectors because an"
1625 " access isn't contiguous.\n");
1626 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1627 return;
1628 }
1629
1630 if (!VECTOR_MODE_P (vecmode))
1631 {
1632 if (dump_enabled_p ())
1633 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1634 "can't operate on partial vectors when emulating"
1635 " vector operations.\n");
1636 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1637 return;
1638 }
1639
1640 /* We might load more scalars than we need for permuting SLP loads.
1641 We checked in get_group_load_store_type that the extra elements
1642 don't leak into a new vector. */
1643 auto group_memory_nvectors = [](poly_uint64 size, poly_uint64 nunits)
1644 {
1645 unsigned int nvectors;
1646 if (can_div_away_from_zero_p (size, nunits, &nvectors))
1647 return nvectors;
1648 gcc_unreachable ();
1649 };
1650
1651 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1652 poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1653 machine_mode mask_mode;
1654 machine_mode vmode;
1655 bool using_partial_vectors_p = false;
1656 if (get_len_load_store_mode
1657 (vecmode, is_load, nullptr, elsvals).exists (&vmode))
1658 {
1659 nvectors = group_memory_nvectors (group_size * vf, nunits);
1660 unsigned factor = (vecmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vecmode);
1661 vect_record_loop_len (loop_vinfo, lens, nvectors, vectype, factor);
1662 using_partial_vectors_p = true;
1663 }
1664 else if (targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
1665 && can_vec_mask_load_store_p (vecmode, mask_mode, is_load, NULL,
1666 elsvals))
1667 {
1668 nvectors = group_memory_nvectors (group_size * vf, nunits);
1669 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype, scalar_mask);
1670 using_partial_vectors_p = true;
1671 }
1672
1673 if (!using_partial_vectors_p)
1674 {
1675 if (dump_enabled_p ())
1676 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1677 "can't operate on partial vectors because the"
1678 " target doesn't have the appropriate partial"
1679 " vectorization load or store.\n");
1680 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
1681 }
1682 }
1683
1684 /* Return the mask input to a masked load or store. VEC_MASK is the vectorized
1685 form of the scalar mask condition and LOOP_MASK, if nonnull, is the mask
1686 that needs to be applied to all loads and stores in a vectorized loop.
1687 Return VEC_MASK if LOOP_MASK is null or if VEC_MASK is already masked,
1688 otherwise return VEC_MASK & LOOP_MASK.
1689
1690 MASK_TYPE is the type of both masks. If new statements are needed,
1691 insert them before GSI. */
1692
1693 tree
1694 prepare_vec_mask (loop_vec_info loop_vinfo, tree mask_type, tree loop_mask,
1695 tree vec_mask, gimple_stmt_iterator *gsi)
1696 {
1697 gcc_assert (useless_type_conversion_p (mask_type, TREE_TYPE (vec_mask)));
1698 if (!loop_mask)
1699 return vec_mask;
1700
1701 gcc_assert (TREE_TYPE (loop_mask) == mask_type);
1702
1703 if (loop_vinfo->vec_cond_masked_set.contains ({ vec_mask, loop_mask }))
1704 return vec_mask;
1705
1706 tree and_res = make_temp_ssa_name (mask_type, NULL, "vec_mask_and");
1707 gimple *and_stmt = gimple_build_assign (and_res, BIT_AND_EXPR,
1708 vec_mask, loop_mask);
1709
1710 gsi_insert_before (gsi, and_stmt, GSI_SAME_STMT);
1711 return and_res;
1712 }
1713
1714 /* Determine whether we can use a gather load or scatter store to vectorize
1715 strided load or store STMT_INFO by truncating the current offset to a
1716 smaller width. We need to be able to construct an offset vector:
1717
1718 { 0, X, X*2, X*3, ... }
1719
1720 without loss of precision, where X is STMT_INFO's DR_STEP.
1721
1722 Return true if this is possible, describing the gather load or scatter
1723 store in GS_INFO. MASKED_P is true if the load or store is conditional.
1724
1725 If we can use gather/scatter and ELSVALS is nonzero the supported
1726 else values will be stored in the vector ELSVALS points to. */
1727
1728 static bool
1729 vect_truncate_gather_scatter_offset (stmt_vec_info stmt_info,
1730 loop_vec_info loop_vinfo, bool masked_p,
1731 gather_scatter_info *gs_info,
1732 vec<int> *elsvals)
1733 {
1734 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1735 data_reference *dr = dr_info->dr;
1736 tree step = DR_STEP (dr);
1737 if (TREE_CODE (step) != INTEGER_CST)
1738 {
1739 /* ??? Perhaps we could use range information here? */
1740 if (dump_enabled_p ())
1741 dump_printf_loc (MSG_NOTE, vect_location,
1742 "cannot truncate variable step.\n");
1743 return false;
1744 }
1745
1746 /* Get the number of bits in an element. */
1747 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1748 scalar_mode element_mode = SCALAR_TYPE_MODE (TREE_TYPE (vectype));
1749 unsigned int element_bits = GET_MODE_BITSIZE (element_mode);
1750
1751 /* Set COUNT to the upper limit on the number of elements - 1.
1752 Start with the maximum vectorization factor. */
1753 unsigned HOST_WIDE_INT count = vect_max_vf (loop_vinfo) - 1;
1754
1755 /* Try lowering COUNT to the number of scalar latch iterations. */
1756 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1757 widest_int max_iters;
1758 if (max_loop_iterations (loop, &max_iters)
1759 && max_iters < count)
1760 count = max_iters.to_shwi ();
1761
1762 /* Try scales of 1 and the element size. */
1763 unsigned int scales[] = { 1, vect_get_scalar_dr_size (dr_info) };
1764 wi::overflow_type overflow = wi::OVF_NONE;
1765 for (int i = 0; i < 2; ++i)
1766 {
1767 unsigned int scale = scales[i];
1768 widest_int factor;
1769 if (!wi::multiple_of_p (wi::to_widest (step), scale, SIGNED, &factor))
1770 continue;
1771
1772 /* Determine the minimum precision of (COUNT - 1) * STEP / SCALE. */
1773 widest_int range = wi::mul (count, factor, SIGNED, &overflow);
1774 if (overflow)
1775 continue;
1776 signop sign = range >= 0 ? UNSIGNED : SIGNED;
1777 unsigned int min_offset_bits = wi::min_precision (range, sign);
1778
1779 /* Find the narrowest viable offset type. */
1780 unsigned int offset_bits = 1U << ceil_log2 (min_offset_bits);
1781 tree offset_type = build_nonstandard_integer_type (offset_bits,
1782 sign == UNSIGNED);
1783
1784 /* See whether the target supports the operation with an offset
1785 no narrower than OFFSET_TYPE. */
1786 tree memory_type = TREE_TYPE (DR_REF (dr));
1787 if (!vect_gather_scatter_fn_p (loop_vinfo, DR_IS_READ (dr), masked_p,
1788 vectype, memory_type, offset_type, scale,
1789 &gs_info->ifn, &gs_info->offset_vectype,
1790 elsvals)
1791 || gs_info->ifn == IFN_LAST)
1792 continue;
1793
1794 gs_info->decl = NULL_TREE;
1795 /* Logically the sum of DR_BASE_ADDRESS, DR_INIT and DR_OFFSET,
1796 but we don't need to store that here. */
1797 gs_info->base = NULL_TREE;
1798 gs_info->element_type = TREE_TYPE (vectype);
1799 gs_info->offset = fold_convert (offset_type, step);
1800 gs_info->offset_dt = vect_constant_def;
1801 gs_info->scale = scale;
1802 gs_info->memory_type = memory_type;
1803 return true;
1804 }
1805
1806 if (overflow && dump_enabled_p ())
1807 dump_printf_loc (MSG_NOTE, vect_location,
1808 "truncating gather/scatter offset to %d bits"
1809 " might change its value.\n", element_bits);
1810
1811 return false;
1812 }
1813
1814 /* Return true if we can use gather/scatter internal functions to
1815 vectorize STMT_INFO, which is a grouped or strided load or store.
1816 MASKED_P is true if load or store is conditional. When returning
1817 true, fill in GS_INFO with the information required to perform the
1818 operation.
1819
1820 If we can use gather/scatter and ELSVALS is nonzero the supported
1821 else values will be stored in the vector ELSVALS points to. */
1822
1823 static bool
1824 vect_use_strided_gather_scatters_p (stmt_vec_info stmt_info,
1825 loop_vec_info loop_vinfo, bool masked_p,
1826 gather_scatter_info *gs_info,
1827 vec<int> *elsvals)
1828 {
1829 if (!vect_check_gather_scatter (stmt_info, loop_vinfo, gs_info, elsvals)
1830 || gs_info->ifn == IFN_LAST)
1831 return vect_truncate_gather_scatter_offset (stmt_info, loop_vinfo,
1832 masked_p, gs_info, elsvals);
1833
1834 tree old_offset_type = TREE_TYPE (gs_info->offset);
1835 tree new_offset_type = TREE_TYPE (gs_info->offset_vectype);
1836
1837 gcc_assert (TYPE_PRECISION (new_offset_type)
1838 >= TYPE_PRECISION (old_offset_type));
1839 gs_info->offset = fold_convert (new_offset_type, gs_info->offset);
1840
1841 if (dump_enabled_p ())
1842 dump_printf_loc (MSG_NOTE, vect_location,
1843 "using gather/scatter for strided/grouped access,"
1844 " scale = %d\n", gs_info->scale);
1845
1846 return true;
1847 }
1848
1849 /* STMT_INFO is a non-strided load or store, meaning that it accesses
1850 elements with a known constant step. Return -1 if that step
1851 is negative, 0 if it is zero, and 1 if it is greater than zero. */
1852
1853 int
1854 compare_step_with_zero (vec_info *vinfo, stmt_vec_info stmt_info)
1855 {
1856 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1857 return tree_int_cst_compare (vect_dr_behavior (vinfo, dr_info)->step,
1858 size_zero_node);
1859 }
1860
1861 /* If the target supports a permute mask that reverses the elements in
1862 a vector of type VECTYPE, return that mask, otherwise return null. */
1863
1864 tree
1865 perm_mask_for_reverse (tree vectype)
1866 {
1867 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1868
1869 /* The encoding has a single stepped pattern. */
1870 vec_perm_builder sel (nunits, 1, 3);
1871 for (int i = 0; i < 3; ++i)
1872 sel.quick_push (nunits - 1 - i);
1873
1874 vec_perm_indices indices (sel, 1, nunits);
1875 if (!can_vec_perm_const_p (TYPE_MODE (vectype), TYPE_MODE (vectype),
1876 indices))
1877 return NULL_TREE;
1878 return vect_gen_perm_mask_checked (vectype, indices);
1879 }
1880
1881 /* A subroutine of get_load_store_type, with a subset of the same
1882 arguments. Handle the case where STMT_INFO is a load or store that
1883 accesses consecutive elements with a negative step. Sets *POFFSET
1884 to the offset to be applied to the DR for the first access. */
1885
1886 static vect_memory_access_type
1887 get_negative_load_store_type (vec_info *vinfo,
1888 stmt_vec_info stmt_info, tree vectype,
1889 vec_load_store_type vls_type,
1890 unsigned int ncopies, poly_int64 *poffset)
1891 {
1892 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
1893 dr_alignment_support alignment_support_scheme;
1894
1895 if (ncopies > 1)
1896 {
1897 if (dump_enabled_p ())
1898 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1899 "multiple types with negative step.\n");
1900 return VMAT_ELEMENTWISE;
1901 }
1902
1903 /* For backward running DRs the first access in vectype actually is
1904 N-1 elements before the address of the DR. */
1905 *poffset = ((-TYPE_VECTOR_SUBPARTS (vectype) + 1)
1906 * TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype))));
1907
1908 int misalignment = dr_misalignment (dr_info, vectype, *poffset);
1909 alignment_support_scheme
1910 = vect_supportable_dr_alignment (vinfo, dr_info, vectype, misalignment);
1911 if (alignment_support_scheme != dr_aligned
1912 && alignment_support_scheme != dr_unaligned_supported)
1913 {
1914 if (dump_enabled_p ())
1915 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1916 "negative step but alignment required.\n");
1917 *poffset = 0;
1918 return VMAT_ELEMENTWISE;
1919 }
1920
1921 if (vls_type == VLS_STORE_INVARIANT)
1922 {
1923 if (dump_enabled_p ())
1924 dump_printf_loc (MSG_NOTE, vect_location,
1925 "negative step with invariant source;"
1926 " no permute needed.\n");
1927 return VMAT_CONTIGUOUS_DOWN;
1928 }
1929
1930 if (!perm_mask_for_reverse (vectype))
1931 {
1932 if (dump_enabled_p ())
1933 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1934 "negative step and reversing not supported.\n");
1935 *poffset = 0;
1936 return VMAT_ELEMENTWISE;
1937 }
1938
1939 return VMAT_CONTIGUOUS_REVERSE;
1940 }
1941
1942 /* STMT_INFO is either a masked or unconditional store. Return the value
1943 being stored. */
1944
1945 tree
1946 vect_get_store_rhs (stmt_vec_info stmt_info)
1947 {
1948 if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
1949 {
1950 gcc_assert (gimple_assign_single_p (assign));
1951 return gimple_assign_rhs1 (assign);
1952 }
1953 if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
1954 {
1955 internal_fn ifn = gimple_call_internal_fn (call);
1956 int index = internal_fn_stored_value_index (ifn);
1957 gcc_assert (index >= 0);
1958 return gimple_call_arg (call, index);
1959 }
1960 gcc_unreachable ();
1961 }
1962
1963 /* Function VECTOR_VECTOR_COMPOSITION_TYPE
1964
1965 This function returns a vector type which can be composed with NETLS pieces,
1966 whose type is recorded in PTYPE. VTYPE should be a vector type, and has the
1967 same vector size as the return vector. It checks target whether supports
1968 pieces-size vector mode for construction firstly, if target fails to, check
1969 pieces-size scalar mode for construction further. It returns NULL_TREE if
1970 fails to find the available composition.
1971
1972 For example, for (vtype=V16QI, nelts=4), we can probably get:
1973 - V16QI with PTYPE V4QI.
1974 - V4SI with PTYPE SI.
1975 - NULL_TREE. */
1976
1977 static tree
1978 vector_vector_composition_type (tree vtype, poly_uint64 nelts, tree *ptype)
1979 {
1980 gcc_assert (VECTOR_TYPE_P (vtype));
1981 gcc_assert (known_gt (nelts, 0U));
1982
1983 machine_mode vmode = TYPE_MODE (vtype);
1984 if (!VECTOR_MODE_P (vmode))
1985 return NULL_TREE;
1986
1987 /* When we are asked to compose the vector from its components let
1988 that happen directly. */
1989 if (known_eq (TYPE_VECTOR_SUBPARTS (vtype), nelts))
1990 {
1991 *ptype = TREE_TYPE (vtype);
1992 return vtype;
1993 }
1994
1995 poly_uint64 vbsize = GET_MODE_BITSIZE (vmode);
1996 unsigned int pbsize;
1997 if (constant_multiple_p (vbsize, nelts, &pbsize))
1998 {
1999 /* First check if vec_init optab supports construction from
2000 vector pieces directly. */
2001 scalar_mode elmode = SCALAR_TYPE_MODE (TREE_TYPE (vtype));
2002 poly_uint64 inelts = pbsize / GET_MODE_BITSIZE (elmode);
2003 machine_mode rmode;
2004 if (related_vector_mode (vmode, elmode, inelts).exists (&rmode)
2005 && (convert_optab_handler (vec_init_optab, vmode, rmode)
2006 != CODE_FOR_nothing))
2007 {
2008 *ptype = build_vector_type (TREE_TYPE (vtype), inelts);
2009 return vtype;
2010 }
2011
2012 /* Otherwise check if exists an integer type of the same piece size and
2013 if vec_init optab supports construction from it directly. */
2014 if (int_mode_for_size (pbsize, 0).exists (&elmode)
2015 && related_vector_mode (vmode, elmode, nelts).exists (&rmode)
2016 && (convert_optab_handler (vec_init_optab, rmode, elmode)
2017 != CODE_FOR_nothing))
2018 {
2019 *ptype = build_nonstandard_integer_type (pbsize, 1);
2020 return build_vector_type (*ptype, nelts);
2021 }
2022 }
2023
2024 return NULL_TREE;
2025 }
2026
2027 /* A subroutine of get_load_store_type, with a subset of the same
2028 arguments. Handle the case where STMT_INFO is part of a grouped load
2029 or store.
2030
2031 For stores, the statements in the group are all consecutive
2032 and there is no gap at the end. For loads, the statements in the
2033 group might not be consecutive; there can be gaps between statements
2034 as well as at the end.
2035
2036 If we can use gather/scatter and ELSVALS is nonzero the supported
2037 else values will be stored in the vector ELSVALS points to.
2038 */
2039
2040 static bool
2041 get_group_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2042 tree vectype, slp_tree slp_node,
2043 bool masked_p, vec_load_store_type vls_type,
2044 vect_memory_access_type *memory_access_type,
2045 poly_int64 *poffset,
2046 dr_alignment_support *alignment_support_scheme,
2047 int *misalignment,
2048 gather_scatter_info *gs_info,
2049 internal_fn *lanes_ifn,
2050 vec<int> *elsvals)
2051 {
2052 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2053 class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
2054 stmt_vec_info first_stmt_info;
2055 unsigned int group_size;
2056 unsigned HOST_WIDE_INT gap;
2057 bool single_element_p;
2058 poly_int64 neg_ldst_offset = 0;
2059 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2060 {
2061 first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2062 group_size = DR_GROUP_SIZE (first_stmt_info);
2063 gap = DR_GROUP_GAP (first_stmt_info);
2064 single_element_p = (stmt_info == first_stmt_info
2065 && !DR_GROUP_NEXT_ELEMENT (stmt_info));
2066 }
2067 else
2068 {
2069 first_stmt_info = stmt_info;
2070 group_size = 1;
2071 gap = 0;
2072 single_element_p = true;
2073 }
2074 dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
2075 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2076
2077 /* True if the vectorized statements would access beyond the last
2078 statement in the group. */
2079 bool overrun_p = false;
2080
2081 /* True if we can cope with such overrun by peeling for gaps, so that
2082 there is at least one final scalar iteration after the vector loop. */
2083 bool can_overrun_p = (!masked_p
2084 && vls_type == VLS_LOAD
2085 && loop_vinfo
2086 && !loop->inner);
2087
2088 /* There can only be a gap at the end of the group if the stride is
2089 known at compile time. */
2090 gcc_assert (!STMT_VINFO_STRIDED_P (first_stmt_info) || gap == 0);
2091
2092 /* Stores can't yet have gaps. */
2093 gcc_assert (slp_node || vls_type == VLS_LOAD || gap == 0);
2094
2095 if (slp_node)
2096 {
2097 /* For SLP vectorization we directly vectorize a subchain
2098 without permutation. */
2099 if (! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2100 first_dr_info
2101 = STMT_VINFO_DR_INFO (SLP_TREE_SCALAR_STMTS (slp_node)[0]);
2102 if (STMT_VINFO_STRIDED_P (first_stmt_info))
2103 /* Try to use consecutive accesses of as many elements as possible,
2104 separated by the stride, until we have a complete vector.
2105 Fall back to scalar accesses if that isn't possible. */
2106 *memory_access_type = VMAT_STRIDED_SLP;
2107 else
2108 {
2109 int cmp = compare_step_with_zero (vinfo, stmt_info);
2110 if (cmp < 0)
2111 {
2112 if (single_element_p)
2113 /* ??? The VMAT_CONTIGUOUS_REVERSE code generation is
2114 only correct for single element "interleaving" SLP. */
2115 *memory_access_type = get_negative_load_store_type
2116 (vinfo, stmt_info, vectype, vls_type, 1,
2117 &neg_ldst_offset);
2118 else
2119 {
2120 /* Try to use consecutive accesses of DR_GROUP_SIZE elements,
2121 separated by the stride, until we have a complete vector.
2122 Fall back to scalar accesses if that isn't possible. */
2123 if (multiple_p (nunits, group_size))
2124 *memory_access_type = VMAT_STRIDED_SLP;
2125 else
2126 *memory_access_type = VMAT_ELEMENTWISE;
2127 }
2128 }
2129 else if (cmp == 0 && loop_vinfo)
2130 {
2131 gcc_assert (vls_type == VLS_LOAD);
2132 *memory_access_type = VMAT_INVARIANT;
2133 }
2134 /* Try using LOAD/STORE_LANES. */
2135 else if (slp_node->ldst_lanes
2136 && (*lanes_ifn
2137 = (vls_type == VLS_LOAD
2138 ? vect_load_lanes_supported (vectype, group_size,
2139 masked_p, elsvals)
2140 : vect_store_lanes_supported (vectype, group_size,
2141 masked_p))) != IFN_LAST)
2142 *memory_access_type = VMAT_LOAD_STORE_LANES;
2143 else
2144 *memory_access_type = VMAT_CONTIGUOUS;
2145
2146 /* If this is single-element interleaving with an element
2147 distance that leaves unused vector loads around fall back
2148 to elementwise access if possible - we otherwise least
2149 create very sub-optimal code in that case (and
2150 blow up memory, see PR65518). */
2151 if (loop_vinfo
2152 && single_element_p
2153 && (*memory_access_type == VMAT_CONTIGUOUS
2154 || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2155 && maybe_gt (group_size, TYPE_VECTOR_SUBPARTS (vectype)))
2156 {
2157 if (SLP_TREE_LANES (slp_node) == 1)
2158 {
2159 *memory_access_type = VMAT_ELEMENTWISE;
2160 if (dump_enabled_p ())
2161 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2162 "single-element interleaving not supported "
2163 "for not adjacent vector loads, using "
2164 "elementwise access\n");
2165 }
2166 else
2167 {
2168 if (dump_enabled_p ())
2169 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2170 "single-element interleaving not supported "
2171 "for not adjacent vector loads\n");
2172 return false;
2173 }
2174 }
2175
2176 /* For single-element interleaving also fall back to elementwise
2177 access in case we did not lower a permutation and cannot
2178 code generate it. */
2179 auto_vec<tree> temv;
2180 unsigned n_perms;
2181 if (loop_vinfo
2182 && single_element_p
2183 && SLP_TREE_LANES (slp_node) == 1
2184 && (*memory_access_type == VMAT_CONTIGUOUS
2185 || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2186 && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2187 && !vect_transform_slp_perm_load
2188 (loop_vinfo, slp_node, temv, NULL,
2189 LOOP_VINFO_VECT_FACTOR (loop_vinfo), true, &n_perms))
2190 {
2191 *memory_access_type = VMAT_ELEMENTWISE;
2192 if (dump_enabled_p ())
2193 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2194 "single-element interleaving permutation not "
2195 "supported, using elementwise access\n");
2196 }
2197
2198 overrun_p = (loop_vinfo && gap != 0
2199 && *memory_access_type != VMAT_ELEMENTWISE);
2200 if (overrun_p && vls_type != VLS_LOAD)
2201 {
2202 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2203 "Grouped store with gaps requires"
2204 " non-consecutive accesses\n");
2205 return false;
2206 }
2207
2208 unsigned HOST_WIDE_INT dr_size
2209 = vect_get_scalar_dr_size (first_dr_info);
2210 poly_int64 off = 0;
2211 if (*memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2212 off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
2213
2214 /* An overrun is fine if the trailing elements are smaller
2215 than the alignment boundary B. Every vector access will
2216 be a multiple of B and so we are guaranteed to access a
2217 non-gap element in the same B-sized block. */
2218 if (overrun_p
2219 && gap < (vect_known_alignment_in_bytes (first_dr_info,
2220 vectype, off) / dr_size))
2221 overrun_p = false;
2222
2223 /* When we have a contiguous access across loop iterations
2224 but the access in the loop doesn't cover the full vector
2225 we can end up with no gap recorded but still excess
2226 elements accessed, see PR103116. Make sure we peel for
2227 gaps if necessary and sufficient and give up if not.
2228
2229 If there is a combination of the access not covering the full
2230 vector and a gap recorded then we may need to peel twice. */
2231 bool large_vector_overrun_p = false;
2232 if (loop_vinfo
2233 && (*memory_access_type == VMAT_CONTIGUOUS
2234 || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2235 && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
2236 && !multiple_p (group_size * LOOP_VINFO_VECT_FACTOR (loop_vinfo),
2237 nunits))
2238 large_vector_overrun_p = overrun_p = true;
2239
2240 /* If the gap splits the vector in half and the target
2241 can do half-vector operations avoid the epilogue peeling
2242 by simply loading half of the vector only. Usually
2243 the construction with an upper zero half will be elided. */
2244 dr_alignment_support alss;
2245 int misalign = dr_misalignment (first_dr_info, vectype, off);
2246 tree half_vtype;
2247 poly_uint64 remain;
2248 unsigned HOST_WIDE_INT tem, num;
2249 if (overrun_p
2250 && !masked_p
2251 && *memory_access_type != VMAT_LOAD_STORE_LANES
2252 && (((alss = vect_supportable_dr_alignment (vinfo, first_dr_info,
2253 vectype, misalign)))
2254 == dr_aligned
2255 || alss == dr_unaligned_supported)
2256 && can_div_trunc_p (group_size
2257 * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2258 nunits, &tem, &remain)
2259 && (known_eq (remain, 0u)
2260 || (known_ne (remain, 0u)
2261 && constant_multiple_p (nunits, remain, &num)
2262 && (vector_vector_composition_type (vectype, num,
2263 &half_vtype)
2264 != NULL_TREE))))
2265 overrun_p = false;
2266
2267 if (overrun_p && !can_overrun_p)
2268 {
2269 if (dump_enabled_p ())
2270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2271 "Peeling for outer loop is not supported\n");
2272 return false;
2273 }
2274
2275 /* Peeling for gaps assumes that a single scalar iteration
2276 is enough to make sure the last vector iteration doesn't
2277 access excess elements. */
2278 if (overrun_p
2279 && (!can_div_trunc_p (group_size
2280 * LOOP_VINFO_VECT_FACTOR (loop_vinfo) - gap,
2281 nunits, &tem, &remain)
2282 || maybe_lt (remain + group_size, nunits)))
2283 {
2284 /* But peeling a single scalar iteration is enough if
2285 we can use the next power-of-two sized partial
2286 access and that is sufficiently small to be covered
2287 by the single scalar iteration. */
2288 unsigned HOST_WIDE_INT cnunits, cvf, cremain, cpart_size;
2289 if (masked_p
2290 || !nunits.is_constant (&cnunits)
2291 || !LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&cvf)
2292 || (((cremain = (group_size * cvf - gap) % cnunits), true)
2293 && ((cpart_size = (1 << ceil_log2 (cremain))), true)
2294 && (cremain + group_size < cpart_size
2295 || vector_vector_composition_type
2296 (vectype, cnunits / cpart_size,
2297 &half_vtype) == NULL_TREE)))
2298 {
2299 /* If all fails we can still resort to niter masking unless
2300 the vectors used are too big, so enforce the use of
2301 partial vectors. */
2302 if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
2303 && !large_vector_overrun_p)
2304 {
2305 if (dump_enabled_p ())
2306 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2307 "peeling for gaps insufficient for "
2308 "access unless using partial "
2309 "vectors\n");
2310 LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2311 }
2312 else
2313 {
2314 if (dump_enabled_p ())
2315 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2316 "peeling for gaps insufficient for "
2317 "access\n");
2318 return false;
2319 }
2320 }
2321 else if (large_vector_overrun_p)
2322 {
2323 if (dump_enabled_p ())
2324 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2325 "can't operate on partial vectors because "
2326 "only unmasked loads handle access "
2327 "shortening required because of gaps at "
2328 "the end of the access\n");
2329 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
2330 }
2331 }
2332 }
2333 }
2334 else
2335 {
2336 /* We can always handle this case using elementwise accesses,
2337 but see if something more efficient is available. */
2338 *memory_access_type = VMAT_ELEMENTWISE;
2339
2340 /* If there is a gap at the end of the group then these optimizations
2341 would access excess elements in the last iteration. */
2342 bool would_overrun_p = (gap != 0);
2343 /* An overrun is fine if the trailing elements are smaller than the
2344 alignment boundary B. Every vector access will be a multiple of B
2345 and so we are guaranteed to access a non-gap element in the
2346 same B-sized block. */
2347 if (would_overrun_p
2348 && !masked_p
2349 && gap < (vect_known_alignment_in_bytes (first_dr_info, vectype)
2350 / vect_get_scalar_dr_size (first_dr_info)))
2351 would_overrun_p = false;
2352
2353 if (!STMT_VINFO_STRIDED_P (first_stmt_info)
2354 && (can_overrun_p || !would_overrun_p)
2355 && compare_step_with_zero (vinfo, stmt_info) > 0)
2356 {
2357 /* First cope with the degenerate case of a single-element
2358 vector. */
2359 if (known_eq (TYPE_VECTOR_SUBPARTS (vectype), 1U))
2360 ;
2361
2362 else
2363 {
2364 /* Otherwise try using LOAD/STORE_LANES. */
2365 *lanes_ifn
2366 = vls_type == VLS_LOAD
2367 ? vect_load_lanes_supported (vectype, group_size, masked_p,
2368 elsvals)
2369 : vect_store_lanes_supported (vectype, group_size,
2370 masked_p);
2371 if (*lanes_ifn != IFN_LAST)
2372 {
2373 *memory_access_type = VMAT_LOAD_STORE_LANES;
2374 overrun_p = would_overrun_p;
2375 }
2376
2377 /* If that fails, try using permuting loads. */
2378 else if (vls_type == VLS_LOAD
2379 ? vect_grouped_load_supported (vectype,
2380 single_element_p,
2381 group_size)
2382 : vect_grouped_store_supported (vectype, group_size))
2383 {
2384 *memory_access_type = VMAT_CONTIGUOUS_PERMUTE;
2385 overrun_p = would_overrun_p;
2386 }
2387 }
2388 }
2389 }
2390
2391 /* As a last resort, trying using a gather load or scatter store.
2392
2393 ??? Although the code can handle all group sizes correctly,
2394 it probably isn't a win to use separate strided accesses based
2395 on nearby locations. Or, even if it's a win over scalar code,
2396 it might not be a win over vectorizing at a lower VF, if that
2397 allows us to use contiguous accesses. */
2398 if ((*memory_access_type == VMAT_ELEMENTWISE
2399 || *memory_access_type == VMAT_STRIDED_SLP)
2400 && single_element_p
2401 && (!slp_node || SLP_TREE_LANES (slp_node) == 1)
2402 && loop_vinfo
2403 && vect_use_strided_gather_scatters_p (stmt_info, loop_vinfo,
2404 masked_p, gs_info, elsvals))
2405 *memory_access_type = VMAT_GATHER_SCATTER;
2406
2407 if (*memory_access_type == VMAT_CONTIGUOUS_DOWN
2408 || *memory_access_type == VMAT_CONTIGUOUS_REVERSE)
2409 *poffset = neg_ldst_offset;
2410
2411 if (*memory_access_type == VMAT_GATHER_SCATTER
2412 || *memory_access_type == VMAT_ELEMENTWISE
2413 || *memory_access_type == VMAT_STRIDED_SLP
2414 || *memory_access_type == VMAT_INVARIANT)
2415 {
2416 *alignment_support_scheme = dr_unaligned_supported;
2417 *misalignment = DR_MISALIGNMENT_UNKNOWN;
2418 }
2419 else
2420 {
2421 *misalignment = dr_misalignment (first_dr_info, vectype, *poffset);
2422 *alignment_support_scheme
2423 = vect_supportable_dr_alignment (vinfo, first_dr_info, vectype,
2424 *misalignment);
2425 }
2426
2427 if (vls_type != VLS_LOAD && first_stmt_info == stmt_info)
2428 {
2429 /* STMT is the leader of the group. Check the operands of all the
2430 stmts of the group. */
2431 stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info);
2432 while (next_stmt_info)
2433 {
2434 tree op = vect_get_store_rhs (next_stmt_info);
2435 enum vect_def_type dt;
2436 if (!vect_is_simple_use (op, vinfo, &dt))
2437 {
2438 if (dump_enabled_p ())
2439 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2440 "use not simple.\n");
2441 return false;
2442 }
2443 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
2444 }
2445 }
2446
2447 if (overrun_p)
2448 {
2449 gcc_assert (can_overrun_p);
2450 if (dump_enabled_p ())
2451 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2452 "Data access with gaps requires scalar "
2453 "epilogue loop\n");
2454 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2455 }
2456
2457 return true;
2458 }
2459
2460 /* Analyze load or store statement STMT_INFO of type VLS_TYPE. Return true
2461 if there is a memory access type that the vectorized form can use,
2462 storing it in *MEMORY_ACCESS_TYPE if so. If we decide to use gathers
2463 or scatters, fill in GS_INFO accordingly. In addition
2464 *ALIGNMENT_SUPPORT_SCHEME is filled out and false is returned if
2465 the target does not support the alignment scheme. *MISALIGNMENT
2466 is set according to the alignment of the access (including
2467 DR_MISALIGNMENT_UNKNOWN when it is unknown).
2468
2469 SLP says whether we're performing SLP rather than loop vectorization.
2470 MASKED_P is true if the statement is conditional on a vectorized mask.
2471 VECTYPE is the vector type that the vectorized statements will use.
2472 NCOPIES is the number of vector statements that will be needed.
2473
2474 If ELSVALS is nonzero the supported else values will be stored in the
2475 vector ELSVALS points to. */
2476
2477 static bool
2478 get_load_store_type (vec_info *vinfo, stmt_vec_info stmt_info,
2479 tree vectype, slp_tree slp_node,
2480 bool masked_p, vec_load_store_type vls_type,
2481 unsigned int ncopies,
2482 vect_memory_access_type *memory_access_type,
2483 poly_int64 *poffset,
2484 dr_alignment_support *alignment_support_scheme,
2485 int *misalignment,
2486 gather_scatter_info *gs_info,
2487 internal_fn *lanes_ifn,
2488 vec<int> *elsvals = nullptr)
2489 {
2490 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
2491 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
2492 *misalignment = DR_MISALIGNMENT_UNKNOWN;
2493 *poffset = 0;
2494 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
2495 {
2496 *memory_access_type = VMAT_GATHER_SCATTER;
2497 if (!vect_check_gather_scatter (stmt_info, loop_vinfo, gs_info,
2498 elsvals))
2499 gcc_unreachable ();
2500 /* When using internal functions, we rely on pattern recognition
2501 to convert the type of the offset to the type that the target
2502 requires, with the result being a call to an internal function.
2503 If that failed for some reason (e.g. because another pattern
2504 took priority), just handle cases in which the offset already
2505 has the right type. */
2506 else if (gs_info->ifn != IFN_LAST
2507 && !is_gimple_call (stmt_info->stmt)
2508 && !tree_nop_conversion_p (TREE_TYPE (gs_info->offset),
2509 TREE_TYPE (gs_info->offset_vectype)))
2510 {
2511 if (dump_enabled_p ())
2512 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2513 "%s offset requires a conversion\n",
2514 vls_type == VLS_LOAD ? "gather" : "scatter");
2515 return false;
2516 }
2517 else if (!vect_is_simple_use (gs_info->offset, vinfo,
2518 &gs_info->offset_dt,
2519 &gs_info->offset_vectype))
2520 {
2521 if (dump_enabled_p ())
2522 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2523 "%s index use not simple.\n",
2524 vls_type == VLS_LOAD ? "gather" : "scatter");
2525 return false;
2526 }
2527 else if (gs_info->ifn == IFN_LAST && !gs_info->decl)
2528 {
2529 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant ()
2530 || !TYPE_VECTOR_SUBPARTS (gs_info->offset_vectype).is_constant ()
2531 || VECTOR_BOOLEAN_TYPE_P (gs_info->offset_vectype)
2532 || !constant_multiple_p (TYPE_VECTOR_SUBPARTS
2533 (gs_info->offset_vectype),
2534 TYPE_VECTOR_SUBPARTS (vectype)))
2535 {
2536 if (dump_enabled_p ())
2537 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2538 "unsupported vector types for emulated "
2539 "gather.\n");
2540 return false;
2541 }
2542 }
2543 /* Gather-scatter accesses perform only component accesses, alignment
2544 is irrelevant for them. */
2545 *alignment_support_scheme = dr_unaligned_supported;
2546 }
2547 else if (STMT_VINFO_GROUPED_ACCESS (stmt_info) || slp_node)
2548 {
2549 if (!get_group_load_store_type (vinfo, stmt_info, vectype, slp_node,
2550 masked_p,
2551 vls_type, memory_access_type, poffset,
2552 alignment_support_scheme,
2553 misalignment, gs_info, lanes_ifn,
2554 elsvals))
2555 return false;
2556 }
2557 else if (STMT_VINFO_STRIDED_P (stmt_info))
2558 {
2559 gcc_assert (!slp_node);
2560 if (loop_vinfo
2561 && vect_use_strided_gather_scatters_p (stmt_info, loop_vinfo,
2562 masked_p, gs_info, elsvals))
2563 *memory_access_type = VMAT_GATHER_SCATTER;
2564 else
2565 *memory_access_type = VMAT_ELEMENTWISE;
2566 /* Alignment is irrelevant here. */
2567 *alignment_support_scheme = dr_unaligned_supported;
2568 }
2569 else
2570 {
2571 int cmp = compare_step_with_zero (vinfo, stmt_info);
2572 if (cmp == 0)
2573 {
2574 gcc_assert (vls_type == VLS_LOAD);
2575 *memory_access_type = VMAT_INVARIANT;
2576 /* Invariant accesses perform only component accesses, alignment
2577 is irrelevant for them. */
2578 *alignment_support_scheme = dr_unaligned_supported;
2579 }
2580 else
2581 {
2582 if (cmp < 0)
2583 *memory_access_type = get_negative_load_store_type
2584 (vinfo, stmt_info, vectype, vls_type, ncopies, poffset);
2585 else
2586 *memory_access_type = VMAT_CONTIGUOUS;
2587 *misalignment = dr_misalignment (STMT_VINFO_DR_INFO (stmt_info),
2588 vectype, *poffset);
2589 *alignment_support_scheme
2590 = vect_supportable_dr_alignment (vinfo,
2591 STMT_VINFO_DR_INFO (stmt_info),
2592 vectype, *misalignment);
2593 }
2594 }
2595
2596 if ((*memory_access_type == VMAT_ELEMENTWISE
2597 || *memory_access_type == VMAT_STRIDED_SLP)
2598 && !nunits.is_constant ())
2599 {
2600 if (dump_enabled_p ())
2601 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2602 "Not using elementwise accesses due to variable "
2603 "vectorization factor.\n");
2604 return false;
2605 }
2606
2607
2608 /* Checks if all scalar iterations are known to be inbounds. */
2609 bool inbounds = DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (stmt_info));
2610
2611 /* Check if we support the operation if early breaks are needed. Here we
2612 must ensure that we don't access any more than the scalar code would
2613 have. A masked operation would ensure this, so for these load types
2614 force masking. */
2615 if (loop_vinfo
2616 && dr_safe_speculative_read_required (stmt_info)
2617 && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
2618 && (*memory_access_type == VMAT_GATHER_SCATTER
2619 || *memory_access_type == VMAT_STRIDED_SLP))
2620 {
2621 if (dump_enabled_p ())
2622 dump_printf_loc (MSG_NOTE, vect_location,
2623 "early break not supported: cannot peel for "
2624 "alignment. With non-contiguous memory vectorization"
2625 " could read out of bounds at %G ",
2626 STMT_VINFO_STMT (stmt_info));
2627 if (inbounds)
2628 LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2629 else
2630 return false;
2631 }
2632
2633 /* If this DR needs alignment for correctness, we must ensure the target
2634 alignment is a constant power-of-two multiple of the amount read per
2635 vector iteration or force masking. */
2636 if (dr_safe_speculative_read_required (stmt_info)
2637 && *alignment_support_scheme == dr_aligned)
2638 {
2639 /* We can only peel for loops, of course. */
2640 gcc_checking_assert (loop_vinfo);
2641
2642 auto target_alignment
2643 = DR_TARGET_ALIGNMENT (STMT_VINFO_DR_INFO (stmt_info));
2644 unsigned HOST_WIDE_INT target_align;
2645
2646 bool group_aligned = false;
2647 if (target_alignment.is_constant (&target_align)
2648 && nunits.is_constant ())
2649 {
2650 poly_uint64 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2651 auto vectype_size
2652 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
2653 poly_uint64 required_alignment = vf * vectype_size;
2654 /* If we have a grouped access we require that the alignment be N * elem. */
2655 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
2656 required_alignment *=
2657 DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info));
2658 if (!multiple_p (target_alignment, required_alignment))
2659 {
2660 if (dump_enabled_p ())
2661 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2662 "desired alignment %wu not met. Instead got %wu "
2663 "for DR alignment at %G",
2664 required_alignment.to_constant (),
2665 target_align, STMT_VINFO_STMT (stmt_info));
2666 return false;
2667 }
2668
2669 if (!pow2p_hwi (target_align))
2670 {
2671 if (dump_enabled_p ())
2672 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2673 "non-power-of-two vector alignment %wd "
2674 "for DR alignment at %G",
2675 target_align, STMT_VINFO_STMT (stmt_info));
2676 return false;
2677 }
2678
2679 /* For VLA we have to insert a runtime check that the vector loads
2680 per iterations don't exceed a page size. For now we can use
2681 POLY_VALUE_MAX as a proxy as we can't peel for VLA. */
2682 if (known_gt (required_alignment, (unsigned)param_min_pagesize))
2683 {
2684 if (dump_enabled_p ())
2685 {
2686 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2687 "alignment required for correctness (");
2688 dump_dec (MSG_MISSED_OPTIMIZATION, required_alignment);
2689 dump_printf (MSG_NOTE, ") may exceed page size\n");
2690 }
2691 return false;
2692 }
2693
2694 group_aligned = true;
2695 }
2696
2697 /* There are multiple loads that have a misalignment that we couldn't
2698 align. We would need LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P to
2699 vectorize. */
2700 if (!group_aligned)
2701 {
2702 if (inbounds)
2703 LOOP_VINFO_MUST_USE_PARTIAL_VECTORS_P (loop_vinfo) = true;
2704 else
2705 return false;
2706 }
2707
2708 /* When using a group access the first element may be aligned but the
2709 subsequent loads may not be. For LOAD_LANES since the loads are based
2710 on the first DR then all loads in the group are aligned. For
2711 non-LOAD_LANES this is not the case. In particular a load + blend when
2712 there are gaps can have the non first loads issued unaligned, even
2713 partially overlapping the memory of the first load in order to simplify
2714 the blend. This is what the x86_64 backend does for instance. As
2715 such only the first load in the group is aligned, the rest are not.
2716 Because of this the permutes may break the alignment requirements that
2717 have been set, and as such we should for now, reject them. */
2718 if (slp_node && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
2719 {
2720 if (dump_enabled_p ())
2721 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2722 "loads with load permutations not supported for "
2723 "speculative early break loads for %G",
2724 STMT_VINFO_STMT (stmt_info));
2725 return false;
2726 }
2727 }
2728
2729 if (*alignment_support_scheme == dr_unaligned_unsupported)
2730 {
2731 if (dump_enabled_p ())
2732 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2733 "unsupported unaligned access\n");
2734 return false;
2735 }
2736
2737 /* FIXME: At the moment the cost model seems to underestimate the
2738 cost of using elementwise accesses. This check preserves the
2739 traditional behavior until that can be fixed. */
2740 stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
2741 if (!first_stmt_info)
2742 first_stmt_info = stmt_info;
2743 if (*memory_access_type == VMAT_ELEMENTWISE
2744 && !STMT_VINFO_STRIDED_P (first_stmt_info)
2745 && !(stmt_info == DR_GROUP_FIRST_ELEMENT (stmt_info)
2746 && !DR_GROUP_NEXT_ELEMENT (stmt_info)
2747 && !pow2p_hwi (DR_GROUP_SIZE (stmt_info))))
2748 {
2749 if (dump_enabled_p ())
2750 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2751 "not falling back to elementwise accesses\n");
2752 return false;
2753 }
2754 return true;
2755 }
2756
2757 /* Return true if boolean argument at MASK_INDEX is suitable for vectorizing
2758 conditional operation STMT_INFO. When returning true, store the mask
2759 in *MASK, the type of its definition in *MASK_DT_OUT, the type of the
2760 vectorized mask in *MASK_VECTYPE_OUT and the SLP node corresponding
2761 to the mask in *MASK_NODE if MASK_NODE is not NULL. */
2762
2763 static bool
2764 vect_check_scalar_mask (vec_info *vinfo, stmt_vec_info stmt_info,
2765 slp_tree slp_node, unsigned mask_index,
2766 tree *mask, slp_tree *mask_node,
2767 vect_def_type *mask_dt_out, tree *mask_vectype_out)
2768 {
2769 enum vect_def_type mask_dt;
2770 tree mask_vectype;
2771 slp_tree mask_node_1;
2772 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, mask_index,
2773 mask, &mask_node_1, &mask_dt, &mask_vectype))
2774 {
2775 if (dump_enabled_p ())
2776 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2777 "mask use not simple.\n");
2778 return false;
2779 }
2780
2781 if ((mask_dt == vect_constant_def || mask_dt == vect_external_def)
2782 && !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (*mask)))
2783 {
2784 if (dump_enabled_p ())
2785 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2786 "mask argument is not a boolean.\n");
2787 return false;
2788 }
2789
2790 /* If the caller is not prepared for adjusting an external/constant
2791 SLP mask vector type fail. */
2792 if (slp_node
2793 && !mask_node
2794 && SLP_TREE_DEF_TYPE (mask_node_1) != vect_internal_def)
2795 {
2796 if (dump_enabled_p ())
2797 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2798 "SLP mask argument is not vectorized.\n");
2799 return false;
2800 }
2801
2802 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2803 if (!mask_vectype)
2804 mask_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (vectype),
2805 mask_node_1);
2806
2807 if (!mask_vectype || !VECTOR_BOOLEAN_TYPE_P (mask_vectype))
2808 {
2809 if (dump_enabled_p ())
2810 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2811 "could not find an appropriate vector mask type.\n");
2812 return false;
2813 }
2814
2815 if (maybe_ne (TYPE_VECTOR_SUBPARTS (mask_vectype),
2816 TYPE_VECTOR_SUBPARTS (vectype)))
2817 {
2818 if (dump_enabled_p ())
2819 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2820 "vector mask type %T"
2821 " does not match vector data type %T.\n",
2822 mask_vectype, vectype);
2823
2824 return false;
2825 }
2826
2827 *mask_dt_out = mask_dt;
2828 *mask_vectype_out = mask_vectype;
2829 if (mask_node)
2830 *mask_node = mask_node_1;
2831 return true;
2832 }
2833
2834 /* Return true if stored value is suitable for vectorizing store
2835 statement STMT_INFO. When returning true, store the scalar stored
2836 in *RHS and *RHS_NODE, the type of the definition in *RHS_DT_OUT,
2837 the type of the vectorized store value in
2838 *RHS_VECTYPE_OUT and the type of the store in *VLS_TYPE_OUT. */
2839
2840 static bool
2841 vect_check_store_rhs (vec_info *vinfo, stmt_vec_info stmt_info,
2842 slp_tree slp_node, tree *rhs, slp_tree *rhs_node,
2843 vect_def_type *rhs_dt_out, tree *rhs_vectype_out,
2844 vec_load_store_type *vls_type_out)
2845 {
2846 int op_no = 0;
2847 if (gcall *call = dyn_cast <gcall *> (stmt_info->stmt))
2848 {
2849 if (gimple_call_internal_p (call)
2850 && internal_store_fn_p (gimple_call_internal_fn (call)))
2851 op_no = internal_fn_stored_value_index (gimple_call_internal_fn (call));
2852 }
2853 if (slp_node)
2854 op_no = vect_slp_child_index_for_operand
2855 (stmt_info->stmt, op_no, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
2856
2857 enum vect_def_type rhs_dt;
2858 tree rhs_vectype;
2859 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, op_no,
2860 rhs, rhs_node, &rhs_dt, &rhs_vectype))
2861 {
2862 if (dump_enabled_p ())
2863 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2864 "use not simple.\n");
2865 return false;
2866 }
2867
2868 /* In the case this is a store from a constant make sure
2869 native_encode_expr can handle it. */
2870 if (rhs_dt == vect_constant_def
2871 && CONSTANT_CLASS_P (*rhs) && native_encode_expr (*rhs, NULL, 64) == 0)
2872 {
2873 if (dump_enabled_p ())
2874 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2875 "cannot encode constant as a byte sequence.\n");
2876 return false;
2877 }
2878
2879 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2880 if (rhs_vectype && !useless_type_conversion_p (vectype, rhs_vectype))
2881 {
2882 if (dump_enabled_p ())
2883 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2884 "incompatible vector types.\n");
2885 return false;
2886 }
2887
2888 *rhs_dt_out = rhs_dt;
2889 *rhs_vectype_out = rhs_vectype;
2890 if (rhs_dt == vect_constant_def || rhs_dt == vect_external_def)
2891 *vls_type_out = VLS_STORE_INVARIANT;
2892 else
2893 *vls_type_out = VLS_STORE;
2894 return true;
2895 }
2896
2897 /* Build an all-ones vector mask of type MASKTYPE while vectorizing STMT_INFO.
2898 Note that we support masks with floating-point type, in which case the
2899 floats are interpreted as a bitmask. */
2900
2901 static tree
2902 vect_build_all_ones_mask (vec_info *vinfo,
2903 stmt_vec_info stmt_info, tree masktype)
2904 {
2905 if (TREE_CODE (masktype) == INTEGER_TYPE)
2906 return build_int_cst (masktype, -1);
2907 else if (VECTOR_BOOLEAN_TYPE_P (masktype)
2908 || TREE_CODE (TREE_TYPE (masktype)) == INTEGER_TYPE)
2909 {
2910 tree mask = build_int_cst (TREE_TYPE (masktype), -1);
2911 mask = build_vector_from_val (masktype, mask);
2912 return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2913 }
2914 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (masktype)))
2915 {
2916 REAL_VALUE_TYPE r;
2917 long tmp[6];
2918 for (int j = 0; j < 6; ++j)
2919 tmp[j] = -1;
2920 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (masktype)));
2921 tree mask = build_real (TREE_TYPE (masktype), r);
2922 mask = build_vector_from_val (masktype, mask);
2923 return vect_init_vector (vinfo, stmt_info, mask, masktype, NULL);
2924 }
2925 gcc_unreachable ();
2926 }
2927
2928 /* Build an all-zero merge value of type VECTYPE while vectorizing
2929 STMT_INFO as a gather load. */
2930
2931 static tree
2932 vect_build_zero_merge_argument (vec_info *vinfo,
2933 stmt_vec_info stmt_info, tree vectype)
2934 {
2935 tree merge;
2936 if (TREE_CODE (TREE_TYPE (vectype)) == INTEGER_TYPE)
2937 merge = build_int_cst (TREE_TYPE (vectype), 0);
2938 else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (vectype)))
2939 {
2940 REAL_VALUE_TYPE r;
2941 long tmp[6];
2942 for (int j = 0; j < 6; ++j)
2943 tmp[j] = 0;
2944 real_from_target (&r, tmp, TYPE_MODE (TREE_TYPE (vectype)));
2945 merge = build_real (TREE_TYPE (vectype), r);
2946 }
2947 else
2948 gcc_unreachable ();
2949 merge = build_vector_from_val (vectype, merge);
2950 return vect_init_vector (vinfo, stmt_info, merge, vectype, NULL);
2951 }
2952
2953 /* Return the corresponding else value for an else value constant
2954 ELSVAL with type TYPE. */
2955
2956 tree
2957 vect_get_mask_load_else (int elsval, tree type)
2958 {
2959 tree els;
2960 if (elsval == MASK_LOAD_ELSE_UNDEFINED)
2961 {
2962 tree tmp = create_tmp_var (type);
2963 /* No need to warn about anything. */
2964 TREE_NO_WARNING (tmp) = 1;
2965 els = get_or_create_ssa_default_def (cfun, tmp);
2966 }
2967 else if (elsval == MASK_LOAD_ELSE_M1)
2968 els = build_minus_one_cst (type);
2969 else if (elsval == MASK_LOAD_ELSE_ZERO)
2970 els = build_zero_cst (type);
2971 else
2972 gcc_unreachable ();
2973
2974 return els;
2975 }
2976
2977 /* Build a gather load call while vectorizing STMT_INFO. Insert new
2978 instructions before GSI and add them to VEC_STMT. GS_INFO describes
2979 the gather load operation. If the load is conditional, MASK is the
2980 vectorized condition, otherwise MASK is null. PTR is the base
2981 pointer and OFFSET is the vectorized offset. */
2982
2983 static gimple *
2984 vect_build_one_gather_load_call (vec_info *vinfo, stmt_vec_info stmt_info,
2985 gimple_stmt_iterator *gsi,
2986 gather_scatter_info *gs_info,
2987 tree ptr, tree offset, tree mask)
2988 {
2989 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
2990 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info->decl));
2991 tree rettype = TREE_TYPE (TREE_TYPE (gs_info->decl));
2992 tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2993 /* ptrtype */ arglist = TREE_CHAIN (arglist);
2994 tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2995 tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
2996 tree scaletype = TREE_VALUE (arglist);
2997 tree var;
2998 gcc_checking_assert (types_compatible_p (srctype, rettype)
2999 && (!mask
3000 || TREE_CODE (masktype) == INTEGER_TYPE
3001 || types_compatible_p (srctype, masktype)));
3002
3003 tree op = offset;
3004 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3005 {
3006 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3007 TYPE_VECTOR_SUBPARTS (idxtype)));
3008 var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3009 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3010 gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3011 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3012 op = var;
3013 }
3014
3015 tree src_op = NULL_TREE;
3016 tree mask_op = NULL_TREE;
3017 if (mask)
3018 {
3019 if (!useless_type_conversion_p (masktype, TREE_TYPE (mask)))
3020 {
3021 tree utype, optype = TREE_TYPE (mask);
3022 if (VECTOR_TYPE_P (masktype)
3023 || TYPE_MODE (masktype) == TYPE_MODE (optype))
3024 utype = masktype;
3025 else
3026 utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3027 var = vect_get_new_ssa_name (utype, vect_scalar_var);
3028 tree mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask);
3029 gassign *new_stmt
3030 = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3031 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3032 mask_arg = var;
3033 if (!useless_type_conversion_p (masktype, utype))
3034 {
3035 gcc_assert (TYPE_PRECISION (utype)
3036 <= TYPE_PRECISION (masktype));
3037 var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3038 new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3039 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3040 mask_arg = var;
3041 }
3042 src_op = build_zero_cst (srctype);
3043 mask_op = mask_arg;
3044 }
3045 else
3046 {
3047 src_op = mask;
3048 mask_op = mask;
3049 }
3050 }
3051 else
3052 {
3053 src_op = vect_build_zero_merge_argument (vinfo, stmt_info, rettype);
3054 mask_op = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
3055 }
3056
3057 tree scale = build_int_cst (scaletype, gs_info->scale);
3058 gimple *new_stmt = gimple_build_call (gs_info->decl, 5, src_op, ptr, op,
3059 mask_op, scale);
3060
3061 if (!useless_type_conversion_p (vectype, rettype))
3062 {
3063 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
3064 TYPE_VECTOR_SUBPARTS (rettype)));
3065 op = vect_get_new_ssa_name (rettype, vect_simple_var);
3066 gimple_call_set_lhs (new_stmt, op);
3067 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3068 op = build1 (VIEW_CONVERT_EXPR, vectype, op);
3069 new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR, op);
3070 }
3071
3072 return new_stmt;
3073 }
3074
3075 /* Build a scatter store call while vectorizing STMT_INFO. Insert new
3076 instructions before GSI. GS_INFO describes the scatter store operation.
3077 PTR is the base pointer, OFFSET the vectorized offsets and OPRND the
3078 vectorized data to store.
3079 If the store is conditional, MASK is the vectorized condition, otherwise
3080 MASK is null. */
3081
3082 static gimple *
3083 vect_build_one_scatter_store_call (vec_info *vinfo, stmt_vec_info stmt_info,
3084 gimple_stmt_iterator *gsi,
3085 gather_scatter_info *gs_info,
3086 tree ptr, tree offset, tree oprnd, tree mask)
3087 {
3088 tree rettype = TREE_TYPE (TREE_TYPE (gs_info->decl));
3089 tree arglist = TYPE_ARG_TYPES (TREE_TYPE (gs_info->decl));
3090 /* tree ptrtype = TREE_VALUE (arglist); */ arglist = TREE_CHAIN (arglist);
3091 tree masktype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3092 tree idxtype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3093 tree srctype = TREE_VALUE (arglist); arglist = TREE_CHAIN (arglist);
3094 tree scaletype = TREE_VALUE (arglist);
3095 gcc_checking_assert (TREE_CODE (masktype) == INTEGER_TYPE
3096 && TREE_CODE (rettype) == VOID_TYPE);
3097
3098 tree mask_arg = NULL_TREE;
3099 if (mask)
3100 {
3101 mask_arg = mask;
3102 tree optype = TREE_TYPE (mask_arg);
3103 tree utype;
3104 if (TYPE_MODE (masktype) == TYPE_MODE (optype))
3105 utype = masktype;
3106 else
3107 utype = lang_hooks.types.type_for_mode (TYPE_MODE (optype), 1);
3108 tree var = vect_get_new_ssa_name (utype, vect_scalar_var);
3109 mask_arg = build1 (VIEW_CONVERT_EXPR, utype, mask_arg);
3110 gassign *new_stmt
3111 = gimple_build_assign (var, VIEW_CONVERT_EXPR, mask_arg);
3112 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3113 mask_arg = var;
3114 if (!useless_type_conversion_p (masktype, utype))
3115 {
3116 gcc_assert (TYPE_PRECISION (utype) <= TYPE_PRECISION (masktype));
3117 tree var = vect_get_new_ssa_name (masktype, vect_scalar_var);
3118 new_stmt = gimple_build_assign (var, NOP_EXPR, mask_arg);
3119 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3120 mask_arg = var;
3121 }
3122 }
3123 else
3124 {
3125 mask_arg = build_int_cst (masktype, -1);
3126 mask_arg = vect_init_vector (vinfo, stmt_info, mask_arg, masktype, NULL);
3127 }
3128
3129 tree src = oprnd;
3130 if (!useless_type_conversion_p (srctype, TREE_TYPE (src)))
3131 {
3132 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (src)),
3133 TYPE_VECTOR_SUBPARTS (srctype)));
3134 tree var = vect_get_new_ssa_name (srctype, vect_simple_var);
3135 src = build1 (VIEW_CONVERT_EXPR, srctype, src);
3136 gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, src);
3137 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3138 src = var;
3139 }
3140
3141 tree op = offset;
3142 if (!useless_type_conversion_p (idxtype, TREE_TYPE (op)))
3143 {
3144 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (op)),
3145 TYPE_VECTOR_SUBPARTS (idxtype)));
3146 tree var = vect_get_new_ssa_name (idxtype, vect_simple_var);
3147 op = build1 (VIEW_CONVERT_EXPR, idxtype, op);
3148 gassign *new_stmt = gimple_build_assign (var, VIEW_CONVERT_EXPR, op);
3149 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3150 op = var;
3151 }
3152
3153 tree scale = build_int_cst (scaletype, gs_info->scale);
3154 gcall *new_stmt
3155 = gimple_build_call (gs_info->decl, 5, ptr, mask_arg, op, src, scale);
3156 return new_stmt;
3157 }
3158
3159 /* Prepare the base and offset in GS_INFO for vectorization.
3160 Set *DATAREF_PTR to the loop-invariant base address and *VEC_OFFSET
3161 to the vectorized offset argument for the first copy of STMT_INFO.
3162 STMT_INFO is the statement described by GS_INFO and LOOP is the
3163 containing loop. */
3164
3165 static void
3166 vect_get_gather_scatter_ops (loop_vec_info loop_vinfo,
3167 class loop *loop, stmt_vec_info stmt_info,
3168 slp_tree slp_node, gather_scatter_info *gs_info,
3169 tree *dataref_ptr, vec<tree> *vec_offset)
3170 {
3171 gimple_seq stmts = NULL;
3172 *dataref_ptr = force_gimple_operand (gs_info->base, &stmts, true, NULL_TREE);
3173 if (stmts != NULL)
3174 {
3175 basic_block new_bb;
3176 edge pe = loop_preheader_edge (loop);
3177 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3178 gcc_assert (!new_bb);
3179 }
3180 if (slp_node)
3181 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[0], vec_offset);
3182 else
3183 {
3184 unsigned ncopies
3185 = vect_get_num_copies (loop_vinfo, gs_info->offset_vectype);
3186 vect_get_vec_defs_for_operand (loop_vinfo, stmt_info, ncopies,
3187 gs_info->offset, vec_offset,
3188 gs_info->offset_vectype);
3189 }
3190 }
3191
3192 /* Prepare to implement a grouped or strided load or store using
3193 the gather load or scatter store operation described by GS_INFO.
3194 STMT_INFO is the load or store statement.
3195
3196 Set *DATAREF_BUMP to the amount that should be added to the base
3197 address after each copy of the vectorized statement. Set *VEC_OFFSET
3198 to an invariant offset vector in which element I has the value
3199 I * DR_STEP / SCALE. */
3200
3201 static void
3202 vect_get_strided_load_store_ops (stmt_vec_info stmt_info,
3203 loop_vec_info loop_vinfo,
3204 gimple_stmt_iterator *gsi,
3205 gather_scatter_info *gs_info,
3206 tree *dataref_bump, tree *vec_offset,
3207 vec_loop_lens *loop_lens)
3208 {
3209 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3210 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3211
3212 if (LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3213 {
3214 /* _31 = .SELECT_VL (ivtmp_29, POLY_INT_CST [4, 4]);
3215 ivtmp_8 = _31 * 16 (step in bytes);
3216 .MASK_LEN_SCATTER_STORE (vectp_a.9_7, ... );
3217 vectp_a.9_26 = vectp_a.9_7 + ivtmp_8; */
3218 tree loop_len
3219 = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, vectype, 0, 0);
3220 tree tmp
3221 = fold_build2 (MULT_EXPR, sizetype,
3222 fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3223 loop_len);
3224 *dataref_bump = force_gimple_operand_gsi (gsi, tmp, true, NULL_TREE, true,
3225 GSI_SAME_STMT);
3226 }
3227 else
3228 {
3229 tree bump
3230 = size_binop (MULT_EXPR,
3231 fold_convert (sizetype, unshare_expr (DR_STEP (dr))),
3232 size_int (TYPE_VECTOR_SUBPARTS (vectype)));
3233 *dataref_bump = cse_and_gimplify_to_preheader (loop_vinfo, bump);
3234 }
3235
3236 internal_fn ifn
3237 = DR_IS_READ (dr) ? IFN_MASK_LEN_STRIDED_LOAD : IFN_MASK_LEN_STRIDED_STORE;
3238 if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
3239 {
3240 *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo,
3241 unshare_expr (DR_STEP (dr)));
3242 return;
3243 }
3244
3245 /* The offset given in GS_INFO can have pointer type, so use the element
3246 type of the vector instead. */
3247 tree offset_type = TREE_TYPE (gs_info->offset_vectype);
3248
3249 /* Calculate X = DR_STEP / SCALE and convert it to the appropriate type. */
3250 tree step = size_binop (EXACT_DIV_EXPR, unshare_expr (DR_STEP (dr)),
3251 ssize_int (gs_info->scale));
3252 step = fold_convert (offset_type, step);
3253
3254 /* Create {0, X, X*2, X*3, ...}. */
3255 tree offset = fold_build2 (VEC_SERIES_EXPR, gs_info->offset_vectype,
3256 build_zero_cst (offset_type), step);
3257 *vec_offset = cse_and_gimplify_to_preheader (loop_vinfo, offset);
3258 }
3259
3260 /* Prepare the pointer IVs which needs to be updated by a variable amount.
3261 Such variable amount is the outcome of .SELECT_VL. In this case, we can
3262 allow each iteration process the flexible number of elements as long as
3263 the number <= vf elments.
3264
3265 Return data reference according to SELECT_VL.
3266 If new statements are needed, insert them before GSI. */
3267
3268 static tree
3269 vect_get_loop_variant_data_ptr_increment (
3270 vec_info *vinfo, tree aggr_type, gimple_stmt_iterator *gsi,
3271 vec_loop_lens *loop_lens, dr_vec_info *dr_info,
3272 vect_memory_access_type memory_access_type)
3273 {
3274 loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3275 tree step = vect_dr_behavior (vinfo, dr_info)->step;
3276
3277 /* gather/scatter never reach here. */
3278 gcc_assert (memory_access_type != VMAT_GATHER_SCATTER);
3279
3280 /* When we support SELECT_VL pattern, we dynamic adjust
3281 the memory address by .SELECT_VL result.
3282
3283 The result of .SELECT_VL is the number of elements to
3284 be processed of each iteration. So the memory address
3285 adjustment operation should be:
3286
3287 addr = addr + .SELECT_VL (ARG..) * step;
3288 */
3289 tree loop_len
3290 = vect_get_loop_len (loop_vinfo, gsi, loop_lens, 1, aggr_type, 0, 0);
3291 tree len_type = TREE_TYPE (loop_len);
3292 /* Since the outcome of .SELECT_VL is element size, we should adjust
3293 it into bytesize so that it can be used in address pointer variable
3294 amount IVs adjustment. */
3295 tree tmp = fold_build2 (MULT_EXPR, len_type, loop_len,
3296 wide_int_to_tree (len_type, wi::to_widest (step)));
3297 tree bump = make_temp_ssa_name (len_type, NULL, "ivtmp");
3298 gassign *assign = gimple_build_assign (bump, tmp);
3299 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
3300 return bump;
3301 }
3302
3303 /* Return the amount that should be added to a vector pointer to move
3304 to the next or previous copy of AGGR_TYPE. DR_INFO is the data reference
3305 being vectorized and MEMORY_ACCESS_TYPE describes the type of
3306 vectorization. */
3307
3308 static tree
3309 vect_get_data_ptr_increment (vec_info *vinfo, gimple_stmt_iterator *gsi,
3310 dr_vec_info *dr_info, tree aggr_type,
3311 vect_memory_access_type memory_access_type,
3312 vec_loop_lens *loop_lens = nullptr)
3313 {
3314 if (memory_access_type == VMAT_INVARIANT)
3315 return size_zero_node;
3316
3317 loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (vinfo);
3318 if (loop_vinfo && LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo))
3319 return vect_get_loop_variant_data_ptr_increment (vinfo, aggr_type, gsi,
3320 loop_lens, dr_info,
3321 memory_access_type);
3322
3323 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3324 tree step = vect_dr_behavior (vinfo, dr_info)->step;
3325 if (tree_int_cst_sgn (step) == -1)
3326 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3327 return iv_step;
3328 }
3329
3330 /* Check and perform vectorization of BUILT_IN_BSWAP{16,32,64,128}. */
3331
3332 static bool
3333 vectorizable_bswap (vec_info *vinfo,
3334 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3335 gimple **vec_stmt, slp_tree slp_node,
3336 slp_tree *slp_op,
3337 tree vectype_in, stmt_vector_for_cost *cost_vec)
3338 {
3339 tree op, vectype;
3340 gcall *stmt = as_a <gcall *> (stmt_info->stmt);
3341 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3342 unsigned ncopies;
3343
3344 op = gimple_call_arg (stmt, 0);
3345 vectype = STMT_VINFO_VECTYPE (stmt_info);
3346 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3347
3348 /* Multiple types in SLP are handled by creating the appropriate number of
3349 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
3350 case of SLP. */
3351 if (slp_node)
3352 ncopies = 1;
3353 else
3354 ncopies = vect_get_num_copies (loop_vinfo, vectype);
3355
3356 gcc_assert (ncopies >= 1);
3357
3358 if (TYPE_SIZE (vectype_in) != TYPE_SIZE (vectype))
3359 {
3360 if (dump_enabled_p ())
3361 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3362 "mismatched vector sizes %T and %T\n",
3363 vectype_in, vectype);
3364 return false;
3365 }
3366
3367 tree char_vectype = get_same_sized_vectype (char_type_node, vectype_in);
3368 if (! char_vectype)
3369 return false;
3370
3371 poly_uint64 num_bytes = TYPE_VECTOR_SUBPARTS (char_vectype);
3372 unsigned word_bytes;
3373 if (!constant_multiple_p (num_bytes, nunits, &word_bytes))
3374 return false;
3375
3376 /* The encoding uses one stepped pattern for each byte in the word. */
3377 vec_perm_builder elts (num_bytes, word_bytes, 3);
3378 for (unsigned i = 0; i < 3; ++i)
3379 for (unsigned j = 0; j < word_bytes; ++j)
3380 elts.quick_push ((i + 1) * word_bytes - j - 1);
3381
3382 vec_perm_indices indices (elts, 1, num_bytes);
3383 machine_mode vmode = TYPE_MODE (char_vectype);
3384 if (!can_vec_perm_const_p (vmode, vmode, indices))
3385 return false;
3386
3387 if (! vec_stmt)
3388 {
3389 if (slp_node
3390 && !vect_maybe_update_slp_op_vectype (slp_op[0], vectype_in))
3391 {
3392 if (dump_enabled_p ())
3393 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3394 "incompatible vector types for invariants\n");
3395 return false;
3396 }
3397
3398 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
3399 DUMP_VECT_SCOPE ("vectorizable_bswap");
3400 record_stmt_cost (cost_vec,
3401 1, vector_stmt, stmt_info, 0, vect_prologue);
3402 record_stmt_cost (cost_vec,
3403 slp_node
3404 ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) : ncopies,
3405 vec_perm, stmt_info, 0, vect_body);
3406 return true;
3407 }
3408
3409 tree bswap_vconst = vec_perm_indices_to_tree (char_vectype, indices);
3410
3411 /* Transform. */
3412 vec<tree> vec_oprnds = vNULL;
3413 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
3414 op, &vec_oprnds);
3415 /* Arguments are ready. create the new vector stmt. */
3416 unsigned i;
3417 tree vop;
3418 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
3419 {
3420 gimple *new_stmt;
3421 tree tem = make_ssa_name (char_vectype);
3422 new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3423 char_vectype, vop));
3424 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3425 tree tem2 = make_ssa_name (char_vectype);
3426 new_stmt = gimple_build_assign (tem2, VEC_PERM_EXPR,
3427 tem, tem, bswap_vconst);
3428 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3429 tem = make_ssa_name (vectype);
3430 new_stmt = gimple_build_assign (tem, build1 (VIEW_CONVERT_EXPR,
3431 vectype, tem2));
3432 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
3433 if (slp_node)
3434 slp_node->push_vec_def (new_stmt);
3435 else
3436 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
3437 }
3438
3439 if (!slp_node)
3440 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
3441
3442 vec_oprnds.release ();
3443 return true;
3444 }
3445
3446 /* Return true if vector types VECTYPE_IN and VECTYPE_OUT have
3447 integer elements and if we can narrow VECTYPE_IN to VECTYPE_OUT
3448 in a single step. On success, store the binary pack code in
3449 *CONVERT_CODE. */
3450
3451 static bool
3452 simple_integer_narrowing (tree vectype_out, tree vectype_in,
3453 code_helper *convert_code)
3454 {
3455 if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype_out))
3456 || !INTEGRAL_TYPE_P (TREE_TYPE (vectype_in)))
3457 return false;
3458
3459 code_helper code;
3460 int multi_step_cvt = 0;
3461 auto_vec <tree, 8> interm_types;
3462 if (!supportable_narrowing_operation (NOP_EXPR, vectype_out, vectype_in,
3463 &code, &multi_step_cvt, &interm_types)
3464 || multi_step_cvt)
3465 return false;
3466
3467 *convert_code = code;
3468 return true;
3469 }
3470
3471 /* Function vectorizable_call.
3472
3473 Check if STMT_INFO performs a function call that can be vectorized.
3474 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
3475 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3476 Return true if STMT_INFO is vectorizable in this way. */
3477
3478 static bool
3479 vectorizable_call (vec_info *vinfo,
3480 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
3481 gimple **vec_stmt, slp_tree slp_node,
3482 stmt_vector_for_cost *cost_vec)
3483 {
3484 gcall *stmt;
3485 tree vec_dest;
3486 tree scalar_dest;
3487 tree op;
3488 tree vec_oprnd0 = NULL_TREE, vec_oprnd1 = NULL_TREE;
3489 tree vectype_out, vectype_in;
3490 poly_uint64 nunits_in;
3491 poly_uint64 nunits_out;
3492 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
3493 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
3494 tree fndecl, new_temp, rhs_type;
3495 enum vect_def_type dt[4]
3496 = { vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type,
3497 vect_unknown_def_type };
3498 tree vectypes[ARRAY_SIZE (dt)] = {};
3499 slp_tree slp_op[ARRAY_SIZE (dt)] = {};
3500 int ndts = ARRAY_SIZE (dt);
3501 int ncopies, j;
3502 auto_vec<tree, 8> vargs;
3503 enum { NARROW, NONE, WIDEN } modifier;
3504 size_t i, nargs;
3505 tree lhs;
3506 tree clz_ctz_arg1 = NULL_TREE;
3507
3508 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
3509 return false;
3510
3511 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
3512 && ! vec_stmt)
3513 return false;
3514
3515 /* Is STMT_INFO a vectorizable call? */
3516 stmt = dyn_cast <gcall *> (stmt_info->stmt);
3517 if (!stmt)
3518 return false;
3519
3520 if (gimple_call_internal_p (stmt)
3521 && (internal_load_fn_p (gimple_call_internal_fn (stmt))
3522 || internal_store_fn_p (gimple_call_internal_fn (stmt))))
3523 /* Handled by vectorizable_load and vectorizable_store. */
3524 return false;
3525
3526 if (gimple_call_lhs (stmt) == NULL_TREE
3527 || TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
3528 return false;
3529
3530 gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
3531
3532 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
3533
3534 /* Process function arguments. */
3535 rhs_type = NULL_TREE;
3536 vectype_in = NULL_TREE;
3537 nargs = gimple_call_num_args (stmt);
3538
3539 /* Bail out if the function has more than four arguments, we do not have
3540 interesting builtin functions to vectorize with more than two arguments
3541 except for fma. No arguments is also not good. */
3542 if (nargs == 0 || nargs > 4)
3543 return false;
3544
3545 /* Ignore the arguments of IFN_GOMP_SIMD_LANE, they are magic. */
3546 combined_fn cfn = gimple_call_combined_fn (stmt);
3547 if (cfn == CFN_GOMP_SIMD_LANE)
3548 {
3549 nargs = 0;
3550 rhs_type = unsigned_type_node;
3551 }
3552 /* Similarly pretend IFN_CLZ and IFN_CTZ only has one argument, the second
3553 argument just says whether it is well-defined at zero or not and what
3554 value should be returned for it. */
3555 if ((cfn == CFN_CLZ || cfn == CFN_CTZ) && nargs == 2)
3556 {
3557 nargs = 1;
3558 clz_ctz_arg1 = gimple_call_arg (stmt, 1);
3559 }
3560
3561 int mask_opno = -1;
3562 if (internal_fn_p (cfn))
3563 {
3564 /* We can only handle direct internal masked calls here,
3565 vectorizable_simd_clone_call is for the rest. */
3566 if (cfn == CFN_MASK_CALL)
3567 return false;
3568 mask_opno = internal_fn_mask_index (as_internal_fn (cfn));
3569 }
3570
3571 for (i = 0; i < nargs; i++)
3572 {
3573 if ((int) i == mask_opno)
3574 {
3575 if (!vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_opno,
3576 &op, &slp_op[i], &dt[i], &vectypes[i]))
3577 return false;
3578 continue;
3579 }
3580
3581 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
3582 i, &op, &slp_op[i], &dt[i], &vectypes[i]))
3583 {
3584 if (dump_enabled_p ())
3585 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3586 "use not simple.\n");
3587 return false;
3588 }
3589
3590 /* We can only handle calls with arguments of the same type. */
3591 if (rhs_type
3592 && !types_compatible_p (rhs_type, TREE_TYPE (op)))
3593 {
3594 if (dump_enabled_p ())
3595 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3596 "argument types differ.\n");
3597 return false;
3598 }
3599 if (!rhs_type)
3600 rhs_type = TREE_TYPE (op);
3601
3602 if (!vectype_in)
3603 vectype_in = vectypes[i];
3604 else if (vectypes[i]
3605 && !types_compatible_p (vectypes[i], vectype_in))
3606 {
3607 if (dump_enabled_p ())
3608 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3609 "argument vector types differ.\n");
3610 return false;
3611 }
3612 }
3613 /* If all arguments are external or constant defs, infer the vector type
3614 from the scalar type. */
3615 if (!vectype_in)
3616 vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
3617 if (vec_stmt)
3618 gcc_assert (vectype_in);
3619 if (!vectype_in)
3620 {
3621 if (dump_enabled_p ())
3622 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3623 "no vectype for scalar type %T\n", rhs_type);
3624
3625 return false;
3626 }
3627
3628 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
3629 != VECTOR_BOOLEAN_TYPE_P (vectype_in))
3630 {
3631 if (dump_enabled_p ())
3632 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3633 "mixed mask and nonmask vector types\n");
3634 return false;
3635 }
3636
3637 if (vect_emulated_vector_p (vectype_in) || vect_emulated_vector_p (vectype_out))
3638 {
3639 if (dump_enabled_p ())
3640 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3641 "use emulated vector type for call\n");
3642 return false;
3643 }
3644
3645 /* FORNOW */
3646 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
3647 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
3648 if (known_eq (nunits_in * 2, nunits_out))
3649 modifier = NARROW;
3650 else if (known_eq (nunits_out, nunits_in))
3651 modifier = NONE;
3652 else if (known_eq (nunits_out * 2, nunits_in))
3653 modifier = WIDEN;
3654 else
3655 return false;
3656
3657 /* We only handle functions that do not read or clobber memory. */
3658 if (gimple_vuse (stmt))
3659 {
3660 if (dump_enabled_p ())
3661 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3662 "function reads from or writes to memory.\n");
3663 return false;
3664 }
3665
3666 /* For now, we only vectorize functions if a target specific builtin
3667 is available. TODO -- in some cases, it might be profitable to
3668 insert the calls for pieces of the vector, in order to be able
3669 to vectorize other operations in the loop. */
3670 fndecl = NULL_TREE;
3671 internal_fn ifn = IFN_LAST;
3672 tree callee = gimple_call_fndecl (stmt);
3673
3674 /* First try using an internal function. */
3675 code_helper convert_code = MAX_TREE_CODES;
3676 if (cfn != CFN_LAST
3677 && (modifier == NONE
3678 || (modifier == NARROW
3679 && simple_integer_narrowing (vectype_out, vectype_in,
3680 &convert_code))))
3681 ifn = vectorizable_internal_function (cfn, callee, vectype_out,
3682 vectype_in);
3683
3684 /* If that fails, try asking for a target-specific built-in function. */
3685 if (ifn == IFN_LAST)
3686 {
3687 if (cfn != CFN_LAST)
3688 fndecl = targetm.vectorize.builtin_vectorized_function
3689 (cfn, vectype_out, vectype_in);
3690 else if (callee && fndecl_built_in_p (callee, BUILT_IN_MD))
3691 fndecl = targetm.vectorize.builtin_md_vectorized_function
3692 (callee, vectype_out, vectype_in);
3693 }
3694
3695 if (ifn == IFN_LAST && !fndecl)
3696 {
3697 if (cfn == CFN_GOMP_SIMD_LANE
3698 && (!slp_node || SLP_TREE_LANES (slp_node) == 1)
3699 && loop_vinfo
3700 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3701 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
3702 && LOOP_VINFO_LOOP (loop_vinfo)->simduid
3703 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
3704 {
3705 /* We can handle IFN_GOMP_SIMD_LANE by returning a
3706 { 0, 1, 2, ... vf - 1 } vector. */
3707 gcc_assert (nargs == 0);
3708 }
3709 else if (modifier == NONE
3710 && (gimple_call_builtin_p (stmt, BUILT_IN_BSWAP16)
3711 || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP32)
3712 || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP64)
3713 || gimple_call_builtin_p (stmt, BUILT_IN_BSWAP128)))
3714 return vectorizable_bswap (vinfo, stmt_info, gsi, vec_stmt, slp_node,
3715 slp_op, vectype_in, cost_vec);
3716 else
3717 {
3718 if (dump_enabled_p ())
3719 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3720 "function is not vectorizable.\n");
3721 return false;
3722 }
3723 }
3724
3725 if (slp_node)
3726 ncopies = 1;
3727 else if (modifier == NARROW && ifn == IFN_LAST)
3728 ncopies = vect_get_num_copies (loop_vinfo, vectype_out);
3729 else
3730 ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
3731
3732 /* Sanity check: make sure that at least one copy of the vectorized stmt
3733 needs to be generated. */
3734 gcc_assert (ncopies >= 1);
3735
3736 int reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
3737 internal_fn cond_fn = get_conditional_internal_fn (ifn);
3738 internal_fn cond_len_fn = get_len_internal_fn (ifn);
3739 int len_opno = internal_fn_len_index (cond_len_fn);
3740 vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
3741 vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
3742 if (!vec_stmt) /* transformation not required. */
3743 {
3744 if (slp_node)
3745 for (i = 0; i < nargs; ++i)
3746 if (!vect_maybe_update_slp_op_vectype (slp_op[i],
3747 vectypes[i]
3748 ? vectypes[i] : vectype_in))
3749 {
3750 if (dump_enabled_p ())
3751 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3752 "incompatible vector types for invariants\n");
3753 return false;
3754 }
3755 STMT_VINFO_TYPE (stmt_info) = call_vec_info_type;
3756 DUMP_VECT_SCOPE ("vectorizable_call");
3757 vect_model_simple_cost (vinfo, stmt_info,
3758 ncopies, dt, ndts, slp_node, cost_vec);
3759 if (ifn != IFN_LAST && modifier == NARROW && !slp_node)
3760 record_stmt_cost (cost_vec, ncopies / 2,
3761 vec_promote_demote, stmt_info, 0, vect_body);
3762
3763 if (loop_vinfo
3764 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
3765 && (reduc_idx >= 0 || mask_opno >= 0))
3766 {
3767 if (reduc_idx >= 0
3768 && (cond_fn == IFN_LAST
3769 || !direct_internal_fn_supported_p (cond_fn, vectype_out,
3770 OPTIMIZE_FOR_SPEED))
3771 && (cond_len_fn == IFN_LAST
3772 || !direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3773 OPTIMIZE_FOR_SPEED)))
3774 {
3775 if (dump_enabled_p ())
3776 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3777 "can't use a fully-masked loop because no"
3778 " conditional operation is available.\n");
3779 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
3780 }
3781 else
3782 {
3783 unsigned int nvectors
3784 = (slp_node
3785 ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node)
3786 : ncopies);
3787 tree scalar_mask = NULL_TREE;
3788 if (mask_opno >= 0)
3789 scalar_mask = gimple_call_arg (stmt_info->stmt, mask_opno);
3790 if (cond_len_fn != IFN_LAST
3791 && direct_internal_fn_supported_p (cond_len_fn, vectype_out,
3792 OPTIMIZE_FOR_SPEED))
3793 vect_record_loop_len (loop_vinfo, lens, nvectors, vectype_out,
3794 1);
3795 else
3796 vect_record_loop_mask (loop_vinfo, masks, nvectors, vectype_out,
3797 scalar_mask);
3798 }
3799 }
3800 return true;
3801 }
3802
3803 /* Transform. */
3804
3805 if (dump_enabled_p ())
3806 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
3807
3808 /* Handle def. */
3809 scalar_dest = gimple_call_lhs (stmt);
3810 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
3811
3812 bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
3813 bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
3814 unsigned int vect_nargs = nargs;
3815 if (len_loop_p)
3816 {
3817 if (len_opno >= 0)
3818 {
3819 ifn = cond_len_fn;
3820 /* COND_* -> COND_LEN_* takes 2 extra arguments:LEN,BIAS. */
3821 vect_nargs += 2;
3822 }
3823 else if (reduc_idx >= 0)
3824 gcc_unreachable ();
3825 }
3826 else if (masked_loop_p && reduc_idx >= 0)
3827 {
3828 ifn = cond_fn;
3829 vect_nargs += 2;
3830 }
3831 if (clz_ctz_arg1)
3832 ++vect_nargs;
3833
3834 if (modifier == NONE || ifn != IFN_LAST)
3835 {
3836 tree prev_res = NULL_TREE;
3837 vargs.safe_grow (vect_nargs, true);
3838 auto_vec<vec<tree> > vec_defs (nargs);
3839 for (j = 0; j < ncopies; ++j)
3840 {
3841 /* Build argument list for the vectorized call. */
3842 if (slp_node)
3843 {
3844 if (cfn == CFN_GOMP_SIMD_LANE)
3845 {
3846 for (i = 0; i < SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node); ++i)
3847 {
3848 /* ??? For multi-lane SLP we'd need to build
3849 { 0, 0, .., 1, 1, ... }. */
3850 tree cst = build_index_vector (vectype_out,
3851 i * nunits_out, 1);
3852 tree new_var
3853 = vect_get_new_ssa_name (vectype_out, vect_simple_var,
3854 "cst_");
3855 gimple *init_stmt = gimple_build_assign (new_var, cst);
3856 vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
3857 new_temp = make_ssa_name (vec_dest);
3858 gimple *new_stmt
3859 = gimple_build_assign (new_temp, new_var);
3860 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
3861 gsi);
3862 slp_node->push_vec_def (new_stmt);
3863 }
3864 continue;
3865 }
3866
3867 vec<tree> vec_oprnds0;
3868 vect_get_slp_defs (vinfo, slp_node, &vec_defs);
3869 vec_oprnds0 = vec_defs[0];
3870
3871 /* Arguments are ready. Create the new vector stmt. */
3872 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_oprnd0)
3873 {
3874 int varg = 0;
3875 if (masked_loop_p && reduc_idx >= 0)
3876 {
3877 unsigned int vec_num = vec_oprnds0.length ();
3878 /* Always true for SLP. */
3879 gcc_assert (ncopies == 1);
3880 vargs[varg++] = vect_get_loop_mask (loop_vinfo,
3881 gsi, masks, vec_num,
3882 vectype_out, i);
3883 }
3884 size_t k;
3885 for (k = 0; k < nargs; k++)
3886 {
3887 vec<tree> vec_oprndsk = vec_defs[k];
3888 vargs[varg++] = vec_oprndsk[i];
3889 }
3890 if (masked_loop_p && reduc_idx >= 0)
3891 vargs[varg++] = vargs[reduc_idx + 1];
3892 if (clz_ctz_arg1)
3893 vargs[varg++] = clz_ctz_arg1;
3894
3895 gimple *new_stmt;
3896 if (modifier == NARROW)
3897 {
3898 /* We don't define any narrowing conditional functions
3899 at present. */
3900 gcc_assert (mask_opno < 0);
3901 tree half_res = make_ssa_name (vectype_in);
3902 gcall *call
3903 = gimple_build_call_internal_vec (ifn, vargs);
3904 gimple_call_set_lhs (call, half_res);
3905 gimple_call_set_nothrow (call, true);
3906 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3907 if ((i & 1) == 0)
3908 {
3909 prev_res = half_res;
3910 continue;
3911 }
3912 new_temp = make_ssa_name (vec_dest);
3913 new_stmt = vect_gimple_build (new_temp, convert_code,
3914 prev_res, half_res);
3915 vect_finish_stmt_generation (vinfo, stmt_info,
3916 new_stmt, gsi);
3917 }
3918 else
3919 {
3920 if (len_opno >= 0 && len_loop_p)
3921 {
3922 unsigned int vec_num = vec_oprnds0.length ();
3923 /* Always true for SLP. */
3924 gcc_assert (ncopies == 1);
3925 tree len
3926 = vect_get_loop_len (loop_vinfo, gsi, lens, vec_num,
3927 vectype_out, i, 1);
3928 signed char biasval
3929 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3930 tree bias = build_int_cst (intQI_type_node, biasval);
3931 vargs[len_opno] = len;
3932 vargs[len_opno + 1] = bias;
3933 }
3934 else if (mask_opno >= 0 && masked_loop_p)
3935 {
3936 unsigned int vec_num = vec_oprnds0.length ();
3937 /* Always true for SLP. */
3938 gcc_assert (ncopies == 1);
3939 tree mask = vect_get_loop_mask (loop_vinfo,
3940 gsi, masks, vec_num,
3941 vectype_out, i);
3942 vargs[mask_opno] = prepare_vec_mask
3943 (loop_vinfo, TREE_TYPE (mask), mask,
3944 vargs[mask_opno], gsi);
3945 }
3946
3947 gcall *call;
3948 if (ifn != IFN_LAST)
3949 call = gimple_build_call_internal_vec (ifn, vargs);
3950 else
3951 call = gimple_build_call_vec (fndecl, vargs);
3952 new_temp = make_ssa_name (vec_dest, call);
3953 gimple_call_set_lhs (call, new_temp);
3954 gimple_call_set_nothrow (call, true);
3955 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
3956 new_stmt = call;
3957 }
3958 slp_node->push_vec_def (new_stmt);
3959 }
3960 continue;
3961 }
3962
3963 int varg = 0;
3964 if (masked_loop_p && reduc_idx >= 0)
3965 vargs[varg++] = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies,
3966 vectype_out, j);
3967 for (i = 0; i < nargs; i++)
3968 {
3969 op = gimple_call_arg (stmt, i);
3970 if (j == 0)
3971 {
3972 vec_defs.quick_push (vNULL);
3973 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
3974 op, &vec_defs[i],
3975 vectypes[i]);
3976 }
3977 vargs[varg++] = vec_defs[i][j];
3978 }
3979 if (masked_loop_p && reduc_idx >= 0)
3980 vargs[varg++] = vargs[reduc_idx + 1];
3981 if (clz_ctz_arg1)
3982 vargs[varg++] = clz_ctz_arg1;
3983
3984 if (len_opno >= 0 && len_loop_p)
3985 {
3986 tree len = vect_get_loop_len (loop_vinfo, gsi, lens, ncopies,
3987 vectype_out, j, 1);
3988 signed char biasval
3989 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
3990 tree bias = build_int_cst (intQI_type_node, biasval);
3991 vargs[len_opno] = len;
3992 vargs[len_opno + 1] = bias;
3993 }
3994 else if (mask_opno >= 0 && masked_loop_p)
3995 {
3996 tree mask = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies,
3997 vectype_out, j);
3998 vargs[mask_opno]
3999 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
4000 vargs[mask_opno], gsi);
4001 }
4002
4003 gimple *new_stmt;
4004 if (cfn == CFN_GOMP_SIMD_LANE)
4005 {
4006 tree cst = build_index_vector (vectype_out, j * nunits_out, 1);
4007 tree new_var
4008 = vect_get_new_ssa_name (vectype_out, vect_simple_var, "cst_");
4009 gimple *init_stmt = gimple_build_assign (new_var, cst);
4010 vect_init_vector_1 (vinfo, stmt_info, init_stmt, NULL);
4011 new_temp = make_ssa_name (vec_dest);
4012 new_stmt = gimple_build_assign (new_temp, new_var);
4013 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4014 }
4015 else if (modifier == NARROW)
4016 {
4017 /* We don't define any narrowing conditional functions at
4018 present. */
4019 gcc_assert (mask_opno < 0);
4020 tree half_res = make_ssa_name (vectype_in);
4021 gcall *call = gimple_build_call_internal_vec (ifn, vargs);
4022 gimple_call_set_lhs (call, half_res);
4023 gimple_call_set_nothrow (call, true);
4024 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
4025 if ((j & 1) == 0)
4026 {
4027 prev_res = half_res;
4028 continue;
4029 }
4030 new_temp = make_ssa_name (vec_dest);
4031 new_stmt = vect_gimple_build (new_temp, convert_code, prev_res,
4032 half_res);
4033 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4034 }
4035 else
4036 {
4037 gcall *call;
4038 if (ifn != IFN_LAST)
4039 call = gimple_build_call_internal_vec (ifn, vargs);
4040 else
4041 call = gimple_build_call_vec (fndecl, vargs);
4042 new_temp = make_ssa_name (vec_dest, call);
4043 gimple_call_set_lhs (call, new_temp);
4044 gimple_call_set_nothrow (call, true);
4045 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
4046 new_stmt = call;
4047 }
4048
4049 if (j == (modifier == NARROW ? 1 : 0))
4050 *vec_stmt = new_stmt;
4051 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
4052 }
4053 for (i = 0; i < nargs; i++)
4054 {
4055 vec<tree> vec_oprndsi = vec_defs[i];
4056 vec_oprndsi.release ();
4057 }
4058 }
4059 else if (modifier == NARROW)
4060 {
4061 auto_vec<vec<tree> > vec_defs (nargs);
4062 /* We don't define any narrowing conditional functions at present. */
4063 gcc_assert (mask_opno < 0);
4064 for (j = 0; j < ncopies; ++j)
4065 {
4066 /* Build argument list for the vectorized call. */
4067 if (j == 0)
4068 vargs.create (nargs * 2);
4069 else
4070 vargs.truncate (0);
4071
4072 if (slp_node)
4073 {
4074 vec<tree> vec_oprnds0;
4075
4076 vect_get_slp_defs (vinfo, slp_node, &vec_defs);
4077 vec_oprnds0 = vec_defs[0];
4078
4079 /* Arguments are ready. Create the new vector stmt. */
4080 for (i = 0; vec_oprnds0.iterate (i, &vec_oprnd0); i += 2)
4081 {
4082 size_t k;
4083 vargs.truncate (0);
4084 for (k = 0; k < nargs; k++)
4085 {
4086 vec<tree> vec_oprndsk = vec_defs[k];
4087 vargs.quick_push (vec_oprndsk[i]);
4088 vargs.quick_push (vec_oprndsk[i + 1]);
4089 }
4090 gcall *call;
4091 if (ifn != IFN_LAST)
4092 call = gimple_build_call_internal_vec (ifn, vargs);
4093 else
4094 call = gimple_build_call_vec (fndecl, vargs);
4095 new_temp = make_ssa_name (vec_dest, call);
4096 gimple_call_set_lhs (call, new_temp);
4097 gimple_call_set_nothrow (call, true);
4098 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
4099 slp_node->push_vec_def (call);
4100 }
4101 continue;
4102 }
4103
4104 for (i = 0; i < nargs; i++)
4105 {
4106 op = gimple_call_arg (stmt, i);
4107 if (j == 0)
4108 {
4109 vec_defs.quick_push (vNULL);
4110 vect_get_vec_defs_for_operand (vinfo, stmt_info, 2 * ncopies,
4111 op, &vec_defs[i], vectypes[i]);
4112 }
4113 vec_oprnd0 = vec_defs[i][2*j];
4114 vec_oprnd1 = vec_defs[i][2*j+1];
4115
4116 vargs.quick_push (vec_oprnd0);
4117 vargs.quick_push (vec_oprnd1);
4118 }
4119
4120 gcall *new_stmt = gimple_build_call_vec (fndecl, vargs);
4121 new_temp = make_ssa_name (vec_dest, new_stmt);
4122 gimple_call_set_lhs (new_stmt, new_temp);
4123 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
4124
4125 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
4126 }
4127
4128 if (!slp_node)
4129 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
4130
4131 for (i = 0; i < nargs; i++)
4132 {
4133 vec<tree> vec_oprndsi = vec_defs[i];
4134 vec_oprndsi.release ();
4135 }
4136 }
4137 else
4138 /* No current target implements this case. */
4139 return false;
4140
4141 vargs.release ();
4142
4143 /* The call in STMT might prevent it from being removed in dce.
4144 We however cannot remove it here, due to the way the ssa name
4145 it defines is mapped to the new definition. So just replace
4146 rhs of the statement with something harmless. */
4147
4148 if (slp_node)
4149 return true;
4150
4151 stmt_info = vect_orig_stmt (stmt_info);
4152 lhs = gimple_get_lhs (stmt_info->stmt);
4153
4154 gassign *new_stmt
4155 = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
4156 vinfo->replace_stmt (gsi, stmt_info, new_stmt);
4157
4158 return true;
4159 }
4160
4161
4162 struct simd_call_arg_info
4163 {
4164 tree vectype;
4165 tree op;
4166 HOST_WIDE_INT linear_step;
4167 enum vect_def_type dt;
4168 unsigned int align;
4169 bool simd_lane_linear;
4170 };
4171
4172 /* Helper function of vectorizable_simd_clone_call. If OP, an SSA_NAME,
4173 is linear within simd lane (but not within whole loop), note it in
4174 *ARGINFO. */
4175
4176 static void
4177 vect_simd_lane_linear (tree op, class loop *loop,
4178 struct simd_call_arg_info *arginfo)
4179 {
4180 gimple *def_stmt = SSA_NAME_DEF_STMT (op);
4181
4182 if (!is_gimple_assign (def_stmt)
4183 || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR
4184 || !is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
4185 return;
4186
4187 tree base = gimple_assign_rhs1 (def_stmt);
4188 HOST_WIDE_INT linear_step = 0;
4189 tree v = gimple_assign_rhs2 (def_stmt);
4190 while (TREE_CODE (v) == SSA_NAME)
4191 {
4192 tree t;
4193 def_stmt = SSA_NAME_DEF_STMT (v);
4194 if (is_gimple_assign (def_stmt))
4195 switch (gimple_assign_rhs_code (def_stmt))
4196 {
4197 case PLUS_EXPR:
4198 t = gimple_assign_rhs2 (def_stmt);
4199 if (linear_step || TREE_CODE (t) != INTEGER_CST)
4200 return;
4201 base = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, t);
4202 v = gimple_assign_rhs1 (def_stmt);
4203 continue;
4204 case MULT_EXPR:
4205 t = gimple_assign_rhs2 (def_stmt);
4206 if (linear_step || !tree_fits_shwi_p (t) || integer_zerop (t))
4207 return;
4208 linear_step = tree_to_shwi (t);
4209 v = gimple_assign_rhs1 (def_stmt);
4210 continue;
4211 CASE_CONVERT:
4212 t = gimple_assign_rhs1 (def_stmt);
4213 if (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
4214 || (TYPE_PRECISION (TREE_TYPE (v))
4215 < TYPE_PRECISION (TREE_TYPE (t))))
4216 return;
4217 if (!linear_step)
4218 linear_step = 1;
4219 v = t;
4220 continue;
4221 default:
4222 return;
4223 }
4224 else if (gimple_call_internal_p (def_stmt, IFN_GOMP_SIMD_LANE)
4225 && loop->simduid
4226 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME
4227 && (SSA_NAME_VAR (gimple_call_arg (def_stmt, 0))
4228 == loop->simduid))
4229 {
4230 if (!linear_step)
4231 linear_step = 1;
4232 arginfo->linear_step = linear_step;
4233 arginfo->op = base;
4234 arginfo->simd_lane_linear = true;
4235 return;
4236 }
4237 }
4238 }
4239
4240 /* Function vectorizable_simd_clone_call.
4241
4242 Check if STMT_INFO performs a function call that can be vectorized
4243 by calling a simd clone of the function.
4244 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
4245 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
4246 Return true if STMT_INFO is vectorizable in this way. */
4247
4248 static bool
4249 vectorizable_simd_clone_call (vec_info *vinfo, stmt_vec_info stmt_info,
4250 gimple_stmt_iterator *gsi,
4251 gimple **vec_stmt, slp_tree slp_node,
4252 stmt_vector_for_cost *)
4253 {
4254 tree vec_dest;
4255 tree scalar_dest;
4256 tree op, type;
4257 tree vec_oprnd0 = NULL_TREE;
4258 tree vectype;
4259 poly_uint64 nunits;
4260 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
4261 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
4262 class loop *loop = loop_vinfo ? LOOP_VINFO_LOOP (loop_vinfo) : NULL;
4263 tree fndecl, new_temp;
4264 int ncopies, j;
4265 auto_vec<simd_call_arg_info> arginfo;
4266 vec<tree> vargs = vNULL;
4267 size_t i, nargs;
4268 tree lhs, rtype, ratype;
4269 vec<constructor_elt, va_gc> *ret_ctor_elts = NULL;
4270 int masked_call_offset = 0;
4271
4272 /* Is STMT a vectorizable call? */
4273 gcall *stmt = dyn_cast <gcall *> (stmt_info->stmt);
4274 if (!stmt)
4275 return false;
4276
4277 fndecl = gimple_call_fndecl (stmt);
4278 if (fndecl == NULL_TREE
4279 && gimple_call_internal_p (stmt, IFN_MASK_CALL))
4280 {
4281 fndecl = gimple_call_arg (stmt, 0);
4282 gcc_checking_assert (TREE_CODE (fndecl) == ADDR_EXPR);
4283 fndecl = TREE_OPERAND (fndecl, 0);
4284 gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL);
4285 masked_call_offset = 1;
4286 }
4287 if (fndecl == NULL_TREE)
4288 return false;
4289
4290 struct cgraph_node *node = cgraph_node::get (fndecl);
4291 if (node == NULL || node->simd_clones == NULL)
4292 return false;
4293
4294 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
4295 return false;
4296
4297 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
4298 && ! vec_stmt)
4299 return false;
4300
4301 if (gimple_call_lhs (stmt)
4302 && TREE_CODE (gimple_call_lhs (stmt)) != SSA_NAME)
4303 return false;
4304
4305 gcc_checking_assert (!stmt_can_throw_internal (cfun, stmt));
4306
4307 vectype = STMT_VINFO_VECTYPE (stmt_info);
4308
4309 if (loop_vinfo && nested_in_vect_loop_p (loop, stmt_info))
4310 return false;
4311
4312 /* Process function arguments. */
4313 nargs = gimple_call_num_args (stmt) - masked_call_offset;
4314
4315 /* Bail out if the function has zero arguments. */
4316 if (nargs == 0)
4317 return false;
4318
4319 vec<tree>& simd_clone_info = (slp_node ? SLP_TREE_SIMD_CLONE_INFO (slp_node)
4320 : STMT_VINFO_SIMD_CLONE_INFO (stmt_info));
4321 if (!vec_stmt)
4322 simd_clone_info.truncate (0);
4323 arginfo.reserve (nargs, true);
4324 auto_vec<slp_tree> slp_op;
4325 slp_op.safe_grow_cleared (nargs);
4326
4327 for (i = 0; i < nargs; i++)
4328 {
4329 simd_call_arg_info thisarginfo;
4330 affine_iv iv;
4331
4332 thisarginfo.linear_step = 0;
4333 thisarginfo.align = 0;
4334 thisarginfo.op = NULL_TREE;
4335 thisarginfo.simd_lane_linear = false;
4336
4337 int op_no = i + masked_call_offset;
4338 if (slp_node)
4339 op_no = vect_slp_child_index_for_operand (stmt, op_no, false);
4340 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
4341 op_no, &op, &slp_op[i],
4342 &thisarginfo.dt, &thisarginfo.vectype)
4343 || thisarginfo.dt == vect_uninitialized_def)
4344 {
4345 if (dump_enabled_p ())
4346 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4347 "use not simple.\n");
4348 return false;
4349 }
4350
4351 if (thisarginfo.dt == vect_constant_def
4352 || thisarginfo.dt == vect_external_def)
4353 {
4354 /* With SLP we determine the vector type of constants/externals
4355 at analysis time, handling conflicts via
4356 vect_maybe_update_slp_op_vectype. At transform time
4357 we have a vector type recorded for SLP. */
4358 gcc_assert (!vec_stmt
4359 || !slp_node
4360 || thisarginfo.vectype != NULL_TREE);
4361 if (!vec_stmt)
4362 thisarginfo.vectype = get_vectype_for_scalar_type (vinfo,
4363 TREE_TYPE (op),
4364 slp_node);
4365 }
4366 else
4367 gcc_assert (thisarginfo.vectype != NULL_TREE);
4368
4369 /* For linear arguments, the analyze phase should have saved
4370 the base and step in {STMT_VINFO,SLP_TREE}_SIMD_CLONE_INFO. */
4371 if (vec_stmt
4372 && i * 3 + 4 <= simd_clone_info.length ()
4373 && simd_clone_info[i * 3 + 2])
4374 {
4375 thisarginfo.linear_step = tree_to_shwi (simd_clone_info[i * 3 + 2]);
4376 thisarginfo.op = simd_clone_info[i * 3 + 1];
4377 thisarginfo.simd_lane_linear
4378 = (simd_clone_info[i * 3 + 3] == boolean_true_node);
4379 /* If loop has been peeled for alignment, we need to adjust it. */
4380 tree n1 = LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo);
4381 tree n2 = LOOP_VINFO_NITERS (loop_vinfo);
4382 if (n1 != n2 && !thisarginfo.simd_lane_linear)
4383 {
4384 tree bias = fold_build2 (MINUS_EXPR, TREE_TYPE (n1), n1, n2);
4385 tree step = simd_clone_info[i * 3 + 2];
4386 tree opt = TREE_TYPE (thisarginfo.op);
4387 bias = fold_convert (TREE_TYPE (step), bias);
4388 bias = fold_build2 (MULT_EXPR, TREE_TYPE (step), bias, step);
4389 thisarginfo.op
4390 = fold_build2 (POINTER_TYPE_P (opt)
4391 ? POINTER_PLUS_EXPR : PLUS_EXPR, opt,
4392 thisarginfo.op, bias);
4393 }
4394 }
4395 else if (!vec_stmt
4396 && thisarginfo.dt != vect_constant_def
4397 && thisarginfo.dt != vect_external_def
4398 && loop_vinfo
4399 && TREE_CODE (op) == SSA_NAME
4400 && simple_iv (loop, loop_containing_stmt (stmt), op,
4401 &iv, false)
4402 && tree_fits_shwi_p (iv.step))
4403 {
4404 thisarginfo.linear_step = tree_to_shwi (iv.step);
4405 thisarginfo.op = iv.base;
4406 }
4407 else if ((thisarginfo.dt == vect_constant_def
4408 || thisarginfo.dt == vect_external_def)
4409 && POINTER_TYPE_P (TREE_TYPE (op)))
4410 thisarginfo.align = get_pointer_alignment (op) / BITS_PER_UNIT;
4411 /* Addresses of array elements indexed by GOMP_SIMD_LANE are
4412 linear too. */
4413 if (POINTER_TYPE_P (TREE_TYPE (op))
4414 && !thisarginfo.linear_step
4415 && !vec_stmt
4416 && thisarginfo.dt != vect_constant_def
4417 && thisarginfo.dt != vect_external_def
4418 && loop_vinfo
4419 && TREE_CODE (op) == SSA_NAME)
4420 vect_simd_lane_linear (op, loop, &thisarginfo);
4421
4422 arginfo.quick_push (thisarginfo);
4423 }
4424
4425 poly_uint64 vf = loop_vinfo ? LOOP_VINFO_VECT_FACTOR (loop_vinfo) : 1;
4426 unsigned group_size = slp_node ? SLP_TREE_LANES (slp_node) : 1;
4427 unsigned int badness = 0;
4428 struct cgraph_node *bestn = NULL;
4429 if (vec_stmt)
4430 bestn = cgraph_node::get (simd_clone_info[0]);
4431 else
4432 for (struct cgraph_node *n = node->simd_clones; n != NULL;
4433 n = n->simdclone->next_clone)
4434 {
4435 unsigned int this_badness = 0;
4436 unsigned int num_calls;
4437 /* The number of arguments in the call and the number of parameters in
4438 the simdclone should match. However, when the simdclone is
4439 'inbranch', it could have one more paramater than nargs when using
4440 an inbranch simdclone to call a non-inbranch call, either in a
4441 non-masked loop using a all true constant mask, or inside a masked
4442 loop using it's mask. */
4443 size_t simd_nargs = n->simdclone->nargs;
4444 if (!masked_call_offset && n->simdclone->inbranch)
4445 simd_nargs--;
4446 if (!constant_multiple_p (vf * group_size, n->simdclone->simdlen,
4447 &num_calls)
4448 || (!n->simdclone->inbranch && (masked_call_offset > 0))
4449 || (nargs != simd_nargs))
4450 continue;
4451 if (num_calls != 1)
4452 this_badness += floor_log2 (num_calls) * 4096;
4453 if (n->simdclone->inbranch)
4454 this_badness += 8192;
4455
4456 /* If STMT_VINFO_VECTYPE has not been set yet pass the general vector
4457 mode, which for targets that use it will determine what ISA we can
4458 vectorize this code with. */
4459 machine_mode vector_mode = vinfo->vector_mode;
4460 if (vectype)
4461 vector_mode = TYPE_MODE (vectype);
4462 int target_badness = targetm.simd_clone.usable (n, vector_mode);
4463 if (target_badness < 0)
4464 continue;
4465 this_badness += target_badness * 512;
4466 for (i = 0; i < nargs; i++)
4467 {
4468 switch (n->simdclone->args[i].arg_type)
4469 {
4470 case SIMD_CLONE_ARG_TYPE_VECTOR:
4471 if (!useless_type_conversion_p
4472 (n->simdclone->args[i].orig_type,
4473 TREE_TYPE (gimple_call_arg (stmt,
4474 i + masked_call_offset))))
4475 i = -1;
4476 else if (arginfo[i].dt == vect_constant_def
4477 || arginfo[i].dt == vect_external_def
4478 || arginfo[i].linear_step)
4479 this_badness += 64;
4480 break;
4481 case SIMD_CLONE_ARG_TYPE_UNIFORM:
4482 if (arginfo[i].dt != vect_constant_def
4483 && arginfo[i].dt != vect_external_def)
4484 i = -1;
4485 break;
4486 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4487 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4488 if (arginfo[i].dt == vect_constant_def
4489 || arginfo[i].dt == vect_external_def
4490 || (arginfo[i].linear_step
4491 != n->simdclone->args[i].linear_step))
4492 i = -1;
4493 break;
4494 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
4495 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
4496 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
4497 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
4498 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
4499 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
4500 /* FORNOW */
4501 i = -1;
4502 break;
4503 case SIMD_CLONE_ARG_TYPE_MASK:
4504 /* While we can create a traditional data vector from
4505 an incoming integer mode mask we have no good way to
4506 force generate an integer mode mask from a traditional
4507 boolean vector input. */
4508 if (SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4509 && !SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4510 i = -1;
4511 else if (!SCALAR_INT_MODE_P (n->simdclone->mask_mode)
4512 && SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype)))
4513 this_badness += 2048;
4514 break;
4515 }
4516 if (i == (size_t) -1)
4517 break;
4518 if (n->simdclone->args[i].alignment > arginfo[i].align)
4519 {
4520 i = -1;
4521 break;
4522 }
4523 if (arginfo[i].align)
4524 this_badness += (exact_log2 (arginfo[i].align)
4525 - exact_log2 (n->simdclone->args[i].alignment));
4526 }
4527 if (i == (size_t) -1)
4528 continue;
4529 if (masked_call_offset == 0
4530 && n->simdclone->inbranch
4531 && n->simdclone->nargs > nargs)
4532 {
4533 gcc_assert (n->simdclone->args[n->simdclone->nargs - 1].arg_type ==
4534 SIMD_CLONE_ARG_TYPE_MASK);
4535 /* Penalize using a masked SIMD clone in a non-masked loop, that is
4536 not in a branch, as we'd have to construct an all-true mask. */
4537 if (!loop_vinfo || !LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4538 this_badness += 64;
4539 }
4540 if (bestn == NULL || this_badness < badness)
4541 {
4542 bestn = n;
4543 badness = this_badness;
4544 }
4545 }
4546
4547 if (bestn == NULL)
4548 return false;
4549
4550 unsigned int num_mask_args = 0;
4551 if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4552 for (i = 0; i < nargs; i++)
4553 if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4554 num_mask_args++;
4555
4556 for (i = 0; i < nargs; i++)
4557 {
4558 if ((arginfo[i].dt == vect_constant_def
4559 || arginfo[i].dt == vect_external_def)
4560 && bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR)
4561 {
4562 tree arg_type = TREE_TYPE (gimple_call_arg (stmt,
4563 i + masked_call_offset));
4564 arginfo[i].vectype = get_vectype_for_scalar_type (vinfo, arg_type,
4565 slp_node);
4566 if (arginfo[i].vectype == NULL
4567 || !constant_multiple_p (bestn->simdclone->simdlen,
4568 TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4569 return false;
4570 }
4571
4572 if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_VECTOR
4573 && VECTOR_BOOLEAN_TYPE_P (bestn->simdclone->args[i].vector_type))
4574 {
4575 if (dump_enabled_p ())
4576 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4577 "vector mask arguments are not supported.\n");
4578 return false;
4579 }
4580
4581 if (bestn->simdclone->args[i].arg_type == SIMD_CLONE_ARG_TYPE_MASK)
4582 {
4583 tree clone_arg_vectype = bestn->simdclone->args[i].vector_type;
4584 if (bestn->simdclone->mask_mode == VOIDmode)
4585 {
4586 if (maybe_ne (TYPE_VECTOR_SUBPARTS (clone_arg_vectype),
4587 TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4588 {
4589 /* FORNOW we only have partial support for vector-type masks
4590 that can't hold all of simdlen. */
4591 if (dump_enabled_p ())
4592 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4593 vect_location,
4594 "in-branch vector clones are not yet"
4595 " supported for mismatched vector sizes.\n");
4596 return false;
4597 }
4598 if (!expand_vec_cond_expr_p (clone_arg_vectype,
4599 arginfo[i].vectype))
4600 {
4601 if (dump_enabled_p ())
4602 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4603 vect_location,
4604 "cannot compute mask argument for"
4605 " in-branch vector clones.\n");
4606 return false;
4607 }
4608 }
4609 else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4610 {
4611 if (!SCALAR_INT_MODE_P (TYPE_MODE (arginfo[i].vectype))
4612 || maybe_ne (exact_div (bestn->simdclone->simdlen,
4613 num_mask_args),
4614 TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4615 {
4616 /* FORNOW we only have partial support for integer-type masks
4617 that represent the same number of lanes as the
4618 vectorized mask inputs. */
4619 if (dump_enabled_p ())
4620 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4621 vect_location,
4622 "in-branch vector clones are not yet "
4623 "supported for mismatched vector sizes.\n");
4624 return false;
4625 }
4626 }
4627 else
4628 {
4629 if (dump_enabled_p ())
4630 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
4631 vect_location,
4632 "in-branch vector clones not supported"
4633 " on this target.\n");
4634 return false;
4635 }
4636 }
4637 }
4638
4639 fndecl = bestn->decl;
4640 nunits = bestn->simdclone->simdlen;
4641 if (slp_node)
4642 ncopies = vector_unroll_factor (vf * group_size, nunits);
4643 else
4644 ncopies = vector_unroll_factor (vf, nunits);
4645
4646 /* If the function isn't const, only allow it in simd loops where user
4647 has asserted that at least nunits consecutive iterations can be
4648 performed using SIMD instructions. */
4649 if ((loop == NULL || maybe_lt ((unsigned) loop->safelen, nunits))
4650 && gimple_vuse (stmt))
4651 return false;
4652
4653 /* Sanity check: make sure that at least one copy of the vectorized stmt
4654 needs to be generated. */
4655 gcc_assert (ncopies >= 1);
4656
4657 if (!vec_stmt) /* transformation not required. */
4658 {
4659 if (slp_node)
4660 for (unsigned i = 0; i < nargs; ++i)
4661 if (!vect_maybe_update_slp_op_vectype (slp_op[i], arginfo[i].vectype))
4662 {
4663 if (dump_enabled_p ())
4664 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4665 "incompatible vector types for invariants\n");
4666 return false;
4667 }
4668 /* When the original call is pure or const but the SIMD ABI dictates
4669 an aggregate return we will have to use a virtual definition and
4670 in a loop eventually even need to add a virtual PHI. That's
4671 not straight-forward so allow to fix this up via renaming. */
4672 if (gimple_call_lhs (stmt)
4673 && !gimple_vdef (stmt)
4674 && TREE_CODE (TREE_TYPE (TREE_TYPE (bestn->decl))) == ARRAY_TYPE)
4675 vinfo->any_known_not_updated_vssa = true;
4676 /* ??? For SLP code-gen we end up inserting after the last
4677 vector argument def rather than at the original call position
4678 so automagic virtual operand updating doesn't work. */
4679 if (gimple_vuse (stmt) && slp_node)
4680 vinfo->any_known_not_updated_vssa = true;
4681 simd_clone_info.safe_push (bestn->decl);
4682 for (i = 0; i < bestn->simdclone->nargs; i++)
4683 {
4684 switch (bestn->simdclone->args[i].arg_type)
4685 {
4686 default:
4687 continue;
4688 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
4689 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
4690 {
4691 simd_clone_info.safe_grow_cleared (i * 3 + 1, true);
4692 simd_clone_info.safe_push (arginfo[i].op);
4693 tree lst = POINTER_TYPE_P (TREE_TYPE (arginfo[i].op))
4694 ? size_type_node : TREE_TYPE (arginfo[i].op);
4695 tree ls = build_int_cst (lst, arginfo[i].linear_step);
4696 simd_clone_info.safe_push (ls);
4697 tree sll = arginfo[i].simd_lane_linear
4698 ? boolean_true_node : boolean_false_node;
4699 simd_clone_info.safe_push (sll);
4700 }
4701 break;
4702 case SIMD_CLONE_ARG_TYPE_MASK:
4703 if (loop_vinfo
4704 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4705 vect_record_loop_mask (loop_vinfo,
4706 &LOOP_VINFO_MASKS (loop_vinfo),
4707 ncopies, vectype, op);
4708
4709 break;
4710 }
4711 }
4712
4713 if (!bestn->simdclone->inbranch && loop_vinfo)
4714 {
4715 if (dump_enabled_p ()
4716 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
4717 dump_printf_loc (MSG_NOTE, vect_location,
4718 "can't use a fully-masked loop because a"
4719 " non-masked simd clone was selected.\n");
4720 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
4721 }
4722
4723 STMT_VINFO_TYPE (stmt_info) = call_simd_clone_vec_info_type;
4724 DUMP_VECT_SCOPE ("vectorizable_simd_clone_call");
4725 /* vect_model_simple_cost (vinfo, stmt_info, ncopies,
4726 dt, slp_node, cost_vec); */
4727 return true;
4728 }
4729
4730 /* Transform. */
4731
4732 if (dump_enabled_p ())
4733 dump_printf_loc (MSG_NOTE, vect_location, "transform call.\n");
4734
4735 /* Handle def. */
4736 scalar_dest = gimple_call_lhs (stmt);
4737 vec_dest = NULL_TREE;
4738 rtype = NULL_TREE;
4739 ratype = NULL_TREE;
4740 if (scalar_dest)
4741 {
4742 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4743 rtype = TREE_TYPE (TREE_TYPE (fndecl));
4744 if (TREE_CODE (rtype) == ARRAY_TYPE)
4745 {
4746 ratype = rtype;
4747 rtype = TREE_TYPE (ratype);
4748 }
4749 }
4750
4751 auto_vec<vec<tree> > vec_oprnds;
4752 auto_vec<unsigned> vec_oprnds_i;
4753 vec_oprnds_i.safe_grow_cleared (nargs, true);
4754 if (slp_node)
4755 {
4756 vec_oprnds.reserve_exact (nargs);
4757 vect_get_slp_defs (vinfo, slp_node, &vec_oprnds);
4758 }
4759 else
4760 vec_oprnds.safe_grow_cleared (nargs, true);
4761 for (j = 0; j < ncopies; ++j)
4762 {
4763 poly_uint64 callee_nelements;
4764 poly_uint64 caller_nelements;
4765 /* Build argument list for the vectorized call. */
4766 if (j == 0)
4767 vargs.create (nargs);
4768 else
4769 vargs.truncate (0);
4770
4771 for (i = 0; i < nargs; i++)
4772 {
4773 unsigned int k, l, m, o;
4774 tree atype;
4775 op = gimple_call_arg (stmt, i + masked_call_offset);
4776 switch (bestn->simdclone->args[i].arg_type)
4777 {
4778 case SIMD_CLONE_ARG_TYPE_VECTOR:
4779 atype = bestn->simdclone->args[i].vector_type;
4780 caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4781 callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4782 o = vector_unroll_factor (nunits, callee_nelements);
4783 for (m = j * o; m < (j + 1) * o; m++)
4784 {
4785 if (known_lt (callee_nelements, caller_nelements))
4786 {
4787 poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (atype));
4788 if (!constant_multiple_p (caller_nelements,
4789 callee_nelements, &k))
4790 gcc_unreachable ();
4791
4792 gcc_assert ((k & (k - 1)) == 0);
4793 if (m == 0)
4794 {
4795 if (!slp_node)
4796 vect_get_vec_defs_for_operand (vinfo, stmt_info,
4797 ncopies * o / k, op,
4798 &vec_oprnds[i]);
4799 vec_oprnds_i[i] = 0;
4800 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4801 }
4802 else
4803 {
4804 vec_oprnd0 = arginfo[i].op;
4805 if ((m & (k - 1)) == 0)
4806 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4807 }
4808 arginfo[i].op = vec_oprnd0;
4809 vec_oprnd0
4810 = build3 (BIT_FIELD_REF, atype, vec_oprnd0,
4811 bitsize_int (prec),
4812 bitsize_int ((m & (k - 1)) * prec));
4813 gassign *new_stmt
4814 = gimple_build_assign (make_ssa_name (atype),
4815 vec_oprnd0);
4816 vect_finish_stmt_generation (vinfo, stmt_info,
4817 new_stmt, gsi);
4818 vargs.safe_push (gimple_assign_lhs (new_stmt));
4819 }
4820 else
4821 {
4822 if (!constant_multiple_p (callee_nelements,
4823 caller_nelements, &k))
4824 gcc_unreachable ();
4825 gcc_assert ((k & (k - 1)) == 0);
4826 vec<constructor_elt, va_gc> *ctor_elts;
4827 if (k != 1)
4828 vec_alloc (ctor_elts, k);
4829 else
4830 ctor_elts = NULL;
4831 for (l = 0; l < k; l++)
4832 {
4833 if (m == 0 && l == 0)
4834 {
4835 if (!slp_node)
4836 vect_get_vec_defs_for_operand (vinfo, stmt_info,
4837 k * o * ncopies,
4838 op,
4839 &vec_oprnds[i]);
4840 vec_oprnds_i[i] = 0;
4841 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4842 }
4843 else
4844 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4845 arginfo[i].op = vec_oprnd0;
4846 if (k == 1)
4847 break;
4848 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE,
4849 vec_oprnd0);
4850 }
4851 if (k == 1)
4852 if (!useless_type_conversion_p (TREE_TYPE (vec_oprnd0),
4853 atype))
4854 {
4855 vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, atype,
4856 vec_oprnd0);
4857 gassign *new_stmt
4858 = gimple_build_assign (make_ssa_name (atype),
4859 vec_oprnd0);
4860 vect_finish_stmt_generation (vinfo, stmt_info,
4861 new_stmt, gsi);
4862 vargs.safe_push (gimple_get_lhs (new_stmt));
4863 }
4864 else
4865 vargs.safe_push (vec_oprnd0);
4866 else
4867 {
4868 vec_oprnd0 = build_constructor (atype, ctor_elts);
4869 gassign *new_stmt
4870 = gimple_build_assign (make_ssa_name (atype),
4871 vec_oprnd0);
4872 vect_finish_stmt_generation (vinfo, stmt_info,
4873 new_stmt, gsi);
4874 vargs.safe_push (gimple_assign_lhs (new_stmt));
4875 }
4876 }
4877 }
4878 break;
4879 case SIMD_CLONE_ARG_TYPE_MASK:
4880 if (bestn->simdclone->mask_mode == VOIDmode)
4881 {
4882 atype = bestn->simdclone->args[i].vector_type;
4883 tree elt_type = TREE_TYPE (atype);
4884 tree one = fold_convert (elt_type, integer_one_node);
4885 tree zero = fold_convert (elt_type, integer_zero_node);
4886 callee_nelements = TYPE_VECTOR_SUBPARTS (atype);
4887 caller_nelements = TYPE_VECTOR_SUBPARTS (arginfo[i].vectype);
4888 o = vector_unroll_factor (nunits, callee_nelements);
4889 for (m = j * o; m < (j + 1) * o; m++)
4890 {
4891 if (maybe_lt (callee_nelements, caller_nelements))
4892 {
4893 /* The mask type has fewer elements than simdlen. */
4894
4895 /* FORNOW */
4896 gcc_unreachable ();
4897 }
4898 else if (known_eq (callee_nelements, caller_nelements))
4899 {
4900 /* The SIMD clone function has the same number of
4901 elements as the current function. */
4902 if (m == 0)
4903 {
4904 if (!slp_node)
4905 vect_get_vec_defs_for_operand (vinfo, stmt_info,
4906 o * ncopies,
4907 op,
4908 &vec_oprnds[i]);
4909 vec_oprnds_i[i] = 0;
4910 }
4911 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4912 if (loop_vinfo
4913 && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
4914 {
4915 vec_loop_masks *loop_masks
4916 = &LOOP_VINFO_MASKS (loop_vinfo);
4917 tree loop_mask
4918 = vect_get_loop_mask (loop_vinfo, gsi,
4919 loop_masks, ncopies,
4920 vectype, j);
4921 vec_oprnd0
4922 = prepare_vec_mask (loop_vinfo,
4923 TREE_TYPE (loop_mask),
4924 loop_mask, vec_oprnd0,
4925 gsi);
4926 loop_vinfo->vec_cond_masked_set.add ({ vec_oprnd0,
4927 loop_mask });
4928
4929 }
4930 vec_oprnd0
4931 = build3 (VEC_COND_EXPR, atype, vec_oprnd0,
4932 build_vector_from_val (atype, one),
4933 build_vector_from_val (atype, zero));
4934 gassign *new_stmt
4935 = gimple_build_assign (make_ssa_name (atype),
4936 vec_oprnd0);
4937 vect_finish_stmt_generation (vinfo, stmt_info,
4938 new_stmt, gsi);
4939 vargs.safe_push (gimple_assign_lhs (new_stmt));
4940 }
4941 else
4942 {
4943 /* The mask type has more elements than simdlen. */
4944
4945 /* FORNOW */
4946 gcc_unreachable ();
4947 }
4948 }
4949 }
4950 else if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
4951 {
4952 atype = bestn->simdclone->args[i].vector_type;
4953 /* Guess the number of lanes represented by atype. */
4954 poly_uint64 atype_subparts
4955 = exact_div (bestn->simdclone->simdlen,
4956 num_mask_args);
4957 o = vector_unroll_factor (nunits, atype_subparts);
4958 for (m = j * o; m < (j + 1) * o; m++)
4959 {
4960 if (m == 0)
4961 {
4962 if (!slp_node)
4963 vect_get_vec_defs_for_operand (vinfo, stmt_info,
4964 o * ncopies,
4965 op,
4966 &vec_oprnds[i]);
4967 vec_oprnds_i[i] = 0;
4968 }
4969 if (maybe_lt (atype_subparts,
4970 TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4971 {
4972 /* The mask argument has fewer elements than the
4973 input vector. */
4974 /* FORNOW */
4975 gcc_unreachable ();
4976 }
4977 else if (known_eq (atype_subparts,
4978 TYPE_VECTOR_SUBPARTS (arginfo[i].vectype)))
4979 {
4980 /* The vector mask argument matches the input
4981 in the number of lanes, but not necessarily
4982 in the mode. */
4983 vec_oprnd0 = vec_oprnds[i][vec_oprnds_i[i]++];
4984 tree st = lang_hooks.types.type_for_mode
4985 (TYPE_MODE (TREE_TYPE (vec_oprnd0)), 1);
4986 vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, st,
4987 vec_oprnd0);
4988 gassign *new_stmt
4989 = gimple_build_assign (make_ssa_name (st),
4990 vec_oprnd0);
4991 vect_finish_stmt_generation (vinfo, stmt_info,
4992 new_stmt, gsi);
4993 if (!types_compatible_p (atype, st))
4994 {
4995 new_stmt
4996 = gimple_build_assign (make_ssa_name (atype),
4997 NOP_EXPR,
4998 gimple_assign_lhs
4999 (new_stmt));
5000 vect_finish_stmt_generation (vinfo, stmt_info,
5001 new_stmt, gsi);
5002 }
5003 vargs.safe_push (gimple_assign_lhs (new_stmt));
5004 }
5005 else
5006 {
5007 /* The mask argument has more elements than the
5008 input vector. */
5009 /* FORNOW */
5010 gcc_unreachable ();
5011 }
5012 }
5013 }
5014 else
5015 gcc_unreachable ();
5016 break;
5017 case SIMD_CLONE_ARG_TYPE_UNIFORM:
5018 vargs.safe_push (op);
5019 break;
5020 case SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP:
5021 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_CONSTANT_STEP:
5022 if (j == 0)
5023 {
5024 gimple_seq stmts;
5025 arginfo[i].op
5026 = force_gimple_operand (unshare_expr (arginfo[i].op),
5027 &stmts, true, NULL_TREE);
5028 if (stmts != NULL)
5029 {
5030 basic_block new_bb;
5031 edge pe = loop_preheader_edge (loop);
5032 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
5033 gcc_assert (!new_bb);
5034 }
5035 if (arginfo[i].simd_lane_linear)
5036 {
5037 vargs.safe_push (arginfo[i].op);
5038 break;
5039 }
5040 tree phi_res = copy_ssa_name (op);
5041 gphi *new_phi = create_phi_node (phi_res, loop->header);
5042 add_phi_arg (new_phi, arginfo[i].op,
5043 loop_preheader_edge (loop), UNKNOWN_LOCATION);
5044 enum tree_code code
5045 = POINTER_TYPE_P (TREE_TYPE (op))
5046 ? POINTER_PLUS_EXPR : PLUS_EXPR;
5047 tree type = POINTER_TYPE_P (TREE_TYPE (op))
5048 ? sizetype : TREE_TYPE (op);
5049 poly_widest_int cst
5050 = wi::mul (bestn->simdclone->args[i].linear_step,
5051 ncopies * nunits);
5052 tree tcst = wide_int_to_tree (type, cst);
5053 tree phi_arg = copy_ssa_name (op);
5054 gassign *new_stmt
5055 = gimple_build_assign (phi_arg, code, phi_res, tcst);
5056 gimple_stmt_iterator si = gsi_after_labels (loop->header);
5057 gsi_insert_after (&si, new_stmt, GSI_NEW_STMT);
5058 add_phi_arg (new_phi, phi_arg, loop_latch_edge (loop),
5059 UNKNOWN_LOCATION);
5060 arginfo[i].op = phi_res;
5061 vargs.safe_push (phi_res);
5062 }
5063 else
5064 {
5065 enum tree_code code
5066 = POINTER_TYPE_P (TREE_TYPE (op))
5067 ? POINTER_PLUS_EXPR : PLUS_EXPR;
5068 tree type = POINTER_TYPE_P (TREE_TYPE (op))
5069 ? sizetype : TREE_TYPE (op);
5070 poly_widest_int cst
5071 = wi::mul (bestn->simdclone->args[i].linear_step,
5072 j * nunits);
5073 tree tcst = wide_int_to_tree (type, cst);
5074 new_temp = make_ssa_name (TREE_TYPE (op));
5075 gassign *new_stmt
5076 = gimple_build_assign (new_temp, code,
5077 arginfo[i].op, tcst);
5078 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5079 vargs.safe_push (new_temp);
5080 }
5081 break;
5082 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_CONSTANT_STEP:
5083 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_CONSTANT_STEP:
5084 case SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP:
5085 case SIMD_CLONE_ARG_TYPE_LINEAR_REF_VARIABLE_STEP:
5086 case SIMD_CLONE_ARG_TYPE_LINEAR_VAL_VARIABLE_STEP:
5087 case SIMD_CLONE_ARG_TYPE_LINEAR_UVAL_VARIABLE_STEP:
5088 default:
5089 gcc_unreachable ();
5090 }
5091 }
5092
5093 if (masked_call_offset == 0
5094 && bestn->simdclone->inbranch
5095 && bestn->simdclone->nargs > nargs)
5096 {
5097 unsigned long m, o;
5098 size_t mask_i = bestn->simdclone->nargs - 1;
5099 tree mask;
5100 gcc_assert (bestn->simdclone->args[mask_i].arg_type ==
5101 SIMD_CLONE_ARG_TYPE_MASK);
5102
5103 tree masktype = bestn->simdclone->args[mask_i].vector_type;
5104 if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
5105 /* Guess the number of lanes represented by masktype. */
5106 callee_nelements = exact_div (bestn->simdclone->simdlen,
5107 bestn->simdclone->nargs - nargs);
5108 else
5109 callee_nelements = TYPE_VECTOR_SUBPARTS (masktype);
5110 o = vector_unroll_factor (nunits, callee_nelements);
5111 for (m = j * o; m < (j + 1) * o; m++)
5112 {
5113 if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
5114 {
5115 vec_loop_masks *loop_masks = &LOOP_VINFO_MASKS (loop_vinfo);
5116 mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
5117 ncopies, masktype, j);
5118 }
5119 else
5120 mask = vect_build_all_ones_mask (vinfo, stmt_info, masktype);
5121
5122 gassign *new_stmt;
5123 if (SCALAR_INT_MODE_P (bestn->simdclone->mask_mode))
5124 {
5125 /* This means we are dealing with integer mask modes.
5126 First convert to an integer type with the same size as
5127 the current vector type. */
5128 unsigned HOST_WIDE_INT intermediate_size
5129 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (mask)));
5130 tree mid_int_type =
5131 build_nonstandard_integer_type (intermediate_size, 1);
5132 mask = build1 (VIEW_CONVERT_EXPR, mid_int_type, mask);
5133 new_stmt
5134 = gimple_build_assign (make_ssa_name (mid_int_type),
5135 mask);
5136 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
5137 /* Then zero-extend to the mask mode. */
5138 mask = fold_build1 (NOP_EXPR, masktype,
5139 gimple_get_lhs (new_stmt));
5140 }
5141 else if (bestn->simdclone->mask_mode == VOIDmode)
5142 {
5143 tree one = fold_convert (TREE_TYPE (masktype),
5144 integer_one_node);
5145 tree zero = fold_convert (TREE_TYPE (masktype),
5146 integer_zero_node);
5147 mask = build3 (VEC_COND_EXPR, masktype, mask,
5148 build_vector_from_val (masktype, one),
5149 build_vector_from_val (masktype, zero));
5150 }
5151 else
5152 gcc_unreachable ();
5153
5154 new_stmt = gimple_build_assign (make_ssa_name (masktype), mask);
5155 vect_finish_stmt_generation (vinfo, stmt_info,
5156 new_stmt, gsi);
5157 mask = gimple_assign_lhs (new_stmt);
5158 vargs.safe_push (mask);
5159 }
5160 }
5161
5162 gcall *new_call = gimple_build_call_vec (fndecl, vargs);
5163 if (vec_dest)
5164 {
5165 gcc_assert (ratype
5166 || known_eq (TYPE_VECTOR_SUBPARTS (rtype), nunits));
5167 if (ratype)
5168 new_temp = create_tmp_var (ratype);
5169 else if (useless_type_conversion_p (vectype, rtype))
5170 new_temp = make_ssa_name (vec_dest, new_call);
5171 else
5172 new_temp = make_ssa_name (rtype, new_call);
5173 gimple_call_set_lhs (new_call, new_temp);
5174 }
5175 vect_finish_stmt_generation (vinfo, stmt_info, new_call, gsi);
5176 gimple *new_stmt = new_call;
5177
5178 if (vec_dest)
5179 {
5180 if (!multiple_p (TYPE_VECTOR_SUBPARTS (vectype), nunits))
5181 {
5182 unsigned int k, l;
5183 poly_uint64 prec = GET_MODE_BITSIZE (TYPE_MODE (vectype));
5184 poly_uint64 bytes = GET_MODE_SIZE (TYPE_MODE (vectype));
5185 k = vector_unroll_factor (nunits,
5186 TYPE_VECTOR_SUBPARTS (vectype));
5187 gcc_assert ((k & (k - 1)) == 0);
5188 for (l = 0; l < k; l++)
5189 {
5190 tree t;
5191 if (ratype)
5192 {
5193 t = build_fold_addr_expr (new_temp);
5194 t = build2 (MEM_REF, vectype, t,
5195 build_int_cst (TREE_TYPE (t), l * bytes));
5196 }
5197 else
5198 t = build3 (BIT_FIELD_REF, vectype, new_temp,
5199 bitsize_int (prec), bitsize_int (l * prec));
5200 new_stmt = gimple_build_assign (make_ssa_name (vectype), t);
5201 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5202
5203 if (j == 0 && l == 0)
5204 *vec_stmt = new_stmt;
5205 if (slp_node)
5206 SLP_TREE_VEC_DEFS (slp_node)
5207 .quick_push (gimple_assign_lhs (new_stmt));
5208 else
5209 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5210 }
5211
5212 if (ratype)
5213 vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5214 continue;
5215 }
5216 else if (!multiple_p (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
5217 {
5218 unsigned int k;
5219 if (!constant_multiple_p (TYPE_VECTOR_SUBPARTS (vectype),
5220 TYPE_VECTOR_SUBPARTS (rtype), &k))
5221 gcc_unreachable ();
5222 gcc_assert ((k & (k - 1)) == 0);
5223 if ((j & (k - 1)) == 0)
5224 vec_alloc (ret_ctor_elts, k);
5225 if (ratype)
5226 {
5227 unsigned int m, o;
5228 o = vector_unroll_factor (nunits,
5229 TYPE_VECTOR_SUBPARTS (rtype));
5230 for (m = 0; m < o; m++)
5231 {
5232 tree tem = build4 (ARRAY_REF, rtype, new_temp,
5233 size_int (m), NULL_TREE, NULL_TREE);
5234 new_stmt = gimple_build_assign (make_ssa_name (rtype),
5235 tem);
5236 vect_finish_stmt_generation (vinfo, stmt_info,
5237 new_stmt, gsi);
5238 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE,
5239 gimple_assign_lhs (new_stmt));
5240 }
5241 vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5242 }
5243 else
5244 CONSTRUCTOR_APPEND_ELT (ret_ctor_elts, NULL_TREE, new_temp);
5245 if ((j & (k - 1)) != k - 1)
5246 continue;
5247 vec_oprnd0 = build_constructor (vectype, ret_ctor_elts);
5248 new_stmt
5249 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5250 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5251
5252 if ((unsigned) j == k - 1)
5253 *vec_stmt = new_stmt;
5254 if (slp_node)
5255 SLP_TREE_VEC_DEFS (slp_node)
5256 .quick_push (gimple_assign_lhs (new_stmt));
5257 else
5258 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5259 continue;
5260 }
5261 else if (ratype)
5262 {
5263 tree t = build_fold_addr_expr (new_temp);
5264 t = build2 (MEM_REF, vectype, t,
5265 build_int_cst (TREE_TYPE (t), 0));
5266 new_stmt = gimple_build_assign (make_ssa_name (vec_dest), t);
5267 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5268 vect_clobber_variable (vinfo, stmt_info, gsi, new_temp);
5269 }
5270 else if (!useless_type_conversion_p (vectype, rtype))
5271 {
5272 vec_oprnd0 = build1 (VIEW_CONVERT_EXPR, vectype, new_temp);
5273 new_stmt
5274 = gimple_build_assign (make_ssa_name (vec_dest), vec_oprnd0);
5275 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5276 }
5277 }
5278
5279 if (j == 0)
5280 *vec_stmt = new_stmt;
5281 if (slp_node)
5282 SLP_TREE_VEC_DEFS (slp_node).quick_push (gimple_get_lhs (new_stmt));
5283 else
5284 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5285 }
5286
5287 for (i = 0; i < nargs; ++i)
5288 {
5289 vec<tree> oprndsi = vec_oprnds[i];
5290 oprndsi.release ();
5291 }
5292 vargs.release ();
5293
5294 /* Mark the clone as no longer being a candidate for GC. */
5295 bestn->gc_candidate = false;
5296
5297 /* The call in STMT might prevent it from being removed in dce.
5298 We however cannot remove it here, due to the way the ssa name
5299 it defines is mapped to the new definition. So just replace
5300 rhs of the statement with something harmless. */
5301
5302 if (slp_node)
5303 return true;
5304
5305 gimple *new_stmt;
5306 if (scalar_dest)
5307 {
5308 type = TREE_TYPE (scalar_dest);
5309 lhs = gimple_call_lhs (vect_orig_stmt (stmt_info)->stmt);
5310 new_stmt = gimple_build_assign (lhs, build_zero_cst (type));
5311 }
5312 else
5313 new_stmt = gimple_build_nop ();
5314 vinfo->replace_stmt (gsi, vect_orig_stmt (stmt_info), new_stmt);
5315 unlink_stmt_vdef (stmt);
5316
5317 return true;
5318 }
5319
5320
5321 /* Function vect_gen_widened_results_half
5322
5323 Create a vector stmt whose code, type, number of arguments, and result
5324 variable are CODE, OP_TYPE, and VEC_DEST, and its arguments are
5325 VEC_OPRND0 and VEC_OPRND1. The new vector stmt is to be inserted at GSI.
5326 In the case that CODE is a CALL_EXPR, this means that a call to DECL
5327 needs to be created (DECL is a function-decl of a target-builtin).
5328 STMT_INFO is the original scalar stmt that we are vectorizing. */
5329
5330 static gimple *
5331 vect_gen_widened_results_half (vec_info *vinfo, code_helper ch,
5332 tree vec_oprnd0, tree vec_oprnd1, int op_type,
5333 tree vec_dest, gimple_stmt_iterator *gsi,
5334 stmt_vec_info stmt_info)
5335 {
5336 gimple *new_stmt;
5337 tree new_temp;
5338
5339 /* Generate half of the widened result: */
5340 if (op_type != binary_op)
5341 vec_oprnd1 = NULL;
5342 new_stmt = vect_gimple_build (vec_dest, ch, vec_oprnd0, vec_oprnd1);
5343 new_temp = make_ssa_name (vec_dest, new_stmt);
5344 gimple_set_lhs (new_stmt, new_temp);
5345 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5346
5347 return new_stmt;
5348 }
5349
5350
5351 /* Create vectorized demotion statements for vector operands from VEC_OPRNDS.
5352 For multi-step conversions store the resulting vectors and call the function
5353 recursively. When NARROW_SRC_P is true, there's still a conversion after
5354 narrowing, don't store the vectors in the SLP_NODE or in vector info of
5355 the scalar statement(or in STMT_VINFO_RELATED_STMT chain). */
5356
5357 static void
5358 vect_create_vectorized_demotion_stmts (vec_info *vinfo, vec<tree> *vec_oprnds,
5359 int multi_step_cvt,
5360 stmt_vec_info stmt_info,
5361 vec<tree> &vec_dsts,
5362 gimple_stmt_iterator *gsi,
5363 slp_tree slp_node, code_helper code,
5364 bool narrow_src_p)
5365 {
5366 unsigned int i;
5367 tree vop0, vop1, new_tmp, vec_dest;
5368
5369 vec_dest = vec_dsts.pop ();
5370
5371 for (i = 0; i < vec_oprnds->length (); i += 2)
5372 {
5373 /* Create demotion operation. */
5374 vop0 = (*vec_oprnds)[i];
5375 vop1 = (*vec_oprnds)[i + 1];
5376 gimple *new_stmt = vect_gimple_build (vec_dest, code, vop0, vop1);
5377 new_tmp = make_ssa_name (vec_dest, new_stmt);
5378 gimple_set_lhs (new_stmt, new_tmp);
5379 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
5380 if (multi_step_cvt || narrow_src_p)
5381 /* Store the resulting vector for next recursive call,
5382 or return the resulting vector_tmp for NARROW FLOAT_EXPR. */
5383 (*vec_oprnds)[i/2] = new_tmp;
5384 else
5385 {
5386 /* This is the last step of the conversion sequence. Store the
5387 vectors in SLP_NODE or in vector info of the scalar statement
5388 (or in STMT_VINFO_RELATED_STMT chain). */
5389 if (slp_node)
5390 slp_node->push_vec_def (new_stmt);
5391 else
5392 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
5393 }
5394 }
5395
5396 /* For multi-step demotion operations we first generate demotion operations
5397 from the source type to the intermediate types, and then combine the
5398 results (stored in VEC_OPRNDS) in demotion operation to the destination
5399 type. */
5400 if (multi_step_cvt)
5401 {
5402 /* At each level of recursion we have half of the operands we had at the
5403 previous level. */
5404 vec_oprnds->truncate ((i+1)/2);
5405 vect_create_vectorized_demotion_stmts (vinfo, vec_oprnds,
5406 multi_step_cvt - 1,
5407 stmt_info, vec_dsts, gsi,
5408 slp_node, VEC_PACK_TRUNC_EXPR,
5409 narrow_src_p);
5410 }
5411
5412 vec_dsts.quick_push (vec_dest);
5413 }
5414
5415
5416 /* Create vectorized promotion statements for vector operands from VEC_OPRNDS0
5417 and VEC_OPRNDS1, for a binary operation associated with scalar statement
5418 STMT_INFO. For multi-step conversions store the resulting vectors and
5419 call the function recursively. */
5420
5421 static void
5422 vect_create_vectorized_promotion_stmts (vec_info *vinfo,
5423 vec<tree> *vec_oprnds0,
5424 vec<tree> *vec_oprnds1,
5425 stmt_vec_info stmt_info, tree vec_dest,
5426 gimple_stmt_iterator *gsi,
5427 code_helper ch1,
5428 code_helper ch2, int op_type)
5429 {
5430 int i;
5431 tree vop0, vop1, new_tmp1, new_tmp2;
5432 gimple *new_stmt1, *new_stmt2;
5433 vec<tree> vec_tmp = vNULL;
5434
5435 vec_tmp.create (vec_oprnds0->length () * 2);
5436 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5437 {
5438 if (op_type == binary_op)
5439 vop1 = (*vec_oprnds1)[i];
5440 else
5441 vop1 = NULL_TREE;
5442
5443 /* Generate the two halves of promotion operation. */
5444 new_stmt1 = vect_gen_widened_results_half (vinfo, ch1, vop0, vop1,
5445 op_type, vec_dest, gsi,
5446 stmt_info);
5447 new_stmt2 = vect_gen_widened_results_half (vinfo, ch2, vop0, vop1,
5448 op_type, vec_dest, gsi,
5449 stmt_info);
5450 if (is_gimple_call (new_stmt1))
5451 {
5452 new_tmp1 = gimple_call_lhs (new_stmt1);
5453 new_tmp2 = gimple_call_lhs (new_stmt2);
5454 }
5455 else
5456 {
5457 new_tmp1 = gimple_assign_lhs (new_stmt1);
5458 new_tmp2 = gimple_assign_lhs (new_stmt2);
5459 }
5460
5461 /* Store the results for the next step. */
5462 vec_tmp.quick_push (new_tmp1);
5463 vec_tmp.quick_push (new_tmp2);
5464 }
5465
5466 vec_oprnds0->release ();
5467 *vec_oprnds0 = vec_tmp;
5468 }
5469
5470 /* Create vectorized promotion stmts for widening stmts using only half the
5471 potential vector size for input. */
5472 static void
5473 vect_create_half_widening_stmts (vec_info *vinfo,
5474 vec<tree> *vec_oprnds0,
5475 vec<tree> *vec_oprnds1,
5476 stmt_vec_info stmt_info, tree vec_dest,
5477 gimple_stmt_iterator *gsi,
5478 code_helper code1,
5479 int op_type)
5480 {
5481 int i;
5482 tree vop0, vop1;
5483 gimple *new_stmt1;
5484 gimple *new_stmt2;
5485 gimple *new_stmt3;
5486 vec<tree> vec_tmp = vNULL;
5487
5488 vec_tmp.create (vec_oprnds0->length ());
5489 FOR_EACH_VEC_ELT (*vec_oprnds0, i, vop0)
5490 {
5491 tree new_tmp1, new_tmp2, new_tmp3, out_type;
5492
5493 gcc_assert (op_type == binary_op);
5494 vop1 = (*vec_oprnds1)[i];
5495
5496 /* Widen the first vector input. */
5497 out_type = TREE_TYPE (vec_dest);
5498 new_tmp1 = make_ssa_name (out_type);
5499 new_stmt1 = gimple_build_assign (new_tmp1, NOP_EXPR, vop0);
5500 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt1, gsi);
5501 if (VECTOR_TYPE_P (TREE_TYPE (vop1)))
5502 {
5503 /* Widen the second vector input. */
5504 new_tmp2 = make_ssa_name (out_type);
5505 new_stmt2 = gimple_build_assign (new_tmp2, NOP_EXPR, vop1);
5506 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt2, gsi);
5507 /* Perform the operation. With both vector inputs widened. */
5508 new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, new_tmp2);
5509 }
5510 else
5511 {
5512 /* Perform the operation. With the single vector input widened. */
5513 new_stmt3 = vect_gimple_build (vec_dest, code1, new_tmp1, vop1);
5514 }
5515
5516 new_tmp3 = make_ssa_name (vec_dest, new_stmt3);
5517 gimple_assign_set_lhs (new_stmt3, new_tmp3);
5518 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt3, gsi);
5519
5520 /* Store the results for the next step. */
5521 vec_tmp.quick_push (new_tmp3);
5522 }
5523
5524 vec_oprnds0->release ();
5525 *vec_oprnds0 = vec_tmp;
5526 }
5527
5528
5529 /* Check if STMT_INFO performs a conversion operation that can be vectorized.
5530 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
5531 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5532 Return true if STMT_INFO is vectorizable in this way. */
5533
5534 static bool
5535 vectorizable_conversion (vec_info *vinfo,
5536 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
5537 gimple **vec_stmt, slp_tree slp_node,
5538 stmt_vector_for_cost *cost_vec)
5539 {
5540 tree vec_dest, cvt_op = NULL_TREE;
5541 tree scalar_dest;
5542 tree op0, op1 = NULL_TREE;
5543 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
5544 tree_code tc1;
5545 code_helper code, code1, code2;
5546 code_helper codecvt1 = ERROR_MARK, codecvt2 = ERROR_MARK;
5547 tree new_temp;
5548 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
5549 int ndts = 2;
5550 poly_uint64 nunits_in;
5551 poly_uint64 nunits_out;
5552 tree vectype_out, vectype_in;
5553 int ncopies, i;
5554 tree lhs_type, rhs_type;
5555 /* For conversions between floating point and integer, there're 2 NARROW
5556 cases. NARROW_SRC is for FLOAT_EXPR, means
5557 integer --DEMOTION--> integer --FLOAT_EXPR--> floating point.
5558 This is safe when the range of the source integer can fit into the lower
5559 precision. NARROW_DST is for FIX_TRUNC_EXPR, means
5560 floating point --FIX_TRUNC_EXPR--> integer --DEMOTION--> INTEGER.
5561 For other conversions, when there's narrowing, NARROW_DST is used as
5562 default. */
5563 enum { NARROW_SRC, NARROW_DST, NONE, WIDEN } modifier;
5564 vec<tree> vec_oprnds0 = vNULL;
5565 vec<tree> vec_oprnds1 = vNULL;
5566 tree vop0;
5567 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
5568 int multi_step_cvt = 0;
5569 vec<tree> interm_types = vNULL;
5570 tree intermediate_type, cvt_type = NULL_TREE;
5571 int op_type;
5572 unsigned short fltsz;
5573
5574 /* Is STMT a vectorizable conversion? */
5575
5576 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
5577 return false;
5578
5579 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
5580 && ! vec_stmt)
5581 return false;
5582
5583 gimple* stmt = stmt_info->stmt;
5584 if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
5585 return false;
5586
5587 if (gimple_get_lhs (stmt) == NULL_TREE
5588 || TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5589 return false;
5590
5591 if (TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
5592 return false;
5593
5594 if (is_gimple_assign (stmt))
5595 {
5596 code = gimple_assign_rhs_code (stmt);
5597 op_type = TREE_CODE_LENGTH ((tree_code) code);
5598 }
5599 else if (gimple_call_internal_p (stmt))
5600 {
5601 code = gimple_call_internal_fn (stmt);
5602 op_type = gimple_call_num_args (stmt);
5603 }
5604 else
5605 return false;
5606
5607 bool widen_arith = (code == WIDEN_MULT_EXPR
5608 || code == WIDEN_LSHIFT_EXPR
5609 || widening_fn_p (code));
5610
5611 if (!widen_arith
5612 && !CONVERT_EXPR_CODE_P (code)
5613 && code != FIX_TRUNC_EXPR
5614 && code != FLOAT_EXPR)
5615 return false;
5616
5617 /* Check types of lhs and rhs. */
5618 scalar_dest = gimple_get_lhs (stmt);
5619 lhs_type = TREE_TYPE (scalar_dest);
5620 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
5621
5622 /* Check the operands of the operation. */
5623 slp_tree slp_op0, slp_op1 = NULL;
5624 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
5625 0, &op0, &slp_op0, &dt[0], &vectype_in))
5626 {
5627 if (dump_enabled_p ())
5628 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5629 "use not simple.\n");
5630 return false;
5631 }
5632
5633 rhs_type = TREE_TYPE (op0);
5634 if ((code != FIX_TRUNC_EXPR && code != FLOAT_EXPR)
5635 && !((INTEGRAL_TYPE_P (lhs_type)
5636 && INTEGRAL_TYPE_P (rhs_type))
5637 || (SCALAR_FLOAT_TYPE_P (lhs_type)
5638 && SCALAR_FLOAT_TYPE_P (rhs_type))))
5639 return false;
5640
5641 if (!VECTOR_BOOLEAN_TYPE_P (vectype_out)
5642 && ((INTEGRAL_TYPE_P (lhs_type)
5643 && !type_has_mode_precision_p (lhs_type))
5644 || (INTEGRAL_TYPE_P (rhs_type)
5645 && !type_has_mode_precision_p (rhs_type))))
5646 {
5647 if (dump_enabled_p ())
5648 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5649 "type conversion to/from bit-precision unsupported."
5650 "\n");
5651 return false;
5652 }
5653
5654 if (op_type == binary_op)
5655 {
5656 gcc_assert (code == WIDEN_MULT_EXPR
5657 || code == WIDEN_LSHIFT_EXPR
5658 || widening_fn_p (code));
5659
5660 op1 = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) :
5661 gimple_call_arg (stmt, 0);
5662 tree vectype1_in;
5663 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1,
5664 &op1, &slp_op1, &dt[1], &vectype1_in))
5665 {
5666 if (dump_enabled_p ())
5667 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5668 "use not simple.\n");
5669 return false;
5670 }
5671 /* For WIDEN_MULT_EXPR, if OP0 is a constant, use the type of
5672 OP1. */
5673 if (!vectype_in)
5674 vectype_in = vectype1_in;
5675 }
5676
5677 /* If op0 is an external or constant def, infer the vector type
5678 from the scalar type. */
5679 if (!vectype_in)
5680 vectype_in = get_vectype_for_scalar_type (vinfo, rhs_type, slp_node);
5681 if (vec_stmt)
5682 gcc_assert (vectype_in);
5683 if (!vectype_in)
5684 {
5685 if (dump_enabled_p ())
5686 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5687 "no vectype for scalar type %T\n", rhs_type);
5688
5689 return false;
5690 }
5691
5692 if (VECTOR_BOOLEAN_TYPE_P (vectype_out)
5693 && !VECTOR_BOOLEAN_TYPE_P (vectype_in))
5694 {
5695 if (dump_enabled_p ())
5696 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5697 "can't convert between boolean and non "
5698 "boolean vectors %T\n", rhs_type);
5699
5700 return false;
5701 }
5702
5703 nunits_in = TYPE_VECTOR_SUBPARTS (vectype_in);
5704 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
5705 if (known_eq (nunits_out, nunits_in))
5706 if (widen_arith)
5707 modifier = WIDEN;
5708 else
5709 modifier = NONE;
5710 else if (multiple_p (nunits_out, nunits_in))
5711 modifier = NARROW_DST;
5712 else
5713 {
5714 gcc_checking_assert (multiple_p (nunits_in, nunits_out));
5715 modifier = WIDEN;
5716 }
5717
5718 /* Multiple types in SLP are handled by creating the appropriate number of
5719 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
5720 case of SLP. */
5721 if (slp_node)
5722 ncopies = 1;
5723 else if (modifier == NARROW_DST)
5724 ncopies = vect_get_num_copies (loop_vinfo, vectype_out);
5725 else
5726 ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
5727
5728 /* Sanity check: make sure that at least one copy of the vectorized stmt
5729 needs to be generated. */
5730 gcc_assert (ncopies >= 1);
5731
5732 bool found_mode = false;
5733 scalar_mode lhs_mode = SCALAR_TYPE_MODE (lhs_type);
5734 scalar_mode rhs_mode = SCALAR_TYPE_MODE (rhs_type);
5735 opt_scalar_mode rhs_mode_iter;
5736 auto_vec<std::pair<tree, tree_code> > converts;
5737 bool evenodd_ok = false;
5738
5739 /* Supportable by target? */
5740 switch (modifier)
5741 {
5742 case NONE:
5743 if (code != FIX_TRUNC_EXPR
5744 && code != FLOAT_EXPR
5745 && !CONVERT_EXPR_CODE_P (code))
5746 return false;
5747 gcc_assert (code.is_tree_code ());
5748 if (supportable_indirect_convert_operation (code,
5749 vectype_out, vectype_in,
5750 converts, op0, slp_op0))
5751 {
5752 gcc_assert (converts.length () <= 2);
5753 if (converts.length () == 1)
5754 code1 = converts[0].second;
5755 else
5756 {
5757 cvt_type = NULL_TREE;
5758 multi_step_cvt = converts.length () - 1;
5759 codecvt1 = converts[0].second;
5760 code1 = converts[1].second;
5761 interm_types.safe_push (converts[0].first);
5762 }
5763 break;
5764 }
5765
5766 /* FALLTHRU */
5767 unsupported:
5768 if (dump_enabled_p ())
5769 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5770 "conversion not supported by target.\n");
5771 return false;
5772
5773 case WIDEN:
5774 if (known_eq (nunits_in, nunits_out))
5775 {
5776 if (!(code.is_tree_code ()
5777 && supportable_half_widening_operation ((tree_code) code,
5778 vectype_out, vectype_in,
5779 &tc1)))
5780 goto unsupported;
5781 code1 = tc1;
5782 gcc_assert (!(multi_step_cvt && op_type == binary_op));
5783 break;
5784 }
5785 /* Elements in a vector can only be reordered if used in a reduction
5786 operation only. */
5787 if (code == WIDEN_MULT_EXPR
5788 && loop_vinfo
5789 && !nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo), stmt_info)
5790 /* For a SLP reduction we cannot swizzle lanes, detecting a
5791 reduction chain isn't possible here. */
5792 && SLP_TREE_LANES (slp_node) == 1)
5793 {
5794 /* ??? There is no way to look for SLP uses, so work on
5795 the stmt and what the stmt-based cycle detection gives us. */
5796 tree lhs = gimple_get_lhs (vect_orig_stmt (stmt_info)->stmt);
5797 stmt_vec_info use_stmt_info
5798 = lhs ? loop_vinfo->lookup_single_use (lhs) : NULL;
5799 if (use_stmt_info
5800 && STMT_VINFO_REDUC_DEF (use_stmt_info))
5801 evenodd_ok = true;
5802 }
5803 if (supportable_widening_operation (code, vectype_out, vectype_in,
5804 evenodd_ok, &code1,
5805 &code2, &multi_step_cvt,
5806 &interm_types))
5807 {
5808 /* Binary widening operation can only be supported directly by the
5809 architecture. */
5810 gcc_assert (!(multi_step_cvt && op_type == binary_op));
5811 break;
5812 }
5813
5814 if (code != FLOAT_EXPR
5815 || GET_MODE_SIZE (lhs_mode) <= GET_MODE_SIZE (rhs_mode))
5816 goto unsupported;
5817
5818 fltsz = GET_MODE_SIZE (lhs_mode);
5819 FOR_EACH_2XWIDER_MODE (rhs_mode_iter, rhs_mode)
5820 {
5821 rhs_mode = rhs_mode_iter.require ();
5822 if (GET_MODE_SIZE (rhs_mode) > fltsz)
5823 break;
5824
5825 cvt_type
5826 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5827 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5828 if (cvt_type == NULL_TREE)
5829 goto unsupported;
5830
5831 if (GET_MODE_SIZE (rhs_mode) == fltsz)
5832 {
5833 tc1 = ERROR_MARK;
5834 gcc_assert (code.is_tree_code ());
5835 if (!supportable_convert_operation ((tree_code) code, vectype_out,
5836 cvt_type, &tc1))
5837 goto unsupported;
5838 codecvt1 = tc1;
5839 }
5840 else if (!supportable_widening_operation (code, vectype_out,
5841 cvt_type, evenodd_ok,
5842 &codecvt1,
5843 &codecvt2, &multi_step_cvt,
5844 &interm_types))
5845 continue;
5846 else
5847 gcc_assert (multi_step_cvt == 0);
5848
5849 if (supportable_widening_operation (NOP_EXPR, cvt_type,
5850 vectype_in, evenodd_ok, &code1,
5851 &code2, &multi_step_cvt,
5852 &interm_types))
5853 {
5854 found_mode = true;
5855 break;
5856 }
5857 }
5858
5859 if (!found_mode)
5860 goto unsupported;
5861
5862 if (GET_MODE_SIZE (rhs_mode) == fltsz)
5863 codecvt2 = ERROR_MARK;
5864 else
5865 {
5866 multi_step_cvt++;
5867 interm_types.safe_push (cvt_type);
5868 cvt_type = NULL_TREE;
5869 }
5870 break;
5871
5872 case NARROW_DST:
5873 gcc_assert (op_type == unary_op);
5874 if (supportable_narrowing_operation (code, vectype_out, vectype_in,
5875 &code1, &multi_step_cvt,
5876 &interm_types))
5877 break;
5878
5879 if (GET_MODE_SIZE (lhs_mode) >= GET_MODE_SIZE (rhs_mode))
5880 goto unsupported;
5881
5882 if (code == FIX_TRUNC_EXPR)
5883 {
5884 cvt_type
5885 = build_nonstandard_integer_type (GET_MODE_BITSIZE (rhs_mode), 0);
5886 cvt_type = get_same_sized_vectype (cvt_type, vectype_in);
5887 if (cvt_type == NULL_TREE)
5888 goto unsupported;
5889 if (supportable_convert_operation ((tree_code) code, cvt_type, vectype_in,
5890 &tc1))
5891 codecvt1 = tc1;
5892 else
5893 goto unsupported;
5894 if (supportable_narrowing_operation (NOP_EXPR, vectype_out, cvt_type,
5895 &code1, &multi_step_cvt,
5896 &interm_types))
5897 break;
5898 }
5899 /* If op0 can be represented with low precision integer,
5900 truncate it to cvt_type and the do FLOAT_EXPR. */
5901 else if (code == FLOAT_EXPR)
5902 {
5903 wide_int op_min_value, op_max_value;
5904 if (slp_node)
5905 {
5906 tree def;
5907 /* ??? Merge ranges in case of more than one lane. */
5908 if (SLP_TREE_LANES (slp_op0) != 1
5909 || !(def = vect_get_slp_scalar_def (slp_op0, 0))
5910 || !vect_get_range_info (def, &op_min_value, &op_max_value))
5911 goto unsupported;
5912 }
5913 else if (!vect_get_range_info (op0, &op_min_value, &op_max_value))
5914 goto unsupported;
5915
5916 cvt_type
5917 = build_nonstandard_integer_type (GET_MODE_BITSIZE (lhs_mode), 0);
5918 if (cvt_type == NULL_TREE
5919 || (wi::min_precision (op_max_value, SIGNED)
5920 > TYPE_PRECISION (cvt_type))
5921 || (wi::min_precision (op_min_value, SIGNED)
5922 > TYPE_PRECISION (cvt_type)))
5923 goto unsupported;
5924
5925 cvt_type = get_same_sized_vectype (cvt_type, vectype_out);
5926 if (cvt_type == NULL_TREE)
5927 goto unsupported;
5928 if (!supportable_narrowing_operation (NOP_EXPR, cvt_type, vectype_in,
5929 &code1, &multi_step_cvt,
5930 &interm_types))
5931 goto unsupported;
5932 if (supportable_convert_operation ((tree_code) code, vectype_out,
5933 cvt_type, &tc1))
5934 {
5935 codecvt1 = tc1;
5936 modifier = NARROW_SRC;
5937 break;
5938 }
5939 }
5940
5941 goto unsupported;
5942
5943 default:
5944 gcc_unreachable ();
5945 }
5946
5947 if (modifier == WIDEN
5948 && loop_vinfo
5949 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
5950 && (code1 == VEC_WIDEN_MULT_EVEN_EXPR
5951 || widening_evenodd_fn_p (code1)))
5952 {
5953 if (dump_enabled_p ())
5954 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5955 "can't use a fully-masked loop because"
5956 " widening operation on even/odd elements"
5957 " mixes up lanes.\n");
5958 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
5959 }
5960
5961 if (!vec_stmt) /* transformation not required. */
5962 {
5963 if (slp_node
5964 && (!vect_maybe_update_slp_op_vectype (slp_op0, vectype_in)
5965 || !vect_maybe_update_slp_op_vectype (slp_op1, vectype_in)))
5966 {
5967 if (dump_enabled_p ())
5968 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5969 "incompatible vector types for invariants\n");
5970 return false;
5971 }
5972 DUMP_VECT_SCOPE ("vectorizable_conversion");
5973 if (modifier == NONE)
5974 {
5975 STMT_VINFO_TYPE (stmt_info) = type_conversion_vec_info_type;
5976 vect_model_simple_cost (vinfo, stmt_info,
5977 ncopies * (1 + multi_step_cvt),
5978 dt, ndts, slp_node, cost_vec);
5979 }
5980 else if (modifier == NARROW_SRC || modifier == NARROW_DST)
5981 {
5982 STMT_VINFO_TYPE (stmt_info) = type_demotion_vec_info_type;
5983 /* The final packing step produces one vector result per copy. */
5984 unsigned int nvectors
5985 = (slp_node ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) : ncopies);
5986 vect_model_promotion_demotion_cost (stmt_info, dt, nvectors,
5987 multi_step_cvt, cost_vec,
5988 widen_arith);
5989 }
5990 else
5991 {
5992 STMT_VINFO_TYPE (stmt_info) = type_promotion_vec_info_type;
5993 /* The initial unpacking step produces two vector results
5994 per copy. MULTI_STEP_CVT is 0 for a single conversion,
5995 so >> MULTI_STEP_CVT divides by 2^(number of steps - 1). */
5996 unsigned int nvectors
5997 = (slp_node
5998 ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) >> multi_step_cvt
5999 : ncopies * 2);
6000 vect_model_promotion_demotion_cost (stmt_info, dt, nvectors,
6001 multi_step_cvt, cost_vec,
6002 widen_arith);
6003 }
6004 interm_types.release ();
6005 return true;
6006 }
6007
6008 /* Transform. */
6009 if (dump_enabled_p ())
6010 dump_printf_loc (MSG_NOTE, vect_location,
6011 "transform conversion. ncopies = %d.\n", ncopies);
6012
6013 if (op_type == binary_op)
6014 {
6015 if (CONSTANT_CLASS_P (op0))
6016 op0 = fold_convert (TREE_TYPE (op1), op0);
6017 else if (CONSTANT_CLASS_P (op1))
6018 op1 = fold_convert (TREE_TYPE (op0), op1);
6019 }
6020
6021 /* In case of multi-step conversion, we first generate conversion operations
6022 to the intermediate types, and then from that types to the final one.
6023 We create vector destinations for the intermediate type (TYPES) received
6024 from supportable_*_operation, and store them in the correct order
6025 for future use in vect_create_vectorized_*_stmts (). */
6026 auto_vec<tree> vec_dsts (multi_step_cvt + 1);
6027 bool widen_or_narrow_float_p
6028 = cvt_type && (modifier == WIDEN || modifier == NARROW_SRC);
6029 vec_dest = vect_create_destination_var (scalar_dest,
6030 widen_or_narrow_float_p
6031 ? cvt_type : vectype_out);
6032 vec_dsts.quick_push (vec_dest);
6033
6034 if (multi_step_cvt)
6035 {
6036 for (i = interm_types.length () - 1;
6037 interm_types.iterate (i, &intermediate_type); i--)
6038 {
6039 vec_dest = vect_create_destination_var (scalar_dest,
6040 intermediate_type);
6041 vec_dsts.quick_push (vec_dest);
6042 }
6043 }
6044
6045 if (cvt_type)
6046 vec_dest = vect_create_destination_var (scalar_dest,
6047 widen_or_narrow_float_p
6048 ? vectype_out : cvt_type);
6049
6050 int ninputs = 1;
6051 if (!slp_node)
6052 {
6053 if (modifier == WIDEN)
6054 ;
6055 else if (modifier == NARROW_SRC || modifier == NARROW_DST)
6056 {
6057 if (multi_step_cvt)
6058 ninputs = vect_pow2 (multi_step_cvt);
6059 ninputs *= 2;
6060 }
6061 }
6062
6063 switch (modifier)
6064 {
6065 case NONE:
6066 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
6067 op0, vectype_in, &vec_oprnds0);
6068 /* vec_dest is intermediate type operand when multi_step_cvt. */
6069 if (multi_step_cvt)
6070 {
6071 cvt_op = vec_dest;
6072 vec_dest = vec_dsts[0];
6073 }
6074
6075 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6076 {
6077 /* Arguments are ready, create the new vector stmt. */
6078 gimple* new_stmt;
6079 if (multi_step_cvt)
6080 {
6081 gcc_assert (multi_step_cvt == 1);
6082 new_stmt = vect_gimple_build (cvt_op, codecvt1, vop0);
6083 new_temp = make_ssa_name (cvt_op, new_stmt);
6084 gimple_assign_set_lhs (new_stmt, new_temp);
6085 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6086 vop0 = new_temp;
6087 }
6088 new_stmt = vect_gimple_build (vec_dest, code1, vop0);
6089 new_temp = make_ssa_name (vec_dest, new_stmt);
6090 gimple_set_lhs (new_stmt, new_temp);
6091 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6092
6093 if (slp_node)
6094 slp_node->push_vec_def (new_stmt);
6095 else
6096 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6097 }
6098 break;
6099
6100 case WIDEN:
6101 /* In case the vectorization factor (VF) is bigger than the number
6102 of elements that we can fit in a vectype (nunits), we have to
6103 generate more than one vector stmt - i.e - we need to "unroll"
6104 the vector stmt by a factor VF/nunits. */
6105 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies * ninputs,
6106 op0, vectype_in, &vec_oprnds0,
6107 code == WIDEN_LSHIFT_EXPR ? NULL_TREE : op1,
6108 vectype_in, &vec_oprnds1);
6109 if (code == WIDEN_LSHIFT_EXPR)
6110 {
6111 int oprnds_size = vec_oprnds0.length ();
6112 vec_oprnds1.create (oprnds_size);
6113 for (i = 0; i < oprnds_size; ++i)
6114 vec_oprnds1.quick_push (op1);
6115 }
6116 /* Arguments are ready. Create the new vector stmts. */
6117 for (i = multi_step_cvt; i >= 0; i--)
6118 {
6119 tree this_dest = vec_dsts[i];
6120 code_helper c1 = code1, c2 = code2;
6121 if (i == 0 && codecvt2 != ERROR_MARK)
6122 {
6123 c1 = codecvt1;
6124 c2 = codecvt2;
6125 }
6126 if (known_eq (nunits_out, nunits_in))
6127 vect_create_half_widening_stmts (vinfo, &vec_oprnds0, &vec_oprnds1,
6128 stmt_info, this_dest, gsi, c1,
6129 op_type);
6130 else
6131 vect_create_vectorized_promotion_stmts (vinfo, &vec_oprnds0,
6132 &vec_oprnds1, stmt_info,
6133 this_dest, gsi,
6134 c1, c2, op_type);
6135 }
6136
6137 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6138 {
6139 gimple *new_stmt;
6140 if (cvt_type)
6141 {
6142 new_temp = make_ssa_name (vec_dest);
6143 new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
6144 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6145 }
6146 else
6147 new_stmt = SSA_NAME_DEF_STMT (vop0);
6148
6149 if (slp_node)
6150 slp_node->push_vec_def (new_stmt);
6151 else
6152 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6153 }
6154 break;
6155
6156 case NARROW_SRC:
6157 case NARROW_DST:
6158 /* In case the vectorization factor (VF) is bigger than the number
6159 of elements that we can fit in a vectype (nunits), we have to
6160 generate more than one vector stmt - i.e - we need to "unroll"
6161 the vector stmt by a factor VF/nunits. */
6162 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies * ninputs,
6163 op0, vectype_in, &vec_oprnds0);
6164 /* Arguments are ready. Create the new vector stmts. */
6165 if (cvt_type && modifier == NARROW_DST)
6166 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6167 {
6168 new_temp = make_ssa_name (vec_dest);
6169 gimple *new_stmt = vect_gimple_build (new_temp, codecvt1, vop0);
6170 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6171 vec_oprnds0[i] = new_temp;
6172 }
6173
6174 vect_create_vectorized_demotion_stmts (vinfo, &vec_oprnds0,
6175 multi_step_cvt,
6176 stmt_info, vec_dsts, gsi,
6177 slp_node, code1,
6178 modifier == NARROW_SRC);
6179 /* After demoting op0 to cvt_type, convert it to dest. */
6180 if (cvt_type && code == FLOAT_EXPR)
6181 {
6182 for (unsigned int i = 0; i != vec_oprnds0.length() / 2; i++)
6183 {
6184 /* Arguments are ready, create the new vector stmt. */
6185 gcc_assert (TREE_CODE_LENGTH ((tree_code) codecvt1) == unary_op);
6186 gimple *new_stmt
6187 = vect_gimple_build (vec_dest, codecvt1, vec_oprnds0[i]);
6188 new_temp = make_ssa_name (vec_dest, new_stmt);
6189 gimple_set_lhs (new_stmt, new_temp);
6190 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6191
6192 /* This is the last step of the conversion sequence. Store the
6193 vectors in SLP_NODE or in vector info of the scalar statement
6194 (or in STMT_VINFO_RELATED_STMT chain). */
6195 if (slp_node)
6196 slp_node->push_vec_def (new_stmt);
6197 else
6198 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6199 }
6200 }
6201 break;
6202 }
6203 if (!slp_node)
6204 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
6205
6206 vec_oprnds0.release ();
6207 vec_oprnds1.release ();
6208 interm_types.release ();
6209
6210 return true;
6211 }
6212
6213 /* Return true if we can assume from the scalar form of STMT_INFO that
6214 neither the scalar nor the vector forms will generate code. STMT_INFO
6215 is known not to involve a data reference. */
6216
6217 bool
6218 vect_nop_conversion_p (stmt_vec_info stmt_info)
6219 {
6220 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6221 if (!stmt)
6222 return false;
6223
6224 tree lhs = gimple_assign_lhs (stmt);
6225 tree_code code = gimple_assign_rhs_code (stmt);
6226 tree rhs = gimple_assign_rhs1 (stmt);
6227
6228 if (code == SSA_NAME || code == VIEW_CONVERT_EXPR)
6229 return true;
6230
6231 if (CONVERT_EXPR_CODE_P (code))
6232 return tree_nop_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs));
6233
6234 return false;
6235 }
6236
6237 /* Function vectorizable_assignment.
6238
6239 Check if STMT_INFO performs an assignment (copy) that can be vectorized.
6240 If VEC_STMT is also passed, vectorize the STMT_INFO: create a vectorized
6241 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6242 Return true if STMT_INFO is vectorizable in this way. */
6243
6244 static bool
6245 vectorizable_assignment (vec_info *vinfo,
6246 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6247 gimple **vec_stmt, slp_tree slp_node,
6248 stmt_vector_for_cost *cost_vec)
6249 {
6250 tree vec_dest;
6251 tree scalar_dest;
6252 tree op;
6253 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6254 tree new_temp;
6255 enum vect_def_type dt[1] = {vect_unknown_def_type};
6256 int ndts = 1;
6257 int ncopies;
6258 int i;
6259 vec<tree> vec_oprnds = vNULL;
6260 tree vop;
6261 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6262 enum tree_code code;
6263 tree vectype_in;
6264
6265 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6266 return false;
6267
6268 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6269 && ! vec_stmt)
6270 return false;
6271
6272 /* Is vectorizable assignment? */
6273 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6274 if (!stmt)
6275 return false;
6276
6277 scalar_dest = gimple_assign_lhs (stmt);
6278 if (TREE_CODE (scalar_dest) != SSA_NAME)
6279 return false;
6280
6281 if (STMT_VINFO_DATA_REF (stmt_info))
6282 return false;
6283
6284 code = gimple_assign_rhs_code (stmt);
6285 if (!(gimple_assign_single_p (stmt)
6286 || code == PAREN_EXPR
6287 || CONVERT_EXPR_CODE_P (code)))
6288 return false;
6289
6290 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6291 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
6292
6293 /* Multiple types in SLP are handled by creating the appropriate number of
6294 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6295 case of SLP. */
6296 if (slp_node)
6297 ncopies = 1;
6298 else
6299 ncopies = vect_get_num_copies (loop_vinfo, vectype);
6300
6301 gcc_assert (ncopies >= 1);
6302
6303 slp_tree slp_op;
6304 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &op, &slp_op,
6305 &dt[0], &vectype_in))
6306 {
6307 if (dump_enabled_p ())
6308 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6309 "use not simple.\n");
6310 return false;
6311 }
6312 if (!vectype_in)
6313 vectype_in = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op), slp_node);
6314
6315 /* We can handle VIEW_CONVERT conversions that do not change the number
6316 of elements or the vector size or other conversions when the component
6317 types are nop-convertible. */
6318 if (!vectype_in
6319 || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in), nunits)
6320 || (code == VIEW_CONVERT_EXPR
6321 && maybe_ne (GET_MODE_SIZE (TYPE_MODE (vectype)),
6322 GET_MODE_SIZE (TYPE_MODE (vectype_in))))
6323 || (CONVERT_EXPR_CODE_P (code)
6324 && !tree_nop_conversion_p (TREE_TYPE (vectype),
6325 TREE_TYPE (vectype_in))))
6326 return false;
6327
6328 if (VECTOR_BOOLEAN_TYPE_P (vectype) != VECTOR_BOOLEAN_TYPE_P (vectype_in))
6329 {
6330 if (dump_enabled_p ())
6331 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6332 "can't convert between boolean and non "
6333 "boolean vectors %T\n", TREE_TYPE (op));
6334
6335 return false;
6336 }
6337
6338 /* We do not handle bit-precision changes. */
6339 if ((CONVERT_EXPR_CODE_P (code)
6340 || code == VIEW_CONVERT_EXPR)
6341 && ((INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6342 && !type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6343 || (INTEGRAL_TYPE_P (TREE_TYPE (op))
6344 && !type_has_mode_precision_p (TREE_TYPE (op))))
6345 /* But a conversion that does not change the bit-pattern is ok. */
6346 && !(INTEGRAL_TYPE_P (TREE_TYPE (scalar_dest))
6347 && INTEGRAL_TYPE_P (TREE_TYPE (op))
6348 && (((TYPE_PRECISION (TREE_TYPE (scalar_dest))
6349 > TYPE_PRECISION (TREE_TYPE (op)))
6350 && TYPE_UNSIGNED (TREE_TYPE (op)))
6351 || (TYPE_PRECISION (TREE_TYPE (scalar_dest))
6352 == TYPE_PRECISION (TREE_TYPE (op))))))
6353 {
6354 if (dump_enabled_p ())
6355 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6356 "type conversion to/from bit-precision "
6357 "unsupported.\n");
6358 return false;
6359 }
6360
6361 if (!vec_stmt) /* transformation not required. */
6362 {
6363 if (slp_node
6364 && !vect_maybe_update_slp_op_vectype (slp_op, vectype_in))
6365 {
6366 if (dump_enabled_p ())
6367 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6368 "incompatible vector types for invariants\n");
6369 return false;
6370 }
6371 STMT_VINFO_TYPE (stmt_info) = assignment_vec_info_type;
6372 DUMP_VECT_SCOPE ("vectorizable_assignment");
6373 if (!vect_nop_conversion_p (stmt_info))
6374 vect_model_simple_cost (vinfo, stmt_info, ncopies, dt, ndts, slp_node,
6375 cost_vec);
6376 return true;
6377 }
6378
6379 /* Transform. */
6380 if (dump_enabled_p ())
6381 dump_printf_loc (MSG_NOTE, vect_location, "transform assignment.\n");
6382
6383 /* Handle def. */
6384 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6385
6386 /* Handle use. */
6387 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies, op, &vec_oprnds);
6388
6389 /* Arguments are ready. create the new vector stmt. */
6390 FOR_EACH_VEC_ELT (vec_oprnds, i, vop)
6391 {
6392 if (CONVERT_EXPR_CODE_P (code)
6393 || code == VIEW_CONVERT_EXPR)
6394 vop = build1 (VIEW_CONVERT_EXPR, vectype, vop);
6395 gassign *new_stmt = gimple_build_assign (vec_dest, vop);
6396 new_temp = make_ssa_name (vec_dest, new_stmt);
6397 gimple_assign_set_lhs (new_stmt, new_temp);
6398 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6399 if (slp_node)
6400 slp_node->push_vec_def (new_stmt);
6401 else
6402 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6403 }
6404 if (!slp_node)
6405 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
6406
6407 vec_oprnds.release ();
6408 return true;
6409 }
6410
6411
6412 /* Return TRUE if CODE (a shift operation) is supported for SCALAR_TYPE
6413 either as shift by a scalar or by a vector. */
6414
6415 bool
6416 vect_supportable_shift (vec_info *vinfo, enum tree_code code, tree scalar_type)
6417 {
6418 optab optab;
6419 tree vectype;
6420
6421 vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
6422 if (!vectype)
6423 return false;
6424
6425 optab = optab_for_tree_code (code, vectype, optab_scalar);
6426 if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6427 return true;
6428
6429 optab = optab_for_tree_code (code, vectype, optab_vector);
6430 if (optab && can_implement_p (optab, TYPE_MODE (vectype)))
6431 return true;
6432
6433 return false;
6434 }
6435
6436
6437 /* Function vectorizable_shift.
6438
6439 Check if STMT_INFO performs a shift operation that can be vectorized.
6440 If VEC_STMT is also passed, vectorize the STMT_INFO: create a vectorized
6441 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6442 Return true if STMT_INFO is vectorizable in this way. */
6443
6444 static bool
6445 vectorizable_shift (vec_info *vinfo,
6446 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6447 gimple **vec_stmt, slp_tree slp_node,
6448 stmt_vector_for_cost *cost_vec)
6449 {
6450 tree vec_dest;
6451 tree scalar_dest;
6452 tree op0, op1 = NULL;
6453 tree vec_oprnd1 = NULL_TREE;
6454 tree vectype;
6455 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6456 enum tree_code code;
6457 machine_mode vec_mode;
6458 tree new_temp;
6459 optab optab;
6460 int icode;
6461 machine_mode optab_op2_mode;
6462 enum vect_def_type dt[2] = {vect_unknown_def_type, vect_unknown_def_type};
6463 int ndts = 2;
6464 poly_uint64 nunits_in;
6465 poly_uint64 nunits_out;
6466 tree vectype_out;
6467 tree op1_vectype;
6468 int ncopies;
6469 int i;
6470 vec<tree> vec_oprnds0 = vNULL;
6471 vec<tree> vec_oprnds1 = vNULL;
6472 tree vop0, vop1;
6473 unsigned int k;
6474 bool scalar_shift_arg = true;
6475 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6476 bool incompatible_op1_vectype_p = false;
6477
6478 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6479 return false;
6480
6481 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6482 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle
6483 && ! vec_stmt)
6484 return false;
6485
6486 /* Is STMT a vectorizable binary/unary operation? */
6487 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6488 if (!stmt)
6489 return false;
6490
6491 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
6492 return false;
6493
6494 code = gimple_assign_rhs_code (stmt);
6495
6496 if (!(code == LSHIFT_EXPR || code == RSHIFT_EXPR || code == LROTATE_EXPR
6497 || code == RROTATE_EXPR))
6498 return false;
6499
6500 scalar_dest = gimple_assign_lhs (stmt);
6501 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
6502 if (!type_has_mode_precision_p (TREE_TYPE (scalar_dest)))
6503 {
6504 if (dump_enabled_p ())
6505 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6506 "bit-precision shifts not supported.\n");
6507 return false;
6508 }
6509
6510 slp_tree slp_op0;
6511 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6512 0, &op0, &slp_op0, &dt[0], &vectype))
6513 {
6514 if (dump_enabled_p ())
6515 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6516 "use not simple.\n");
6517 return false;
6518 }
6519 /* If op0 is an external or constant def, infer the vector type
6520 from the scalar type. */
6521 if (!vectype)
6522 vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0), slp_node);
6523 if (vec_stmt)
6524 gcc_assert (vectype);
6525 if (!vectype)
6526 {
6527 if (dump_enabled_p ())
6528 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6529 "no vectype for scalar type\n");
6530 return false;
6531 }
6532
6533 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
6534 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
6535 if (maybe_ne (nunits_out, nunits_in))
6536 return false;
6537
6538 stmt_vec_info op1_def_stmt_info;
6539 slp_tree slp_op1;
6540 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1, &op1, &slp_op1,
6541 &dt[1], &op1_vectype, &op1_def_stmt_info))
6542 {
6543 if (dump_enabled_p ())
6544 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6545 "use not simple.\n");
6546 return false;
6547 }
6548
6549 /* Multiple types in SLP are handled by creating the appropriate number of
6550 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
6551 case of SLP. */
6552 if (slp_node)
6553 ncopies = 1;
6554 else
6555 ncopies = vect_get_num_copies (loop_vinfo, vectype);
6556
6557 gcc_assert (ncopies >= 1);
6558
6559 /* Determine whether the shift amount is a vector, or scalar. If the
6560 shift/rotate amount is a vector, use the vector/vector shift optabs. */
6561
6562 if ((dt[1] == vect_internal_def
6563 || dt[1] == vect_induction_def
6564 || dt[1] == vect_nested_cycle)
6565 && (!slp_node || SLP_TREE_LANES (slp_node) == 1))
6566 scalar_shift_arg = false;
6567 else if (dt[1] == vect_constant_def
6568 || dt[1] == vect_external_def
6569 || dt[1] == vect_internal_def)
6570 {
6571 /* In SLP, need to check whether the shift count is the same,
6572 in loops if it is a constant or invariant, it is always
6573 a scalar shift. */
6574 if (slp_node)
6575 {
6576 vec<stmt_vec_info> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
6577 stmt_vec_info slpstmt_info;
6578
6579 FOR_EACH_VEC_ELT (stmts, k, slpstmt_info)
6580 if (slpstmt_info)
6581 {
6582 gassign *slpstmt = as_a <gassign *> (slpstmt_info->stmt);
6583 if (!operand_equal_p (gimple_assign_rhs2 (slpstmt), op1, 0))
6584 scalar_shift_arg = false;
6585 }
6586
6587 /* For internal SLP defs we have to make sure we see scalar stmts
6588 for all vector elements.
6589 ??? For different vectors we could resort to a different
6590 scalar shift operand but code-generation below simply always
6591 takes the first. */
6592 if (dt[1] == vect_internal_def
6593 && maybe_ne (nunits_out * SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node),
6594 stmts.length ()))
6595 scalar_shift_arg = false;
6596 }
6597
6598 /* If the shift amount is computed by a pattern stmt we cannot
6599 use the scalar amount directly thus give up and use a vector
6600 shift. */
6601 if (op1_def_stmt_info && is_pattern_stmt_p (op1_def_stmt_info))
6602 scalar_shift_arg = false;
6603 }
6604 else
6605 {
6606 if (dump_enabled_p ())
6607 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6608 "operand mode requires invariant argument.\n");
6609 return false;
6610 }
6611
6612 /* Vector shifted by vector. */
6613 bool was_scalar_shift_arg = scalar_shift_arg;
6614 if (!scalar_shift_arg)
6615 {
6616 optab = optab_for_tree_code (code, vectype, optab_vector);
6617 if (dump_enabled_p ())
6618 dump_printf_loc (MSG_NOTE, vect_location,
6619 "vector/vector shift/rotate found.\n");
6620
6621 if (!op1_vectype)
6622 op1_vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op1),
6623 slp_op1);
6624 incompatible_op1_vectype_p
6625 = (op1_vectype == NULL_TREE
6626 || maybe_ne (TYPE_VECTOR_SUBPARTS (op1_vectype),
6627 TYPE_VECTOR_SUBPARTS (vectype))
6628 || TYPE_MODE (op1_vectype) != TYPE_MODE (vectype));
6629 if (incompatible_op1_vectype_p
6630 && (!slp_node
6631 || SLP_TREE_DEF_TYPE (slp_op1) != vect_constant_def
6632 || slp_op1->refcnt != 1))
6633 {
6634 if (dump_enabled_p ())
6635 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6636 "unusable type for last operand in"
6637 " vector/vector shift/rotate.\n");
6638 return false;
6639 }
6640 }
6641 /* See if the machine has a vector shifted by scalar insn and if not
6642 then see if it has a vector shifted by vector insn. */
6643 else
6644 {
6645 optab = optab_for_tree_code (code, vectype, optab_scalar);
6646 if (optab
6647 && can_implement_p (optab, TYPE_MODE (vectype)))
6648 {
6649 if (dump_enabled_p ())
6650 dump_printf_loc (MSG_NOTE, vect_location,
6651 "vector/scalar shift/rotate found.\n");
6652 }
6653 else
6654 {
6655 optab = optab_for_tree_code (code, vectype, optab_vector);
6656 if (optab
6657 && can_implement_p (optab, TYPE_MODE (vectype)))
6658 {
6659 scalar_shift_arg = false;
6660
6661 if (dump_enabled_p ())
6662 dump_printf_loc (MSG_NOTE, vect_location,
6663 "vector/vector shift/rotate found.\n");
6664
6665 if (!op1_vectype)
6666 op1_vectype = get_vectype_for_scalar_type (vinfo,
6667 TREE_TYPE (op1),
6668 slp_op1);
6669
6670 /* Unlike the other binary operators, shifts/rotates have
6671 the rhs being int, instead of the same type as the lhs,
6672 so make sure the scalar is the right type if we are
6673 dealing with vectors of long long/long/short/char. */
6674 incompatible_op1_vectype_p
6675 = (!op1_vectype
6676 || !tree_nop_conversion_p (TREE_TYPE (vectype),
6677 TREE_TYPE (op1)));
6678 if (incompatible_op1_vectype_p
6679 && dt[1] == vect_internal_def)
6680 {
6681 if (dump_enabled_p ())
6682 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6683 "unusable type for last operand in"
6684 " vector/vector shift/rotate.\n");
6685 return false;
6686 }
6687 }
6688 }
6689 }
6690
6691 /* Supportable by target? */
6692 if (!optab)
6693 {
6694 if (dump_enabled_p ())
6695 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6696 "no optab.\n");
6697 return false;
6698 }
6699 vec_mode = TYPE_MODE (vectype);
6700 icode = (int) optab_handler (optab, vec_mode);
6701 if (icode == CODE_FOR_nothing)
6702 {
6703 if (dump_enabled_p ())
6704 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6705 "op not supported by target.\n");
6706 return false;
6707 }
6708 /* vector lowering cannot optimize vector shifts using word arithmetic. */
6709 if (vect_emulated_vector_p (vectype))
6710 return false;
6711
6712 if (!vec_stmt) /* transformation not required. */
6713 {
6714 if (slp_node
6715 && (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
6716 || ((!scalar_shift_arg || dt[1] == vect_internal_def)
6717 && (!incompatible_op1_vectype_p
6718 || dt[1] == vect_constant_def)
6719 && !vect_maybe_update_slp_op_vectype
6720 (slp_op1,
6721 incompatible_op1_vectype_p ? vectype : op1_vectype))))
6722 {
6723 if (dump_enabled_p ())
6724 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6725 "incompatible vector types for invariants\n");
6726 return false;
6727 }
6728 /* Now adjust the constant shift amount in place. */
6729 if (slp_node
6730 && incompatible_op1_vectype_p
6731 && dt[1] == vect_constant_def)
6732 {
6733 for (unsigned i = 0;
6734 i < SLP_TREE_SCALAR_OPS (slp_op1).length (); ++i)
6735 {
6736 SLP_TREE_SCALAR_OPS (slp_op1)[i]
6737 = fold_convert (TREE_TYPE (vectype),
6738 SLP_TREE_SCALAR_OPS (slp_op1)[i]);
6739 gcc_assert ((TREE_CODE (SLP_TREE_SCALAR_OPS (slp_op1)[i])
6740 == INTEGER_CST));
6741 }
6742 }
6743 STMT_VINFO_TYPE (stmt_info) = shift_vec_info_type;
6744 DUMP_VECT_SCOPE ("vectorizable_shift");
6745 vect_model_simple_cost (vinfo, stmt_info, ncopies, dt,
6746 scalar_shift_arg ? 1 : ndts, slp_node, cost_vec);
6747 return true;
6748 }
6749
6750 /* Transform. */
6751
6752 if (dump_enabled_p ())
6753 dump_printf_loc (MSG_NOTE, vect_location,
6754 "transform binary/unary operation.\n");
6755
6756 if (incompatible_op1_vectype_p && !slp_node)
6757 {
6758 gcc_assert (!scalar_shift_arg && was_scalar_shift_arg);
6759 op1 = fold_convert (TREE_TYPE (vectype), op1);
6760 if (dt[1] != vect_constant_def)
6761 op1 = vect_init_vector (vinfo, stmt_info, op1,
6762 TREE_TYPE (vectype), NULL);
6763 }
6764
6765 /* Handle def. */
6766 vec_dest = vect_create_destination_var (scalar_dest, vectype);
6767
6768 if (scalar_shift_arg && dt[1] != vect_internal_def)
6769 {
6770 /* Vector shl and shr insn patterns can be defined with scalar
6771 operand 2 (shift operand). In this case, use constant or loop
6772 invariant op1 directly, without extending it to vector mode
6773 first. */
6774 optab_op2_mode = insn_data[icode].operand[2].mode;
6775 if (!VECTOR_MODE_P (optab_op2_mode))
6776 {
6777 if (dump_enabled_p ())
6778 dump_printf_loc (MSG_NOTE, vect_location,
6779 "operand 1 using scalar mode.\n");
6780 vec_oprnd1 = op1;
6781 vec_oprnds1.create (slp_node ? slp_node->vec_stmts_size : ncopies);
6782 vec_oprnds1.quick_push (vec_oprnd1);
6783 /* Store vec_oprnd1 for every vector stmt to be created.
6784 We check during the analysis that all the shift arguments
6785 are the same.
6786 TODO: Allow different constants for different vector
6787 stmts generated for an SLP instance. */
6788 for (k = 0;
6789 k < (slp_node ? slp_node->vec_stmts_size - 1 : ncopies - 1); k++)
6790 vec_oprnds1.quick_push (vec_oprnd1);
6791 }
6792 }
6793 else if (!scalar_shift_arg && slp_node && incompatible_op1_vectype_p)
6794 {
6795 if (was_scalar_shift_arg)
6796 {
6797 /* If the argument was the same in all lanes create the
6798 correctly typed vector shift amount directly. Note
6799 we made SLP scheduling think we use the original scalars,
6800 so place the compensation code next to the shift which
6801 is conservative. See PR119640 where it otherwise breaks. */
6802 op1 = fold_convert (TREE_TYPE (vectype), op1);
6803 op1 = vect_init_vector (vinfo, stmt_info, op1, TREE_TYPE (vectype),
6804 gsi);
6805 vec_oprnd1 = vect_init_vector (vinfo, stmt_info, op1, vectype,
6806 gsi);
6807 vec_oprnds1.create (slp_node->vec_stmts_size);
6808 for (k = 0; k < slp_node->vec_stmts_size; k++)
6809 vec_oprnds1.quick_push (vec_oprnd1);
6810 }
6811 else if (dt[1] == vect_constant_def)
6812 /* The constant shift amount has been adjusted in place. */
6813 ;
6814 else
6815 gcc_assert (TYPE_MODE (op1_vectype) == TYPE_MODE (vectype));
6816 }
6817
6818 /* vec_oprnd1 is available if operand 1 should be of a scalar-type
6819 (a special case for certain kind of vector shifts); otherwise,
6820 operand 1 should be of a vector type (the usual case). */
6821 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
6822 op0, &vec_oprnds0,
6823 vec_oprnd1 ? NULL_TREE : op1, &vec_oprnds1);
6824
6825 /* Arguments are ready. Create the new vector stmt. */
6826 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
6827 {
6828 /* For internal defs where we need to use a scalar shift arg
6829 extract the first lane. */
6830 if (scalar_shift_arg && dt[1] == vect_internal_def)
6831 {
6832 vop1 = vec_oprnds1[0];
6833 new_temp = make_ssa_name (TREE_TYPE (TREE_TYPE (vop1)));
6834 gassign *new_stmt
6835 = gimple_build_assign (new_temp,
6836 build3 (BIT_FIELD_REF, TREE_TYPE (new_temp),
6837 vop1,
6838 TYPE_SIZE (TREE_TYPE (new_temp)),
6839 bitsize_zero_node));
6840 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6841 vop1 = new_temp;
6842 }
6843 else
6844 vop1 = vec_oprnds1[i];
6845 gassign *new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1);
6846 new_temp = make_ssa_name (vec_dest, new_stmt);
6847 gimple_assign_set_lhs (new_stmt, new_temp);
6848 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
6849 if (slp_node)
6850 slp_node->push_vec_def (new_stmt);
6851 else
6852 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
6853 }
6854
6855 if (!slp_node)
6856 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
6857
6858 vec_oprnds0.release ();
6859 vec_oprnds1.release ();
6860
6861 return true;
6862 }
6863
6864 /* Function vectorizable_operation.
6865
6866 Check if STMT_INFO performs a binary, unary or ternary operation that can
6867 be vectorized.
6868 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
6869 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
6870 Return true if STMT_INFO is vectorizable in this way. */
6871
6872 static bool
6873 vectorizable_operation (vec_info *vinfo,
6874 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
6875 gimple **vec_stmt, slp_tree slp_node,
6876 stmt_vector_for_cost *cost_vec)
6877 {
6878 tree vec_dest;
6879 tree scalar_dest;
6880 tree op0, op1 = NULL_TREE, op2 = NULL_TREE;
6881 tree vectype;
6882 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
6883 enum tree_code code, orig_code;
6884 machine_mode vec_mode;
6885 tree new_temp;
6886 int op_type;
6887 optab optab;
6888 bool target_support_p;
6889 enum vect_def_type dt[3]
6890 = {vect_unknown_def_type, vect_unknown_def_type, vect_unknown_def_type};
6891 int ndts = 3;
6892 poly_uint64 nunits_in;
6893 poly_uint64 nunits_out;
6894 tree vectype_out;
6895 unsigned int ncopies;
6896 unsigned vec_num;
6897 int i;
6898 vec<tree> vec_oprnds0 = vNULL;
6899 vec<tree> vec_oprnds1 = vNULL;
6900 vec<tree> vec_oprnds2 = vNULL;
6901 tree vop0, vop1, vop2;
6902 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
6903
6904 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
6905 return false;
6906
6907 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
6908 && ! vec_stmt)
6909 return false;
6910
6911 /* Is STMT a vectorizable binary/unary operation? */
6912 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
6913 if (!stmt)
6914 return false;
6915
6916 /* Loads and stores are handled in vectorizable_{load,store}. */
6917 if (STMT_VINFO_DATA_REF (stmt_info))
6918 return false;
6919
6920 orig_code = code = gimple_assign_rhs_code (stmt);
6921
6922 /* Shifts are handled in vectorizable_shift. */
6923 if (code == LSHIFT_EXPR
6924 || code == RSHIFT_EXPR
6925 || code == LROTATE_EXPR
6926 || code == RROTATE_EXPR)
6927 return false;
6928
6929 /* Comparisons are handled in vectorizable_comparison. */
6930 if (TREE_CODE_CLASS (code) == tcc_comparison)
6931 return false;
6932
6933 /* Conditions are handled in vectorizable_condition. */
6934 if (code == COND_EXPR)
6935 return false;
6936
6937 /* For pointer addition and subtraction, we should use the normal
6938 plus and minus for the vector operation. */
6939 if (code == POINTER_PLUS_EXPR)
6940 code = PLUS_EXPR;
6941 if (code == POINTER_DIFF_EXPR)
6942 code = MINUS_EXPR;
6943
6944 /* Support only unary or binary operations. */
6945 op_type = TREE_CODE_LENGTH (code);
6946 if (op_type != unary_op && op_type != binary_op && op_type != ternary_op)
6947 {
6948 if (dump_enabled_p ())
6949 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6950 "num. args = %d (not unary/binary/ternary op).\n",
6951 op_type);
6952 return false;
6953 }
6954
6955 scalar_dest = gimple_assign_lhs (stmt);
6956 vectype_out = STMT_VINFO_VECTYPE (stmt_info);
6957
6958 /* Most operations cannot handle bit-precision types without extra
6959 truncations. */
6960 bool mask_op_p = VECTOR_BOOLEAN_TYPE_P (vectype_out);
6961 if (!mask_op_p
6962 && !type_has_mode_precision_p (TREE_TYPE (scalar_dest))
6963 /* Exception are bitwise binary operations. */
6964 && code != BIT_IOR_EXPR
6965 && code != BIT_XOR_EXPR
6966 && code != BIT_AND_EXPR)
6967 {
6968 if (dump_enabled_p ())
6969 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6970 "bit-precision arithmetic not supported.\n");
6971 return false;
6972 }
6973
6974 slp_tree slp_op0;
6975 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
6976 0, &op0, &slp_op0, &dt[0], &vectype))
6977 {
6978 if (dump_enabled_p ())
6979 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6980 "use not simple.\n");
6981 return false;
6982 }
6983 bool is_invariant = (dt[0] == vect_external_def
6984 || dt[0] == vect_constant_def);
6985 /* If op0 is an external or constant def, infer the vector type
6986 from the scalar type. */
6987 if (!vectype)
6988 {
6989 /* For boolean type we cannot determine vectype by
6990 invariant value (don't know whether it is a vector
6991 of booleans or vector of integers). We use output
6992 vectype because operations on boolean don't change
6993 type. */
6994 if (VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (op0)))
6995 {
6996 if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (scalar_dest)))
6997 {
6998 if (dump_enabled_p ())
6999 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7000 "not supported operation on bool value.\n");
7001 return false;
7002 }
7003 vectype = vectype_out;
7004 }
7005 else
7006 vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (op0),
7007 slp_node);
7008 }
7009 if (vec_stmt)
7010 gcc_assert (vectype);
7011 if (!vectype)
7012 {
7013 if (dump_enabled_p ())
7014 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7015 "no vectype for scalar type %T\n",
7016 TREE_TYPE (op0));
7017
7018 return false;
7019 }
7020
7021 nunits_out = TYPE_VECTOR_SUBPARTS (vectype_out);
7022 nunits_in = TYPE_VECTOR_SUBPARTS (vectype);
7023 if (maybe_ne (nunits_out, nunits_in)
7024 || !tree_nop_conversion_p (TREE_TYPE (vectype_out), TREE_TYPE (vectype)))
7025 return false;
7026
7027 tree vectype2 = NULL_TREE, vectype3 = NULL_TREE;
7028 slp_tree slp_op1 = NULL, slp_op2 = NULL;
7029 if (op_type == binary_op || op_type == ternary_op)
7030 {
7031 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
7032 1, &op1, &slp_op1, &dt[1], &vectype2))
7033 {
7034 if (dump_enabled_p ())
7035 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7036 "use not simple.\n");
7037 return false;
7038 }
7039 is_invariant &= (dt[1] == vect_external_def
7040 || dt[1] == vect_constant_def);
7041 if (vectype2
7042 && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype2))
7043 || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
7044 TREE_TYPE (vectype2))))
7045 return false;
7046 }
7047 if (op_type == ternary_op)
7048 {
7049 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
7050 2, &op2, &slp_op2, &dt[2], &vectype3))
7051 {
7052 if (dump_enabled_p ())
7053 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7054 "use not simple.\n");
7055 return false;
7056 }
7057 is_invariant &= (dt[2] == vect_external_def
7058 || dt[2] == vect_constant_def);
7059 if (vectype3
7060 && (maybe_ne (nunits_out, TYPE_VECTOR_SUBPARTS (vectype3))
7061 || !tree_nop_conversion_p (TREE_TYPE (vectype_out),
7062 TREE_TYPE (vectype3))))
7063 return false;
7064 }
7065
7066 /* Multiple types in SLP are handled by creating the appropriate number of
7067 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
7068 case of SLP. */
7069 if (slp_node)
7070 {
7071 ncopies = 1;
7072 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
7073 }
7074 else
7075 {
7076 ncopies = vect_get_num_copies (loop_vinfo, vectype);
7077 vec_num = 1;
7078 }
7079
7080 gcc_assert (ncopies >= 1);
7081
7082 /* Reject attempts to combine mask types with nonmask types, e.g. if
7083 we have an AND between a (nonmask) boolean loaded from memory and
7084 a (mask) boolean result of a comparison.
7085
7086 TODO: We could easily fix these cases up using pattern statements. */
7087 if (VECTOR_BOOLEAN_TYPE_P (vectype) != mask_op_p
7088 || (vectype2 && VECTOR_BOOLEAN_TYPE_P (vectype2) != mask_op_p)
7089 || (vectype3 && VECTOR_BOOLEAN_TYPE_P (vectype3) != mask_op_p))
7090 {
7091 if (dump_enabled_p ())
7092 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7093 "mixed mask and nonmask vector types\n");
7094 return false;
7095 }
7096
7097 /* Supportable by target? */
7098
7099 vec_mode = TYPE_MODE (vectype);
7100 optab = optab_for_tree_code (code, vectype, optab_default);
7101 if (!optab)
7102 {
7103 if (dump_enabled_p ())
7104 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7105 "no optab.\n");
7106 return false;
7107 }
7108 target_support_p = can_implement_p (optab, vec_mode);
7109
7110 bool using_emulated_vectors_p = vect_emulated_vector_p (vectype);
7111 if (!target_support_p || using_emulated_vectors_p)
7112 {
7113 if (dump_enabled_p ())
7114 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7115 "op not supported by target.\n");
7116 /* When vec_mode is not a vector mode and we verified ops we
7117 do not have to lower like AND are natively supported let
7118 those through even when the mode isn't word_mode. For
7119 ops we have to lower the lowering code assumes we are
7120 dealing with word_mode. */
7121 if (!INTEGRAL_TYPE_P (TREE_TYPE (vectype))
7122 || (((code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR)
7123 || !target_support_p)
7124 && maybe_ne (GET_MODE_SIZE (vec_mode), UNITS_PER_WORD))
7125 /* Check only during analysis. */
7126 || (!vec_stmt && !vect_can_vectorize_without_simd_p (code)))
7127 {
7128 if (dump_enabled_p ())
7129 dump_printf (MSG_NOTE, "using word mode not possible.\n");
7130 return false;
7131 }
7132 if (dump_enabled_p ())
7133 dump_printf_loc (MSG_NOTE, vect_location,
7134 "proceeding using word mode.\n");
7135 using_emulated_vectors_p = true;
7136 }
7137
7138 int reduc_idx = STMT_VINFO_REDUC_IDX (stmt_info);
7139 vec_loop_masks *masks = (loop_vinfo ? &LOOP_VINFO_MASKS (loop_vinfo) : NULL);
7140 vec_loop_lens *lens = (loop_vinfo ? &LOOP_VINFO_LENS (loop_vinfo) : NULL);
7141 internal_fn cond_fn = get_conditional_internal_fn (code);
7142 internal_fn cond_len_fn = get_conditional_len_internal_fn (code);
7143
7144 /* If operating on inactive elements could generate spurious traps,
7145 we need to restrict the operation to active lanes. Note that this
7146 specifically doesn't apply to unhoisted invariants, since they
7147 operate on the same value for every lane.
7148
7149 Similarly, if this operation is part of a reduction, a fully-masked
7150 loop should only change the active lanes of the reduction chain,
7151 keeping the inactive lanes as-is. */
7152 bool mask_out_inactive = ((!is_invariant && gimple_could_trap_p (stmt))
7153 || reduc_idx >= 0);
7154
7155 if (!vec_stmt) /* transformation not required. */
7156 {
7157 if (loop_vinfo
7158 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)
7159 && mask_out_inactive)
7160 {
7161 if (cond_len_fn != IFN_LAST
7162 && direct_internal_fn_supported_p (cond_len_fn, vectype,
7163 OPTIMIZE_FOR_SPEED))
7164 vect_record_loop_len (loop_vinfo, lens, ncopies * vec_num, vectype,
7165 1);
7166 else if (cond_fn != IFN_LAST
7167 && direct_internal_fn_supported_p (cond_fn, vectype,
7168 OPTIMIZE_FOR_SPEED))
7169 vect_record_loop_mask (loop_vinfo, masks, ncopies * vec_num,
7170 vectype, NULL);
7171 else
7172 {
7173 if (dump_enabled_p ())
7174 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7175 "can't use a fully-masked loop because no"
7176 " conditional operation is available.\n");
7177 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
7178 }
7179 }
7180
7181 /* Put types on constant and invariant SLP children. */
7182 if (slp_node
7183 && (!vect_maybe_update_slp_op_vectype (slp_op0, vectype)
7184 || !vect_maybe_update_slp_op_vectype (slp_op1, vectype)
7185 || !vect_maybe_update_slp_op_vectype (slp_op2, vectype)))
7186 {
7187 if (dump_enabled_p ())
7188 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7189 "incompatible vector types for invariants\n");
7190 return false;
7191 }
7192
7193 STMT_VINFO_TYPE (stmt_info) = op_vec_info_type;
7194 DUMP_VECT_SCOPE ("vectorizable_operation");
7195 vect_model_simple_cost (vinfo, stmt_info,
7196 ncopies, dt, ndts, slp_node, cost_vec);
7197 if (using_emulated_vectors_p)
7198 {
7199 /* The above vect_model_simple_cost call handles constants
7200 in the prologue and (mis-)costs one of the stmts as
7201 vector stmt. See below for the actual lowering that will
7202 be applied. */
7203 unsigned n
7204 = slp_node ? SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) : ncopies;
7205 switch (code)
7206 {
7207 case PLUS_EXPR:
7208 n *= 5;
7209 break;
7210 case MINUS_EXPR:
7211 n *= 6;
7212 break;
7213 case NEGATE_EXPR:
7214 n *= 4;
7215 break;
7216 default:
7217 /* Bit operations do not have extra cost and are accounted
7218 as vector stmt by vect_model_simple_cost. */
7219 n = 0;
7220 break;
7221 }
7222 if (n != 0)
7223 {
7224 /* We also need to materialize two large constants. */
7225 record_stmt_cost (cost_vec, 2, scalar_stmt, stmt_info,
7226 0, vect_prologue);
7227 record_stmt_cost (cost_vec, n, scalar_stmt, stmt_info,
7228 0, vect_body);
7229 }
7230 }
7231 return true;
7232 }
7233
7234 /* Transform. */
7235
7236 if (dump_enabled_p ())
7237 dump_printf_loc (MSG_NOTE, vect_location,
7238 "transform binary/unary operation.\n");
7239
7240 bool masked_loop_p = loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
7241 bool len_loop_p = loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
7242
7243 /* POINTER_DIFF_EXPR has pointer arguments which are vectorized as
7244 vectors with unsigned elements, but the result is signed. So, we
7245 need to compute the MINUS_EXPR into vectype temporary and
7246 VIEW_CONVERT_EXPR it into the final vectype_out result. */
7247 tree vec_cvt_dest = NULL_TREE;
7248 if (orig_code == POINTER_DIFF_EXPR)
7249 {
7250 vec_dest = vect_create_destination_var (scalar_dest, vectype);
7251 vec_cvt_dest = vect_create_destination_var (scalar_dest, vectype_out);
7252 }
7253 /* Handle def. */
7254 else
7255 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
7256
7257 /* In case the vectorization factor (VF) is bigger than the number
7258 of elements that we can fit in a vectype (nunits), we have to generate
7259 more than one vector stmt - i.e - we need to "unroll" the
7260 vector stmt by a factor VF/nunits. In doing so, we record a pointer
7261 from one copy of the vector stmt to the next, in the field
7262 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
7263 stages to find the correct vector defs to be used when vectorizing
7264 stmts that use the defs of the current stmt. The example below
7265 illustrates the vectorization process when VF=16 and nunits=4 (i.e.,
7266 we need to create 4 vectorized stmts):
7267
7268 before vectorization:
7269 RELATED_STMT VEC_STMT
7270 S1: x = memref - -
7271 S2: z = x + 1 - -
7272
7273 step 1: vectorize stmt S1 (done in vectorizable_load. See more details
7274 there):
7275 RELATED_STMT VEC_STMT
7276 VS1_0: vx0 = memref0 VS1_1 -
7277 VS1_1: vx1 = memref1 VS1_2 -
7278 VS1_2: vx2 = memref2 VS1_3 -
7279 VS1_3: vx3 = memref3 - -
7280 S1: x = load - VS1_0
7281 S2: z = x + 1 - -
7282
7283 step2: vectorize stmt S2 (done here):
7284 To vectorize stmt S2 we first need to find the relevant vector
7285 def for the first operand 'x'. This is, as usual, obtained from
7286 the vector stmt recorded in the STMT_VINFO_VEC_STMT of the stmt
7287 that defines 'x' (S1). This way we find the stmt VS1_0, and the
7288 relevant vector def 'vx0'. Having found 'vx0' we can generate
7289 the vector stmt VS2_0, and as usual, record it in the
7290 STMT_VINFO_VEC_STMT of stmt S2.
7291 When creating the second copy (VS2_1), we obtain the relevant vector
7292 def from the vector stmt recorded in the STMT_VINFO_RELATED_STMT of
7293 stmt VS1_0. This way we find the stmt VS1_1 and the relevant
7294 vector def 'vx1'. Using 'vx1' we create stmt VS2_1 and record a
7295 pointer to it in the STMT_VINFO_RELATED_STMT of the vector stmt VS2_0.
7296 Similarly when creating stmts VS2_2 and VS2_3. This is the resulting
7297 chain of stmts and pointers:
7298 RELATED_STMT VEC_STMT
7299 VS1_0: vx0 = memref0 VS1_1 -
7300 VS1_1: vx1 = memref1 VS1_2 -
7301 VS1_2: vx2 = memref2 VS1_3 -
7302 VS1_3: vx3 = memref3 - -
7303 S1: x = load - VS1_0
7304 VS2_0: vz0 = vx0 + v1 VS2_1 -
7305 VS2_1: vz1 = vx1 + v1 VS2_2 -
7306 VS2_2: vz2 = vx2 + v1 VS2_3 -
7307 VS2_3: vz3 = vx3 + v1 - -
7308 S2: z = x + 1 - VS2_0 */
7309
7310 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
7311 op0, &vec_oprnds0, op1, &vec_oprnds1, op2, &vec_oprnds2);
7312 /* Arguments are ready. Create the new vector stmt. */
7313 FOR_EACH_VEC_ELT (vec_oprnds0, i, vop0)
7314 {
7315 gimple *new_stmt = NULL;
7316 vop1 = ((op_type == binary_op || op_type == ternary_op)
7317 ? vec_oprnds1[i] : NULL_TREE);
7318 vop2 = ((op_type == ternary_op) ? vec_oprnds2[i] : NULL_TREE);
7319 if (using_emulated_vectors_p
7320 && (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR))
7321 {
7322 /* Lower the operation. This follows vector lowering. */
7323 unsigned int width = vector_element_bits (vectype);
7324 tree inner_type = TREE_TYPE (vectype);
7325 tree word_type
7326 = build_nonstandard_integer_type (GET_MODE_BITSIZE (word_mode), 1);
7327 HOST_WIDE_INT max = GET_MODE_MASK (TYPE_MODE (inner_type));
7328 tree low_bits = build_replicated_int_cst (word_type, width, max >> 1);
7329 tree high_bits
7330 = build_replicated_int_cst (word_type, width, max & ~(max >> 1));
7331 tree wvop0 = make_ssa_name (word_type);
7332 new_stmt = gimple_build_assign (wvop0, VIEW_CONVERT_EXPR,
7333 build1 (VIEW_CONVERT_EXPR,
7334 word_type, vop0));
7335 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7336 tree result_low, signs;
7337 if (code == PLUS_EXPR || code == MINUS_EXPR)
7338 {
7339 tree wvop1 = make_ssa_name (word_type);
7340 new_stmt = gimple_build_assign (wvop1, VIEW_CONVERT_EXPR,
7341 build1 (VIEW_CONVERT_EXPR,
7342 word_type, vop1));
7343 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7344 signs = make_ssa_name (word_type);
7345 new_stmt = gimple_build_assign (signs,
7346 BIT_XOR_EXPR, wvop0, wvop1);
7347 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7348 tree b_low = make_ssa_name (word_type);
7349 new_stmt = gimple_build_assign (b_low,
7350 BIT_AND_EXPR, wvop1, low_bits);
7351 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7352 tree a_low = make_ssa_name (word_type);
7353 if (code == PLUS_EXPR)
7354 new_stmt = gimple_build_assign (a_low,
7355 BIT_AND_EXPR, wvop0, low_bits);
7356 else
7357 new_stmt = gimple_build_assign (a_low,
7358 BIT_IOR_EXPR, wvop0, high_bits);
7359 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7360 if (code == MINUS_EXPR)
7361 {
7362 new_stmt = gimple_build_assign (NULL_TREE,
7363 BIT_NOT_EXPR, signs);
7364 signs = make_ssa_name (word_type);
7365 gimple_assign_set_lhs (new_stmt, signs);
7366 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7367 }
7368 new_stmt = gimple_build_assign (NULL_TREE,
7369 BIT_AND_EXPR, signs, high_bits);
7370 signs = make_ssa_name (word_type);
7371 gimple_assign_set_lhs (new_stmt, signs);
7372 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7373 result_low = make_ssa_name (word_type);
7374 new_stmt = gimple_build_assign (result_low, code, a_low, b_low);
7375 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7376 }
7377 else
7378 {
7379 tree a_low = make_ssa_name (word_type);
7380 new_stmt = gimple_build_assign (a_low,
7381 BIT_AND_EXPR, wvop0, low_bits);
7382 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7383 signs = make_ssa_name (word_type);
7384 new_stmt = gimple_build_assign (signs, BIT_NOT_EXPR, wvop0);
7385 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7386 new_stmt = gimple_build_assign (NULL_TREE,
7387 BIT_AND_EXPR, signs, high_bits);
7388 signs = make_ssa_name (word_type);
7389 gimple_assign_set_lhs (new_stmt, signs);
7390 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7391 result_low = make_ssa_name (word_type);
7392 new_stmt = gimple_build_assign (result_low,
7393 MINUS_EXPR, high_bits, a_low);
7394 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7395 }
7396 new_stmt = gimple_build_assign (NULL_TREE, BIT_XOR_EXPR, result_low,
7397 signs);
7398 result_low = make_ssa_name (word_type);
7399 gimple_assign_set_lhs (new_stmt, result_low);
7400 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7401 new_stmt = gimple_build_assign (NULL_TREE, VIEW_CONVERT_EXPR,
7402 build1 (VIEW_CONVERT_EXPR,
7403 vectype, result_low));
7404 new_temp = make_ssa_name (vectype);
7405 gimple_assign_set_lhs (new_stmt, new_temp);
7406 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7407 }
7408 else if ((masked_loop_p || len_loop_p) && mask_out_inactive)
7409 {
7410 tree mask;
7411 if (masked_loop_p)
7412 mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7413 vec_num * ncopies, vectype, i);
7414 else
7415 /* Dummy mask. */
7416 mask = build_minus_one_cst (truth_type_for (vectype));
7417 auto_vec<tree> vops (6);
7418 vops.quick_push (mask);
7419 vops.quick_push (vop0);
7420 if (vop1)
7421 vops.quick_push (vop1);
7422 if (vop2)
7423 vops.quick_push (vop2);
7424 if (reduc_idx >= 0)
7425 {
7426 /* Perform the operation on active elements only and take
7427 inactive elements from the reduction chain input. */
7428 gcc_assert (!vop2);
7429 vops.quick_push (reduc_idx == 1 ? vop1 : vop0);
7430 }
7431 else
7432 {
7433 auto else_value = targetm.preferred_else_value
7434 (cond_fn, vectype, vops.length () - 1, &vops[1]);
7435 vops.quick_push (else_value);
7436 }
7437 if (len_loop_p)
7438 {
7439 tree len = vect_get_loop_len (loop_vinfo, gsi, lens,
7440 vec_num * ncopies, vectype, i, 1);
7441 signed char biasval
7442 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
7443 tree bias = build_int_cst (intQI_type_node, biasval);
7444 vops.quick_push (len);
7445 vops.quick_push (bias);
7446 }
7447 gcall *call
7448 = gimple_build_call_internal_vec (masked_loop_p ? cond_fn
7449 : cond_len_fn,
7450 vops);
7451 new_temp = make_ssa_name (vec_dest, call);
7452 gimple_call_set_lhs (call, new_temp);
7453 gimple_call_set_nothrow (call, true);
7454 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
7455 new_stmt = call;
7456 }
7457 else
7458 {
7459 tree mask = NULL_TREE;
7460 /* When combining two masks check if either of them is elsewhere
7461 combined with a loop mask, if that's the case we can mark that the
7462 new combined mask doesn't need to be combined with a loop mask. */
7463 if (masked_loop_p
7464 && code == BIT_AND_EXPR
7465 && VECTOR_BOOLEAN_TYPE_P (vectype))
7466 {
7467 if (loop_vinfo->scalar_cond_masked_set.contains
7468 ({ op0, vec_num * ncopies}))
7469 {
7470 mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7471 vec_num * ncopies, vectype, i);
7472
7473 vop0 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7474 vop0, gsi);
7475 }
7476
7477 if (loop_vinfo->scalar_cond_masked_set.contains
7478 ({ op1, vec_num * ncopies }))
7479 {
7480 mask = vect_get_loop_mask (loop_vinfo, gsi, masks,
7481 vec_num * ncopies, vectype, i);
7482
7483 vop1 = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
7484 vop1, gsi);
7485 }
7486 }
7487
7488 new_stmt = gimple_build_assign (vec_dest, code, vop0, vop1, vop2);
7489 new_temp = make_ssa_name (vec_dest, new_stmt);
7490 gimple_assign_set_lhs (new_stmt, new_temp);
7491 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
7492 if (using_emulated_vectors_p)
7493 suppress_warning (new_stmt, OPT_Wvector_operation_performance);
7494
7495 /* Enter the combined value into the vector cond hash so we don't
7496 AND it with a loop mask again. */
7497 if (mask)
7498 loop_vinfo->vec_cond_masked_set.add ({ new_temp, mask });
7499 }
7500
7501 if (vec_cvt_dest)
7502 {
7503 new_temp = build1 (VIEW_CONVERT_EXPR, vectype_out, new_temp);
7504 new_stmt = gimple_build_assign (vec_cvt_dest, VIEW_CONVERT_EXPR,
7505 new_temp);
7506 new_temp = make_ssa_name (vec_cvt_dest, new_stmt);
7507 gimple_assign_set_lhs (new_stmt, new_temp);
7508 vect_finish_stmt_generation (vinfo, stmt_info,
7509 new_stmt, gsi);
7510 }
7511
7512 if (slp_node)
7513 slp_node->push_vec_def (new_stmt);
7514 else
7515 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
7516 }
7517
7518 if (!slp_node)
7519 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
7520
7521 vec_oprnds0.release ();
7522 vec_oprnds1.release ();
7523 vec_oprnds2.release ();
7524
7525 return true;
7526 }
7527
7528 /* A helper function to ensure data reference DR_INFO's base alignment. */
7529
7530 static void
7531 ensure_base_align (dr_vec_info *dr_info)
7532 {
7533 /* Alignment is only analyzed for the first element of a DR group,
7534 use that to look at base alignment we need to enforce. */
7535 if (STMT_VINFO_GROUPED_ACCESS (dr_info->stmt))
7536 dr_info = STMT_VINFO_DR_INFO (DR_GROUP_FIRST_ELEMENT (dr_info->stmt));
7537
7538 gcc_assert (dr_info->misalignment != DR_MISALIGNMENT_UNINITIALIZED);
7539
7540 if (dr_info->base_misaligned)
7541 {
7542 tree base_decl = dr_info->base_decl;
7543
7544 // We should only be able to increase the alignment of a base object if
7545 // we know what its new alignment should be at compile time.
7546 unsigned HOST_WIDE_INT align_base_to =
7547 DR_TARGET_ALIGNMENT (dr_info).to_constant () * BITS_PER_UNIT;
7548
7549 if (decl_in_symtab_p (base_decl))
7550 symtab_node::get (base_decl)->increase_alignment (align_base_to);
7551 else if (DECL_ALIGN (base_decl) < align_base_to)
7552 {
7553 SET_DECL_ALIGN (base_decl, align_base_to);
7554 DECL_USER_ALIGN (base_decl) = 1;
7555 }
7556 dr_info->base_misaligned = false;
7557 }
7558 }
7559
7560
7561 /* Function get_group_alias_ptr_type.
7562
7563 Return the alias type for the group starting at FIRST_STMT_INFO. */
7564
7565 static tree
7566 get_group_alias_ptr_type (stmt_vec_info first_stmt_info)
7567 {
7568 struct data_reference *first_dr, *next_dr;
7569
7570 first_dr = STMT_VINFO_DATA_REF (first_stmt_info);
7571 stmt_vec_info next_stmt_info = DR_GROUP_NEXT_ELEMENT (first_stmt_info);
7572 while (next_stmt_info)
7573 {
7574 next_dr = STMT_VINFO_DATA_REF (next_stmt_info);
7575 if (get_alias_set (DR_REF (first_dr))
7576 != get_alias_set (DR_REF (next_dr)))
7577 {
7578 if (dump_enabled_p ())
7579 dump_printf_loc (MSG_NOTE, vect_location,
7580 "conflicting alias set types.\n");
7581 return ptr_type_node;
7582 }
7583 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
7584 }
7585 return reference_alias_ptr_type (DR_REF (first_dr));
7586 }
7587
7588
7589 /* Function scan_operand_equal_p.
7590
7591 Helper function for check_scan_store. Compare two references
7592 with .GOMP_SIMD_LANE bases. */
7593
7594 static bool
7595 scan_operand_equal_p (tree ref1, tree ref2)
7596 {
7597 tree ref[2] = { ref1, ref2 };
7598 poly_int64 bitsize[2], bitpos[2];
7599 tree offset[2], base[2];
7600 for (int i = 0; i < 2; ++i)
7601 {
7602 machine_mode mode;
7603 int unsignedp, reversep, volatilep = 0;
7604 base[i] = get_inner_reference (ref[i], &bitsize[i], &bitpos[i],
7605 &offset[i], &mode, &unsignedp,
7606 &reversep, &volatilep);
7607 if (reversep || volatilep || maybe_ne (bitpos[i], 0))
7608 return false;
7609 if (TREE_CODE (base[i]) == MEM_REF
7610 && offset[i] == NULL_TREE
7611 && TREE_CODE (TREE_OPERAND (base[i], 0)) == SSA_NAME)
7612 {
7613 gimple *def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base[i], 0));
7614 if (is_gimple_assign (def_stmt)
7615 && gimple_assign_rhs_code (def_stmt) == POINTER_PLUS_EXPR
7616 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == ADDR_EXPR
7617 && TREE_CODE (gimple_assign_rhs2 (def_stmt)) == SSA_NAME)
7618 {
7619 if (maybe_ne (mem_ref_offset (base[i]), 0))
7620 return false;
7621 base[i] = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
7622 offset[i] = gimple_assign_rhs2 (def_stmt);
7623 }
7624 }
7625 }
7626
7627 if (!operand_equal_p (base[0], base[1], 0))
7628 return false;
7629 if (maybe_ne (bitsize[0], bitsize[1]))
7630 return false;
7631 if (offset[0] != offset[1])
7632 {
7633 if (!offset[0] || !offset[1])
7634 return false;
7635 if (!operand_equal_p (offset[0], offset[1], 0))
7636 {
7637 tree step[2];
7638 for (int i = 0; i < 2; ++i)
7639 {
7640 step[i] = integer_one_node;
7641 if (TREE_CODE (offset[i]) == SSA_NAME)
7642 {
7643 gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7644 if (is_gimple_assign (def_stmt)
7645 && gimple_assign_rhs_code (def_stmt) == MULT_EXPR
7646 && (TREE_CODE (gimple_assign_rhs2 (def_stmt))
7647 == INTEGER_CST))
7648 {
7649 step[i] = gimple_assign_rhs2 (def_stmt);
7650 offset[i] = gimple_assign_rhs1 (def_stmt);
7651 }
7652 }
7653 else if (TREE_CODE (offset[i]) == MULT_EXPR)
7654 {
7655 step[i] = TREE_OPERAND (offset[i], 1);
7656 offset[i] = TREE_OPERAND (offset[i], 0);
7657 }
7658 tree rhs1 = NULL_TREE;
7659 if (TREE_CODE (offset[i]) == SSA_NAME)
7660 {
7661 gimple *def_stmt = SSA_NAME_DEF_STMT (offset[i]);
7662 if (gimple_assign_cast_p (def_stmt))
7663 rhs1 = gimple_assign_rhs1 (def_stmt);
7664 }
7665 else if (CONVERT_EXPR_P (offset[i]))
7666 rhs1 = TREE_OPERAND (offset[i], 0);
7667 if (rhs1
7668 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
7669 && INTEGRAL_TYPE_P (TREE_TYPE (offset[i]))
7670 && (TYPE_PRECISION (TREE_TYPE (offset[i]))
7671 >= TYPE_PRECISION (TREE_TYPE (rhs1))))
7672 offset[i] = rhs1;
7673 }
7674 if (!operand_equal_p (offset[0], offset[1], 0)
7675 || !operand_equal_p (step[0], step[1], 0))
7676 return false;
7677 }
7678 }
7679 return true;
7680 }
7681
7682
7683 enum scan_store_kind {
7684 /* Normal permutation. */
7685 scan_store_kind_perm,
7686
7687 /* Whole vector left shift permutation with zero init. */
7688 scan_store_kind_lshift_zero,
7689
7690 /* Whole vector left shift permutation and VEC_COND_EXPR. */
7691 scan_store_kind_lshift_cond
7692 };
7693
7694 /* Function check_scan_store.
7695
7696 Verify if we can perform the needed permutations or whole vector shifts.
7697 Return -1 on failure, otherwise exact log2 of vectype's nunits.
7698 USE_WHOLE_VECTOR is a vector of enum scan_store_kind which operation
7699 to do at each step. */
7700
7701 static int
7702 scan_store_can_perm_p (tree vectype, tree init,
7703 vec<enum scan_store_kind> *use_whole_vector = NULL)
7704 {
7705 enum machine_mode vec_mode = TYPE_MODE (vectype);
7706 unsigned HOST_WIDE_INT nunits;
7707 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
7708 return -1;
7709 int units_log2 = exact_log2 (nunits);
7710 if (units_log2 <= 0)
7711 return -1;
7712
7713 int i;
7714 enum scan_store_kind whole_vector_shift_kind = scan_store_kind_perm;
7715 for (i = 0; i <= units_log2; ++i)
7716 {
7717 unsigned HOST_WIDE_INT j, k;
7718 enum scan_store_kind kind = scan_store_kind_perm;
7719 vec_perm_builder sel (nunits, nunits, 1);
7720 sel.quick_grow (nunits);
7721 if (i == units_log2)
7722 {
7723 for (j = 0; j < nunits; ++j)
7724 sel[j] = nunits - 1;
7725 }
7726 else
7727 {
7728 for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
7729 sel[j] = j;
7730 for (k = 0; j < nunits; ++j, ++k)
7731 sel[j] = nunits + k;
7732 }
7733 vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
7734 if (!can_vec_perm_const_p (vec_mode, vec_mode, indices))
7735 {
7736 if (i == units_log2)
7737 return -1;
7738
7739 if (whole_vector_shift_kind == scan_store_kind_perm)
7740 {
7741 if (!can_implement_p (vec_shl_optab, vec_mode))
7742 return -1;
7743 whole_vector_shift_kind = scan_store_kind_lshift_zero;
7744 /* Whole vector shifts shift in zeros, so if init is all zero
7745 constant, there is no need to do anything further. */
7746 if ((TREE_CODE (init) != INTEGER_CST
7747 && TREE_CODE (init) != REAL_CST)
7748 || !initializer_zerop (init))
7749 {
7750 tree masktype = truth_type_for (vectype);
7751 if (!expand_vec_cond_expr_p (vectype, masktype))
7752 return -1;
7753 whole_vector_shift_kind = scan_store_kind_lshift_cond;
7754 }
7755 }
7756 kind = whole_vector_shift_kind;
7757 }
7758 if (use_whole_vector)
7759 {
7760 if (kind != scan_store_kind_perm && use_whole_vector->is_empty ())
7761 use_whole_vector->safe_grow_cleared (i, true);
7762 if (kind != scan_store_kind_perm || !use_whole_vector->is_empty ())
7763 use_whole_vector->safe_push (kind);
7764 }
7765 }
7766
7767 return units_log2;
7768 }
7769
7770
7771 /* Function check_scan_store.
7772
7773 Check magic stores for #pragma omp scan {in,ex}clusive reductions. */
7774
7775 static bool
7776 check_scan_store (vec_info *vinfo, stmt_vec_info stmt_info, tree vectype,
7777 enum vect_def_type rhs_dt, slp_tree slp_node, tree mask,
7778 vect_memory_access_type memory_access_type)
7779 {
7780 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
7781 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
7782 tree ref_type;
7783
7784 gcc_assert (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1);
7785 if ((slp_node && SLP_TREE_LANES (slp_node) > 1)
7786 || mask
7787 || memory_access_type != VMAT_CONTIGUOUS
7788 || TREE_CODE (DR_BASE_ADDRESS (dr_info->dr)) != ADDR_EXPR
7789 || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0))
7790 || loop_vinfo == NULL
7791 || LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
7792 || STMT_VINFO_GROUPED_ACCESS (stmt_info)
7793 || !integer_zerop (get_dr_vinfo_offset (vinfo, dr_info))
7794 || !integer_zerop (DR_INIT (dr_info->dr))
7795 || !(ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr)))
7796 || !alias_sets_conflict_p (get_alias_set (vectype),
7797 get_alias_set (TREE_TYPE (ref_type))))
7798 {
7799 if (dump_enabled_p ())
7800 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7801 "unsupported OpenMP scan store.\n");
7802 return false;
7803 }
7804
7805 /* We need to pattern match code built by OpenMP lowering and simplified
7806 by following optimizations into something we can handle.
7807 #pragma omp simd reduction(inscan,+:r)
7808 for (...)
7809 {
7810 r += something ();
7811 #pragma omp scan inclusive (r)
7812 use (r);
7813 }
7814 shall have body with:
7815 // Initialization for input phase, store the reduction initializer:
7816 _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7817 _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7818 D.2042[_21] = 0;
7819 // Actual input phase:
7820 ...
7821 r.0_5 = D.2042[_20];
7822 _6 = _4 + r.0_5;
7823 D.2042[_20] = _6;
7824 // Initialization for scan phase:
7825 _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 2);
7826 _26 = D.2043[_25];
7827 _27 = D.2042[_25];
7828 _28 = _26 + _27;
7829 D.2043[_25] = _28;
7830 D.2042[_25] = _28;
7831 // Actual scan phase:
7832 ...
7833 r.1_8 = D.2042[_20];
7834 ...
7835 The "omp simd array" variable D.2042 holds the privatized copy used
7836 inside of the loop and D.2043 is another one that holds copies of
7837 the current original list item. The separate GOMP_SIMD_LANE ifn
7838 kinds are there in order to allow optimizing the initializer store
7839 and combiner sequence, e.g. if it is originally some C++ish user
7840 defined reduction, but allow the vectorizer to pattern recognize it
7841 and turn into the appropriate vectorized scan.
7842
7843 For exclusive scan, this is slightly different:
7844 #pragma omp simd reduction(inscan,+:r)
7845 for (...)
7846 {
7847 use (r);
7848 #pragma omp scan exclusive (r)
7849 r += something ();
7850 }
7851 shall have body with:
7852 // Initialization for input phase, store the reduction initializer:
7853 _20 = .GOMP_SIMD_LANE (simduid.3_14(D), 0);
7854 _21 = .GOMP_SIMD_LANE (simduid.3_14(D), 1);
7855 D.2042[_21] = 0;
7856 // Actual input phase:
7857 ...
7858 r.0_5 = D.2042[_20];
7859 _6 = _4 + r.0_5;
7860 D.2042[_20] = _6;
7861 // Initialization for scan phase:
7862 _25 = .GOMP_SIMD_LANE (simduid.3_14(D), 3);
7863 _26 = D.2043[_25];
7864 D.2044[_25] = _26;
7865 _27 = D.2042[_25];
7866 _28 = _26 + _27;
7867 D.2043[_25] = _28;
7868 // Actual scan phase:
7869 ...
7870 r.1_8 = D.2044[_20];
7871 ... */
7872
7873 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 2)
7874 {
7875 /* Match the D.2042[_21] = 0; store above. Just require that
7876 it is a constant or external definition store. */
7877 if (rhs_dt != vect_constant_def && rhs_dt != vect_external_def)
7878 {
7879 fail_init:
7880 if (dump_enabled_p ())
7881 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7882 "unsupported OpenMP scan initializer store.\n");
7883 return false;
7884 }
7885
7886 if (! loop_vinfo->scan_map)
7887 loop_vinfo->scan_map = new hash_map<tree, tree>;
7888 tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7889 tree &cached = loop_vinfo->scan_map->get_or_insert (var);
7890 if (cached)
7891 goto fail_init;
7892 cached = gimple_assign_rhs1 (STMT_VINFO_STMT (stmt_info));
7893
7894 /* These stores can be vectorized normally. */
7895 return true;
7896 }
7897
7898 if (rhs_dt != vect_internal_def)
7899 {
7900 fail:
7901 if (dump_enabled_p ())
7902 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
7903 "unsupported OpenMP scan combiner pattern.\n");
7904 return false;
7905 }
7906
7907 gimple *stmt = STMT_VINFO_STMT (stmt_info);
7908 tree rhs = gimple_assign_rhs1 (stmt);
7909 if (TREE_CODE (rhs) != SSA_NAME)
7910 goto fail;
7911
7912 gimple *other_store_stmt = NULL;
7913 tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
7914 bool inscan_var_store
7915 = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
7916
7917 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
7918 {
7919 if (!inscan_var_store)
7920 {
7921 use_operand_p use_p;
7922 imm_use_iterator iter;
7923 FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7924 {
7925 gimple *use_stmt = USE_STMT (use_p);
7926 if (use_stmt == stmt || is_gimple_debug (use_stmt))
7927 continue;
7928 if (gimple_bb (use_stmt) != gimple_bb (stmt)
7929 || !is_gimple_assign (use_stmt)
7930 || gimple_assign_rhs_class (use_stmt) != GIMPLE_BINARY_RHS
7931 || other_store_stmt
7932 || TREE_CODE (gimple_assign_lhs (use_stmt)) != SSA_NAME)
7933 goto fail;
7934 other_store_stmt = use_stmt;
7935 }
7936 if (other_store_stmt == NULL)
7937 goto fail;
7938 rhs = gimple_assign_lhs (other_store_stmt);
7939 if (!single_imm_use (rhs, &use_p, &other_store_stmt))
7940 goto fail;
7941 }
7942 }
7943 else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3)
7944 {
7945 use_operand_p use_p;
7946 imm_use_iterator iter;
7947 FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
7948 {
7949 gimple *use_stmt = USE_STMT (use_p);
7950 if (use_stmt == stmt || is_gimple_debug (use_stmt))
7951 continue;
7952 if (other_store_stmt)
7953 goto fail;
7954 other_store_stmt = use_stmt;
7955 }
7956 }
7957 else
7958 goto fail;
7959
7960 gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
7961 if (gimple_bb (def_stmt) != gimple_bb (stmt)
7962 || !is_gimple_assign (def_stmt)
7963 || gimple_assign_rhs_class (def_stmt) != GIMPLE_BINARY_RHS)
7964 goto fail;
7965
7966 enum tree_code code = gimple_assign_rhs_code (def_stmt);
7967 /* For pointer addition, we should use the normal plus for the vector
7968 operation. */
7969 switch (code)
7970 {
7971 case POINTER_PLUS_EXPR:
7972 code = PLUS_EXPR;
7973 break;
7974 case MULT_HIGHPART_EXPR:
7975 goto fail;
7976 default:
7977 break;
7978 }
7979 if (TREE_CODE_LENGTH (code) != binary_op || !commutative_tree_code (code))
7980 goto fail;
7981
7982 tree rhs1 = gimple_assign_rhs1 (def_stmt);
7983 tree rhs2 = gimple_assign_rhs2 (def_stmt);
7984 if (TREE_CODE (rhs1) != SSA_NAME || TREE_CODE (rhs2) != SSA_NAME)
7985 goto fail;
7986
7987 gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
7988 gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
7989 if (gimple_bb (load1_stmt) != gimple_bb (stmt)
7990 || !gimple_assign_load_p (load1_stmt)
7991 || gimple_bb (load2_stmt) != gimple_bb (stmt)
7992 || !gimple_assign_load_p (load2_stmt))
7993 goto fail;
7994
7995 stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
7996 stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
7997 if (load1_stmt_info == NULL
7998 || load2_stmt_info == NULL
7999 || (STMT_VINFO_SIMD_LANE_ACCESS_P (load1_stmt_info)
8000 != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info))
8001 || (STMT_VINFO_SIMD_LANE_ACCESS_P (load2_stmt_info)
8002 != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
8003 goto fail;
8004
8005 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && inscan_var_store)
8006 {
8007 dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
8008 if (TREE_CODE (DR_BASE_ADDRESS (load1_dr_info->dr)) != ADDR_EXPR
8009 || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0)))
8010 goto fail;
8011 tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
8012 tree lrhs;
8013 if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
8014 lrhs = rhs1;
8015 else
8016 lrhs = rhs2;
8017 use_operand_p use_p;
8018 imm_use_iterator iter;
8019 FOR_EACH_IMM_USE_FAST (use_p, iter, lrhs)
8020 {
8021 gimple *use_stmt = USE_STMT (use_p);
8022 if (use_stmt == def_stmt || is_gimple_debug (use_stmt))
8023 continue;
8024 if (other_store_stmt)
8025 goto fail;
8026 other_store_stmt = use_stmt;
8027 }
8028 }
8029
8030 if (other_store_stmt == NULL)
8031 goto fail;
8032 if (gimple_bb (other_store_stmt) != gimple_bb (stmt)
8033 || !gimple_store_p (other_store_stmt))
8034 goto fail;
8035
8036 stmt_vec_info other_store_stmt_info
8037 = loop_vinfo->lookup_stmt (other_store_stmt);
8038 if (other_store_stmt_info == NULL
8039 || (STMT_VINFO_SIMD_LANE_ACCESS_P (other_store_stmt_info)
8040 != STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info)))
8041 goto fail;
8042
8043 gimple *stmt1 = stmt;
8044 gimple *stmt2 = other_store_stmt;
8045 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
8046 std::swap (stmt1, stmt2);
8047 if (scan_operand_equal_p (gimple_assign_lhs (stmt1),
8048 gimple_assign_rhs1 (load2_stmt)))
8049 {
8050 std::swap (rhs1, rhs2);
8051 std::swap (load1_stmt, load2_stmt);
8052 std::swap (load1_stmt_info, load2_stmt_info);
8053 }
8054 if (!scan_operand_equal_p (gimple_assign_lhs (stmt1),
8055 gimple_assign_rhs1 (load1_stmt)))
8056 goto fail;
8057
8058 tree var3 = NULL_TREE;
8059 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 3
8060 && !scan_operand_equal_p (gimple_assign_lhs (stmt2),
8061 gimple_assign_rhs1 (load2_stmt)))
8062 goto fail;
8063 else if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8064 {
8065 dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
8066 if (TREE_CODE (DR_BASE_ADDRESS (load2_dr_info->dr)) != ADDR_EXPR
8067 || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0)))
8068 goto fail;
8069 var3 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
8070 if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var3))
8071 || lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var3))
8072 || lookup_attribute ("omp simd inscan exclusive",
8073 DECL_ATTRIBUTES (var3)))
8074 goto fail;
8075 }
8076
8077 dr_vec_info *other_dr_info = STMT_VINFO_DR_INFO (other_store_stmt_info);
8078 if (TREE_CODE (DR_BASE_ADDRESS (other_dr_info->dr)) != ADDR_EXPR
8079 || !VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0)))
8080 goto fail;
8081
8082 tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
8083 tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (other_dr_info->dr), 0);
8084 if (!lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var1))
8085 || !lookup_attribute ("omp simd array", DECL_ATTRIBUTES (var2))
8086 || (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
8087 == (!lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var2))))
8088 goto fail;
8089
8090 if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
8091 std::swap (var1, var2);
8092
8093 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8094 {
8095 if (!lookup_attribute ("omp simd inscan exclusive",
8096 DECL_ATTRIBUTES (var1)))
8097 goto fail;
8098 var1 = var3;
8099 }
8100
8101 if (loop_vinfo->scan_map == NULL)
8102 goto fail;
8103 tree *init = loop_vinfo->scan_map->get (var1);
8104 if (init == NULL)
8105 goto fail;
8106
8107 /* The IL is as expected, now check if we can actually vectorize it.
8108 Inclusive scan:
8109 _26 = D.2043[_25];
8110 _27 = D.2042[_25];
8111 _28 = _26 + _27;
8112 D.2043[_25] = _28;
8113 D.2042[_25] = _28;
8114 should be vectorized as (where _40 is the vectorized rhs
8115 from the D.2042[_21] = 0; store):
8116 _30 = MEM <vector(8) int> [(int *)&D.2043];
8117 _31 = MEM <vector(8) int> [(int *)&D.2042];
8118 _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
8119 _33 = _31 + _32;
8120 // _33 = { _31[0], _31[0]+_31[1], _31[1]+_31[2], ..., _31[6]+_31[7] };
8121 _34 = VEC_PERM_EXPR <_40, _33, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
8122 _35 = _33 + _34;
8123 // _35 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
8124 // _31[1]+.._31[4], ... _31[4]+.._31[7] };
8125 _36 = VEC_PERM_EXPR <_40, _35, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
8126 _37 = _35 + _36;
8127 // _37 = { _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
8128 // _31[0]+.._31[4], ... _31[0]+.._31[7] };
8129 _38 = _30 + _37;
8130 _39 = VEC_PERM_EXPR <_38, _38, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
8131 MEM <vector(8) int> [(int *)&D.2043] = _39;
8132 MEM <vector(8) int> [(int *)&D.2042] = _38;
8133 Exclusive scan:
8134 _26 = D.2043[_25];
8135 D.2044[_25] = _26;
8136 _27 = D.2042[_25];
8137 _28 = _26 + _27;
8138 D.2043[_25] = _28;
8139 should be vectorized as (where _40 is the vectorized rhs
8140 from the D.2042[_21] = 0; store):
8141 _30 = MEM <vector(8) int> [(int *)&D.2043];
8142 _31 = MEM <vector(8) int> [(int *)&D.2042];
8143 _32 = VEC_PERM_EXPR <_40, _31, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
8144 _33 = VEC_PERM_EXPR <_40, _32, { 0, 8, 9, 10, 11, 12, 13, 14 }>;
8145 _34 = _32 + _33;
8146 // _34 = { 0, _31[0], _31[0]+_31[1], _31[1]+_31[2], _31[2]+_31[3],
8147 // _31[3]+_31[4], ... _31[5]+.._31[6] };
8148 _35 = VEC_PERM_EXPR <_40, _34, { 0, 1, 8, 9, 10, 11, 12, 13 }>;
8149 _36 = _34 + _35;
8150 // _36 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
8151 // _31[1]+.._31[4], ... _31[3]+.._31[6] };
8152 _37 = VEC_PERM_EXPR <_40, _36, { 0, 1, 2, 3, 8, 9, 10, 11 }>;
8153 _38 = _36 + _37;
8154 // _38 = { 0, _31[0], _31[0]+_31[1], _31[0]+.._31[2], _31[0]+.._31[3],
8155 // _31[0]+.._31[4], ... _31[0]+.._31[6] };
8156 _39 = _30 + _38;
8157 _50 = _31 + _39;
8158 _51 = VEC_PERM_EXPR <_50, _50, { 7, 7, 7, 7, 7, 7, 7, 7 }>;
8159 MEM <vector(8) int> [(int *)&D.2044] = _39;
8160 MEM <vector(8) int> [(int *)&D.2042] = _51; */
8161 enum machine_mode vec_mode = TYPE_MODE (vectype);
8162 optab optab = optab_for_tree_code (code, vectype, optab_default);
8163 if (!optab || !can_implement_p (optab, vec_mode))
8164 goto fail;
8165
8166 int units_log2 = scan_store_can_perm_p (vectype, *init);
8167 if (units_log2 == -1)
8168 goto fail;
8169
8170 return true;
8171 }
8172
8173
8174 /* Function vectorizable_scan_store.
8175
8176 Helper of vectorizable_score, arguments like on vectorizable_store.
8177 Handle only the transformation, checking is done in check_scan_store. */
8178
8179 static bool
8180 vectorizable_scan_store (vec_info *vinfo, stmt_vec_info stmt_info,
8181 slp_tree slp_node, gimple_stmt_iterator *gsi,
8182 gimple **vec_stmt, int ncopies)
8183 {
8184 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
8185 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
8186 tree ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
8187 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
8188
8189 if (dump_enabled_p ())
8190 dump_printf_loc (MSG_NOTE, vect_location,
8191 "transform scan store. ncopies = %d\n", ncopies);
8192
8193 gimple *stmt = STMT_VINFO_STMT (stmt_info);
8194 tree rhs = gimple_assign_rhs1 (stmt);
8195 gcc_assert (TREE_CODE (rhs) == SSA_NAME);
8196
8197 tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
8198 bool inscan_var_store
8199 = lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)) != NULL;
8200
8201 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
8202 {
8203 use_operand_p use_p;
8204 imm_use_iterator iter;
8205 FOR_EACH_IMM_USE_FAST (use_p, iter, rhs)
8206 {
8207 gimple *use_stmt = USE_STMT (use_p);
8208 if (use_stmt == stmt || is_gimple_debug (use_stmt))
8209 continue;
8210 rhs = gimple_assign_lhs (use_stmt);
8211 break;
8212 }
8213 }
8214
8215 gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
8216 enum tree_code code = gimple_assign_rhs_code (def_stmt);
8217 if (code == POINTER_PLUS_EXPR)
8218 code = PLUS_EXPR;
8219 gcc_assert (TREE_CODE_LENGTH (code) == binary_op
8220 && commutative_tree_code (code));
8221 tree rhs1 = gimple_assign_rhs1 (def_stmt);
8222 tree rhs2 = gimple_assign_rhs2 (def_stmt);
8223 gcc_assert (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME);
8224 gimple *load1_stmt = SSA_NAME_DEF_STMT (rhs1);
8225 gimple *load2_stmt = SSA_NAME_DEF_STMT (rhs2);
8226 stmt_vec_info load1_stmt_info = loop_vinfo->lookup_stmt (load1_stmt);
8227 stmt_vec_info load2_stmt_info = loop_vinfo->lookup_stmt (load2_stmt);
8228 dr_vec_info *load1_dr_info = STMT_VINFO_DR_INFO (load1_stmt_info);
8229 dr_vec_info *load2_dr_info = STMT_VINFO_DR_INFO (load2_stmt_info);
8230 tree var1 = TREE_OPERAND (DR_BASE_ADDRESS (load1_dr_info->dr), 0);
8231 tree var2 = TREE_OPERAND (DR_BASE_ADDRESS (load2_dr_info->dr), 0);
8232
8233 if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var1)))
8234 {
8235 std::swap (rhs1, rhs2);
8236 std::swap (var1, var2);
8237 std::swap (load1_dr_info, load2_dr_info);
8238 }
8239
8240 tree *init = loop_vinfo->scan_map->get (var1);
8241 gcc_assert (init);
8242
8243 unsigned HOST_WIDE_INT nunits;
8244 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits))
8245 gcc_unreachable ();
8246 auto_vec<enum scan_store_kind, 16> use_whole_vector;
8247 int units_log2 = scan_store_can_perm_p (vectype, *init, &use_whole_vector);
8248 gcc_assert (units_log2 > 0);
8249 auto_vec<tree, 16> perms;
8250 perms.quick_grow (units_log2 + 1);
8251 tree zero_vec = NULL_TREE, masktype = NULL_TREE;
8252 for (int i = 0; i <= units_log2; ++i)
8253 {
8254 unsigned HOST_WIDE_INT j, k;
8255 vec_perm_builder sel (nunits, nunits, 1);
8256 sel.quick_grow (nunits);
8257 if (i == units_log2)
8258 for (j = 0; j < nunits; ++j)
8259 sel[j] = nunits - 1;
8260 else
8261 {
8262 for (j = 0; j < (HOST_WIDE_INT_1U << i); ++j)
8263 sel[j] = j;
8264 for (k = 0; j < nunits; ++j, ++k)
8265 sel[j] = nunits + k;
8266 }
8267 vec_perm_indices indices (sel, i == units_log2 ? 1 : 2, nunits);
8268 if (!use_whole_vector.is_empty ()
8269 && use_whole_vector[i] != scan_store_kind_perm)
8270 {
8271 if (zero_vec == NULL_TREE)
8272 zero_vec = build_zero_cst (vectype);
8273 if (masktype == NULL_TREE
8274 && use_whole_vector[i] == scan_store_kind_lshift_cond)
8275 masktype = truth_type_for (vectype);
8276 perms[i] = vect_gen_perm_mask_any (vectype, indices);
8277 }
8278 else
8279 perms[i] = vect_gen_perm_mask_checked (vectype, indices);
8280 }
8281
8282 tree vec_oprnd1 = NULL_TREE;
8283 tree vec_oprnd2 = NULL_TREE;
8284 tree vec_oprnd3 = NULL_TREE;
8285 tree dataref_ptr = DR_BASE_ADDRESS (dr_info->dr);
8286 tree dataref_offset = build_int_cst (ref_type, 0);
8287 tree bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info,
8288 vectype, VMAT_CONTIGUOUS);
8289 tree ldataref_ptr = NULL_TREE;
8290 tree orig = NULL_TREE;
8291 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4 && !inscan_var_store)
8292 ldataref_ptr = DR_BASE_ADDRESS (load1_dr_info->dr);
8293 /* The initialization is invariant. */
8294 vec_oprnd1 = vect_init_vector (vinfo, stmt_info, *init, vectype, NULL);
8295 auto_vec<tree> vec_oprnds2;
8296 auto_vec<tree> vec_oprnds3;
8297 if (ldataref_ptr == NULL)
8298 {
8299 /* We want to lookup the vector operands of the reduction, not those
8300 of the store - for SLP we have to use the proper SLP node for the
8301 lookup, which should be the single child of the scan store. */
8302 vect_get_vec_defs (vinfo, stmt_info, SLP_TREE_CHILDREN (slp_node)[0],
8303 ncopies, rhs1, &vec_oprnds2, rhs2, &vec_oprnds3);
8304 /* ??? For SLP we do not key the def on 'rhs1' or 'rhs2' but get
8305 them in SLP child order. So we have to swap here with logic
8306 similar to above. */
8307 stmt_vec_info load
8308 = SLP_TREE_SCALAR_STMTS (SLP_TREE_CHILDREN
8309 (SLP_TREE_CHILDREN (slp_node)[0])[0])[0];
8310 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (load);
8311 tree var = TREE_OPERAND (DR_BASE_ADDRESS (dr_info->dr), 0);
8312 if (lookup_attribute ("omp simd inscan", DECL_ATTRIBUTES (var)))
8313 for (unsigned i = 0; i < vec_oprnds2.length (); ++i)
8314 std::swap (vec_oprnds2[i], vec_oprnds3[i]);;
8315 }
8316 else
8317 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
8318 rhs2, &vec_oprnds3);
8319 for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8320 {
8321 if (ldataref_ptr == NULL)
8322 vec_oprnd2 = vec_oprnds2[j];
8323 vec_oprnd3 = vec_oprnds3[j];
8324 if (j == 0)
8325 orig = vec_oprnd3;
8326 else if (!inscan_var_store)
8327 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8328
8329 if (ldataref_ptr)
8330 {
8331 vec_oprnd2 = make_ssa_name (vectype);
8332 tree data_ref = fold_build2 (MEM_REF, vectype,
8333 unshare_expr (ldataref_ptr),
8334 dataref_offset);
8335 vect_copy_ref_info (data_ref, DR_REF (load1_dr_info->dr));
8336 gimple *g = gimple_build_assign (vec_oprnd2, data_ref);
8337 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8338 if (! slp_node)
8339 {
8340 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8341 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
8342 }
8343 }
8344
8345 tree v = vec_oprnd2;
8346 for (int i = 0; i < units_log2; ++i)
8347 {
8348 tree new_temp = make_ssa_name (vectype);
8349 gimple *g = gimple_build_assign (new_temp, VEC_PERM_EXPR,
8350 (zero_vec
8351 && (use_whole_vector[i]
8352 != scan_store_kind_perm))
8353 ? zero_vec : vec_oprnd1, v,
8354 perms[i]);
8355 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8356 if (! slp_node)
8357 {
8358 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8359 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
8360 }
8361
8362 if (zero_vec && use_whole_vector[i] == scan_store_kind_lshift_cond)
8363 {
8364 /* Whole vector shift shifted in zero bits, but if *init
8365 is not initializer_zerop, we need to replace those elements
8366 with elements from vec_oprnd1. */
8367 tree_vector_builder vb (masktype, nunits, 1);
8368 for (unsigned HOST_WIDE_INT k = 0; k < nunits; ++k)
8369 vb.quick_push (k < (HOST_WIDE_INT_1U << i)
8370 ? boolean_false_node : boolean_true_node);
8371
8372 tree new_temp2 = make_ssa_name (vectype);
8373 g = gimple_build_assign (new_temp2, VEC_COND_EXPR, vb.build (),
8374 new_temp, vec_oprnd1);
8375 vect_finish_stmt_generation (vinfo, stmt_info,
8376 g, gsi);
8377 if (! slp_node)
8378 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8379 new_temp = new_temp2;
8380 }
8381
8382 /* For exclusive scan, perform the perms[i] permutation once
8383 more. */
8384 if (i == 0
8385 && STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4
8386 && v == vec_oprnd2)
8387 {
8388 v = new_temp;
8389 --i;
8390 continue;
8391 }
8392
8393 tree new_temp2 = make_ssa_name (vectype);
8394 g = gimple_build_assign (new_temp2, code, v, new_temp);
8395 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8396 if (! slp_node)
8397 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8398
8399 v = new_temp2;
8400 }
8401
8402 tree new_temp = make_ssa_name (vectype);
8403 gimple *g = gimple_build_assign (new_temp, code, orig, v);
8404 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8405 if (! slp_node)
8406 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8407
8408 tree last_perm_arg = new_temp;
8409 /* For exclusive scan, new_temp computed above is the exclusive scan
8410 prefix sum. Turn it into inclusive prefix sum for the broadcast
8411 of the last element into orig. */
8412 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) == 4)
8413 {
8414 last_perm_arg = make_ssa_name (vectype);
8415 g = gimple_build_assign (last_perm_arg, code, new_temp, vec_oprnd2);
8416 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8417 if (! slp_node)
8418 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8419 }
8420
8421 orig = make_ssa_name (vectype);
8422 g = gimple_build_assign (orig, VEC_PERM_EXPR, last_perm_arg,
8423 last_perm_arg, perms[units_log2]);
8424 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8425 if (! slp_node)
8426 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8427
8428 if (!inscan_var_store)
8429 {
8430 tree data_ref = fold_build2 (MEM_REF, vectype,
8431 unshare_expr (dataref_ptr),
8432 dataref_offset);
8433 vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8434 g = gimple_build_assign (data_ref, new_temp);
8435 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8436 if (! slp_node)
8437 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8438 }
8439 }
8440
8441 if (inscan_var_store)
8442 for (unsigned j = 0; j < vec_oprnds3.length (); j++)
8443 {
8444 if (j != 0)
8445 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
8446
8447 tree data_ref = fold_build2 (MEM_REF, vectype,
8448 unshare_expr (dataref_ptr),
8449 dataref_offset);
8450 vect_copy_ref_info (data_ref, DR_REF (dr_info->dr));
8451 gimple *g = gimple_build_assign (data_ref, orig);
8452 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
8453 if (! slp_node)
8454 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (g);
8455 }
8456 return true;
8457 }
8458
8459
8460 /* Function vectorizable_store.
8461
8462 Check if STMT_INFO defines a non scalar data-ref (array/pointer/structure)
8463 that can be vectorized.
8464 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
8465 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
8466 Return true if STMT_INFO is vectorizable in this way. */
8467
8468 static bool
8469 vectorizable_store (vec_info *vinfo,
8470 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
8471 gimple **vec_stmt, slp_tree slp_node,
8472 stmt_vector_for_cost *cost_vec)
8473 {
8474 tree data_ref;
8475 tree vec_oprnd = NULL_TREE;
8476 tree elem_type;
8477 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
8478 class loop *loop = NULL;
8479 machine_mode vec_mode;
8480 tree dummy;
8481 enum vect_def_type rhs_dt = vect_unknown_def_type;
8482 enum vect_def_type mask_dt = vect_unknown_def_type;
8483 tree dataref_ptr = NULL_TREE;
8484 tree dataref_offset = NULL_TREE;
8485 gimple *ptr_incr = NULL;
8486 int ncopies;
8487 int j;
8488 stmt_vec_info first_stmt_info;
8489 bool grouped_store;
8490 unsigned int group_size, i;
8491 bool slp = (slp_node != NULL);
8492 unsigned int vec_num;
8493 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
8494 tree aggr_type;
8495 gather_scatter_info gs_info;
8496 poly_uint64 vf;
8497 vec_load_store_type vls_type;
8498 tree ref_type;
8499
8500 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
8501 return false;
8502
8503 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
8504 && ! vec_stmt)
8505 return false;
8506
8507 /* Is vectorizable store? */
8508
8509 tree mask = NULL_TREE, mask_vectype = NULL_TREE;
8510 slp_tree mask_node = NULL;
8511 if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
8512 {
8513 tree scalar_dest = gimple_assign_lhs (assign);
8514 if (TREE_CODE (scalar_dest) == VIEW_CONVERT_EXPR
8515 && is_pattern_stmt_p (stmt_info))
8516 scalar_dest = TREE_OPERAND (scalar_dest, 0);
8517 if (TREE_CODE (scalar_dest) != ARRAY_REF
8518 && TREE_CODE (scalar_dest) != BIT_FIELD_REF
8519 && TREE_CODE (scalar_dest) != INDIRECT_REF
8520 && TREE_CODE (scalar_dest) != COMPONENT_REF
8521 && TREE_CODE (scalar_dest) != IMAGPART_EXPR
8522 && TREE_CODE (scalar_dest) != REALPART_EXPR
8523 && TREE_CODE (scalar_dest) != MEM_REF)
8524 return false;
8525 }
8526 else
8527 {
8528 gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
8529 if (!call || !gimple_call_internal_p (call))
8530 return false;
8531
8532 internal_fn ifn = gimple_call_internal_fn (call);
8533 if (!internal_store_fn_p (ifn))
8534 return false;
8535
8536 int mask_index = internal_fn_mask_index (ifn);
8537 if (mask_index >= 0 && slp_node)
8538 mask_index = vect_slp_child_index_for_operand
8539 (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
8540 if (mask_index >= 0
8541 && !vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_index,
8542 &mask, &mask_node, &mask_dt,
8543 &mask_vectype))
8544 return false;
8545 }
8546
8547 /* Cannot have hybrid store SLP -- that would mean storing to the
8548 same location twice. */
8549 gcc_assert (slp == PURE_SLP_STMT (stmt_info));
8550
8551 tree vectype = STMT_VINFO_VECTYPE (stmt_info), rhs_vectype = NULL_TREE;
8552 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
8553
8554 if (loop_vinfo)
8555 {
8556 loop = LOOP_VINFO_LOOP (loop_vinfo);
8557 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
8558 }
8559 else
8560 vf = 1;
8561
8562 /* Multiple types in SLP are handled by creating the appropriate number of
8563 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
8564 case of SLP. */
8565 if (slp)
8566 ncopies = 1;
8567 else
8568 ncopies = vect_get_num_copies (loop_vinfo, vectype);
8569
8570 gcc_assert (ncopies >= 1);
8571
8572 /* FORNOW. This restriction should be relaxed. */
8573 if (loop
8574 && nested_in_vect_loop_p (loop, stmt_info)
8575 && (ncopies > 1 || (slp && SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) > 1)))
8576 {
8577 if (dump_enabled_p ())
8578 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8579 "multiple types in nested loop.\n");
8580 return false;
8581 }
8582
8583 tree op;
8584 slp_tree op_node;
8585 if (!vect_check_store_rhs (vinfo, stmt_info, slp_node,
8586 &op, &op_node, &rhs_dt, &rhs_vectype, &vls_type))
8587 return false;
8588
8589 elem_type = TREE_TYPE (vectype);
8590 vec_mode = TYPE_MODE (vectype);
8591
8592 if (!STMT_VINFO_DATA_REF (stmt_info))
8593 return false;
8594
8595 vect_memory_access_type memory_access_type;
8596 enum dr_alignment_support alignment_support_scheme;
8597 int misalignment;
8598 poly_int64 poffset;
8599 internal_fn lanes_ifn;
8600 if (!get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask, vls_type,
8601 ncopies, &memory_access_type, &poffset,
8602 &alignment_support_scheme, &misalignment, &gs_info,
8603 &lanes_ifn))
8604 return false;
8605
8606 if (slp_node
8607 && slp_node->ldst_lanes
8608 && memory_access_type != VMAT_LOAD_STORE_LANES)
8609 {
8610 if (dump_enabled_p ())
8611 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8612 "discovered store-lane but cannot use it.\n");
8613 return false;
8614 }
8615
8616 if (mask)
8617 {
8618 if (memory_access_type == VMAT_CONTIGUOUS)
8619 {
8620 if (!VECTOR_MODE_P (vec_mode)
8621 || !can_vec_mask_load_store_p (vec_mode,
8622 TYPE_MODE (mask_vectype), false))
8623 return false;
8624 }
8625 else if (memory_access_type != VMAT_LOAD_STORE_LANES
8626 && (memory_access_type != VMAT_GATHER_SCATTER
8627 || (gs_info.decl && !VECTOR_BOOLEAN_TYPE_P (mask_vectype))))
8628 {
8629 if (dump_enabled_p ())
8630 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8631 "unsupported access type for masked store.\n");
8632 return false;
8633 }
8634 else if (memory_access_type == VMAT_GATHER_SCATTER
8635 && gs_info.ifn == IFN_LAST
8636 && !gs_info.decl)
8637 {
8638 if (dump_enabled_p ())
8639 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8640 "unsupported masked emulated scatter.\n");
8641 return false;
8642 }
8643 }
8644 else
8645 {
8646 /* FORNOW. In some cases can vectorize even if data-type not supported
8647 (e.g. - array initialization with 0). */
8648 if (!can_implement_p (mov_optab, vec_mode))
8649 return false;
8650 }
8651
8652 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
8653 grouped_store = (STMT_VINFO_GROUPED_ACCESS (stmt_info)
8654 && memory_access_type != VMAT_GATHER_SCATTER
8655 && (slp || memory_access_type != VMAT_CONTIGUOUS));
8656 if (grouped_store)
8657 {
8658 first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
8659 first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8660 group_size = DR_GROUP_SIZE (first_stmt_info);
8661 }
8662 else
8663 {
8664 first_stmt_info = stmt_info;
8665 first_dr_info = dr_info;
8666 group_size = vec_num = 1;
8667 }
8668
8669 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) > 1 && !vec_stmt)
8670 {
8671 if (!check_scan_store (vinfo, stmt_info, vectype, rhs_dt, slp_node, mask,
8672 memory_access_type))
8673 return false;
8674 }
8675
8676 bool costing_p = !vec_stmt;
8677 if (costing_p) /* transformation not required. */
8678 {
8679 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
8680 if (slp_node)
8681 SLP_TREE_MEMORY_ACCESS_TYPE (slp_node) = memory_access_type;
8682
8683 if (loop_vinfo
8684 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
8685 check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
8686 vls_type, group_size,
8687 memory_access_type, &gs_info,
8688 mask);
8689
8690 if (slp_node
8691 && (!vect_maybe_update_slp_op_vectype (op_node, vectype)
8692 || (mask
8693 && !vect_maybe_update_slp_op_vectype (mask_node,
8694 mask_vectype))))
8695 {
8696 if (dump_enabled_p ())
8697 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
8698 "incompatible vector types for invariants\n");
8699 return false;
8700 }
8701
8702 if (dump_enabled_p ()
8703 && memory_access_type != VMAT_ELEMENTWISE
8704 && memory_access_type != VMAT_GATHER_SCATTER
8705 && memory_access_type != VMAT_STRIDED_SLP
8706 && memory_access_type != VMAT_INVARIANT
8707 && alignment_support_scheme != dr_aligned)
8708 dump_printf_loc (MSG_NOTE, vect_location,
8709 "Vectorizing an unaligned access.\n");
8710
8711 STMT_VINFO_TYPE (stmt_info) = store_vec_info_type;
8712
8713 /* As function vect_transform_stmt shows, for interleaving stores
8714 the whole chain is vectorized when the last store in the chain
8715 is reached, the other stores in the group are skipped. So we
8716 want to only cost the last one here, but it's not trivial to
8717 get the last, as it's equivalent to use the first one for
8718 costing, use the first one instead. */
8719 if (grouped_store
8720 && !slp
8721 && first_stmt_info != stmt_info)
8722 return true;
8723 }
8724 if (slp_node)
8725 gcc_assert (memory_access_type == SLP_TREE_MEMORY_ACCESS_TYPE (stmt_info));
8726 else
8727 gcc_assert (memory_access_type == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
8728
8729 /* Transform. */
8730
8731 ensure_base_align (dr_info);
8732
8733 if (STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) >= 3)
8734 {
8735 gcc_assert (memory_access_type == VMAT_CONTIGUOUS);
8736 gcc_assert (!slp || SLP_TREE_LANES (slp_node) == 1);
8737 if (costing_p)
8738 {
8739 unsigned int inside_cost = 0, prologue_cost = 0;
8740 if (vls_type == VLS_STORE_INVARIANT)
8741 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
8742 stmt_info, 0, vect_prologue);
8743 vect_get_store_cost (vinfo, stmt_info, slp_node, ncopies,
8744 alignment_support_scheme, misalignment,
8745 &inside_cost, cost_vec);
8746
8747 if (dump_enabled_p ())
8748 dump_printf_loc (MSG_NOTE, vect_location,
8749 "vect_model_store_cost: inside_cost = %d, "
8750 "prologue_cost = %d .\n",
8751 inside_cost, prologue_cost);
8752
8753 return true;
8754 }
8755 return vectorizable_scan_store (vinfo, stmt_info, slp_node,
8756 gsi, vec_stmt, ncopies);
8757 }
8758
8759 if (grouped_store || slp)
8760 {
8761 /* FORNOW */
8762 gcc_assert (!grouped_store
8763 || !loop
8764 || !nested_in_vect_loop_p (loop, stmt_info));
8765
8766 if (slp)
8767 {
8768 grouped_store = false;
8769 /* VEC_NUM is the number of vect stmts to be created for this
8770 group. */
8771 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
8772 first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
8773 gcc_assert (!STMT_VINFO_GROUPED_ACCESS (first_stmt_info)
8774 || (DR_GROUP_FIRST_ELEMENT (first_stmt_info)
8775 == first_stmt_info));
8776 first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
8777 op = vect_get_store_rhs (first_stmt_info);
8778 }
8779 else
8780 /* VEC_NUM is the number of vect stmts to be created for this
8781 group. */
8782 vec_num = group_size;
8783
8784 ref_type = get_group_alias_ptr_type (first_stmt_info);
8785 }
8786 else
8787 ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
8788
8789 if (!costing_p && dump_enabled_p ())
8790 dump_printf_loc (MSG_NOTE, vect_location, "transform store. ncopies = %d\n",
8791 ncopies);
8792
8793 /* Check if we need to update prologue cost for invariant,
8794 and update it accordingly if so. If it's not for
8795 interleaving store, we can just check vls_type; but if
8796 it's for interleaving store, need to check the def_type
8797 of the stored value since the current vls_type is just
8798 for first_stmt_info. */
8799 auto update_prologue_cost = [&](unsigned *prologue_cost, tree store_rhs)
8800 {
8801 gcc_assert (costing_p);
8802 if (slp)
8803 return;
8804 if (grouped_store)
8805 {
8806 gcc_assert (store_rhs);
8807 enum vect_def_type cdt;
8808 gcc_assert (vect_is_simple_use (store_rhs, vinfo, &cdt));
8809 if (cdt != vect_constant_def && cdt != vect_external_def)
8810 return;
8811 }
8812 else if (vls_type != VLS_STORE_INVARIANT)
8813 return;
8814 *prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec, stmt_info,
8815 slp_node, 0, vect_prologue);
8816 };
8817
8818 if (memory_access_type == VMAT_ELEMENTWISE
8819 || memory_access_type == VMAT_STRIDED_SLP)
8820 {
8821 unsigned inside_cost = 0, prologue_cost = 0;
8822 gimple_stmt_iterator incr_gsi;
8823 bool insert_after;
8824 gimple *incr;
8825 tree offvar = NULL_TREE;
8826 tree ivstep;
8827 tree running_off;
8828 tree stride_base, stride_step, alias_off;
8829 tree vec_oprnd = NULL_TREE;
8830 tree dr_offset;
8831 unsigned int g;
8832 /* Checked by get_load_store_type. */
8833 unsigned int const_nunits = nunits.to_constant ();
8834
8835 gcc_assert (!LOOP_VINFO_FULLY_MASKED_P (loop_vinfo));
8836 gcc_assert (!nested_in_vect_loop_p (loop, stmt_info));
8837
8838 dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
8839 stride_base
8840 = fold_build_pointer_plus
8841 (DR_BASE_ADDRESS (first_dr_info->dr),
8842 size_binop (PLUS_EXPR,
8843 convert_to_ptrofftype (dr_offset),
8844 convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
8845 stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
8846
8847 /* For a store with loop-invariant (but other than power-of-2)
8848 stride (i.e. not a grouped access) like so:
8849
8850 for (i = 0; i < n; i += stride)
8851 array[i] = ...;
8852
8853 we generate a new induction variable and new stores from
8854 the components of the (vectorized) rhs:
8855
8856 for (j = 0; ; j += VF*stride)
8857 vectemp = ...;
8858 tmp1 = vectemp[0];
8859 array[j] = tmp1;
8860 tmp2 = vectemp[1];
8861 array[j + stride] = tmp2;
8862 ...
8863 */
8864
8865 unsigned nstores = const_nunits;
8866 unsigned lnel = 1;
8867 tree ltype = elem_type;
8868 tree lvectype = vectype;
8869 if (slp)
8870 {
8871 HOST_WIDE_INT n = gcd (group_size, const_nunits);
8872 if (n == const_nunits)
8873 {
8874 int mis_align = dr_misalignment (first_dr_info, vectype);
8875 dr_alignment_support dr_align
8876 = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
8877 mis_align);
8878 if (dr_align == dr_aligned
8879 || dr_align == dr_unaligned_supported)
8880 {
8881 nstores = 1;
8882 lnel = const_nunits;
8883 ltype = vectype;
8884 lvectype = vectype;
8885 alignment_support_scheme = dr_align;
8886 misalignment = mis_align;
8887 }
8888 }
8889 else if (n > 1)
8890 {
8891 nstores = const_nunits / n;
8892 lnel = n;
8893 ltype = build_vector_type (elem_type, n);
8894 lvectype = vectype;
8895 int mis_align = dr_misalignment (first_dr_info, ltype);
8896 dr_alignment_support dr_align
8897 = vect_supportable_dr_alignment (vinfo, dr_info, ltype,
8898 mis_align);
8899 alignment_support_scheme = dr_align;
8900 misalignment = mis_align;
8901
8902 /* First check if vec_extract optab doesn't support extraction
8903 of vector elts directly. */
8904 scalar_mode elmode = SCALAR_TYPE_MODE (elem_type);
8905 machine_mode vmode;
8906 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
8907 || !related_vector_mode (TYPE_MODE (vectype), elmode,
8908 n).exists (&vmode)
8909 || (convert_optab_handler (vec_extract_optab,
8910 TYPE_MODE (vectype), vmode)
8911 == CODE_FOR_nothing)
8912 || !(dr_align == dr_aligned
8913 || dr_align == dr_unaligned_supported))
8914 {
8915 /* Try to avoid emitting an extract of vector elements
8916 by performing the extracts using an integer type of the
8917 same size, extracting from a vector of those and then
8918 re-interpreting it as the original vector type if
8919 supported. */
8920 unsigned lsize
8921 = n * GET_MODE_BITSIZE (elmode);
8922 unsigned int lnunits = const_nunits / n;
8923 /* If we can't construct such a vector fall back to
8924 element extracts from the original vector type and
8925 element size stores. */
8926 if (int_mode_for_size (lsize, 0).exists (&elmode)
8927 && VECTOR_MODE_P (TYPE_MODE (vectype))
8928 && related_vector_mode (TYPE_MODE (vectype), elmode,
8929 lnunits).exists (&vmode)
8930 && (convert_optab_handler (vec_extract_optab,
8931 vmode, elmode)
8932 != CODE_FOR_nothing))
8933 {
8934 nstores = lnunits;
8935 lnel = n;
8936 ltype = build_nonstandard_integer_type (lsize, 1);
8937 lvectype = build_vector_type (ltype, nstores);
8938 }
8939 /* Else fall back to vector extraction anyway.
8940 Fewer stores are more important than avoiding spilling
8941 of the vector we extract from. Compared to the
8942 construction case in vectorizable_load no store-forwarding
8943 issue exists here for reasonable archs. But only
8944 if the store is supported. */
8945 else if (!(dr_align == dr_aligned
8946 || dr_align == dr_unaligned_supported))
8947 {
8948 nstores = const_nunits;
8949 lnel = 1;
8950 ltype = elem_type;
8951 lvectype = vectype;
8952 }
8953 }
8954 }
8955 unsigned align;
8956 /* ??? We'd want to use
8957 if (alignment_support_scheme == dr_aligned)
8958 align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
8959 since doing that is what we assume we can in the above checks.
8960 But this interferes with groups with gaps where for example
8961 VF == 2 makes the group in the unrolled loop aligned but the
8962 fact that we advance with step between the two subgroups
8963 makes the access to the second unaligned. See PR119586.
8964 We have to anticipate that here or adjust code generation to
8965 avoid the misaligned loads by means of permutations. */
8966 align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
8967 /* Alignment is at most the access size if we do multiple stores. */
8968 if (nstores > 1)
8969 align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
8970 ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
8971 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
8972 }
8973
8974 if (!costing_p)
8975 {
8976 ivstep = stride_step;
8977 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (ivstep), ivstep,
8978 build_int_cst (TREE_TYPE (ivstep), vf));
8979
8980 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
8981
8982 stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
8983 ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
8984 create_iv (stride_base, PLUS_EXPR, ivstep, NULL, loop, &incr_gsi,
8985 insert_after, &offvar, NULL);
8986 incr = gsi_stmt (incr_gsi);
8987
8988 stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
8989 }
8990
8991 alias_off = build_int_cst (ref_type, 0);
8992 stmt_vec_info next_stmt_info = first_stmt_info;
8993 auto_vec<tree> vec_oprnds;
8994 /* For costing some adjacent vector stores, we'd like to cost with
8995 the total number of them once instead of cost each one by one. */
8996 unsigned int n_adjacent_stores = 0;
8997 for (g = 0; g < group_size; g++)
8998 {
8999 running_off = offvar;
9000 if (!costing_p)
9001 {
9002 if (g)
9003 {
9004 tree size = TYPE_SIZE_UNIT (ltype);
9005 tree pos
9006 = fold_build2 (MULT_EXPR, sizetype, size_int (g), size);
9007 tree newoff = copy_ssa_name (running_off, NULL);
9008 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
9009 running_off, pos);
9010 vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
9011 running_off = newoff;
9012 }
9013 }
9014 if (!slp)
9015 op = vect_get_store_rhs (next_stmt_info);
9016 if (!costing_p)
9017 vect_get_vec_defs (vinfo, next_stmt_info, slp_node, ncopies, op,
9018 &vec_oprnds);
9019 else
9020 update_prologue_cost (&prologue_cost, op);
9021 unsigned int group_el = 0;
9022 unsigned HOST_WIDE_INT
9023 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
9024 for (j = 0; j < ncopies; j++)
9025 {
9026 if (!costing_p)
9027 {
9028 vec_oprnd = vec_oprnds[j];
9029 /* Pun the vector to extract from if necessary. */
9030 if (lvectype != vectype)
9031 {
9032 tree tem = make_ssa_name (lvectype);
9033 tree cvt
9034 = build1 (VIEW_CONVERT_EXPR, lvectype, vec_oprnd);
9035 gimple *pun = gimple_build_assign (tem, cvt);
9036 vect_finish_stmt_generation (vinfo, stmt_info, pun, gsi);
9037 vec_oprnd = tem;
9038 }
9039 }
9040 for (i = 0; i < nstores; i++)
9041 {
9042 if (costing_p)
9043 {
9044 n_adjacent_stores++;
9045 continue;
9046 }
9047 tree newref, newoff;
9048 gimple *incr, *assign;
9049 tree size = TYPE_SIZE (ltype);
9050 /* Extract the i'th component. */
9051 tree pos = fold_build2 (MULT_EXPR, bitsizetype,
9052 bitsize_int (i), size);
9053 tree elem = fold_build3 (BIT_FIELD_REF, ltype, vec_oprnd,
9054 size, pos);
9055
9056 elem = force_gimple_operand_gsi (gsi, elem, true,
9057 NULL_TREE, true,
9058 GSI_SAME_STMT);
9059
9060 tree this_off = build_int_cst (TREE_TYPE (alias_off),
9061 group_el * elsz);
9062 newref = build2 (MEM_REF, ltype,
9063 running_off, this_off);
9064 vect_copy_ref_info (newref, DR_REF (first_dr_info->dr));
9065
9066 /* And store it to *running_off. */
9067 assign = gimple_build_assign (newref, elem);
9068 vect_finish_stmt_generation (vinfo, stmt_info, assign, gsi);
9069
9070 group_el += lnel;
9071 if (! slp
9072 || group_el == group_size)
9073 {
9074 newoff = copy_ssa_name (running_off, NULL);
9075 incr = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
9076 running_off, stride_step);
9077 vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
9078
9079 running_off = newoff;
9080 group_el = 0;
9081 }
9082 if (g == group_size - 1
9083 && !slp)
9084 {
9085 if (j == 0 && i == 0)
9086 *vec_stmt = assign;
9087 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (assign);
9088 }
9089 }
9090 }
9091 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
9092 vec_oprnds.truncate(0);
9093 if (slp)
9094 break;
9095 }
9096
9097 if (costing_p)
9098 {
9099 if (n_adjacent_stores > 0)
9100 {
9101 /* Take a single lane vector type store as scalar
9102 store to avoid ICE like 110776. */
9103 if (VECTOR_TYPE_P (ltype)
9104 && maybe_ne (TYPE_VECTOR_SUBPARTS (ltype), 1U))
9105 vect_get_store_cost (vinfo, stmt_info, slp_node,
9106 n_adjacent_stores, alignment_support_scheme,
9107 misalignment, &inside_cost, cost_vec);
9108 else
9109 inside_cost
9110 += record_stmt_cost (cost_vec, n_adjacent_stores,
9111 scalar_store, stmt_info, 0, vect_body);
9112 /* Only need vector extracting when there are more
9113 than one stores. */
9114 if (nstores > 1)
9115 inside_cost
9116 += record_stmt_cost (cost_vec, n_adjacent_stores,
9117 vec_to_scalar, stmt_info, slp_node,
9118 0, vect_body);
9119 }
9120 if (dump_enabled_p ())
9121 dump_printf_loc (MSG_NOTE, vect_location,
9122 "vect_model_store_cost: inside_cost = %d, "
9123 "prologue_cost = %d .\n",
9124 inside_cost, prologue_cost);
9125 }
9126
9127 return true;
9128 }
9129
9130 gcc_assert (alignment_support_scheme);
9131 vec_loop_masks *loop_masks
9132 = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
9133 ? &LOOP_VINFO_MASKS (loop_vinfo)
9134 : NULL);
9135 vec_loop_lens *loop_lens
9136 = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
9137 ? &LOOP_VINFO_LENS (loop_vinfo)
9138 : NULL);
9139
9140 /* The vect_transform_stmt and vect_analyze_stmt will go here but there
9141 are some difference here. We cannot enable both the lens and masks
9142 during transform but it is allowed during analysis.
9143 Shouldn't go with length-based approach if fully masked. */
9144 if (cost_vec == NULL)
9145 /* The cost_vec is NULL during transfrom. */
9146 gcc_assert ((!loop_lens || !loop_masks));
9147
9148 /* Targets with store-lane instructions must not require explicit
9149 realignment. vect_supportable_dr_alignment always returns either
9150 dr_aligned or dr_unaligned_supported for masked operations. */
9151 gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
9152 && !mask
9153 && !loop_masks)
9154 || alignment_support_scheme == dr_aligned
9155 || alignment_support_scheme == dr_unaligned_supported);
9156
9157 tree offset = NULL_TREE;
9158 if (!known_eq (poffset, 0))
9159 offset = size_int (poffset);
9160
9161 tree bump;
9162 tree vec_offset = NULL_TREE;
9163 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9164 {
9165 aggr_type = NULL_TREE;
9166 bump = NULL_TREE;
9167 }
9168 else if (memory_access_type == VMAT_GATHER_SCATTER)
9169 {
9170 aggr_type = elem_type;
9171 if (!costing_p)
9172 vect_get_strided_load_store_ops (stmt_info, loop_vinfo, gsi, &gs_info,
9173 &bump, &vec_offset, loop_lens);
9174 }
9175 else
9176 {
9177 if (memory_access_type == VMAT_LOAD_STORE_LANES)
9178 aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
9179 else
9180 aggr_type = vectype;
9181 if (!costing_p)
9182 bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
9183 memory_access_type, loop_lens);
9184 }
9185
9186 if (loop_vinfo && mask && !costing_p)
9187 LOOP_VINFO_HAS_MASK_STORE (loop_vinfo) = true;
9188
9189 /* In case the vectorization factor (VF) is bigger than the number
9190 of elements that we can fit in a vectype (nunits), we have to generate
9191 more than one vector stmt - i.e - we need to "unroll" the
9192 vector stmt by a factor VF/nunits. */
9193
9194 /* In case of interleaving (non-unit grouped access):
9195
9196 S1: &base + 2 = x2
9197 S2: &base = x0
9198 S3: &base + 1 = x1
9199 S4: &base + 3 = x3
9200
9201 We create vectorized stores starting from base address (the access of the
9202 first stmt in the chain (S2 in the above example), when the last store stmt
9203 of the chain (S4) is reached:
9204
9205 VS1: &base = vx2
9206 VS2: &base + vec_size*1 = vx0
9207 VS3: &base + vec_size*2 = vx1
9208 VS4: &base + vec_size*3 = vx3
9209
9210 Then permutation statements are generated:
9211
9212 VS5: vx5 = VEC_PERM_EXPR < vx0, vx3, {0, 8, 1, 9, 2, 10, 3, 11} >
9213 VS6: vx6 = VEC_PERM_EXPR < vx0, vx3, {4, 12, 5, 13, 6, 14, 7, 15} >
9214 ...
9215
9216 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
9217 (the order of the data-refs in the output of vect_permute_store_chain
9218 corresponds to the order of scalar stmts in the interleaving chain - see
9219 the documentation of vect_permute_store_chain()).
9220
9221 In case of both multiple types and interleaving, above vector stores and
9222 permutation stmts are created for every copy. The result vector stmts are
9223 put in STMT_VINFO_VEC_STMT for the first copy and in the corresponding
9224 STMT_VINFO_RELATED_STMT for the next copies.
9225 */
9226
9227 auto_vec<tree> dr_chain (group_size);
9228 auto_vec<tree> vec_masks;
9229 tree vec_mask = NULL;
9230 auto_delete_vec<auto_vec<tree>> gvec_oprnds (group_size);
9231 for (i = 0; i < group_size; i++)
9232 gvec_oprnds.quick_push (new auto_vec<tree> ());
9233
9234 if (memory_access_type == VMAT_LOAD_STORE_LANES)
9235 {
9236 if (costing_p && slp_node)
9237 /* Update all incoming store operand nodes, the general handling
9238 above only handles the mask and the first store operand node. */
9239 for (slp_tree child : SLP_TREE_CHILDREN (slp_node))
9240 if (child != mask_node
9241 && !vect_maybe_update_slp_op_vectype (child, vectype))
9242 {
9243 if (dump_enabled_p ())
9244 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
9245 "incompatible vector types for invariants\n");
9246 return false;
9247 }
9248 unsigned inside_cost = 0, prologue_cost = 0;
9249 /* For costing some adjacent vector stores, we'd like to cost with
9250 the total number of them once instead of cost each one by one. */
9251 unsigned int n_adjacent_stores = 0;
9252 if (slp)
9253 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) / group_size;
9254 for (j = 0; j < ncopies; j++)
9255 {
9256 gimple *new_stmt;
9257 if (j == 0)
9258 {
9259 /* For interleaved stores we collect vectorized defs for all
9260 the stores in the group in DR_CHAIN. DR_CHAIN is then used
9261 as an input to vect_permute_store_chain(). */
9262 stmt_vec_info next_stmt_info = first_stmt_info;
9263 for (i = 0; i < group_size; i++)
9264 {
9265 /* Since gaps are not supported for interleaved stores,
9266 DR_GROUP_SIZE is the exact number of stmts in the
9267 chain. Therefore, NEXT_STMT_INFO can't be NULL_TREE. */
9268 op = vect_get_store_rhs (next_stmt_info);
9269 if (costing_p)
9270 update_prologue_cost (&prologue_cost, op);
9271 else if (!slp)
9272 {
9273 vect_get_vec_defs_for_operand (vinfo, next_stmt_info,
9274 ncopies, op,
9275 gvec_oprnds[i]);
9276 vec_oprnd = (*gvec_oprnds[i])[0];
9277 dr_chain.quick_push (vec_oprnd);
9278 }
9279 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
9280 }
9281
9282 if (!costing_p)
9283 {
9284 if (mask)
9285 {
9286 if (slp_node)
9287 vect_get_slp_defs (mask_node, &vec_masks);
9288 else
9289 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
9290 mask, &vec_masks,
9291 mask_vectype);
9292 vec_mask = vec_masks[0];
9293 }
9294
9295 dataref_ptr
9296 = vect_create_data_ref_ptr (vinfo, first_stmt_info,
9297 aggr_type, NULL, offset, &dummy,
9298 gsi, &ptr_incr, false, bump);
9299 }
9300 }
9301 else if (!costing_p)
9302 {
9303 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
9304 /* DR_CHAIN is then used as an input to
9305 vect_permute_store_chain(). */
9306 if (!slp)
9307 {
9308 /* We should have caught mismatched types earlier. */
9309 gcc_assert (
9310 useless_type_conversion_p (vectype, TREE_TYPE (vec_oprnd)));
9311 for (i = 0; i < group_size; i++)
9312 {
9313 vec_oprnd = (*gvec_oprnds[i])[j];
9314 dr_chain[i] = vec_oprnd;
9315 }
9316 }
9317 if (mask)
9318 vec_mask = vec_masks[j];
9319 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9320 stmt_info, bump);
9321 }
9322
9323 if (costing_p)
9324 {
9325 n_adjacent_stores += group_size;
9326 continue;
9327 }
9328
9329 /* Get an array into which we can store the individual vectors. */
9330 tree vec_array = create_vector_array (vectype, group_size);
9331
9332 /* Invalidate the current contents of VEC_ARRAY. This should
9333 become an RTL clobber too, which prevents the vector registers
9334 from being upward-exposed. */
9335 vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
9336
9337 /* Store the individual vectors into the array. */
9338 for (i = 0; i < group_size; i++)
9339 {
9340 if (slp)
9341 {
9342 slp_tree child;
9343 if (i == 0 || !mask_node)
9344 child = SLP_TREE_CHILDREN (slp_node)[i];
9345 else
9346 child = SLP_TREE_CHILDREN (slp_node)[i + 1];
9347 vec_oprnd = SLP_TREE_VEC_DEFS (child)[j];
9348 }
9349 else
9350 vec_oprnd = dr_chain[i];
9351 write_vector_array (vinfo, stmt_info, gsi, vec_oprnd, vec_array,
9352 i);
9353 }
9354
9355 tree final_mask = NULL;
9356 tree final_len = NULL;
9357 tree bias = NULL;
9358 if (loop_masks)
9359 final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9360 ncopies, vectype, j);
9361 if (vec_mask)
9362 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9363 vec_mask, gsi);
9364
9365 if (lanes_ifn == IFN_MASK_LEN_STORE_LANES)
9366 {
9367 if (loop_lens)
9368 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
9369 ncopies, vectype, j, 1);
9370 else
9371 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9372 signed char biasval
9373 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9374 bias = build_int_cst (intQI_type_node, biasval);
9375 if (!final_mask)
9376 {
9377 mask_vectype = truth_type_for (vectype);
9378 final_mask = build_minus_one_cst (mask_vectype);
9379 }
9380 }
9381
9382 gcall *call;
9383 if (final_len && final_mask)
9384 {
9385 /* Emit:
9386 MASK_LEN_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
9387 LEN, BIAS, VEC_ARRAY). */
9388 unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
9389 tree alias_ptr = build_int_cst (ref_type, align);
9390 call = gimple_build_call_internal (IFN_MASK_LEN_STORE_LANES, 6,
9391 dataref_ptr, alias_ptr,
9392 final_mask, final_len, bias,
9393 vec_array);
9394 }
9395 else if (final_mask)
9396 {
9397 /* Emit:
9398 MASK_STORE_LANES (DATAREF_PTR, ALIAS_PTR, VEC_MASK,
9399 VEC_ARRAY). */
9400 unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
9401 tree alias_ptr = build_int_cst (ref_type, align);
9402 call = gimple_build_call_internal (IFN_MASK_STORE_LANES, 4,
9403 dataref_ptr, alias_ptr,
9404 final_mask, vec_array);
9405 }
9406 else
9407 {
9408 /* Emit:
9409 MEM_REF[...all elements...] = STORE_LANES (VEC_ARRAY). */
9410 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
9411 call = gimple_build_call_internal (IFN_STORE_LANES, 1, vec_array);
9412 gimple_call_set_lhs (call, data_ref);
9413 }
9414 gimple_call_set_nothrow (call, true);
9415 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9416 new_stmt = call;
9417
9418 /* Record that VEC_ARRAY is now dead. */
9419 vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
9420 if (j == 0 && !slp)
9421 *vec_stmt = new_stmt;
9422 if (!slp)
9423 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
9424 }
9425
9426 if (costing_p)
9427 {
9428 if (n_adjacent_stores > 0)
9429 vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
9430 alignment_support_scheme, misalignment,
9431 &inside_cost, cost_vec);
9432 if (dump_enabled_p ())
9433 dump_printf_loc (MSG_NOTE, vect_location,
9434 "vect_model_store_cost: inside_cost = %d, "
9435 "prologue_cost = %d .\n",
9436 inside_cost, prologue_cost);
9437 }
9438
9439 return true;
9440 }
9441
9442 if (memory_access_type == VMAT_GATHER_SCATTER)
9443 {
9444 gcc_assert (!grouped_store);
9445 auto_vec<tree> vec_offsets;
9446 unsigned int inside_cost = 0, prologue_cost = 0;
9447 int num_stmts = ncopies * vec_num;
9448 for (j = 0; j < num_stmts; j++)
9449 {
9450 gimple *new_stmt;
9451 if (j == 0)
9452 {
9453 if (costing_p && vls_type == VLS_STORE_INVARIANT)
9454 prologue_cost += record_stmt_cost (cost_vec, 1, scalar_to_vec,
9455 stmt_info, slp_node, 0,
9456 vect_prologue);
9457 else if (!costing_p)
9458 {
9459 /* Since the store is not grouped, DR_GROUP_SIZE is 1, and
9460 DR_CHAIN is of size 1. */
9461 gcc_assert (group_size == 1);
9462 if (slp_node)
9463 vect_get_slp_defs (op_node, gvec_oprnds[0]);
9464 else
9465 vect_get_vec_defs_for_operand (vinfo, first_stmt_info,
9466 num_stmts, op, gvec_oprnds[0]);
9467 if (mask)
9468 {
9469 if (slp_node)
9470 vect_get_slp_defs (mask_node, &vec_masks);
9471 else
9472 vect_get_vec_defs_for_operand (vinfo, stmt_info,
9473 num_stmts,
9474 mask, &vec_masks,
9475 mask_vectype);
9476 }
9477
9478 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9479 vect_get_gather_scatter_ops (loop_vinfo, loop, stmt_info,
9480 slp_node, &gs_info,
9481 &dataref_ptr, &vec_offsets);
9482 else
9483 dataref_ptr
9484 = vect_create_data_ref_ptr (vinfo, first_stmt_info,
9485 aggr_type, NULL, offset,
9486 &dummy, gsi, &ptr_incr, false,
9487 bump);
9488 }
9489 }
9490 else if (!costing_p)
9491 {
9492 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
9493 if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9494 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
9495 gsi, stmt_info, bump);
9496 }
9497
9498 new_stmt = NULL;
9499 if (!costing_p)
9500 {
9501 vec_oprnd = (*gvec_oprnds[0])[j];
9502 if (mask)
9503 vec_mask = vec_masks[j];
9504 /* We should have catched mismatched types earlier. */
9505 gcc_assert (useless_type_conversion_p (vectype,
9506 TREE_TYPE (vec_oprnd)));
9507 }
9508 unsigned HOST_WIDE_INT align;
9509 tree final_mask = NULL_TREE;
9510 tree final_len = NULL_TREE;
9511 tree bias = NULL_TREE;
9512 if (!costing_p)
9513 {
9514 if (loop_masks)
9515 final_mask = vect_get_loop_mask (loop_vinfo, gsi,
9516 loop_masks, num_stmts,
9517 vectype, j);
9518 if (vec_mask)
9519 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
9520 final_mask, vec_mask, gsi);
9521 }
9522
9523 if (gs_info.ifn != IFN_LAST)
9524 {
9525 if (costing_p)
9526 {
9527 unsigned int cnunits = vect_nunits_for_cost (vectype);
9528 inside_cost
9529 += record_stmt_cost (cost_vec, cnunits, scalar_store,
9530 stmt_info, slp_node, 0,
9531 vect_body);
9532 continue;
9533 }
9534
9535 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
9536 vec_offset = vec_offsets[j];
9537
9538 tree scale = size_int (gs_info.scale);
9539
9540 if (gs_info.ifn == IFN_MASK_LEN_SCATTER_STORE)
9541 {
9542 if (loop_lens)
9543 final_len = vect_get_loop_len (loop_vinfo, gsi,
9544 loop_lens, num_stmts,
9545 vectype, j, 1);
9546 else
9547 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
9548
9549 signed char biasval
9550 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
9551 bias = build_int_cst (intQI_type_node, biasval);
9552 if (!final_mask)
9553 {
9554 mask_vectype = truth_type_for (vectype);
9555 final_mask = build_minus_one_cst (mask_vectype);
9556 }
9557 }
9558
9559 gcall *call;
9560 if (final_len && final_mask)
9561 {
9562 if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
9563 call = gimple_build_call_internal (
9564 IFN_MASK_LEN_SCATTER_STORE, 7, dataref_ptr,
9565 vec_offset, scale, vec_oprnd, final_mask, final_len,
9566 bias);
9567 else
9568 /* Non-vector offset indicates that prefer to take
9569 MASK_LEN_STRIDED_STORE instead of the
9570 IFN_MASK_SCATTER_STORE with direct stride arg. */
9571 call = gimple_build_call_internal (
9572 IFN_MASK_LEN_STRIDED_STORE, 6, dataref_ptr,
9573 vec_offset, vec_oprnd, final_mask, final_len, bias);
9574 }
9575 else if (final_mask)
9576 call = gimple_build_call_internal
9577 (IFN_MASK_SCATTER_STORE, 5, dataref_ptr,
9578 vec_offset, scale, vec_oprnd, final_mask);
9579 else
9580 call = gimple_build_call_internal (IFN_SCATTER_STORE, 4,
9581 dataref_ptr, vec_offset,
9582 scale, vec_oprnd);
9583 gimple_call_set_nothrow (call, true);
9584 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
9585 new_stmt = call;
9586 }
9587 else if (gs_info.decl)
9588 {
9589 /* The builtin decls path for scatter is legacy, x86 only. */
9590 gcc_assert (nunits.is_constant ()
9591 && (!final_mask
9592 || SCALAR_INT_MODE_P
9593 (TYPE_MODE (TREE_TYPE (final_mask)))));
9594 if (costing_p)
9595 {
9596 unsigned int cnunits = vect_nunits_for_cost (vectype);
9597 inside_cost
9598 += record_stmt_cost (cost_vec, cnunits, scalar_store,
9599 stmt_info, slp_node, 0, vect_body);
9600 continue;
9601 }
9602
9603 poly_uint64 offset_nunits
9604 = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
9605 if (known_eq (nunits, offset_nunits))
9606 {
9607 new_stmt = vect_build_one_scatter_store_call
9608 (vinfo, stmt_info, gsi, &gs_info,
9609 dataref_ptr, vec_offsets[j],
9610 vec_oprnd, final_mask);
9611 vect_finish_stmt_generation (vinfo, stmt_info,
9612 new_stmt, gsi);
9613 }
9614 else if (known_eq (nunits, offset_nunits * 2))
9615 {
9616 /* We have a offset vector with half the number of
9617 lanes but the builtins will store full vectype
9618 data from the lower lanes. */
9619 new_stmt = vect_build_one_scatter_store_call
9620 (vinfo, stmt_info, gsi, &gs_info,
9621 dataref_ptr, vec_offsets[2 * j],
9622 vec_oprnd, final_mask);
9623 vect_finish_stmt_generation (vinfo, stmt_info,
9624 new_stmt, gsi);
9625 int count = nunits.to_constant ();
9626 vec_perm_builder sel (count, count, 1);
9627 sel.quick_grow (count);
9628 for (int i = 0; i < count; ++i)
9629 sel[i] = i | (count / 2);
9630 vec_perm_indices indices (sel, 2, count);
9631 tree perm_mask
9632 = vect_gen_perm_mask_checked (vectype, indices);
9633 new_stmt = gimple_build_assign (NULL_TREE, VEC_PERM_EXPR,
9634 vec_oprnd, vec_oprnd,
9635 perm_mask);
9636 vec_oprnd = make_ssa_name (vectype);
9637 gimple_set_lhs (new_stmt, vec_oprnd);
9638 vect_finish_stmt_generation (vinfo, stmt_info,
9639 new_stmt, gsi);
9640 if (final_mask)
9641 {
9642 new_stmt = gimple_build_assign (NULL_TREE,
9643 VEC_UNPACK_HI_EXPR,
9644 final_mask);
9645 final_mask = make_ssa_name
9646 (truth_type_for (gs_info.offset_vectype));
9647 gimple_set_lhs (new_stmt, final_mask);
9648 vect_finish_stmt_generation (vinfo, stmt_info,
9649 new_stmt, gsi);
9650 }
9651
9652 new_stmt = vect_build_one_scatter_store_call
9653 (vinfo, stmt_info, gsi, &gs_info,
9654 dataref_ptr, vec_offsets[2 * j + 1],
9655 vec_oprnd, final_mask);
9656 vect_finish_stmt_generation (vinfo, stmt_info,
9657 new_stmt, gsi);
9658 }
9659 else if (known_eq (nunits * 2, offset_nunits))
9660 {
9661 /* We have a offset vector with double the number of
9662 lanes. Select the low/high part accordingly. */
9663 vec_offset = vec_offsets[j / 2];
9664 if (j & 1)
9665 {
9666 int count = offset_nunits.to_constant ();
9667 vec_perm_builder sel (count, count, 1);
9668 sel.quick_grow (count);
9669 for (int i = 0; i < count; ++i)
9670 sel[i] = i | (count / 2);
9671 vec_perm_indices indices (sel, 2, count);
9672 tree perm_mask = vect_gen_perm_mask_checked
9673 (TREE_TYPE (vec_offset), indices);
9674 new_stmt = gimple_build_assign (NULL_TREE,
9675 VEC_PERM_EXPR,
9676 vec_offset,
9677 vec_offset,
9678 perm_mask);
9679 vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
9680 gimple_set_lhs (new_stmt, vec_offset);
9681 vect_finish_stmt_generation (vinfo, stmt_info,
9682 new_stmt, gsi);
9683 }
9684
9685 new_stmt = vect_build_one_scatter_store_call
9686 (vinfo, stmt_info, gsi, &gs_info,
9687 dataref_ptr, vec_offset,
9688 vec_oprnd, final_mask);
9689 vect_finish_stmt_generation (vinfo, stmt_info,
9690 new_stmt, gsi);
9691 }
9692 else
9693 gcc_unreachable ();
9694 }
9695 else
9696 {
9697 /* Emulated scatter. */
9698 gcc_assert (!final_mask);
9699 if (costing_p)
9700 {
9701 unsigned int cnunits = vect_nunits_for_cost (vectype);
9702 /* For emulated scatter N offset vector element extracts
9703 (we assume the scalar scaling and ptr + offset add is
9704 consumed by the load). */
9705 inside_cost
9706 += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9707 stmt_info, slp_node, 0, vect_body);
9708 /* N scalar stores plus extracting the elements. */
9709 inside_cost
9710 += record_stmt_cost (cost_vec, cnunits, vec_to_scalar,
9711 stmt_info, slp_node, 0, vect_body);
9712 inside_cost
9713 += record_stmt_cost (cost_vec, cnunits, scalar_store,
9714 stmt_info, slp_node, 0, vect_body);
9715 continue;
9716 }
9717
9718 unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
9719 unsigned HOST_WIDE_INT const_offset_nunits
9720 = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype).to_constant ();
9721 vec<constructor_elt, va_gc> *ctor_elts;
9722 vec_alloc (ctor_elts, const_nunits);
9723 gimple_seq stmts = NULL;
9724 tree elt_type = TREE_TYPE (vectype);
9725 unsigned HOST_WIDE_INT elt_size
9726 = tree_to_uhwi (TYPE_SIZE (elt_type));
9727 /* We support offset vectors with more elements
9728 than the data vector for now. */
9729 unsigned HOST_WIDE_INT factor
9730 = const_offset_nunits / const_nunits;
9731 vec_offset = vec_offsets[j / factor];
9732 unsigned elt_offset
9733 = (j % factor) * const_nunits;
9734 tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
9735 tree scale = size_int (gs_info.scale);
9736 align = get_object_alignment (DR_REF (first_dr_info->dr));
9737 tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
9738 for (unsigned k = 0; k < const_nunits; ++k)
9739 {
9740 /* Compute the offsetted pointer. */
9741 tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
9742 bitsize_int (k + elt_offset));
9743 tree idx
9744 = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
9745 vec_offset, TYPE_SIZE (idx_type), boff);
9746 idx = gimple_convert (&stmts, sizetype, idx);
9747 idx = gimple_build (&stmts, MULT_EXPR, sizetype,
9748 idx, scale);
9749 tree ptr
9750 = gimple_build (&stmts, PLUS_EXPR,
9751 TREE_TYPE (dataref_ptr),
9752 dataref_ptr, idx);
9753 ptr = gimple_convert (&stmts, ptr_type_node, ptr);
9754 /* Extract the element to be stored. */
9755 tree elt
9756 = gimple_build (&stmts, BIT_FIELD_REF,
9757 TREE_TYPE (vectype),
9758 vec_oprnd, TYPE_SIZE (elt_type),
9759 bitsize_int (k * elt_size));
9760 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
9761 stmts = NULL;
9762 tree ref
9763 = build2 (MEM_REF, ltype, ptr,
9764 build_int_cst (ref_type, 0));
9765 new_stmt = gimple_build_assign (ref, elt);
9766 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
9767 }
9768
9769 if (slp)
9770 slp_node->push_vec_def (new_stmt);
9771 }
9772
9773 if (!slp && !costing_p)
9774 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
9775 }
9776
9777 if (!slp && !costing_p)
9778 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
9779
9780 if (costing_p && dump_enabled_p ())
9781 dump_printf_loc (MSG_NOTE, vect_location,
9782 "vect_model_store_cost: inside_cost = %d, "
9783 "prologue_cost = %d .\n",
9784 inside_cost, prologue_cost);
9785
9786 return true;
9787 }
9788
9789 gcc_assert (memory_access_type == VMAT_CONTIGUOUS
9790 || memory_access_type == VMAT_CONTIGUOUS_DOWN
9791 || memory_access_type == VMAT_CONTIGUOUS_PERMUTE
9792 || memory_access_type == VMAT_CONTIGUOUS_REVERSE);
9793
9794 unsigned inside_cost = 0, prologue_cost = 0;
9795 /* For costing some adjacent vector stores, we'd like to cost with
9796 the total number of them once instead of cost each one by one. */
9797 unsigned int n_adjacent_stores = 0;
9798 auto_vec<tree> result_chain (group_size);
9799 auto_vec<tree, 1> vec_oprnds;
9800 for (j = 0; j < ncopies; j++)
9801 {
9802 gimple *new_stmt;
9803 if (j == 0)
9804 {
9805 if (slp && !costing_p)
9806 {
9807 /* Get vectorized arguments for SLP_NODE. */
9808 vect_get_vec_defs (vinfo, stmt_info, slp_node, 1, op,
9809 &vec_oprnds, mask, &vec_masks);
9810 vec_oprnd = vec_oprnds[0];
9811 if (mask)
9812 vec_mask = vec_masks[0];
9813 }
9814 else
9815 {
9816 /* For interleaved stores we collect vectorized defs for all the
9817 stores in the group in DR_CHAIN. DR_CHAIN is then used as an
9818 input to vect_permute_store_chain().
9819
9820 If the store is not grouped, DR_GROUP_SIZE is 1, and DR_CHAIN
9821 is of size 1. */
9822 stmt_vec_info next_stmt_info = first_stmt_info;
9823 for (i = 0; i < group_size; i++)
9824 {
9825 /* Since gaps are not supported for interleaved stores,
9826 DR_GROUP_SIZE is the exact number of stmts in the chain.
9827 Therefore, NEXT_STMT_INFO can't be NULL_TREE. In case
9828 that there is no interleaving, DR_GROUP_SIZE is 1,
9829 and only one iteration of the loop will be executed. */
9830 op = vect_get_store_rhs (next_stmt_info);
9831 if (costing_p)
9832 update_prologue_cost (&prologue_cost, op);
9833 else
9834 {
9835 vect_get_vec_defs_for_operand (vinfo, next_stmt_info,
9836 ncopies, op,
9837 gvec_oprnds[i]);
9838 vec_oprnd = (*gvec_oprnds[i])[0];
9839 dr_chain.quick_push (vec_oprnd);
9840 }
9841 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
9842 }
9843 if (mask && !costing_p)
9844 {
9845 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies,
9846 mask, &vec_masks,
9847 mask_vectype);
9848 vec_mask = vec_masks[0];
9849 }
9850 }
9851
9852 /* We should have catched mismatched types earlier. */
9853 gcc_assert (costing_p
9854 || useless_type_conversion_p (vectype,
9855 TREE_TYPE (vec_oprnd)));
9856 bool simd_lane_access_p
9857 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
9858 if (!costing_p
9859 && simd_lane_access_p
9860 && !loop_masks
9861 && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
9862 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
9863 && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
9864 && integer_zerop (DR_INIT (first_dr_info->dr))
9865 && alias_sets_conflict_p (get_alias_set (aggr_type),
9866 get_alias_set (TREE_TYPE (ref_type))))
9867 {
9868 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
9869 dataref_offset = build_int_cst (ref_type, 0);
9870 }
9871 else if (!costing_p)
9872 dataref_ptr
9873 = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
9874 simd_lane_access_p ? loop : NULL,
9875 offset, &dummy, gsi, &ptr_incr,
9876 simd_lane_access_p, bump);
9877 }
9878 else if (!costing_p)
9879 {
9880 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
9881 /* DR_CHAIN is then used as an input to vect_permute_store_chain().
9882 If the store is not grouped, DR_GROUP_SIZE is 1, and DR_CHAIN is
9883 of size 1. */
9884 for (i = 0; i < group_size; i++)
9885 {
9886 vec_oprnd = (*gvec_oprnds[i])[j];
9887 dr_chain[i] = vec_oprnd;
9888 }
9889 if (mask)
9890 vec_mask = vec_masks[j];
9891 if (dataref_offset)
9892 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset, bump);
9893 else
9894 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9895 stmt_info, bump);
9896 }
9897
9898 new_stmt = NULL;
9899 if (grouped_store)
9900 {
9901 /* Permute. */
9902 gcc_assert (memory_access_type == VMAT_CONTIGUOUS_PERMUTE);
9903 if (costing_p)
9904 {
9905 int group_size = DR_GROUP_SIZE (first_stmt_info);
9906 int nstmts = ceil_log2 (group_size) * group_size;
9907 inside_cost += record_stmt_cost (cost_vec, nstmts, vec_perm,
9908 stmt_info, slp_node, 0,
9909 vect_body);
9910 if (dump_enabled_p ())
9911 dump_printf_loc (MSG_NOTE, vect_location,
9912 "vect_model_store_cost: "
9913 "strided group_size = %d .\n",
9914 group_size);
9915 }
9916 else
9917 vect_permute_store_chain (vinfo, dr_chain, group_size, stmt_info,
9918 gsi, &result_chain);
9919 }
9920
9921 stmt_vec_info next_stmt_info = first_stmt_info;
9922 for (i = 0; i < vec_num; i++)
9923 {
9924 if (!costing_p)
9925 {
9926 if (slp)
9927 vec_oprnd = vec_oprnds[i];
9928 else if (grouped_store)
9929 /* For grouped stores vectorized defs are interleaved in
9930 vect_permute_store_chain(). */
9931 vec_oprnd = result_chain[i];
9932 }
9933
9934 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
9935 {
9936 if (costing_p)
9937 inside_cost += record_stmt_cost (cost_vec, 1, vec_perm,
9938 stmt_info, slp_node, 0,
9939 vect_body);
9940 else
9941 {
9942 tree perm_mask = perm_mask_for_reverse (vectype);
9943 tree perm_dest = vect_create_destination_var (
9944 vect_get_store_rhs (stmt_info), vectype);
9945 tree new_temp = make_ssa_name (perm_dest);
9946
9947 /* Generate the permute statement. */
9948 gimple *perm_stmt
9949 = gimple_build_assign (new_temp, VEC_PERM_EXPR, vec_oprnd,
9950 vec_oprnd, perm_mask);
9951 vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt,
9952 gsi);
9953
9954 perm_stmt = SSA_NAME_DEF_STMT (new_temp);
9955 vec_oprnd = new_temp;
9956 }
9957 }
9958
9959 if (costing_p)
9960 {
9961 n_adjacent_stores++;
9962
9963 if (!slp)
9964 {
9965 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
9966 if (!next_stmt_info)
9967 break;
9968 }
9969
9970 continue;
9971 }
9972
9973 tree final_mask = NULL_TREE;
9974 tree final_len = NULL_TREE;
9975 tree bias = NULL_TREE;
9976 if (loop_masks)
9977 final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
9978 vec_num * ncopies, vectype,
9979 vec_num * j + i);
9980 if (slp && vec_mask)
9981 vec_mask = vec_masks[i];
9982 if (vec_mask)
9983 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
9984 vec_mask, gsi);
9985
9986 if (i > 0)
9987 /* Bump the vector pointer. */
9988 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
9989 stmt_info, bump);
9990
9991 unsigned misalign;
9992 unsigned HOST_WIDE_INT align;
9993 align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
9994 if (alignment_support_scheme == dr_aligned)
9995 misalign = 0;
9996 else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
9997 {
9998 align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
9999 misalign = 0;
10000 }
10001 else
10002 misalign = misalignment;
10003 if (dataref_offset == NULL_TREE
10004 && TREE_CODE (dataref_ptr) == SSA_NAME)
10005 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
10006 misalign);
10007 align = least_bit_hwi (misalign | align);
10008
10009 /* Compute IFN when LOOP_LENS or final_mask valid. */
10010 machine_mode vmode = TYPE_MODE (vectype);
10011 machine_mode new_vmode = vmode;
10012 internal_fn partial_ifn = IFN_LAST;
10013 if (loop_lens)
10014 {
10015 opt_machine_mode new_ovmode
10016 = get_len_load_store_mode (vmode, false, &partial_ifn);
10017 new_vmode = new_ovmode.require ();
10018 unsigned factor
10019 = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
10020 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
10021 vec_num * ncopies, vectype,
10022 vec_num * j + i, factor);
10023 }
10024 else if (final_mask)
10025 {
10026 if (!can_vec_mask_load_store_p (
10027 vmode, TYPE_MODE (TREE_TYPE (final_mask)), false,
10028 &partial_ifn))
10029 gcc_unreachable ();
10030 }
10031
10032 if (partial_ifn == IFN_MASK_LEN_STORE)
10033 {
10034 if (!final_len)
10035 {
10036 /* Pass VF value to 'len' argument of
10037 MASK_LEN_STORE if LOOP_LENS is invalid. */
10038 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
10039 }
10040 if (!final_mask)
10041 {
10042 /* Pass all ones value to 'mask' argument of
10043 MASK_LEN_STORE if final_mask is invalid. */
10044 mask_vectype = truth_type_for (vectype);
10045 final_mask = build_minus_one_cst (mask_vectype);
10046 }
10047 }
10048 if (final_len)
10049 {
10050 signed char biasval
10051 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
10052
10053 bias = build_int_cst (intQI_type_node, biasval);
10054 }
10055
10056 /* Arguments are ready. Create the new vector stmt. */
10057 if (final_len)
10058 {
10059 gcall *call;
10060 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
10061 /* Need conversion if it's wrapped with VnQI. */
10062 if (vmode != new_vmode)
10063 {
10064 tree new_vtype
10065 = build_vector_type_for_mode (unsigned_intQI_type_node,
10066 new_vmode);
10067 tree var = vect_get_new_ssa_name (new_vtype, vect_simple_var);
10068 vec_oprnd = build1 (VIEW_CONVERT_EXPR, new_vtype, vec_oprnd);
10069 gassign *new_stmt
10070 = gimple_build_assign (var, VIEW_CONVERT_EXPR, vec_oprnd);
10071 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10072 vec_oprnd = var;
10073 }
10074
10075 if (partial_ifn == IFN_MASK_LEN_STORE)
10076 call = gimple_build_call_internal (IFN_MASK_LEN_STORE, 6,
10077 dataref_ptr, ptr, final_mask,
10078 final_len, bias, vec_oprnd);
10079 else
10080 call = gimple_build_call_internal (IFN_LEN_STORE, 5,
10081 dataref_ptr, ptr, final_len,
10082 bias, vec_oprnd);
10083 gimple_call_set_nothrow (call, true);
10084 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10085 new_stmt = call;
10086 }
10087 else if (final_mask)
10088 {
10089 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
10090 gcall *call
10091 = gimple_build_call_internal (IFN_MASK_STORE, 4, dataref_ptr,
10092 ptr, final_mask, vec_oprnd);
10093 gimple_call_set_nothrow (call, true);
10094 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
10095 new_stmt = call;
10096 }
10097 else
10098 {
10099 data_ref
10100 = fold_build2 (MEM_REF, vectype, dataref_ptr,
10101 dataref_offset ? dataref_offset
10102 : build_int_cst (ref_type, 0));
10103 if (alignment_support_scheme == dr_aligned
10104 && align >= TYPE_ALIGN_UNIT (vectype))
10105 ;
10106 else
10107 TREE_TYPE (data_ref)
10108 = build_aligned_type (TREE_TYPE (data_ref),
10109 align * BITS_PER_UNIT);
10110 vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
10111 new_stmt = gimple_build_assign (data_ref, vec_oprnd);
10112 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
10113 }
10114
10115 if (slp)
10116 continue;
10117
10118 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
10119 if (!next_stmt_info)
10120 break;
10121 }
10122 if (!slp && !costing_p)
10123 {
10124 if (j == 0)
10125 *vec_stmt = new_stmt;
10126 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
10127 }
10128 }
10129
10130 if (costing_p)
10131 {
10132 if (n_adjacent_stores > 0)
10133 vect_get_store_cost (vinfo, stmt_info, slp_node, n_adjacent_stores,
10134 alignment_support_scheme, misalignment,
10135 &inside_cost, cost_vec);
10136
10137 /* When vectorizing a store into the function result assign
10138 a penalty if the function returns in a multi-register location.
10139 In this case we assume we'll end up with having to spill the
10140 vector result and do piecewise loads as a conservative estimate. */
10141 tree base = get_base_address (STMT_VINFO_DATA_REF (stmt_info)->ref);
10142 if (base
10143 && (TREE_CODE (base) == RESULT_DECL
10144 || (DECL_P (base) && cfun_returns (base)))
10145 && !aggregate_value_p (base, cfun->decl))
10146 {
10147 rtx reg = hard_function_value (TREE_TYPE (base), cfun->decl, 0, 1);
10148 /* ??? Handle PARALLEL in some way. */
10149 if (REG_P (reg))
10150 {
10151 int nregs = hard_regno_nregs (REGNO (reg), GET_MODE (reg));
10152 /* Assume that a single reg-reg move is possible and cheap,
10153 do not account for vector to gp register move cost. */
10154 if (nregs > 1)
10155 {
10156 /* Spill. */
10157 prologue_cost
10158 += record_stmt_cost (cost_vec, ncopies, vector_store,
10159 stmt_info, slp_node, 0, vect_epilogue);
10160 /* Loads. */
10161 prologue_cost
10162 += record_stmt_cost (cost_vec, ncopies * nregs, scalar_load,
10163 stmt_info, slp_node, 0, vect_epilogue);
10164 }
10165 }
10166 }
10167 if (dump_enabled_p ())
10168 dump_printf_loc (MSG_NOTE, vect_location,
10169 "vect_model_store_cost: inside_cost = %d, "
10170 "prologue_cost = %d .\n",
10171 inside_cost, prologue_cost);
10172 }
10173
10174 return true;
10175 }
10176
10177 /* Given a vector type VECTYPE, turns permutation SEL into the equivalent
10178 VECTOR_CST mask. No checks are made that the target platform supports the
10179 mask, so callers may wish to test can_vec_perm_const_p separately, or use
10180 vect_gen_perm_mask_checked. */
10181
10182 tree
10183 vect_gen_perm_mask_any (tree vectype, const vec_perm_indices &sel)
10184 {
10185 tree mask_type;
10186
10187 poly_uint64 nunits = sel.length ();
10188 gcc_assert (known_eq (nunits, TYPE_VECTOR_SUBPARTS (vectype)));
10189
10190 mask_type = build_vector_type (ssizetype, nunits);
10191 return vec_perm_indices_to_tree (mask_type, sel);
10192 }
10193
10194 /* Checked version of vect_gen_perm_mask_any. Asserts can_vec_perm_const_p,
10195 i.e. that the target supports the pattern _for arbitrary input vectors_. */
10196
10197 tree
10198 vect_gen_perm_mask_checked (tree vectype, const vec_perm_indices &sel)
10199 {
10200 machine_mode vmode = TYPE_MODE (vectype);
10201 gcc_assert (can_vec_perm_const_p (vmode, vmode, sel));
10202 return vect_gen_perm_mask_any (vectype, sel);
10203 }
10204
10205 /* Given a vector variable X and Y, that was generated for the scalar
10206 STMT_INFO, generate instructions to permute the vector elements of X and Y
10207 using permutation mask MASK_VEC, insert them at *GSI and return the
10208 permuted vector variable. */
10209
10210 static tree
10211 permute_vec_elements (vec_info *vinfo,
10212 tree x, tree y, tree mask_vec, stmt_vec_info stmt_info,
10213 gimple_stmt_iterator *gsi)
10214 {
10215 tree vectype = TREE_TYPE (x);
10216 tree perm_dest, data_ref;
10217 gimple *perm_stmt;
10218
10219 tree scalar_dest = gimple_get_lhs (stmt_info->stmt);
10220 if (scalar_dest && TREE_CODE (scalar_dest) == SSA_NAME)
10221 perm_dest = vect_create_destination_var (scalar_dest, vectype);
10222 else
10223 perm_dest = vect_get_new_vect_var (vectype, vect_simple_var, NULL);
10224 data_ref = make_ssa_name (perm_dest);
10225
10226 /* Generate the permute statement. */
10227 perm_stmt = gimple_build_assign (data_ref, VEC_PERM_EXPR, x, y, mask_vec);
10228 vect_finish_stmt_generation (vinfo, stmt_info, perm_stmt, gsi);
10229
10230 return data_ref;
10231 }
10232
10233 /* Hoist the definitions of all SSA uses on STMT_INFO out of the loop LOOP,
10234 inserting them on the loops preheader edge. Returns true if we
10235 were successful in doing so (and thus STMT_INFO can be moved then),
10236 otherwise returns false. HOIST_P indicates if we want to hoist the
10237 definitions of all SSA uses, it would be false when we are costing. */
10238
10239 static bool
10240 hoist_defs_of_uses (gimple *stmt, class loop *loop, bool hoist_p)
10241 {
10242 ssa_op_iter i;
10243 use_operand_p use_p;
10244 auto_vec<use_operand_p, 8> to_hoist;
10245
10246 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
10247 {
10248 gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
10249 if (!gimple_nop_p (def_stmt)
10250 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
10251 {
10252 /* Make sure we don't need to recurse. While we could do
10253 so in simple cases when there are more complex use webs
10254 we don't have an easy way to preserve stmt order to fulfil
10255 dependencies within them. */
10256 tree op2;
10257 ssa_op_iter i2;
10258 if (gimple_code (def_stmt) == GIMPLE_PHI
10259 || (single_ssa_def_operand (def_stmt, SSA_OP_DEF)
10260 == NULL_DEF_OPERAND_P))
10261 return false;
10262 FOR_EACH_SSA_TREE_OPERAND (op2, def_stmt, i2, SSA_OP_USE)
10263 {
10264 gimple *def_stmt2 = SSA_NAME_DEF_STMT (op2);
10265 if (!gimple_nop_p (def_stmt2)
10266 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt2)))
10267 return false;
10268 }
10269 to_hoist.safe_push (use_p);
10270 }
10271 }
10272
10273 if (to_hoist.is_empty ())
10274 return true;
10275
10276 if (!hoist_p)
10277 return true;
10278
10279 /* Instead of moving defs we copy them so we can zero their UID to not
10280 confuse dominance queries in the preheader. */
10281 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
10282 for (use_operand_p use_p : to_hoist)
10283 {
10284 gimple *def_stmt = SSA_NAME_DEF_STMT (USE_FROM_PTR (use_p));
10285 gimple *copy = gimple_copy (def_stmt);
10286 gimple_set_uid (copy, 0);
10287 def_operand_p def_p = single_ssa_def_operand (def_stmt, SSA_OP_DEF);
10288 tree new_def = duplicate_ssa_name (DEF_FROM_PTR (def_p), copy);
10289 update_stmt (copy);
10290 def_p = single_ssa_def_operand (copy, SSA_OP_DEF);
10291 SET_DEF (def_p, new_def);
10292 SET_USE (use_p, new_def);
10293 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
10294 }
10295
10296 return true;
10297 }
10298
10299 /* vectorizable_load.
10300
10301 Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
10302 that can be vectorized.
10303 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
10304 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
10305 Return true if STMT_INFO is vectorizable in this way. */
10306
10307 static bool
10308 vectorizable_load (vec_info *vinfo,
10309 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
10310 gimple **vec_stmt, slp_tree slp_node,
10311 stmt_vector_for_cost *cost_vec)
10312 {
10313 tree scalar_dest;
10314 tree vec_dest = NULL;
10315 tree data_ref = NULL;
10316 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
10317 class loop *loop = NULL;
10318 class loop *containing_loop = gimple_bb (stmt_info->stmt)->loop_father;
10319 bool nested_in_vect_loop = false;
10320 tree elem_type;
10321 /* Avoid false positive uninitialized warning, see PR110652. */
10322 tree new_temp = NULL_TREE;
10323 machine_mode mode;
10324 tree dummy;
10325 tree dataref_ptr = NULL_TREE;
10326 tree dataref_offset = NULL_TREE;
10327 gimple *ptr_incr = NULL;
10328 int ncopies;
10329 int i, j;
10330 unsigned int group_size;
10331 poly_uint64 group_gap_adj;
10332 tree msq = NULL_TREE, lsq;
10333 tree realignment_token = NULL_TREE;
10334 gphi *phi = NULL;
10335 vec<tree> dr_chain = vNULL;
10336 bool grouped_load = false;
10337 stmt_vec_info first_stmt_info;
10338 stmt_vec_info first_stmt_info_for_drptr = NULL;
10339 bool compute_in_loop = false;
10340 class loop *at_loop;
10341 int vec_num;
10342 bool slp = (slp_node != NULL);
10343 bool slp_perm = false;
10344 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
10345 poly_uint64 vf;
10346 tree aggr_type;
10347 gather_scatter_info gs_info;
10348 tree ref_type;
10349 enum vect_def_type mask_dt = vect_unknown_def_type;
10350 enum vect_def_type els_dt = vect_unknown_def_type;
10351
10352 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
10353 return false;
10354
10355 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def
10356 && ! vec_stmt)
10357 return false;
10358
10359 if (!STMT_VINFO_DATA_REF (stmt_info))
10360 return false;
10361
10362 tree mask = NULL_TREE, mask_vectype = NULL_TREE;
10363 tree els = NULL_TREE; tree els_vectype = NULL_TREE;
10364
10365 int mask_index = -1;
10366 int els_index = -1;
10367 slp_tree slp_op = NULL;
10368 slp_tree els_op = NULL;
10369 if (gassign *assign = dyn_cast <gassign *> (stmt_info->stmt))
10370 {
10371 scalar_dest = gimple_assign_lhs (assign);
10372 if (TREE_CODE (scalar_dest) != SSA_NAME)
10373 return false;
10374
10375 tree_code code = gimple_assign_rhs_code (assign);
10376 if (code != ARRAY_REF
10377 && code != BIT_FIELD_REF
10378 && code != INDIRECT_REF
10379 && code != COMPONENT_REF
10380 && code != IMAGPART_EXPR
10381 && code != REALPART_EXPR
10382 && code != MEM_REF
10383 && TREE_CODE_CLASS (code) != tcc_declaration)
10384 return false;
10385 }
10386 else
10387 {
10388 gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
10389 if (!call || !gimple_call_internal_p (call))
10390 return false;
10391
10392 internal_fn ifn = gimple_call_internal_fn (call);
10393 if (!internal_load_fn_p (ifn))
10394 return false;
10395
10396 scalar_dest = gimple_call_lhs (call);
10397 if (!scalar_dest)
10398 return false;
10399
10400 mask_index = internal_fn_mask_index (ifn);
10401 if (mask_index >= 0 && slp_node)
10402 mask_index = vect_slp_child_index_for_operand
10403 (call, mask_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
10404 if (mask_index >= 0
10405 && !vect_check_scalar_mask (vinfo, stmt_info, slp_node, mask_index,
10406 &mask, &slp_op, &mask_dt, &mask_vectype))
10407 return false;
10408
10409 els_index = internal_fn_else_index (ifn);
10410 if (els_index >= 0 && slp_node)
10411 els_index = vect_slp_child_index_for_operand
10412 (call, els_index, STMT_VINFO_GATHER_SCATTER_P (stmt_info));
10413 if (els_index >= 0
10414 && !vect_is_simple_use (vinfo, stmt_info, slp_node, els_index,
10415 &els, &els_op, &els_dt, &els_vectype))
10416 return false;
10417 }
10418
10419 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
10420 poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
10421
10422 if (loop_vinfo)
10423 {
10424 loop = LOOP_VINFO_LOOP (loop_vinfo);
10425 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt_info);
10426 vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
10427 }
10428 else
10429 vf = 1;
10430
10431 /* Multiple types in SLP are handled by creating the appropriate number of
10432 vectorized stmts for each SLP node. Hence, NCOPIES is always 1 in
10433 case of SLP. */
10434 if (slp)
10435 ncopies = 1;
10436 else
10437 ncopies = vect_get_num_copies (loop_vinfo, vectype);
10438
10439 gcc_assert (ncopies >= 1);
10440
10441 /* FORNOW. This restriction should be relaxed. */
10442 if (nested_in_vect_loop
10443 && (ncopies > 1 || (slp && SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) > 1)))
10444 {
10445 if (dump_enabled_p ())
10446 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10447 "multiple types in nested loop.\n");
10448 return false;
10449 }
10450
10451 /* Invalidate assumptions made by dependence analysis when vectorization
10452 on the unrolled body effectively re-orders stmts. */
10453 if (ncopies > 1
10454 && STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
10455 && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
10456 STMT_VINFO_MIN_NEG_DIST (stmt_info)))
10457 {
10458 if (dump_enabled_p ())
10459 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10460 "cannot perform implicit CSE when unrolling "
10461 "with negative dependence distance\n");
10462 return false;
10463 }
10464
10465 elem_type = TREE_TYPE (vectype);
10466 mode = TYPE_MODE (vectype);
10467
10468 /* FORNOW. In some cases can vectorize even if data-type not supported
10469 (e.g. - data copies). */
10470 if (!can_implement_p (mov_optab, mode))
10471 {
10472 if (dump_enabled_p ())
10473 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10474 "Aligned load, but unsupported type.\n");
10475 return false;
10476 }
10477
10478 /* Check if the load is a part of an interleaving chain. */
10479 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
10480 {
10481 grouped_load = true;
10482 /* FORNOW */
10483 gcc_assert (!nested_in_vect_loop);
10484 gcc_assert (!STMT_VINFO_GATHER_SCATTER_P (stmt_info));
10485
10486 first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10487 group_size = DR_GROUP_SIZE (first_stmt_info);
10488
10489 /* Refuse non-SLP vectorization of SLP-only groups. */
10490 if (!slp && STMT_VINFO_SLP_VECT_ONLY (first_stmt_info))
10491 {
10492 if (dump_enabled_p ())
10493 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10494 "cannot vectorize load in non-SLP mode.\n");
10495 return false;
10496 }
10497
10498 /* Invalidate assumptions made by dependence analysis when vectorization
10499 on the unrolled body effectively re-orders stmts. */
10500 if (STMT_VINFO_MIN_NEG_DIST (stmt_info) != 0
10501 && maybe_gt (LOOP_VINFO_VECT_FACTOR (loop_vinfo),
10502 STMT_VINFO_MIN_NEG_DIST (stmt_info)))
10503 {
10504 if (dump_enabled_p ())
10505 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10506 "cannot perform implicit CSE when performing "
10507 "group loads with negative dependence distance\n");
10508 return false;
10509 }
10510 }
10511 else
10512 group_size = 1;
10513
10514 vect_memory_access_type memory_access_type;
10515 enum dr_alignment_support alignment_support_scheme;
10516 int misalignment;
10517 poly_int64 poffset;
10518 internal_fn lanes_ifn;
10519 auto_vec<int> elsvals;
10520 int maskload_elsval = 0;
10521 bool need_zeroing = false;
10522 if (!get_load_store_type (vinfo, stmt_info, vectype, slp_node, mask, VLS_LOAD,
10523 ncopies, &memory_access_type, &poffset,
10524 &alignment_support_scheme, &misalignment, &gs_info,
10525 &lanes_ifn, &elsvals))
10526 return false;
10527
10528
10529 /* We might need to explicitly zero inactive elements if there are
10530 padding bits in the type that might leak otherwise.
10531 Refer to PR115336. */
10532 tree scalar_type = TREE_TYPE (scalar_dest);
10533 bool type_mode_padding_p
10534 = TYPE_PRECISION (scalar_type) < GET_MODE_PRECISION (GET_MODE_INNER (mode));
10535
10536 /* ??? The following checks should really be part of
10537 get_group_load_store_type. */
10538 if (slp
10539 && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()
10540 && !((memory_access_type == VMAT_ELEMENTWISE
10541 || memory_access_type == VMAT_GATHER_SCATTER)
10542 && SLP_TREE_LANES (slp_node) == 1))
10543 {
10544 slp_perm = true;
10545
10546 if (!loop_vinfo)
10547 {
10548 /* In BB vectorization we may not actually use a loaded vector
10549 accessing elements in excess of DR_GROUP_SIZE. */
10550 stmt_vec_info group_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
10551 group_info = DR_GROUP_FIRST_ELEMENT (group_info);
10552 unsigned HOST_WIDE_INT nunits;
10553 unsigned j, k, maxk = 0;
10554 FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), j, k)
10555 if (k > maxk)
10556 maxk = k;
10557 tree vectype = SLP_TREE_VECTYPE (slp_node);
10558 if (!TYPE_VECTOR_SUBPARTS (vectype).is_constant (&nunits)
10559 || maxk >= (DR_GROUP_SIZE (group_info) & ~(nunits - 1)))
10560 {
10561 if (dump_enabled_p ())
10562 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10563 "BB vectorization with gaps at the end of "
10564 "a load is not supported\n");
10565 return false;
10566 }
10567 }
10568
10569 auto_vec<tree> tem;
10570 unsigned n_perms;
10571 if (!vect_transform_slp_perm_load (vinfo, slp_node, tem, NULL, vf,
10572 true, &n_perms))
10573 {
10574 if (dump_enabled_p ())
10575 dump_printf_loc (MSG_MISSED_OPTIMIZATION,
10576 vect_location,
10577 "unsupported load permutation\n");
10578 return false;
10579 }
10580 }
10581
10582 if (slp_node
10583 && slp_node->ldst_lanes
10584 && memory_access_type != VMAT_LOAD_STORE_LANES)
10585 {
10586 if (dump_enabled_p ())
10587 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10588 "discovered load-lane but cannot use it.\n");
10589 return false;
10590 }
10591
10592 if (mask)
10593 {
10594 if (memory_access_type == VMAT_CONTIGUOUS)
10595 {
10596 machine_mode vec_mode = TYPE_MODE (vectype);
10597 if (!VECTOR_MODE_P (vec_mode)
10598 || !can_vec_mask_load_store_p (vec_mode,
10599 TYPE_MODE (mask_vectype),
10600 true, NULL, &elsvals))
10601 return false;
10602 }
10603 else if (memory_access_type != VMAT_LOAD_STORE_LANES
10604 && memory_access_type != VMAT_GATHER_SCATTER)
10605 {
10606 if (dump_enabled_p ())
10607 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10608 "unsupported access type for masked load.\n");
10609 return false;
10610 }
10611 else if (memory_access_type == VMAT_GATHER_SCATTER
10612 && gs_info.ifn == IFN_LAST
10613 && !gs_info.decl)
10614 {
10615 if (dump_enabled_p ())
10616 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10617 "unsupported masked emulated gather.\n");
10618 return false;
10619 }
10620 else if (memory_access_type == VMAT_ELEMENTWISE
10621 || memory_access_type == VMAT_STRIDED_SLP)
10622 {
10623 if (dump_enabled_p ())
10624 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10625 "unsupported masked strided access.\n");
10626 return false;
10627 }
10628 }
10629
10630 bool costing_p = !vec_stmt;
10631
10632 if (costing_p) /* transformation not required. */
10633 {
10634 if (slp_node
10635 && mask
10636 && !vect_maybe_update_slp_op_vectype (slp_op,
10637 mask_vectype))
10638 {
10639 if (dump_enabled_p ())
10640 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10641 "incompatible vector types for invariants\n");
10642 return false;
10643 }
10644
10645 if (!slp)
10646 STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info) = memory_access_type;
10647 else
10648 SLP_TREE_MEMORY_ACCESS_TYPE (slp_node) = memory_access_type;
10649
10650 if (loop_vinfo
10651 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
10652 check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
10653 VLS_LOAD, group_size,
10654 memory_access_type, &gs_info,
10655 mask, &elsvals);
10656
10657 if (dump_enabled_p ()
10658 && memory_access_type != VMAT_ELEMENTWISE
10659 && memory_access_type != VMAT_GATHER_SCATTER
10660 && memory_access_type != VMAT_STRIDED_SLP
10661 && memory_access_type != VMAT_INVARIANT
10662 && alignment_support_scheme != dr_aligned)
10663 dump_printf_loc (MSG_NOTE, vect_location,
10664 "Vectorizing an unaligned access.\n");
10665
10666 if (memory_access_type == VMAT_LOAD_STORE_LANES)
10667 vinfo->any_known_not_updated_vssa = true;
10668
10669 STMT_VINFO_TYPE (stmt_info) = load_vec_info_type;
10670 }
10671 else
10672 {
10673 /* Here just get the else values. */
10674 if (loop_vinfo
10675 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
10676 check_load_store_for_partial_vectors (loop_vinfo, vectype, slp_node,
10677 VLS_LOAD, group_size,
10678 memory_access_type, &gs_info,
10679 mask, &elsvals);
10680 }
10681
10682 /* If the type needs padding we must zero inactive elements.
10683 Check if we can do that with a VEC_COND_EXPR and store the
10684 elsval we choose in MASKLOAD_ELSVAL. */
10685 if (elsvals.length ()
10686 && type_mode_padding_p
10687 && !elsvals.contains (MASK_LOAD_ELSE_ZERO)
10688 && !expand_vec_cond_expr_p (vectype, truth_type_for (vectype)))
10689 {
10690 if (dump_enabled_p ())
10691 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10692 "cannot zero inactive elements.\n");
10693 return false;
10694 }
10695
10696 /* For now just use the first available else value.
10697 get_supported_else_vals tries MASK_LOAD_ELSE_ZERO first so we will
10698 select it here if it is supported. */
10699 if (elsvals.length ())
10700 maskload_elsval = *elsvals.begin ();
10701
10702 if (!slp)
10703 gcc_assert (memory_access_type
10704 == STMT_VINFO_MEMORY_ACCESS_TYPE (stmt_info));
10705 else
10706 gcc_assert (memory_access_type
10707 == SLP_TREE_MEMORY_ACCESS_TYPE (slp_node));
10708
10709 if (dump_enabled_p () && !costing_p)
10710 dump_printf_loc (MSG_NOTE, vect_location,
10711 "transform load. ncopies = %d\n", ncopies);
10712
10713 /* Transform. */
10714
10715 dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info), *first_dr_info = NULL;
10716 ensure_base_align (dr_info);
10717
10718 if (memory_access_type == VMAT_INVARIANT)
10719 {
10720 gcc_assert (!grouped_load && !mask && !bb_vinfo);
10721 /* If we have versioned for aliasing or the loop doesn't
10722 have any data dependencies that would preclude this,
10723 then we are sure this is a loop invariant load and
10724 thus we can insert it on the preheader edge.
10725 TODO: hoist_defs_of_uses should ideally be computed
10726 once at analysis time, remembered and used in the
10727 transform time. */
10728 bool hoist_p = (LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo)
10729 && !nested_in_vect_loop);
10730
10731 for (stmt_vec_info sinfo : SLP_TREE_SCALAR_STMTS (slp_node))
10732 {
10733 /* It is unsafe to hoist a conditional load over the conditions that
10734 make it valid. When early break this means that any invariant load
10735 can't be hoisted unless it's in the loop header or if we know
10736 something else has verified the load is valid to do. Alignment
10737 peeling would do this since getting through the prologue means the
10738 load was done at least once and so the vector main body is free to
10739 hoist it. However today GCC will hoist the load above the PFA
10740 loop. As such that makes it still invalid and so we can't allow it
10741 today. */
10742 if (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
10743 && !DR_SCALAR_KNOWN_BOUNDS (STMT_VINFO_DR_INFO (sinfo))
10744 && gimple_bb (STMT_VINFO_STMT (vect_orig_stmt (sinfo)))
10745 != loop->header)
10746 {
10747 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
10748 && dump_enabled_p ())
10749 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
10750 "not hoisting invariant load due to early break"
10751 "constraints\n");
10752 else if (dump_enabled_p ())
10753 dump_printf_loc (MSG_NOTE, vect_location,
10754 "not hoisting invariant load due to early break"
10755 "constraints\n");
10756 hoist_p = false;
10757 break;
10758 }
10759
10760 hoist_p = hoist_p && hoist_defs_of_uses (sinfo->stmt, loop, false);
10761 }
10762
10763 if (costing_p)
10764 {
10765 enum vect_cost_model_location cost_loc
10766 = hoist_p ? vect_prologue : vect_body;
10767 unsigned int cost = record_stmt_cost (cost_vec, 1, scalar_load,
10768 stmt_info, slp_node, 0,
10769 cost_loc);
10770 cost += record_stmt_cost (cost_vec, 1, scalar_to_vec, stmt_info,
10771 slp_node, 0, cost_loc);
10772 unsigned int prologue_cost = hoist_p ? cost : 0;
10773 unsigned int inside_cost = hoist_p ? 0 : cost;
10774 if (dump_enabled_p ())
10775 dump_printf_loc (MSG_NOTE, vect_location,
10776 "vect_model_load_cost: inside_cost = %d, "
10777 "prologue_cost = %d .\n",
10778 inside_cost, prologue_cost);
10779 return true;
10780 }
10781 if (hoist_p)
10782 {
10783 gassign *stmt = as_a <gassign *> (stmt_info->stmt);
10784 if (dump_enabled_p ())
10785 dump_printf_loc (MSG_NOTE, vect_location,
10786 "hoisting out of the vectorized loop: %G",
10787 (gimple *) stmt);
10788 scalar_dest = copy_ssa_name (scalar_dest);
10789 tree rhs = unshare_expr (gimple_assign_rhs1 (stmt));
10790 edge pe = loop_preheader_edge (loop);
10791 gphi *vphi = get_virtual_phi (loop->header);
10792 tree vuse;
10793 if (vphi)
10794 vuse = PHI_ARG_DEF_FROM_EDGE (vphi, pe);
10795 else
10796 vuse = gimple_vuse (gsi_stmt (*gsi));
10797 gimple *new_stmt = gimple_build_assign (scalar_dest, rhs);
10798 gimple_set_vuse (new_stmt, vuse);
10799 gsi_insert_on_edge_immediate (pe, new_stmt);
10800 hoist_defs_of_uses (new_stmt, loop, true);
10801 }
10802 /* These copies are all equivalent. */
10803 if (hoist_p)
10804 new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10805 vectype, NULL);
10806 else
10807 {
10808 gimple_stmt_iterator gsi2 = *gsi;
10809 gsi_next (&gsi2);
10810 new_temp = vect_init_vector (vinfo, stmt_info, scalar_dest,
10811 vectype, &gsi2);
10812 }
10813 gimple *new_stmt = SSA_NAME_DEF_STMT (new_temp);
10814 if (slp)
10815 for (j = 0; j < (int) SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node); ++j)
10816 slp_node->push_vec_def (new_stmt);
10817 else
10818 {
10819 for (j = 0; j < ncopies; ++j)
10820 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
10821 *vec_stmt = new_stmt;
10822 }
10823 return true;
10824 }
10825
10826 if (memory_access_type == VMAT_ELEMENTWISE
10827 || memory_access_type == VMAT_STRIDED_SLP)
10828 {
10829 gimple_stmt_iterator incr_gsi;
10830 bool insert_after;
10831 tree offvar = NULL_TREE;
10832 tree ivstep;
10833 tree running_off;
10834 vec<constructor_elt, va_gc> *v = NULL;
10835 tree stride_base, stride_step, alias_off;
10836 /* Checked by get_load_store_type. */
10837 unsigned int const_nunits = nunits.to_constant ();
10838 unsigned HOST_WIDE_INT cst_offset = 0;
10839 tree dr_offset;
10840 unsigned int inside_cost = 0;
10841
10842 gcc_assert (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo));
10843 gcc_assert (!nested_in_vect_loop);
10844
10845 if (grouped_load)
10846 {
10847 first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
10848 first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
10849 }
10850 else
10851 {
10852 first_stmt_info = stmt_info;
10853 first_dr_info = dr_info;
10854 }
10855
10856 if (slp && grouped_load
10857 && memory_access_type == VMAT_STRIDED_SLP)
10858 {
10859 group_size = DR_GROUP_SIZE (first_stmt_info);
10860 ref_type = get_group_alias_ptr_type (first_stmt_info);
10861 }
10862 else
10863 {
10864 if (grouped_load)
10865 cst_offset
10866 = (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)))
10867 * vect_get_place_in_interleaving_chain (stmt_info,
10868 first_stmt_info));
10869 group_size = 1;
10870 ref_type = reference_alias_ptr_type (DR_REF (dr_info->dr));
10871 }
10872
10873 if (!costing_p)
10874 {
10875 dr_offset = get_dr_vinfo_offset (vinfo, first_dr_info);
10876 stride_base = fold_build_pointer_plus (
10877 DR_BASE_ADDRESS (first_dr_info->dr),
10878 size_binop (PLUS_EXPR, convert_to_ptrofftype (dr_offset),
10879 convert_to_ptrofftype (DR_INIT (first_dr_info->dr))));
10880 stride_step = fold_convert (sizetype, DR_STEP (first_dr_info->dr));
10881
10882 /* For a load with loop-invariant (but other than power-of-2)
10883 stride (i.e. not a grouped access) like so:
10884
10885 for (i = 0; i < n; i += stride)
10886 ... = array[i];
10887
10888 we generate a new induction variable and new accesses to
10889 form a new vector (or vectors, depending on ncopies):
10890
10891 for (j = 0; ; j += VF*stride)
10892 tmp1 = array[j];
10893 tmp2 = array[j + stride];
10894 ...
10895 vectemp = {tmp1, tmp2, ...}
10896 */
10897
10898 ivstep = fold_build2 (MULT_EXPR, TREE_TYPE (stride_step), stride_step,
10899 build_int_cst (TREE_TYPE (stride_step), vf));
10900
10901 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
10902
10903 stride_base = cse_and_gimplify_to_preheader (loop_vinfo, stride_base);
10904 ivstep = cse_and_gimplify_to_preheader (loop_vinfo, ivstep);
10905 create_iv (stride_base, PLUS_EXPR, ivstep, NULL,
10906 loop, &incr_gsi, insert_after,
10907 &offvar, NULL);
10908
10909 stride_step = cse_and_gimplify_to_preheader (loop_vinfo, stride_step);
10910 }
10911
10912 running_off = offvar;
10913 alias_off = build_int_cst (ref_type, 0);
10914 int nloads = const_nunits;
10915 int lnel = 1;
10916 tree ltype = TREE_TYPE (vectype);
10917 tree lvectype = vectype;
10918 auto_vec<tree> dr_chain;
10919 if (memory_access_type == VMAT_STRIDED_SLP)
10920 {
10921 HOST_WIDE_INT n = gcd (group_size, const_nunits);
10922 /* Use the target vector type if the group size is a multiple
10923 of it. */
10924 if (n == const_nunits)
10925 {
10926 int mis_align = dr_misalignment (first_dr_info, vectype);
10927 dr_alignment_support dr_align
10928 = vect_supportable_dr_alignment (vinfo, dr_info, vectype,
10929 mis_align);
10930 if (dr_align == dr_aligned
10931 || dr_align == dr_unaligned_supported)
10932 {
10933 nloads = 1;
10934 lnel = const_nunits;
10935 ltype = vectype;
10936 alignment_support_scheme = dr_align;
10937 misalignment = mis_align;
10938 }
10939 }
10940 /* Else use the biggest vector we can load the group without
10941 accessing excess elements. */
10942 else if (n > 1)
10943 {
10944 tree ptype;
10945 tree vtype
10946 = vector_vector_composition_type (vectype, const_nunits / n,
10947 &ptype);
10948 if (vtype != NULL_TREE)
10949 {
10950 dr_alignment_support dr_align;
10951 int mis_align = 0;
10952 if (VECTOR_TYPE_P (ptype))
10953 {
10954 mis_align = dr_misalignment (first_dr_info, ptype);
10955 dr_align
10956 = vect_supportable_dr_alignment (vinfo, dr_info, ptype,
10957 mis_align);
10958 }
10959 else
10960 dr_align = dr_unaligned_supported;
10961 if (dr_align == dr_aligned
10962 || dr_align == dr_unaligned_supported)
10963 {
10964 nloads = const_nunits / n;
10965 lnel = n;
10966 lvectype = vtype;
10967 ltype = ptype;
10968 alignment_support_scheme = dr_align;
10969 misalignment = mis_align;
10970 }
10971 }
10972 }
10973 unsigned align;
10974 /* ??? The above is still wrong, see vectorizable_store. */
10975 align = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
10976 /* Alignment is at most the access size if we do multiple loads. */
10977 if (nloads > 1)
10978 align = MIN (tree_to_uhwi (TYPE_SIZE_UNIT (ltype)), align);
10979 ltype = build_aligned_type (ltype, align * BITS_PER_UNIT);
10980 }
10981
10982 if (slp)
10983 {
10984 /* For SLP permutation support we need to load the whole group,
10985 not only the number of vector stmts the permutation result
10986 fits in. */
10987 if (slp_perm)
10988 {
10989 /* We don't yet generate SLP_TREE_LOAD_PERMUTATIONs for
10990 variable VF. */
10991 unsigned int const_vf = vf.to_constant ();
10992 ncopies = CEIL (group_size * const_vf, const_nunits);
10993 dr_chain.create (ncopies);
10994 }
10995 else
10996 ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
10997 }
10998 unsigned int group_el = 0;
10999 unsigned HOST_WIDE_INT
11000 elsz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
11001 unsigned int n_groups = 0;
11002 /* For costing some adjacent vector loads, we'd like to cost with
11003 the total number of them once instead of cost each one by one. */
11004 unsigned int n_adjacent_loads = 0;
11005 for (j = 0; j < ncopies; j++)
11006 {
11007 if (nloads > 1 && !costing_p)
11008 vec_alloc (v, nloads);
11009 gimple *new_stmt = NULL;
11010 for (i = 0; i < nloads; i++)
11011 {
11012 if (costing_p)
11013 {
11014 /* For VMAT_ELEMENTWISE, just cost it as scalar_load to
11015 avoid ICE, see PR110776. */
11016 if (VECTOR_TYPE_P (ltype)
11017 && memory_access_type != VMAT_ELEMENTWISE)
11018 n_adjacent_loads++;
11019 else
11020 inside_cost += record_stmt_cost (cost_vec, 1, scalar_load,
11021 stmt_info, slp_node, 0,
11022 vect_body);
11023 continue;
11024 }
11025 tree this_off = build_int_cst (TREE_TYPE (alias_off),
11026 group_el * elsz + cst_offset);
11027 tree data_ref = build2 (MEM_REF, ltype, running_off, this_off);
11028 vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11029 new_temp = make_ssa_name (ltype);
11030 new_stmt = gimple_build_assign (new_temp, data_ref);
11031 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11032 if (nloads > 1)
11033 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_temp);
11034
11035 group_el += lnel;
11036 if (! slp
11037 || group_el == group_size)
11038 {
11039 n_groups++;
11040 /* When doing SLP make sure to not load elements from
11041 the next vector iteration, those will not be accessed
11042 so just use the last element again. See PR107451. */
11043 if (!slp || known_lt (n_groups, vf))
11044 {
11045 tree newoff = copy_ssa_name (running_off);
11046 gimple *incr
11047 = gimple_build_assign (newoff, POINTER_PLUS_EXPR,
11048 running_off, stride_step);
11049 vect_finish_stmt_generation (vinfo, stmt_info, incr, gsi);
11050 running_off = newoff;
11051 }
11052 group_el = 0;
11053 }
11054 }
11055
11056 if (nloads > 1)
11057 {
11058 if (costing_p)
11059 inside_cost += record_stmt_cost (cost_vec, 1, vec_construct,
11060 stmt_info, slp_node, 0,
11061 vect_body);
11062 else
11063 {
11064 tree vec_inv = build_constructor (lvectype, v);
11065 new_temp = vect_init_vector (vinfo, stmt_info, vec_inv,
11066 lvectype, gsi);
11067 new_stmt = SSA_NAME_DEF_STMT (new_temp);
11068 if (lvectype != vectype)
11069 {
11070 new_stmt
11071 = gimple_build_assign (make_ssa_name (vectype),
11072 VIEW_CONVERT_EXPR,
11073 build1 (VIEW_CONVERT_EXPR,
11074 vectype, new_temp));
11075 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11076 gsi);
11077 }
11078 }
11079 }
11080 else if (!costing_p && ltype != vectype)
11081 {
11082 new_stmt = gimple_build_assign (make_ssa_name (vectype),
11083 VIEW_CONVERT_EXPR,
11084 build1 (VIEW_CONVERT_EXPR,
11085 vectype, new_temp));
11086 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11087 gsi);
11088 }
11089
11090 if (!costing_p)
11091 {
11092 if (slp)
11093 {
11094 if (slp_perm)
11095 dr_chain.quick_push (gimple_assign_lhs (new_stmt));
11096 else
11097 slp_node->push_vec_def (new_stmt);
11098 }
11099 else
11100 {
11101 if (j == 0)
11102 *vec_stmt = new_stmt;
11103 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
11104 }
11105 }
11106 }
11107 if (slp_perm)
11108 {
11109 unsigned n_perms;
11110 if (costing_p)
11111 {
11112 unsigned n_loads;
11113 vect_transform_slp_perm_load (vinfo, slp_node, vNULL, NULL, vf,
11114 true, &n_perms, &n_loads);
11115 inside_cost += record_stmt_cost (cost_vec, n_perms, vec_perm,
11116 first_stmt_info, slp_node, 0,
11117 vect_body);
11118 }
11119 else
11120 vect_transform_slp_perm_load (vinfo, slp_node, dr_chain, gsi, vf,
11121 false, &n_perms);
11122 }
11123
11124 if (costing_p)
11125 {
11126 if (n_adjacent_loads > 0)
11127 vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11128 alignment_support_scheme, misalignment, false,
11129 &inside_cost, nullptr, cost_vec, cost_vec,
11130 true);
11131 if (dump_enabled_p ())
11132 dump_printf_loc (MSG_NOTE, vect_location,
11133 "vect_model_load_cost: inside_cost = %u, "
11134 "prologue_cost = 0 .\n",
11135 inside_cost);
11136 }
11137
11138 return true;
11139 }
11140
11141 if (memory_access_type == VMAT_GATHER_SCATTER
11142 || (!slp && memory_access_type == VMAT_CONTIGUOUS))
11143 grouped_load = false;
11144
11145 if (grouped_load
11146 || (slp && SLP_TREE_LOAD_PERMUTATION (slp_node).exists ()))
11147 {
11148 if (grouped_load)
11149 {
11150 first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
11151 group_size = DR_GROUP_SIZE (first_stmt_info);
11152 }
11153 else
11154 {
11155 first_stmt_info = stmt_info;
11156 group_size = 1;
11157 }
11158 /* For SLP vectorization we directly vectorize a subchain
11159 without permutation. */
11160 if (slp && ! SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
11161 first_stmt_info = SLP_TREE_SCALAR_STMTS (slp_node)[0];
11162 /* For BB vectorization always use the first stmt to base
11163 the data ref pointer on. */
11164 if (bb_vinfo)
11165 first_stmt_info_for_drptr
11166 = vect_find_first_scalar_stmt_in_slp (slp_node);
11167
11168 /* Check if the chain of loads is already vectorized. */
11169 if (STMT_VINFO_VEC_STMTS (first_stmt_info).exists ()
11170 /* For SLP we would need to copy over SLP_TREE_VEC_DEFS.
11171 ??? But we can only do so if there is exactly one
11172 as we have no way to get at the rest. Leave the CSE
11173 opportunity alone.
11174 ??? With the group load eventually participating
11175 in multiple different permutations (having multiple
11176 slp nodes which refer to the same group) the CSE
11177 is even wrong code. See PR56270. */
11178 && !slp)
11179 {
11180 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
11181 return true;
11182 }
11183 first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
11184 group_gap_adj = 0;
11185
11186 /* VEC_NUM is the number of vect stmts to be created for this group. */
11187 if (slp)
11188 {
11189 grouped_load = false;
11190 /* If an SLP permutation is from N elements to N elements,
11191 and if one vector holds a whole number of N, we can load
11192 the inputs to the permutation in the same way as an
11193 unpermuted sequence. In other cases we need to load the
11194 whole group, not only the number of vector stmts the
11195 permutation result fits in. */
11196 unsigned scalar_lanes = SLP_TREE_LANES (slp_node);
11197 if (nested_in_vect_loop)
11198 /* We do not support grouped accesses in a nested loop,
11199 instead the access is contiguous but it might be
11200 permuted. No gap adjustment is needed though. */
11201 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
11202 else if (slp_perm
11203 && (group_size != scalar_lanes
11204 || !multiple_p (nunits, group_size)))
11205 {
11206 /* We don't yet generate such SLP_TREE_LOAD_PERMUTATIONs for
11207 variable VF; see vect_transform_slp_perm_load. */
11208 unsigned int const_vf = vf.to_constant ();
11209 unsigned int const_nunits = nunits.to_constant ();
11210 vec_num = CEIL (group_size * const_vf, const_nunits);
11211 group_gap_adj = vf * group_size - nunits * vec_num;
11212 }
11213 else
11214 {
11215 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
11216 group_gap_adj
11217 = group_size - scalar_lanes;
11218 }
11219 }
11220 else
11221 vec_num = group_size;
11222
11223 ref_type = get_group_alias_ptr_type (first_stmt_info);
11224 }
11225 else
11226 {
11227 first_stmt_info = stmt_info;
11228 first_dr_info = dr_info;
11229 group_size = vec_num = 1;
11230 group_gap_adj = 0;
11231 ref_type = reference_alias_ptr_type (DR_REF (first_dr_info->dr));
11232 if (slp)
11233 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
11234 }
11235
11236 gcc_assert (alignment_support_scheme);
11237 vec_loop_masks *loop_masks
11238 = (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
11239 ? &LOOP_VINFO_MASKS (loop_vinfo)
11240 : NULL);
11241 vec_loop_lens *loop_lens
11242 = (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
11243 ? &LOOP_VINFO_LENS (loop_vinfo)
11244 : NULL);
11245
11246 /* The vect_transform_stmt and vect_analyze_stmt will go here but there
11247 are some difference here. We cannot enable both the lens and masks
11248 during transform but it is allowed during analysis.
11249 Shouldn't go with length-based approach if fully masked. */
11250 if (cost_vec == NULL)
11251 /* The cost_vec is NULL during transfrom. */
11252 gcc_assert ((!loop_lens || !loop_masks));
11253
11254 /* Targets with store-lane instructions must not require explicit
11255 realignment. vect_supportable_dr_alignment always returns either
11256 dr_aligned or dr_unaligned_supported for masked operations. */
11257 gcc_assert ((memory_access_type != VMAT_LOAD_STORE_LANES
11258 && !mask
11259 && !loop_masks)
11260 || alignment_support_scheme == dr_aligned
11261 || alignment_support_scheme == dr_unaligned_supported);
11262
11263 /* In case the vectorization factor (VF) is bigger than the number
11264 of elements that we can fit in a vectype (nunits), we have to generate
11265 more than one vector stmt - i.e - we need to "unroll" the
11266 vector stmt by a factor VF/nunits. In doing so, we record a pointer
11267 from one copy of the vector stmt to the next, in the field
11268 STMT_VINFO_RELATED_STMT. This is necessary in order to allow following
11269 stages to find the correct vector defs to be used when vectorizing
11270 stmts that use the defs of the current stmt. The example below
11271 illustrates the vectorization process when VF=16 and nunits=4 (i.e., we
11272 need to create 4 vectorized stmts):
11273
11274 before vectorization:
11275 RELATED_STMT VEC_STMT
11276 S1: x = memref - -
11277 S2: z = x + 1 - -
11278
11279 step 1: vectorize stmt S1:
11280 We first create the vector stmt VS1_0, and, as usual, record a
11281 pointer to it in the STMT_VINFO_VEC_STMT of the scalar stmt S1.
11282 Next, we create the vector stmt VS1_1, and record a pointer to
11283 it in the STMT_VINFO_RELATED_STMT of the vector stmt VS1_0.
11284 Similarly, for VS1_2 and VS1_3. This is the resulting chain of
11285 stmts and pointers:
11286 RELATED_STMT VEC_STMT
11287 VS1_0: vx0 = memref0 VS1_1 -
11288 VS1_1: vx1 = memref1 VS1_2 -
11289 VS1_2: vx2 = memref2 VS1_3 -
11290 VS1_3: vx3 = memref3 - -
11291 S1: x = load - VS1_0
11292 S2: z = x + 1 - -
11293 */
11294
11295 /* In case of interleaving (non-unit grouped access):
11296
11297 S1: x2 = &base + 2
11298 S2: x0 = &base
11299 S3: x1 = &base + 1
11300 S4: x3 = &base + 3
11301
11302 Vectorized loads are created in the order of memory accesses
11303 starting from the access of the first stmt of the chain:
11304
11305 VS1: vx0 = &base
11306 VS2: vx1 = &base + vec_size*1
11307 VS3: vx3 = &base + vec_size*2
11308 VS4: vx4 = &base + vec_size*3
11309
11310 Then permutation statements are generated:
11311
11312 VS5: vx5 = VEC_PERM_EXPR < vx0, vx1, { 0, 2, ..., i*2 } >
11313 VS6: vx6 = VEC_PERM_EXPR < vx0, vx1, { 1, 3, ..., i*2+1 } >
11314 ...
11315
11316 And they are put in STMT_VINFO_VEC_STMT of the corresponding scalar stmts
11317 (the order of the data-refs in the output of vect_permute_load_chain
11318 corresponds to the order of scalar stmts in the interleaving chain - see
11319 the documentation of vect_permute_load_chain()).
11320 The generation of permutation stmts and recording them in
11321 STMT_VINFO_VEC_STMT is done in vect_transform_grouped_load().
11322
11323 In case of both multiple types and interleaving, the vector loads and
11324 permutation stmts above are created for every copy. The result vector
11325 stmts are put in STMT_VINFO_VEC_STMT for the first copy and in the
11326 corresponding STMT_VINFO_RELATED_STMT for the next copies. */
11327
11328 /* If the data reference is aligned (dr_aligned) or potentially unaligned
11329 on a target that supports unaligned accesses (dr_unaligned_supported)
11330 we generate the following code:
11331 p = initial_addr;
11332 indx = 0;
11333 loop {
11334 p = p + indx * vectype_size;
11335 vec_dest = *(p);
11336 indx = indx + 1;
11337 }
11338
11339 Otherwise, the data reference is potentially unaligned on a target that
11340 does not support unaligned accesses (dr_explicit_realign_optimized) -
11341 then generate the following code, in which the data in each iteration is
11342 obtained by two vector loads, one from the previous iteration, and one
11343 from the current iteration:
11344 p1 = initial_addr;
11345 msq_init = *(floor(p1))
11346 p2 = initial_addr + VS - 1;
11347 realignment_token = call target_builtin;
11348 indx = 0;
11349 loop {
11350 p2 = p2 + indx * vectype_size
11351 lsq = *(floor(p2))
11352 vec_dest = realign_load (msq, lsq, realignment_token)
11353 indx = indx + 1;
11354 msq = lsq;
11355 } */
11356
11357 /* If the misalignment remains the same throughout the execution of the
11358 loop, we can create the init_addr and permutation mask at the loop
11359 preheader. Otherwise, it needs to be created inside the loop.
11360 This can only occur when vectorizing memory accesses in the inner-loop
11361 nested within an outer-loop that is being vectorized. */
11362
11363 if (nested_in_vect_loop
11364 && !multiple_p (DR_STEP_ALIGNMENT (dr_info->dr),
11365 GET_MODE_SIZE (TYPE_MODE (vectype))))
11366 {
11367 gcc_assert (alignment_support_scheme != dr_explicit_realign_optimized);
11368 compute_in_loop = true;
11369 }
11370
11371 bool diff_first_stmt_info
11372 = first_stmt_info_for_drptr && first_stmt_info != first_stmt_info_for_drptr;
11373
11374 tree offset = NULL_TREE;
11375 if ((alignment_support_scheme == dr_explicit_realign_optimized
11376 || alignment_support_scheme == dr_explicit_realign)
11377 && !compute_in_loop)
11378 {
11379 /* If we have different first_stmt_info, we can't set up realignment
11380 here, since we can't guarantee first_stmt_info DR has been
11381 initialized yet, use first_stmt_info_for_drptr DR by bumping the
11382 distance from first_stmt_info DR instead as below. */
11383 if (!costing_p)
11384 {
11385 if (!diff_first_stmt_info)
11386 msq = vect_setup_realignment (vinfo, first_stmt_info, gsi,
11387 &realignment_token,
11388 alignment_support_scheme, NULL_TREE,
11389 &at_loop);
11390 if (alignment_support_scheme == dr_explicit_realign_optimized)
11391 {
11392 phi = as_a<gphi *> (SSA_NAME_DEF_STMT (msq));
11393 offset = size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (vectype),
11394 size_one_node);
11395 gcc_assert (!first_stmt_info_for_drptr);
11396 }
11397 }
11398 }
11399 else
11400 at_loop = loop;
11401
11402 if (!known_eq (poffset, 0))
11403 offset = (offset
11404 ? size_binop (PLUS_EXPR, offset, size_int (poffset))
11405 : size_int (poffset));
11406
11407 tree bump;
11408 tree vec_offset = NULL_TREE;
11409 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11410 {
11411 aggr_type = NULL_TREE;
11412 bump = NULL_TREE;
11413 }
11414 else if (memory_access_type == VMAT_GATHER_SCATTER)
11415 {
11416 aggr_type = elem_type;
11417 if (!costing_p)
11418 vect_get_strided_load_store_ops (stmt_info, loop_vinfo, gsi, &gs_info,
11419 &bump, &vec_offset, loop_lens);
11420 }
11421 else
11422 {
11423 if (memory_access_type == VMAT_LOAD_STORE_LANES)
11424 aggr_type = build_array_type_nelts (elem_type, group_size * nunits);
11425 else
11426 aggr_type = vectype;
11427 if (!costing_p)
11428 bump = vect_get_data_ptr_increment (vinfo, gsi, dr_info, aggr_type,
11429 memory_access_type, loop_lens);
11430 }
11431
11432 auto_vec<tree> vec_offsets;
11433 auto_vec<tree> vec_masks;
11434 if (mask && !costing_p)
11435 {
11436 if (slp_node)
11437 vect_get_slp_defs (SLP_TREE_CHILDREN (slp_node)[mask_index],
11438 &vec_masks);
11439 else
11440 vect_get_vec_defs_for_operand (vinfo, stmt_info, ncopies, mask,
11441 &vec_masks, mask_vectype);
11442 }
11443
11444 tree vec_mask = NULL_TREE;
11445 tree vec_els = NULL_TREE;
11446 if (memory_access_type == VMAT_LOAD_STORE_LANES)
11447 {
11448 gcc_assert (alignment_support_scheme == dr_aligned
11449 || alignment_support_scheme == dr_unaligned_supported);
11450
11451 unsigned int inside_cost = 0, prologue_cost = 0;
11452 /* For costing some adjacent vector loads, we'd like to cost with
11453 the total number of them once instead of cost each one by one. */
11454 unsigned int n_adjacent_loads = 0;
11455 if (slp_node)
11456 ncopies = slp_node->vec_stmts_size / group_size;
11457 for (j = 0; j < ncopies; j++)
11458 {
11459 if (costing_p)
11460 {
11461 /* An IFN_LOAD_LANES will load all its vector results,
11462 regardless of which ones we actually need. Account
11463 for the cost of unused results. */
11464 if (first_stmt_info == stmt_info)
11465 {
11466 unsigned int gaps = DR_GROUP_SIZE (first_stmt_info);
11467 stmt_vec_info next_stmt_info = first_stmt_info;
11468 do
11469 {
11470 gaps -= 1;
11471 next_stmt_info = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
11472 }
11473 while (next_stmt_info);
11474 if (gaps)
11475 {
11476 if (dump_enabled_p ())
11477 dump_printf_loc (MSG_NOTE, vect_location,
11478 "vect_model_load_cost: %d "
11479 "unused vectors.\n",
11480 gaps);
11481 vect_get_load_cost (vinfo, stmt_info, slp_node, gaps,
11482 alignment_support_scheme,
11483 misalignment, false, &inside_cost,
11484 &prologue_cost, cost_vec, cost_vec,
11485 true);
11486 }
11487 }
11488 n_adjacent_loads++;
11489 continue;
11490 }
11491
11492 /* 1. Create the vector or array pointer update chain. */
11493 if (j == 0)
11494 dataref_ptr
11495 = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11496 at_loop, offset, &dummy, gsi,
11497 &ptr_incr, false, bump);
11498 else
11499 {
11500 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
11501 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
11502 stmt_info, bump);
11503 }
11504 if (mask)
11505 vec_mask = vec_masks[j];
11506
11507 tree vec_array = create_vector_array (vectype, group_size);
11508
11509 tree final_mask = NULL_TREE;
11510 tree final_len = NULL_TREE;
11511 tree bias = NULL_TREE;
11512 if (loop_masks)
11513 final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11514 ncopies, vectype, j);
11515 if (vec_mask)
11516 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype, final_mask,
11517 vec_mask, gsi);
11518
11519 if (lanes_ifn == IFN_MASK_LEN_LOAD_LANES)
11520 {
11521 if (loop_lens)
11522 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11523 ncopies, vectype, j, 1);
11524 else
11525 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
11526 signed char biasval
11527 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11528 bias = build_int_cst (intQI_type_node, biasval);
11529 if (!final_mask)
11530 {
11531 mask_vectype = truth_type_for (vectype);
11532 final_mask = build_minus_one_cst (mask_vectype);
11533 }
11534 }
11535
11536 if (final_mask)
11537 {
11538 vec_els = vect_get_mask_load_else (maskload_elsval, vectype);
11539 if (type_mode_padding_p
11540 && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11541 need_zeroing = true;
11542 }
11543
11544 gcall *call;
11545 if (final_len && final_mask)
11546 {
11547 /* Emit:
11548 VEC_ARRAY = MASK_LEN_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
11549 VEC_MASK, LEN, BIAS). */
11550 unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
11551 tree alias_ptr = build_int_cst (ref_type, align);
11552 call = gimple_build_call_internal (IFN_MASK_LEN_LOAD_LANES, 6,
11553 dataref_ptr, alias_ptr,
11554 final_mask, vec_els,
11555 final_len, bias);
11556 }
11557 else if (final_mask)
11558 {
11559 /* Emit:
11560 VEC_ARRAY = MASK_LOAD_LANES (DATAREF_PTR, ALIAS_PTR,
11561 VEC_MASK). */
11562 unsigned int align = TYPE_ALIGN (TREE_TYPE (vectype));
11563 tree alias_ptr = build_int_cst (ref_type, align);
11564 call = gimple_build_call_internal (IFN_MASK_LOAD_LANES, 4,
11565 dataref_ptr, alias_ptr,
11566 final_mask, vec_els);
11567 }
11568 else
11569 {
11570 /* Emit:
11571 VEC_ARRAY = LOAD_LANES (MEM_REF[...all elements...]). */
11572 data_ref = create_array_ref (aggr_type, dataref_ptr, ref_type);
11573 call = gimple_build_call_internal (IFN_LOAD_LANES, 1, data_ref);
11574 }
11575 gimple_call_set_lhs (call, vec_array);
11576 gimple_call_set_nothrow (call, true);
11577 vect_finish_stmt_generation (vinfo, stmt_info, call, gsi);
11578
11579 if (!slp)
11580 dr_chain.create (group_size);
11581 /* Extract each vector into an SSA_NAME. */
11582 for (unsigned i = 0; i < group_size; i++)
11583 {
11584 new_temp = read_vector_array (vinfo, stmt_info, gsi, scalar_dest,
11585 vec_array, i, need_zeroing,
11586 final_mask);
11587 if (slp)
11588 slp_node->push_vec_def (new_temp);
11589 else
11590 dr_chain.quick_push (new_temp);
11591 }
11592
11593 if (!slp)
11594 /* Record the mapping between SSA_NAMEs and statements. */
11595 vect_record_grouped_load_vectors (vinfo, stmt_info, dr_chain);
11596
11597 /* Record that VEC_ARRAY is now dead. */
11598 vect_clobber_variable (vinfo, stmt_info, gsi, vec_array);
11599
11600 if (!slp)
11601 dr_chain.release ();
11602
11603 if (!slp_node)
11604 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
11605 }
11606
11607 if (costing_p)
11608 {
11609 if (n_adjacent_loads > 0)
11610 vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
11611 alignment_support_scheme, misalignment, false,
11612 &inside_cost, &prologue_cost, cost_vec,
11613 cost_vec, true);
11614 if (dump_enabled_p ())
11615 dump_printf_loc (MSG_NOTE, vect_location,
11616 "vect_model_load_cost: inside_cost = %u, "
11617 "prologue_cost = %u .\n",
11618 inside_cost, prologue_cost);
11619 }
11620
11621 return true;
11622 }
11623
11624 if (memory_access_type == VMAT_GATHER_SCATTER)
11625 {
11626 gcc_assert (alignment_support_scheme == dr_aligned
11627 || alignment_support_scheme == dr_unaligned_supported);
11628 gcc_assert (!grouped_load && !slp_perm);
11629
11630 unsigned int inside_cost = 0, prologue_cost = 0;
11631 for (j = 0; j < ncopies; j++)
11632 {
11633 /* 1. Create the vector or array pointer update chain. */
11634 if (j == 0 && !costing_p)
11635 {
11636 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11637 vect_get_gather_scatter_ops (loop_vinfo, loop, stmt_info,
11638 slp_node, &gs_info, &dataref_ptr,
11639 &vec_offsets);
11640 else
11641 dataref_ptr
11642 = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
11643 at_loop, offset, &dummy, gsi,
11644 &ptr_incr, false, bump);
11645 }
11646 else if (!costing_p)
11647 {
11648 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
11649 if (!STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11650 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11651 gsi, stmt_info, bump);
11652 }
11653
11654 gimple *new_stmt = NULL;
11655 for (i = 0; i < vec_num; i++)
11656 {
11657 tree final_mask = NULL_TREE;
11658 tree final_len = NULL_TREE;
11659 tree bias = NULL_TREE;
11660 if (!costing_p)
11661 {
11662 if (mask)
11663 vec_mask = vec_masks[vec_num * j + i];
11664 if (loop_masks)
11665 final_mask
11666 = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
11667 vec_num * ncopies, vectype,
11668 vec_num * j + i);
11669 if (vec_mask)
11670 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
11671 final_mask, vec_mask, gsi);
11672
11673 if (i > 0 && !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11674 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
11675 gsi, stmt_info, bump);
11676 }
11677
11678 /* 2. Create the vector-load in the loop. */
11679 unsigned HOST_WIDE_INT align;
11680 if (gs_info.ifn != IFN_LAST)
11681 {
11682 if (costing_p)
11683 {
11684 unsigned int cnunits = vect_nunits_for_cost (vectype);
11685 inside_cost
11686 = record_stmt_cost (cost_vec, cnunits, scalar_load,
11687 stmt_info, slp_node, 0, vect_body);
11688 continue;
11689 }
11690 if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
11691 vec_offset = vec_offsets[vec_num * j + i];
11692 tree zero = build_zero_cst (vectype);
11693 tree scale = size_int (gs_info.scale);
11694
11695 if (gs_info.ifn == IFN_MASK_LEN_GATHER_LOAD)
11696 {
11697 if (loop_lens)
11698 final_len
11699 = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
11700 vec_num * ncopies, vectype,
11701 vec_num * j + i, 1);
11702 else
11703 final_len
11704 = build_int_cst (sizetype,
11705 TYPE_VECTOR_SUBPARTS (vectype));
11706 signed char biasval
11707 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
11708 bias = build_int_cst (intQI_type_node, biasval);
11709 if (!final_mask)
11710 {
11711 mask_vectype = truth_type_for (vectype);
11712 final_mask = build_minus_one_cst (mask_vectype);
11713 }
11714 }
11715
11716 if (final_mask)
11717 {
11718 vec_els = vect_get_mask_load_else
11719 (maskload_elsval, vectype);
11720 if (type_mode_padding_p
11721 && maskload_elsval != MASK_LOAD_ELSE_ZERO)
11722 need_zeroing = true;
11723 }
11724
11725 gcall *call;
11726 if (final_len && final_mask)
11727 {
11728 if (VECTOR_TYPE_P (TREE_TYPE (vec_offset)))
11729 call = gimple_build_call_internal (
11730 IFN_MASK_LEN_GATHER_LOAD, 8, dataref_ptr, vec_offset,
11731 scale, zero, final_mask, vec_els, final_len, bias);
11732 else
11733 /* Non-vector offset indicates that prefer to take
11734 MASK_LEN_STRIDED_LOAD instead of the
11735 MASK_LEN_GATHER_LOAD with direct stride arg. */
11736 call = gimple_build_call_internal (
11737 IFN_MASK_LEN_STRIDED_LOAD, 7, dataref_ptr, vec_offset,
11738 zero, final_mask, vec_els, final_len, bias);
11739 }
11740 else if (final_mask)
11741 call = gimple_build_call_internal (IFN_MASK_GATHER_LOAD,
11742 6, dataref_ptr,
11743 vec_offset, scale,
11744 zero, final_mask,
11745 vec_els);
11746 else
11747 call = gimple_build_call_internal (IFN_GATHER_LOAD, 4,
11748 dataref_ptr, vec_offset,
11749 scale, zero);
11750 gimple_call_set_nothrow (call, true);
11751 new_stmt = call;
11752 data_ref = NULL_TREE;
11753 }
11754 else if (gs_info.decl)
11755 {
11756 /* The builtin decls path for gather is legacy, x86 only. */
11757 gcc_assert (!final_len && nunits.is_constant ());
11758 if (costing_p)
11759 {
11760 unsigned int cnunits = vect_nunits_for_cost (vectype);
11761 inside_cost
11762 = record_stmt_cost (cost_vec, cnunits, scalar_load,
11763 stmt_info, slp_node, 0, vect_body);
11764 continue;
11765 }
11766 poly_uint64 offset_nunits
11767 = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype);
11768 if (known_eq (nunits, offset_nunits))
11769 {
11770 new_stmt = vect_build_one_gather_load_call
11771 (vinfo, stmt_info, gsi, &gs_info,
11772 dataref_ptr, vec_offsets[vec_num * j + i],
11773 final_mask);
11774 data_ref = NULL_TREE;
11775 }
11776 else if (known_eq (nunits, offset_nunits * 2))
11777 {
11778 /* We have a offset vector with half the number of
11779 lanes but the builtins will produce full vectype
11780 data with just the lower lanes filled. */
11781 new_stmt = vect_build_one_gather_load_call
11782 (vinfo, stmt_info, gsi, &gs_info,
11783 dataref_ptr, vec_offsets[2 * vec_num * j + 2 * i],
11784 final_mask);
11785 tree low = make_ssa_name (vectype);
11786 gimple_set_lhs (new_stmt, low);
11787 vect_finish_stmt_generation (vinfo, stmt_info,
11788 new_stmt, gsi);
11789
11790 /* now put upper half of final_mask in final_mask low. */
11791 if (final_mask
11792 && !SCALAR_INT_MODE_P
11793 (TYPE_MODE (TREE_TYPE (final_mask))))
11794 {
11795 int count = nunits.to_constant ();
11796 vec_perm_builder sel (count, count, 1);
11797 sel.quick_grow (count);
11798 for (int i = 0; i < count; ++i)
11799 sel[i] = i | (count / 2);
11800 vec_perm_indices indices (sel, 2, count);
11801 tree perm_mask = vect_gen_perm_mask_checked
11802 (TREE_TYPE (final_mask), indices);
11803 new_stmt = gimple_build_assign (NULL_TREE,
11804 VEC_PERM_EXPR,
11805 final_mask,
11806 final_mask,
11807 perm_mask);
11808 final_mask = make_ssa_name (TREE_TYPE (final_mask));
11809 gimple_set_lhs (new_stmt, final_mask);
11810 vect_finish_stmt_generation (vinfo, stmt_info,
11811 new_stmt, gsi);
11812 }
11813 else if (final_mask)
11814 {
11815 new_stmt = gimple_build_assign (NULL_TREE,
11816 VEC_UNPACK_HI_EXPR,
11817 final_mask);
11818 final_mask = make_ssa_name
11819 (truth_type_for (gs_info.offset_vectype));
11820 gimple_set_lhs (new_stmt, final_mask);
11821 vect_finish_stmt_generation (vinfo, stmt_info,
11822 new_stmt, gsi);
11823 }
11824
11825 new_stmt = vect_build_one_gather_load_call
11826 (vinfo, stmt_info, gsi, &gs_info,
11827 dataref_ptr,
11828 vec_offsets[2 * vec_num * j + 2 * i + 1],
11829 final_mask);
11830 tree high = make_ssa_name (vectype);
11831 gimple_set_lhs (new_stmt, high);
11832 vect_finish_stmt_generation (vinfo, stmt_info,
11833 new_stmt, gsi);
11834
11835 /* compose low + high. */
11836 int count = nunits.to_constant ();
11837 vec_perm_builder sel (count, count, 1);
11838 sel.quick_grow (count);
11839 for (int i = 0; i < count; ++i)
11840 sel[i] = i < count / 2 ? i : i + count / 2;
11841 vec_perm_indices indices (sel, 2, count);
11842 tree perm_mask
11843 = vect_gen_perm_mask_checked (vectype, indices);
11844 new_stmt = gimple_build_assign (NULL_TREE,
11845 VEC_PERM_EXPR,
11846 low, high, perm_mask);
11847 data_ref = NULL_TREE;
11848 }
11849 else if (known_eq (nunits * 2, offset_nunits))
11850 {
11851 /* We have a offset vector with double the number of
11852 lanes. Select the low/high part accordingly. */
11853 vec_offset = vec_offsets[(vec_num * j + i) / 2];
11854 if ((vec_num * j + i) & 1)
11855 {
11856 int count = offset_nunits.to_constant ();
11857 vec_perm_builder sel (count, count, 1);
11858 sel.quick_grow (count);
11859 for (int i = 0; i < count; ++i)
11860 sel[i] = i | (count / 2);
11861 vec_perm_indices indices (sel, 2, count);
11862 tree perm_mask = vect_gen_perm_mask_checked
11863 (TREE_TYPE (vec_offset), indices);
11864 new_stmt = gimple_build_assign (NULL_TREE,
11865 VEC_PERM_EXPR,
11866 vec_offset,
11867 vec_offset,
11868 perm_mask);
11869 vec_offset = make_ssa_name (TREE_TYPE (vec_offset));
11870 gimple_set_lhs (new_stmt, vec_offset);
11871 vect_finish_stmt_generation (vinfo, stmt_info,
11872 new_stmt, gsi);
11873 }
11874 new_stmt = vect_build_one_gather_load_call
11875 (vinfo, stmt_info, gsi, &gs_info,
11876 dataref_ptr, vec_offset, final_mask);
11877 data_ref = NULL_TREE;
11878 }
11879 else
11880 gcc_unreachable ();
11881 }
11882 else
11883 {
11884 /* Emulated gather-scatter. */
11885 gcc_assert (!final_mask);
11886 unsigned HOST_WIDE_INT const_nunits = nunits.to_constant ();
11887 if (costing_p)
11888 {
11889 /* For emulated gathers N offset vector element
11890 offset add is consumed by the load). */
11891 inside_cost = record_stmt_cost (cost_vec, const_nunits,
11892 vec_to_scalar, stmt_info,
11893 slp_node, 0, vect_body);
11894 /* N scalar loads plus gathering them into a
11895 vector. */
11896 inside_cost
11897 = record_stmt_cost (cost_vec, const_nunits, scalar_load,
11898 stmt_info, slp_node, 0, vect_body);
11899 inside_cost
11900 = record_stmt_cost (cost_vec, 1, vec_construct,
11901 stmt_info, slp_node, 0, vect_body);
11902 continue;
11903 }
11904 unsigned HOST_WIDE_INT const_offset_nunits
11905 = TYPE_VECTOR_SUBPARTS (gs_info.offset_vectype)
11906 .to_constant ();
11907 vec<constructor_elt, va_gc> *ctor_elts;
11908 vec_alloc (ctor_elts, const_nunits);
11909 gimple_seq stmts = NULL;
11910 /* We support offset vectors with more elements
11911 than the data vector for now. */
11912 unsigned HOST_WIDE_INT factor
11913 = const_offset_nunits / const_nunits;
11914 vec_offset = vec_offsets[(vec_num * j + i) / factor];
11915 unsigned elt_offset
11916 = ((vec_num * j + i) % factor) * const_nunits;
11917 tree idx_type = TREE_TYPE (TREE_TYPE (vec_offset));
11918 tree scale = size_int (gs_info.scale);
11919 align = get_object_alignment (DR_REF (first_dr_info->dr));
11920 tree ltype = build_aligned_type (TREE_TYPE (vectype), align);
11921 for (unsigned k = 0; k < const_nunits; ++k)
11922 {
11923 tree boff = size_binop (MULT_EXPR, TYPE_SIZE (idx_type),
11924 bitsize_int (k + elt_offset));
11925 tree idx
11926 = gimple_build (&stmts, BIT_FIELD_REF, idx_type,
11927 vec_offset, TYPE_SIZE (idx_type), boff);
11928 idx = gimple_convert (&stmts, sizetype, idx);
11929 idx = gimple_build (&stmts, MULT_EXPR, sizetype, idx,
11930 scale);
11931 tree ptr = gimple_build (&stmts, PLUS_EXPR,
11932 TREE_TYPE (dataref_ptr),
11933 dataref_ptr, idx);
11934 ptr = gimple_convert (&stmts, ptr_type_node, ptr);
11935 tree elt = make_ssa_name (TREE_TYPE (vectype));
11936 tree ref = build2 (MEM_REF, ltype, ptr,
11937 build_int_cst (ref_type, 0));
11938 new_stmt = gimple_build_assign (elt, ref);
11939 gimple_set_vuse (new_stmt, gimple_vuse (gsi_stmt (*gsi)));
11940 gimple_seq_add_stmt (&stmts, new_stmt);
11941 CONSTRUCTOR_APPEND_ELT (ctor_elts, NULL_TREE, elt);
11942 }
11943 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
11944 new_stmt = gimple_build_assign (
11945 NULL_TREE, build_constructor (vectype, ctor_elts));
11946 data_ref = NULL_TREE;
11947 }
11948
11949 vec_dest = vect_create_destination_var (scalar_dest, vectype);
11950 /* DATA_REF is null if we've already built the statement. */
11951 if (data_ref)
11952 {
11953 vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
11954 new_stmt = gimple_build_assign (vec_dest, data_ref);
11955 }
11956 new_temp = need_zeroing
11957 ? make_ssa_name (vectype)
11958 : make_ssa_name (vec_dest, new_stmt);
11959 gimple_set_lhs (new_stmt, new_temp);
11960 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
11961
11962 /* If we need to explicitly zero inactive elements emit a
11963 VEC_COND_EXPR that does so. */
11964 if (need_zeroing)
11965 {
11966 vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
11967 vectype);
11968
11969 tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
11970 new_stmt
11971 = gimple_build_assign (new_temp2, VEC_COND_EXPR,
11972 final_mask, new_temp, vec_els);
11973 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
11974 gsi);
11975 new_temp = new_temp2;
11976 }
11977
11978 /* Store vector loads in the corresponding SLP_NODE. */
11979 if (slp)
11980 slp_node->push_vec_def (new_stmt);
11981 }
11982
11983 if (!slp && !costing_p)
11984 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
11985 }
11986
11987 if (!slp && !costing_p)
11988 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
11989
11990 if (costing_p && dump_enabled_p ())
11991 dump_printf_loc (MSG_NOTE, vect_location,
11992 "vect_model_load_cost: inside_cost = %u, "
11993 "prologue_cost = %u .\n",
11994 inside_cost, prologue_cost);
11995 return true;
11996 }
11997
11998 poly_uint64 group_elt = 0;
11999 unsigned int inside_cost = 0, prologue_cost = 0;
12000 /* For costing some adjacent vector loads, we'd like to cost with
12001 the total number of them once instead of cost each one by one. */
12002 unsigned int n_adjacent_loads = 0;
12003 for (j = 0; j < ncopies; j++)
12004 {
12005 /* 1. Create the vector or array pointer update chain. */
12006 if (j == 0 && !costing_p)
12007 {
12008 bool simd_lane_access_p
12009 = STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) != 0;
12010 if (simd_lane_access_p
12011 && TREE_CODE (DR_BASE_ADDRESS (first_dr_info->dr)) == ADDR_EXPR
12012 && VAR_P (TREE_OPERAND (DR_BASE_ADDRESS (first_dr_info->dr), 0))
12013 && integer_zerop (get_dr_vinfo_offset (vinfo, first_dr_info))
12014 && integer_zerop (DR_INIT (first_dr_info->dr))
12015 && alias_sets_conflict_p (get_alias_set (aggr_type),
12016 get_alias_set (TREE_TYPE (ref_type)))
12017 && (alignment_support_scheme == dr_aligned
12018 || alignment_support_scheme == dr_unaligned_supported))
12019 {
12020 dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
12021 dataref_offset = build_int_cst (ref_type, 0);
12022 }
12023 else if (diff_first_stmt_info)
12024 {
12025 dataref_ptr
12026 = vect_create_data_ref_ptr (vinfo, first_stmt_info_for_drptr,
12027 aggr_type, at_loop, offset, &dummy,
12028 gsi, &ptr_incr, simd_lane_access_p,
12029 bump);
12030 /* Adjust the pointer by the difference to first_stmt. */
12031 data_reference_p ptrdr
12032 = STMT_VINFO_DATA_REF (first_stmt_info_for_drptr);
12033 tree diff
12034 = fold_convert (sizetype,
12035 size_binop (MINUS_EXPR,
12036 DR_INIT (first_dr_info->dr),
12037 DR_INIT (ptrdr)));
12038 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
12039 stmt_info, diff);
12040 if (alignment_support_scheme == dr_explicit_realign)
12041 {
12042 msq = vect_setup_realignment (vinfo,
12043 first_stmt_info_for_drptr, gsi,
12044 &realignment_token,
12045 alignment_support_scheme,
12046 dataref_ptr, &at_loop);
12047 gcc_assert (!compute_in_loop);
12048 }
12049 }
12050 else
12051 dataref_ptr
12052 = vect_create_data_ref_ptr (vinfo, first_stmt_info, aggr_type,
12053 at_loop,
12054 offset, &dummy, gsi, &ptr_incr,
12055 simd_lane_access_p, bump);
12056 }
12057 else if (!costing_p)
12058 {
12059 gcc_assert (!LOOP_VINFO_USING_SELECT_VL_P (loop_vinfo));
12060 if (dataref_offset)
12061 dataref_offset = int_const_binop (PLUS_EXPR, dataref_offset,
12062 bump);
12063 else
12064 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
12065 stmt_info, bump);
12066 }
12067
12068 if (grouped_load || slp_perm)
12069 dr_chain.create (vec_num);
12070
12071 gimple *new_stmt = NULL;
12072 for (i = 0; i < vec_num; i++)
12073 {
12074 tree final_mask = NULL_TREE;
12075 tree final_len = NULL_TREE;
12076 tree bias = NULL_TREE;
12077
12078 if (!costing_p)
12079 {
12080 if (mask)
12081 vec_mask = vec_masks[vec_num * j + i];
12082 if (loop_masks)
12083 final_mask = vect_get_loop_mask (loop_vinfo, gsi, loop_masks,
12084 vec_num * ncopies, vectype,
12085 vec_num * j + i);
12086 if (vec_mask)
12087 final_mask = prepare_vec_mask (loop_vinfo, mask_vectype,
12088 final_mask, vec_mask, gsi);
12089
12090 if (i > 0)
12091 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr,
12092 gsi, stmt_info, bump);
12093 }
12094
12095 /* 2. Create the vector-load in the loop. */
12096 switch (alignment_support_scheme)
12097 {
12098 case dr_aligned:
12099 case dr_unaligned_supported:
12100 {
12101 if (costing_p)
12102 break;
12103
12104 unsigned int misalign;
12105 unsigned HOST_WIDE_INT align;
12106 align = known_alignment (DR_TARGET_ALIGNMENT (first_dr_info));
12107 if (alignment_support_scheme == dr_aligned)
12108 misalign = 0;
12109 else if (misalignment == DR_MISALIGNMENT_UNKNOWN)
12110 {
12111 align
12112 = dr_alignment (vect_dr_behavior (vinfo, first_dr_info));
12113 misalign = 0;
12114 }
12115 else
12116 misalign = misalignment;
12117 if (dataref_offset == NULL_TREE
12118 && TREE_CODE (dataref_ptr) == SSA_NAME)
12119 set_ptr_info_alignment (get_ptr_info (dataref_ptr), align,
12120 misalign);
12121 align = least_bit_hwi (misalign | align);
12122
12123 /* Compute IFN when LOOP_LENS or final_mask valid. */
12124 machine_mode vmode = TYPE_MODE (vectype);
12125 machine_mode new_vmode = vmode;
12126 internal_fn partial_ifn = IFN_LAST;
12127 if (loop_lens)
12128 {
12129 opt_machine_mode new_ovmode
12130 = get_len_load_store_mode (vmode, true, &partial_ifn);
12131 new_vmode = new_ovmode.require ();
12132 unsigned factor
12133 = (new_ovmode == vmode) ? 1 : GET_MODE_UNIT_SIZE (vmode);
12134 final_len = vect_get_loop_len (loop_vinfo, gsi, loop_lens,
12135 vec_num * ncopies, vectype,
12136 vec_num * j + i, factor);
12137 }
12138 else if (final_mask)
12139 {
12140 if (!can_vec_mask_load_store_p (
12141 vmode, TYPE_MODE (TREE_TYPE (final_mask)), true,
12142 &partial_ifn))
12143 gcc_unreachable ();
12144 }
12145
12146 if (partial_ifn == IFN_MASK_LEN_LOAD)
12147 {
12148 if (!final_len)
12149 {
12150 /* Pass VF value to 'len' argument of
12151 MASK_LEN_LOAD if LOOP_LENS is invalid. */
12152 final_len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12153 }
12154 if (!final_mask)
12155 {
12156 /* Pass all ones value to 'mask' argument of
12157 MASK_LEN_LOAD if final_mask is invalid. */
12158 mask_vectype = truth_type_for (vectype);
12159 final_mask = build_minus_one_cst (mask_vectype);
12160 }
12161 }
12162 if (final_len)
12163 {
12164 signed char biasval
12165 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
12166
12167 bias = build_int_cst (intQI_type_node, biasval);
12168 }
12169
12170 tree vec_els;
12171
12172 if (final_len)
12173 {
12174 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
12175 gcall *call;
12176 if (partial_ifn == IFN_MASK_LEN_LOAD)
12177 {
12178 vec_els = vect_get_mask_load_else
12179 (maskload_elsval, vectype);
12180 if (type_mode_padding_p
12181 && maskload_elsval != MASK_LOAD_ELSE_ZERO)
12182 need_zeroing = true;
12183 call = gimple_build_call_internal (IFN_MASK_LEN_LOAD,
12184 6, dataref_ptr, ptr,
12185 final_mask, vec_els,
12186 final_len, bias);
12187 }
12188 else
12189 call = gimple_build_call_internal (IFN_LEN_LOAD, 4,
12190 dataref_ptr, ptr,
12191 final_len, bias);
12192 gimple_call_set_nothrow (call, true);
12193 new_stmt = call;
12194 data_ref = NULL_TREE;
12195
12196 /* Need conversion if it's wrapped with VnQI. */
12197 if (vmode != new_vmode)
12198 {
12199 tree new_vtype = build_vector_type_for_mode (
12200 unsigned_intQI_type_node, new_vmode);
12201 tree var
12202 = vect_get_new_ssa_name (new_vtype, vect_simple_var);
12203 gimple_set_lhs (call, var);
12204 vect_finish_stmt_generation (vinfo, stmt_info, call,
12205 gsi);
12206 tree op = build1 (VIEW_CONVERT_EXPR, vectype, var);
12207 new_stmt = gimple_build_assign (vec_dest,
12208 VIEW_CONVERT_EXPR, op);
12209 }
12210 }
12211 else if (final_mask)
12212 {
12213 tree ptr = build_int_cst (ref_type, align * BITS_PER_UNIT);
12214 vec_els = vect_get_mask_load_else
12215 (maskload_elsval, vectype);
12216 if (type_mode_padding_p
12217 && maskload_elsval != MASK_LOAD_ELSE_ZERO)
12218 need_zeroing = true;
12219 gcall *call = gimple_build_call_internal (IFN_MASK_LOAD, 4,
12220 dataref_ptr, ptr,
12221 final_mask,
12222 vec_els);
12223 gimple_call_set_nothrow (call, true);
12224 new_stmt = call;
12225 data_ref = NULL_TREE;
12226 }
12227 else
12228 {
12229 tree ltype = vectype;
12230 tree new_vtype = NULL_TREE;
12231 unsigned HOST_WIDE_INT gap = DR_GROUP_GAP (first_stmt_info);
12232 unsigned HOST_WIDE_INT dr_size
12233 = vect_get_scalar_dr_size (first_dr_info);
12234 poly_int64 off = 0;
12235 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
12236 off = (TYPE_VECTOR_SUBPARTS (vectype) - 1) * -dr_size;
12237 unsigned int vect_align
12238 = vect_known_alignment_in_bytes (first_dr_info, vectype,
12239 off);
12240 /* Try to use a single smaller load when we are about
12241 to load excess elements compared to the unrolled
12242 scalar loop. */
12243 if (known_gt ((vec_num * j + i + 1) * nunits,
12244 (group_size * vf - gap)))
12245 {
12246 poly_uint64 remain = ((group_size * vf - gap)
12247 - (vec_num * j + i) * nunits);
12248 if (known_ge ((vec_num * j + i + 1) * nunits
12249 - (group_size * vf - gap), nunits))
12250 /* DR will be unused. */
12251 ltype = NULL_TREE;
12252 else if (known_ge (vect_align,
12253 tree_to_poly_uint64
12254 (TYPE_SIZE_UNIT (vectype))))
12255 /* Aligned access to excess elements is OK if
12256 at least one element is accessed in the
12257 scalar loop. */
12258 ;
12259 else if (known_gt (vect_align,
12260 ((nunits - remain) * dr_size)))
12261 /* Aligned access to the gap area when there's
12262 at least one element in it is OK. */
12263 ;
12264 else
12265 {
12266 /* remain should now be > 0 and < nunits. */
12267 unsigned num;
12268 if (known_ne (remain, 0u)
12269 && constant_multiple_p (nunits, remain, &num))
12270 {
12271 tree ptype;
12272 new_vtype
12273 = vector_vector_composition_type (vectype,
12274 num,
12275 &ptype);
12276 if (new_vtype)
12277 ltype = ptype;
12278 }
12279 /* Else use multiple loads or a masked load? */
12280 /* For loop vectorization we now should have
12281 an alternate type or LOOP_VINFO_PEELING_FOR_GAPS
12282 set. */
12283 if (loop_vinfo)
12284 gcc_assert (new_vtype
12285 || LOOP_VINFO_PEELING_FOR_GAPS
12286 (loop_vinfo));
12287 /* But still reduce the access size to the next
12288 required power-of-two so peeling a single
12289 scalar iteration is sufficient. */
12290 unsigned HOST_WIDE_INT cremain;
12291 if (remain.is_constant (&cremain))
12292 {
12293 unsigned HOST_WIDE_INT cpart_size
12294 = 1 << ceil_log2 (cremain);
12295 if (known_gt (nunits, cpart_size)
12296 && constant_multiple_p (nunits, cpart_size,
12297 &num))
12298 {
12299 tree ptype;
12300 new_vtype
12301 = vector_vector_composition_type (vectype,
12302 num,
12303 &ptype);
12304 if (new_vtype)
12305 ltype = ptype;
12306 }
12307 }
12308 }
12309 }
12310 tree offset
12311 = (dataref_offset ? dataref_offset
12312 : build_int_cst (ref_type, 0));
12313 if (!ltype)
12314 ;
12315 else if (ltype != vectype
12316 && memory_access_type == VMAT_CONTIGUOUS_REVERSE)
12317 {
12318 poly_uint64 gap_offset
12319 = (tree_to_poly_uint64 (TYPE_SIZE_UNIT (vectype))
12320 - tree_to_poly_uint64 (TYPE_SIZE_UNIT (ltype)));
12321 tree gapcst = build_int_cstu (ref_type, gap_offset);
12322 offset = size_binop (PLUS_EXPR, offset, gapcst);
12323 }
12324 if (ltype)
12325 {
12326 data_ref
12327 = fold_build2 (MEM_REF, ltype, dataref_ptr, offset);
12328 if (alignment_support_scheme == dr_aligned
12329 && align >= TYPE_ALIGN_UNIT (ltype))
12330 ;
12331 else
12332 TREE_TYPE (data_ref)
12333 = build_aligned_type (TREE_TYPE (data_ref),
12334 align * BITS_PER_UNIT);
12335 }
12336 if (!ltype)
12337 data_ref = build_constructor (vectype, NULL);
12338 else if (ltype != vectype)
12339 {
12340 vect_copy_ref_info (data_ref,
12341 DR_REF (first_dr_info->dr));
12342 tree tem = make_ssa_name (ltype);
12343 new_stmt = gimple_build_assign (tem, data_ref);
12344 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
12345 gsi);
12346 data_ref = NULL;
12347 vec<constructor_elt, va_gc> *v;
12348 /* We've computed 'num' above to statically two
12349 or via constant_multiple_p. */
12350 unsigned num
12351 = (exact_div (tree_to_poly_uint64
12352 (TYPE_SIZE_UNIT (vectype)),
12353 tree_to_poly_uint64
12354 (TYPE_SIZE_UNIT (ltype)))
12355 .to_constant ());
12356 vec_alloc (v, num);
12357 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
12358 {
12359 while (--num)
12360 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
12361 build_zero_cst (ltype));
12362 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
12363 }
12364 else
12365 {
12366 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tem);
12367 while (--num)
12368 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
12369 build_zero_cst (ltype));
12370 }
12371 gcc_assert (new_vtype != NULL_TREE);
12372 if (new_vtype == vectype)
12373 new_stmt = gimple_build_assign (
12374 vec_dest, build_constructor (vectype, v));
12375 else
12376 {
12377 tree new_vname = make_ssa_name (new_vtype);
12378 new_stmt = gimple_build_assign (
12379 new_vname, build_constructor (new_vtype, v));
12380 vect_finish_stmt_generation (vinfo, stmt_info,
12381 new_stmt, gsi);
12382 new_stmt = gimple_build_assign (
12383 vec_dest,
12384 build1 (VIEW_CONVERT_EXPR, vectype, new_vname));
12385 }
12386 }
12387 }
12388 break;
12389 }
12390 case dr_explicit_realign:
12391 {
12392 if (costing_p)
12393 break;
12394 tree ptr, bump;
12395
12396 tree vs = size_int (TYPE_VECTOR_SUBPARTS (vectype));
12397
12398 if (compute_in_loop)
12399 msq = vect_setup_realignment (vinfo, first_stmt_info, gsi,
12400 &realignment_token,
12401 dr_explicit_realign,
12402 dataref_ptr, NULL);
12403
12404 if (TREE_CODE (dataref_ptr) == SSA_NAME)
12405 ptr = copy_ssa_name (dataref_ptr);
12406 else
12407 ptr = make_ssa_name (TREE_TYPE (dataref_ptr));
12408 // For explicit realign the target alignment should be
12409 // known at compile time.
12410 unsigned HOST_WIDE_INT align
12411 = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
12412 new_stmt = gimple_build_assign (
12413 ptr, BIT_AND_EXPR, dataref_ptr,
12414 build_int_cst (TREE_TYPE (dataref_ptr),
12415 -(HOST_WIDE_INT) align));
12416 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12417 data_ref
12418 = build2 (MEM_REF, vectype, ptr, build_int_cst (ref_type, 0));
12419 vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
12420 vec_dest = vect_create_destination_var (scalar_dest, vectype);
12421 new_stmt = gimple_build_assign (vec_dest, data_ref);
12422 new_temp = make_ssa_name (vec_dest, new_stmt);
12423 gimple_assign_set_lhs (new_stmt, new_temp);
12424 gimple_move_vops (new_stmt, stmt_info->stmt);
12425 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12426 msq = new_temp;
12427
12428 bump = size_binop (MULT_EXPR, vs, TYPE_SIZE_UNIT (elem_type));
12429 bump = size_binop (MINUS_EXPR, bump, size_one_node);
12430 ptr = bump_vector_ptr (vinfo, dataref_ptr, NULL, gsi, stmt_info,
12431 bump);
12432 new_stmt = gimple_build_assign (
12433 NULL_TREE, BIT_AND_EXPR, ptr,
12434 build_int_cst (TREE_TYPE (ptr), -(HOST_WIDE_INT) align));
12435 if (TREE_CODE (ptr) == SSA_NAME)
12436 ptr = copy_ssa_name (ptr, new_stmt);
12437 else
12438 ptr = make_ssa_name (TREE_TYPE (ptr), new_stmt);
12439 gimple_assign_set_lhs (new_stmt, ptr);
12440 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12441 data_ref
12442 = build2 (MEM_REF, vectype, ptr, build_int_cst (ref_type, 0));
12443 break;
12444 }
12445 case dr_explicit_realign_optimized:
12446 {
12447 if (costing_p)
12448 break;
12449 if (TREE_CODE (dataref_ptr) == SSA_NAME)
12450 new_temp = copy_ssa_name (dataref_ptr);
12451 else
12452 new_temp = make_ssa_name (TREE_TYPE (dataref_ptr));
12453 // We should only be doing this if we know the target
12454 // alignment at compile time.
12455 unsigned HOST_WIDE_INT align
12456 = DR_TARGET_ALIGNMENT (first_dr_info).to_constant ();
12457 new_stmt = gimple_build_assign (
12458 new_temp, BIT_AND_EXPR, dataref_ptr,
12459 build_int_cst (TREE_TYPE (dataref_ptr),
12460 -(HOST_WIDE_INT) align));
12461 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12462 data_ref = build2 (MEM_REF, vectype, new_temp,
12463 build_int_cst (ref_type, 0));
12464 break;
12465 }
12466 default:
12467 gcc_unreachable ();
12468 }
12469
12470 /* One common place to cost the above vect load for different
12471 alignment support schemes. */
12472 if (costing_p)
12473 {
12474 /* For VMAT_CONTIGUOUS_PERMUTE if it's grouped load, we
12475 only need to take care of the first stmt, whose
12476 stmt_info is first_stmt_info, vec_num iterating on it
12477 will cover the cost for the remaining, it's consistent
12478 with transforming. For the prologue cost for realign,
12479 we only need to count it once for the whole group. */
12480 bool first_stmt_info_p = first_stmt_info == stmt_info;
12481 bool add_realign_cost = first_stmt_info_p && i == 0;
12482 if (memory_access_type == VMAT_CONTIGUOUS
12483 || memory_access_type == VMAT_CONTIGUOUS_REVERSE
12484 || (memory_access_type == VMAT_CONTIGUOUS_PERMUTE
12485 && (!grouped_load || first_stmt_info_p)))
12486 {
12487 /* Leave realign cases alone to keep them simple. */
12488 if (alignment_support_scheme == dr_explicit_realign_optimized
12489 || alignment_support_scheme == dr_explicit_realign)
12490 vect_get_load_cost (vinfo, stmt_info, slp_node, 1,
12491 alignment_support_scheme, misalignment,
12492 add_realign_cost, &inside_cost,
12493 &prologue_cost, cost_vec, cost_vec,
12494 true);
12495 else
12496 n_adjacent_loads++;
12497 }
12498 }
12499 else
12500 {
12501 vec_dest = vect_create_destination_var (scalar_dest, vectype);
12502 /* DATA_REF is null if we've already built the statement. */
12503 if (data_ref)
12504 {
12505 vect_copy_ref_info (data_ref, DR_REF (first_dr_info->dr));
12506 new_stmt = gimple_build_assign (vec_dest, data_ref);
12507 }
12508
12509 new_temp = need_zeroing
12510 ? make_ssa_name (vectype)
12511 : make_ssa_name (vec_dest, new_stmt);
12512 gimple_set_lhs (new_stmt, new_temp);
12513 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12514
12515 /* If we need to explicitly zero inactive elements emit a
12516 VEC_COND_EXPR that does so. */
12517 if (need_zeroing)
12518 {
12519 vec_els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
12520 vectype);
12521
12522 tree new_temp2 = make_ssa_name (vec_dest, new_stmt);
12523 new_stmt
12524 = gimple_build_assign (new_temp2, VEC_COND_EXPR,
12525 final_mask, new_temp, vec_els);
12526 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt,
12527 gsi);
12528 new_temp = new_temp2;
12529 }
12530 }
12531
12532 /* 3. Handle explicit realignment if necessary/supported.
12533 Create in loop:
12534 vec_dest = realign_load (msq, lsq, realignment_token) */
12535 if (!costing_p
12536 && (alignment_support_scheme == dr_explicit_realign_optimized
12537 || alignment_support_scheme == dr_explicit_realign))
12538 {
12539 lsq = gimple_assign_lhs (new_stmt);
12540 if (!realignment_token)
12541 realignment_token = dataref_ptr;
12542 vec_dest = vect_create_destination_var (scalar_dest, vectype);
12543 new_stmt = gimple_build_assign (vec_dest, REALIGN_LOAD_EXPR, msq,
12544 lsq, realignment_token);
12545 new_temp = make_ssa_name (vec_dest, new_stmt);
12546 gimple_assign_set_lhs (new_stmt, new_temp);
12547 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
12548
12549 if (alignment_support_scheme == dr_explicit_realign_optimized)
12550 {
12551 gcc_assert (phi);
12552 if (i == vec_num - 1 && j == ncopies - 1)
12553 add_phi_arg (phi, lsq, loop_latch_edge (containing_loop),
12554 UNKNOWN_LOCATION);
12555 msq = lsq;
12556 }
12557 }
12558
12559 if (memory_access_type == VMAT_CONTIGUOUS_REVERSE)
12560 {
12561 if (costing_p)
12562 inside_cost = record_stmt_cost (cost_vec, 1, vec_perm,
12563 stmt_info, slp_node, 0,
12564 vect_body);
12565 else
12566 {
12567 tree perm_mask = perm_mask_for_reverse (vectype);
12568 new_temp = permute_vec_elements (vinfo, new_temp, new_temp,
12569 perm_mask, stmt_info, gsi);
12570 new_stmt = SSA_NAME_DEF_STMT (new_temp);
12571 }
12572 }
12573
12574 /* Collect vector loads and later create their permutation in
12575 vect_transform_grouped_load (). */
12576 if (!costing_p && (grouped_load || slp_perm))
12577 dr_chain.quick_push (new_temp);
12578
12579 /* Store vector loads in the corresponding SLP_NODE. */
12580 if (!costing_p && slp && !slp_perm)
12581 slp_node->push_vec_def (new_stmt);
12582
12583 /* With SLP permutation we load the gaps as well, without
12584 we need to skip the gaps after we manage to fully load
12585 all elements. group_gap_adj is DR_GROUP_SIZE here. */
12586 group_elt += nunits;
12587 if (!costing_p
12588 && maybe_ne (group_gap_adj, 0U)
12589 && !slp_perm
12590 && known_eq (group_elt, group_size - group_gap_adj))
12591 {
12592 poly_wide_int bump_val
12593 = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
12594 if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step)
12595 == -1)
12596 bump_val = -bump_val;
12597 tree bump = wide_int_to_tree (sizetype, bump_val);
12598 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
12599 stmt_info, bump);
12600 group_elt = 0;
12601 }
12602 }
12603 /* Bump the vector pointer to account for a gap or for excess
12604 elements loaded for a permuted SLP load. */
12605 if (!costing_p
12606 && maybe_ne (group_gap_adj, 0U)
12607 && slp_perm)
12608 {
12609 poly_wide_int bump_val
12610 = (wi::to_wide (TYPE_SIZE_UNIT (elem_type)) * group_gap_adj);
12611 if (tree_int_cst_sgn (vect_dr_behavior (vinfo, dr_info)->step) == -1)
12612 bump_val = -bump_val;
12613 tree bump = wide_int_to_tree (sizetype, bump_val);
12614 dataref_ptr = bump_vector_ptr (vinfo, dataref_ptr, ptr_incr, gsi,
12615 stmt_info, bump);
12616 }
12617
12618 if (slp && !slp_perm)
12619 continue;
12620
12621 if (slp_perm)
12622 {
12623 unsigned n_perms;
12624 /* For SLP we know we've seen all possible uses of dr_chain so
12625 direct vect_transform_slp_perm_load to DCE the unused parts.
12626 ??? This is a hack to prevent compile-time issues as seen
12627 in PR101120 and friends. */
12628 if (costing_p)
12629 {
12630 vect_transform_slp_perm_load (vinfo, slp_node, vNULL, nullptr, vf,
12631 true, &n_perms, nullptr);
12632 inside_cost = record_stmt_cost (cost_vec, n_perms, vec_perm,
12633 stmt_info, slp_node, 0,
12634 vect_body);
12635 }
12636 else
12637 {
12638 bool ok = vect_transform_slp_perm_load (vinfo, slp_node, dr_chain,
12639 gsi, vf, false, &n_perms,
12640 nullptr, true);
12641 gcc_assert (ok);
12642 }
12643 }
12644 else
12645 {
12646 if (grouped_load)
12647 {
12648 gcc_assert (memory_access_type == VMAT_CONTIGUOUS_PERMUTE);
12649 /* We assume that the cost of a single load-lanes instruction
12650 is equivalent to the cost of DR_GROUP_SIZE separate loads.
12651 If a grouped access is instead being provided by a
12652 load-and-permute operation, include the cost of the
12653 permutes. */
12654 if (costing_p && first_stmt_info == stmt_info)
12655 {
12656 /* Uses an even and odd extract operations or shuffle
12657 operations for each needed permute. */
12658 int group_size = DR_GROUP_SIZE (first_stmt_info);
12659 int nstmts = ceil_log2 (group_size) * group_size;
12660 inside_cost += record_stmt_cost (cost_vec, nstmts, vec_perm,
12661 stmt_info, slp_node, 0,
12662 vect_body);
12663
12664 if (dump_enabled_p ())
12665 dump_printf_loc (MSG_NOTE, vect_location,
12666 "vect_model_load_cost: "
12667 "strided group_size = %d .\n",
12668 group_size);
12669 }
12670 else if (!costing_p)
12671 {
12672 vect_transform_grouped_load (vinfo, stmt_info, dr_chain,
12673 group_size, gsi);
12674 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
12675 }
12676 }
12677 else if (!costing_p)
12678 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
12679 }
12680 dr_chain.release ();
12681 }
12682 if (!slp && !costing_p)
12683 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
12684
12685 if (costing_p)
12686 {
12687 gcc_assert (memory_access_type == VMAT_CONTIGUOUS
12688 || memory_access_type == VMAT_CONTIGUOUS_REVERSE
12689 || memory_access_type == VMAT_CONTIGUOUS_PERMUTE);
12690 if (n_adjacent_loads > 0)
12691 vect_get_load_cost (vinfo, stmt_info, slp_node, n_adjacent_loads,
12692 alignment_support_scheme, misalignment, false,
12693 &inside_cost, &prologue_cost, cost_vec, cost_vec,
12694 true);
12695 if (dump_enabled_p ())
12696 dump_printf_loc (MSG_NOTE, vect_location,
12697 "vect_model_load_cost: inside_cost = %u, "
12698 "prologue_cost = %u .\n",
12699 inside_cost, prologue_cost);
12700 }
12701
12702 return true;
12703 }
12704
12705 /* Function vect_is_simple_cond.
12706
12707 Input:
12708 LOOP - the loop that is being vectorized.
12709 COND - Condition that is checked for simple use.
12710
12711 Output:
12712 *COMP_VECTYPE - the vector type for the comparison.
12713 *DTS - The def types for the arguments of the comparison
12714
12715 Returns whether a COND can be vectorized. Checks whether
12716 condition operands are supportable using vec_is_simple_use. */
12717
12718 static bool
12719 vect_is_simple_cond (tree cond, vec_info *vinfo, stmt_vec_info stmt_info,
12720 slp_tree slp_node, tree *comp_vectype,
12721 enum vect_def_type *dts, tree vectype)
12722 {
12723 tree lhs, rhs;
12724 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12725 slp_tree slp_op;
12726
12727 /* Mask case. */
12728 if (TREE_CODE (cond) == SSA_NAME
12729 && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
12730 {
12731 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &cond,
12732 &slp_op, &dts[0], comp_vectype)
12733 || !*comp_vectype
12734 || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
12735 return false;
12736 return true;
12737 }
12738
12739 if (!COMPARISON_CLASS_P (cond))
12740 return false;
12741
12742 lhs = TREE_OPERAND (cond, 0);
12743 rhs = TREE_OPERAND (cond, 1);
12744
12745 if (TREE_CODE (lhs) == SSA_NAME)
12746 {
12747 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 0,
12748 &lhs, &slp_op, &dts[0], &vectype1))
12749 return false;
12750 }
12751 else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
12752 || TREE_CODE (lhs) == FIXED_CST)
12753 dts[0] = vect_constant_def;
12754 else
12755 return false;
12756
12757 if (TREE_CODE (rhs) == SSA_NAME)
12758 {
12759 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1,
12760 &rhs, &slp_op, &dts[1], &vectype2))
12761 return false;
12762 }
12763 else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
12764 || TREE_CODE (rhs) == FIXED_CST)
12765 dts[1] = vect_constant_def;
12766 else
12767 return false;
12768
12769 if (vectype1 && vectype2
12770 && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
12771 TYPE_VECTOR_SUBPARTS (vectype2)))
12772 return false;
12773
12774 *comp_vectype = vectype1 ? vectype1 : vectype2;
12775 /* Invariant comparison. */
12776 if (! *comp_vectype)
12777 {
12778 tree scalar_type = TREE_TYPE (lhs);
12779 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
12780 *comp_vectype = truth_type_for (vectype);
12781 else
12782 {
12783 /* If we can widen the comparison to match vectype do so. */
12784 if (INTEGRAL_TYPE_P (scalar_type)
12785 && !slp_node
12786 && tree_int_cst_lt (TYPE_SIZE (scalar_type),
12787 TYPE_SIZE (TREE_TYPE (vectype))))
12788 scalar_type = build_nonstandard_integer_type
12789 (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
12790 *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
12791 slp_node);
12792 }
12793 }
12794
12795 return true;
12796 }
12797
12798 /* vectorizable_condition.
12799
12800 Check if STMT_INFO is conditional modify expression that can be vectorized.
12801 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
12802 stmt using VEC_COND_EXPR to replace it, put it in VEC_STMT, and insert it
12803 at GSI.
12804
12805 When STMT_INFO is vectorized as a nested cycle, for_reduction is true.
12806
12807 Return true if STMT_INFO is vectorizable in this way. */
12808
12809 static bool
12810 vectorizable_condition (vec_info *vinfo,
12811 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
12812 gimple **vec_stmt,
12813 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
12814 {
12815 tree scalar_dest = NULL_TREE;
12816 tree vec_dest = NULL_TREE;
12817 tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
12818 tree then_clause, else_clause;
12819 tree comp_vectype = NULL_TREE;
12820 tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
12821 tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
12822 tree vec_compare;
12823 tree new_temp;
12824 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
12825 enum vect_def_type dts[4]
12826 = {vect_unknown_def_type, vect_unknown_def_type,
12827 vect_unknown_def_type, vect_unknown_def_type};
12828 int ndts = 4;
12829 int ncopies;
12830 int vec_num;
12831 enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
12832 int i;
12833 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
12834 vec<tree> vec_oprnds0 = vNULL;
12835 vec<tree> vec_oprnds1 = vNULL;
12836 vec<tree> vec_oprnds2 = vNULL;
12837 vec<tree> vec_oprnds3 = vNULL;
12838 tree vec_cmp_type;
12839 bool masked = false;
12840
12841 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
12842 return false;
12843
12844 /* Is vectorizable conditional operation? */
12845 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
12846 if (!stmt)
12847 return false;
12848
12849 code = gimple_assign_rhs_code (stmt);
12850 if (code != COND_EXPR)
12851 return false;
12852
12853 stmt_vec_info reduc_info = NULL;
12854 int reduc_index = -1;
12855 vect_reduction_type reduction_type = TREE_CODE_REDUCTION;
12856 bool for_reduction
12857 = STMT_VINFO_REDUC_DEF (vect_orig_stmt (stmt_info)) != NULL;
12858 if (for_reduction)
12859 {
12860 if (slp_node && SLP_TREE_LANES (slp_node) > 1)
12861 return false;
12862 reduc_info = info_for_reduction (vinfo, stmt_info);
12863 reduction_type = STMT_VINFO_REDUC_TYPE (reduc_info);
12864 reduc_index = STMT_VINFO_REDUC_IDX (stmt_info);
12865 gcc_assert (reduction_type != EXTRACT_LAST_REDUCTION
12866 || reduc_index != -1);
12867 }
12868 else
12869 {
12870 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
12871 return false;
12872 }
12873
12874 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
12875 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
12876
12877 if (slp_node)
12878 {
12879 ncopies = 1;
12880 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
12881 }
12882 else
12883 {
12884 ncopies = vect_get_num_copies (loop_vinfo, vectype);
12885 vec_num = 1;
12886 }
12887
12888 gcc_assert (ncopies >= 1);
12889 if (for_reduction && ncopies > 1)
12890 return false; /* FORNOW */
12891
12892 cond_expr = gimple_assign_rhs1 (stmt);
12893 gcc_assert (! COMPARISON_CLASS_P (cond_expr));
12894
12895 if (!vect_is_simple_cond (cond_expr, vinfo, stmt_info, slp_node,
12896 &comp_vectype, &dts[0], vectype)
12897 || !comp_vectype)
12898 return false;
12899
12900 unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
12901 slp_tree then_slp_node, else_slp_node;
12902 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 1 + op_adjust,
12903 &then_clause, &then_slp_node, &dts[2], &vectype1))
12904 return false;
12905 if (!vect_is_simple_use (vinfo, stmt_info, slp_node, 2 + op_adjust,
12906 &else_clause, &else_slp_node, &dts[3], &vectype2))
12907 return false;
12908
12909 if (vectype1 && !useless_type_conversion_p (vectype, vectype1))
12910 return false;
12911
12912 if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
12913 return false;
12914
12915 masked = !COMPARISON_CLASS_P (cond_expr);
12916 vec_cmp_type = truth_type_for (comp_vectype);
12917 if (vec_cmp_type == NULL_TREE
12918 || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
12919 TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
12920 return false;
12921
12922 cond_code = TREE_CODE (cond_expr);
12923 if (!masked)
12924 {
12925 cond_expr0 = TREE_OPERAND (cond_expr, 0);
12926 cond_expr1 = TREE_OPERAND (cond_expr, 1);
12927 }
12928
12929 /* For conditional reductions, the "then" value needs to be the candidate
12930 value calculated by this iteration while the "else" value needs to be
12931 the result carried over from previous iterations. If the COND_EXPR
12932 is the other way around, we need to swap it. */
12933 bool must_invert_cmp_result = false;
12934 if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
12935 {
12936 if (masked)
12937 must_invert_cmp_result = true;
12938 else
12939 {
12940 bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
12941 tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
12942 if (new_code == ERROR_MARK)
12943 must_invert_cmp_result = true;
12944 else
12945 {
12946 cond_code = new_code;
12947 /* Make sure we don't accidentally use the old condition. */
12948 cond_expr = NULL_TREE;
12949 }
12950 }
12951 /* ??? The vectorized operand query below doesn't allow swapping
12952 this way for SLP. */
12953 if (slp_node)
12954 return false;
12955 std::swap (then_clause, else_clause);
12956 }
12957
12958 if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
12959 {
12960 /* Boolean values may have another representation in vectors
12961 and therefore we prefer bit operations over comparison for
12962 them (which also works for scalar masks). We store opcodes
12963 to use in bitop1 and bitop2. Statement is vectorized as
12964 BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
12965 depending on bitop1 and bitop2 arity. */
12966 switch (cond_code)
12967 {
12968 case GT_EXPR:
12969 bitop1 = BIT_NOT_EXPR;
12970 bitop2 = BIT_AND_EXPR;
12971 break;
12972 case GE_EXPR:
12973 bitop1 = BIT_NOT_EXPR;
12974 bitop2 = BIT_IOR_EXPR;
12975 break;
12976 case LT_EXPR:
12977 bitop1 = BIT_NOT_EXPR;
12978 bitop2 = BIT_AND_EXPR;
12979 std::swap (cond_expr0, cond_expr1);
12980 break;
12981 case LE_EXPR:
12982 bitop1 = BIT_NOT_EXPR;
12983 bitop2 = BIT_IOR_EXPR;
12984 std::swap (cond_expr0, cond_expr1);
12985 break;
12986 case NE_EXPR:
12987 bitop1 = BIT_XOR_EXPR;
12988 break;
12989 case EQ_EXPR:
12990 bitop1 = BIT_XOR_EXPR;
12991 bitop2 = BIT_NOT_EXPR;
12992 break;
12993 default:
12994 return false;
12995 }
12996 cond_code = SSA_NAME;
12997 }
12998
12999 if (TREE_CODE_CLASS (cond_code) == tcc_comparison
13000 && reduction_type == EXTRACT_LAST_REDUCTION
13001 && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
13002 {
13003 if (dump_enabled_p ())
13004 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13005 "reduction comparison operation not supported.\n");
13006 return false;
13007 }
13008
13009 if (!vec_stmt)
13010 {
13011 if (bitop1 != NOP_EXPR)
13012 {
13013 machine_mode mode = TYPE_MODE (comp_vectype);
13014 optab optab;
13015
13016 optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
13017 if (!optab || !can_implement_p (optab, mode))
13018 return false;
13019
13020 if (bitop2 != NOP_EXPR)
13021 {
13022 optab = optab_for_tree_code (bitop2, comp_vectype,
13023 optab_default);
13024 if (!optab || !can_implement_p (optab, mode))
13025 return false;
13026 }
13027 }
13028
13029 vect_cost_for_stmt kind = vector_stmt;
13030 if (reduction_type == EXTRACT_LAST_REDUCTION)
13031 /* Count one reduction-like operation per vector. */
13032 kind = vec_to_scalar;
13033 else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
13034 || (!masked
13035 && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
13036 cond_code)
13037 || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
13038 return false;
13039
13040 if (slp_node
13041 && (!vect_maybe_update_slp_op_vectype
13042 (SLP_TREE_CHILDREN (slp_node)[0], comp_vectype)
13043 || (op_adjust == 1
13044 && !vect_maybe_update_slp_op_vectype
13045 (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
13046 || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
13047 || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype)))
13048 {
13049 if (dump_enabled_p ())
13050 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13051 "incompatible vector types for invariants\n");
13052 return false;
13053 }
13054
13055 if (loop_vinfo && for_reduction
13056 && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
13057 {
13058 if (reduction_type == EXTRACT_LAST_REDUCTION)
13059 {
13060 if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
13061 vectype, OPTIMIZE_FOR_SPEED))
13062 vect_record_loop_len (loop_vinfo,
13063 &LOOP_VINFO_LENS (loop_vinfo),
13064 ncopies * vec_num, vectype, 1);
13065 else
13066 vect_record_loop_mask (loop_vinfo,
13067 &LOOP_VINFO_MASKS (loop_vinfo),
13068 ncopies * vec_num, vectype, NULL);
13069 }
13070 /* Extra inactive lanes should be safe for vect_nested_cycle. */
13071 else if (STMT_VINFO_DEF_TYPE (reduc_info) != vect_nested_cycle)
13072 {
13073 if (dump_enabled_p ())
13074 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13075 "conditional reduction prevents the use"
13076 " of partial vectors.\n");
13077 LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo) = false;
13078 }
13079 }
13080
13081 STMT_VINFO_TYPE (stmt_info) = condition_vec_info_type;
13082 vect_model_simple_cost (vinfo, stmt_info, ncopies, dts, ndts, slp_node,
13083 cost_vec, kind);
13084 return true;
13085 }
13086
13087 /* Transform. */
13088
13089 /* Handle def. */
13090 scalar_dest = gimple_assign_lhs (stmt);
13091 if (reduction_type != EXTRACT_LAST_REDUCTION)
13092 vec_dest = vect_create_destination_var (scalar_dest, vectype);
13093
13094 bool swap_cond_operands = false;
13095
13096 /* See whether another part of the vectorized code applies a loop
13097 mask to the condition, or to its inverse. */
13098
13099 vec_loop_masks *masks = NULL;
13100 vec_loop_lens *lens = NULL;
13101 if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo))
13102 {
13103 if (reduction_type == EXTRACT_LAST_REDUCTION)
13104 lens = &LOOP_VINFO_LENS (loop_vinfo);
13105 }
13106 else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo))
13107 {
13108 if (reduction_type == EXTRACT_LAST_REDUCTION)
13109 masks = &LOOP_VINFO_MASKS (loop_vinfo);
13110 else
13111 {
13112 scalar_cond_masked_key cond (cond_expr, ncopies);
13113 if (loop_vinfo->scalar_cond_masked_set.contains (cond))
13114 masks = &LOOP_VINFO_MASKS (loop_vinfo);
13115 else
13116 {
13117 bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
13118 tree_code orig_code = cond.code;
13119 cond.code = invert_tree_comparison (cond.code, honor_nans);
13120 if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
13121 {
13122 masks = &LOOP_VINFO_MASKS (loop_vinfo);
13123 cond_code = cond.code;
13124 swap_cond_operands = true;
13125 }
13126 else
13127 {
13128 /* Try the inverse of the current mask. We check if the
13129 inverse mask is live and if so we generate a negate of
13130 the current mask such that we still honor NaNs. */
13131 cond.inverted_p = true;
13132 cond.code = orig_code;
13133 if (loop_vinfo->scalar_cond_masked_set.contains (cond))
13134 {
13135 masks = &LOOP_VINFO_MASKS (loop_vinfo);
13136 cond_code = cond.code;
13137 swap_cond_operands = true;
13138 must_invert_cmp_result = true;
13139 }
13140 }
13141 }
13142 }
13143 }
13144
13145 /* Handle cond expr. */
13146 if (masked)
13147 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
13148 cond_expr, comp_vectype, &vec_oprnds0,
13149 then_clause, vectype, &vec_oprnds2,
13150 reduction_type != EXTRACT_LAST_REDUCTION
13151 ? else_clause : NULL, vectype, &vec_oprnds3);
13152 else
13153 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
13154 cond_expr0, comp_vectype, &vec_oprnds0,
13155 cond_expr1, comp_vectype, &vec_oprnds1,
13156 then_clause, vectype, &vec_oprnds2,
13157 reduction_type != EXTRACT_LAST_REDUCTION
13158 ? else_clause : NULL, vectype, &vec_oprnds3);
13159
13160 if (reduction_type == EXTRACT_LAST_REDUCTION)
13161 vec_else_clause = else_clause;
13162
13163 /* Arguments are ready. Create the new vector stmt. */
13164 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_cond_lhs)
13165 {
13166 vec_then_clause = vec_oprnds2[i];
13167 if (reduction_type != EXTRACT_LAST_REDUCTION)
13168 vec_else_clause = vec_oprnds3[i];
13169
13170 if (swap_cond_operands)
13171 std::swap (vec_then_clause, vec_else_clause);
13172
13173 if (masked)
13174 vec_compare = vec_cond_lhs;
13175 else
13176 {
13177 vec_cond_rhs = vec_oprnds1[i];
13178 if (bitop1 == NOP_EXPR)
13179 {
13180 gimple_seq stmts = NULL;
13181 vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
13182 vec_cond_lhs, vec_cond_rhs);
13183 gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
13184 }
13185 else
13186 {
13187 new_temp = make_ssa_name (vec_cmp_type);
13188 gassign *new_stmt;
13189 if (bitop1 == BIT_NOT_EXPR)
13190 new_stmt = gimple_build_assign (new_temp, bitop1,
13191 vec_cond_rhs);
13192 else
13193 new_stmt
13194 = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
13195 vec_cond_rhs);
13196 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13197 if (bitop2 == NOP_EXPR)
13198 vec_compare = new_temp;
13199 else if (bitop2 == BIT_NOT_EXPR
13200 && reduction_type != EXTRACT_LAST_REDUCTION)
13201 {
13202 /* Instead of doing ~x ? y : z do x ? z : y. */
13203 vec_compare = new_temp;
13204 std::swap (vec_then_clause, vec_else_clause);
13205 }
13206 else
13207 {
13208 vec_compare = make_ssa_name (vec_cmp_type);
13209 if (bitop2 == BIT_NOT_EXPR)
13210 new_stmt
13211 = gimple_build_assign (vec_compare, bitop2, new_temp);
13212 else
13213 new_stmt
13214 = gimple_build_assign (vec_compare, bitop2,
13215 vec_cond_lhs, new_temp);
13216 vect_finish_stmt_generation (vinfo, stmt_info,
13217 new_stmt, gsi);
13218 }
13219 }
13220 }
13221
13222 /* If we decided to apply a loop mask to the result of the vector
13223 comparison, AND the comparison with the mask now. Later passes
13224 should then be able to reuse the AND results between mulitple
13225 vector statements.
13226
13227 For example:
13228 for (int i = 0; i < 100; ++i)
13229 x[i] = y[i] ? z[i] : 10;
13230
13231 results in following optimized GIMPLE:
13232
13233 mask__35.8_43 = vect__4.7_41 != { 0, ... };
13234 vec_mask_and_46 = loop_mask_40 & mask__35.8_43;
13235 _19 = &MEM[base: z_12(D), index: ivtmp_56, step: 4, offset: 0B];
13236 vect_iftmp.11_47 = .MASK_LOAD (_19, 4B, vec_mask_and_46);
13237 vect_iftmp.12_52 = VEC_COND_EXPR <vec_mask_and_46,
13238 vect_iftmp.11_47, { 10, ... }>;
13239
13240 instead of using a masked and unmasked forms of
13241 vec != { 0, ... } (masked in the MASK_LOAD,
13242 unmasked in the VEC_COND_EXPR). */
13243
13244 /* Force vec_compare to be an SSA_NAME rather than a comparison,
13245 in cases where that's necessary. */
13246
13247 tree len = NULL_TREE, bias = NULL_TREE;
13248 if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION)
13249 {
13250 if (!is_gimple_val (vec_compare))
13251 {
13252 tree vec_compare_name = make_ssa_name (vec_cmp_type);
13253 gassign *new_stmt = gimple_build_assign (vec_compare_name,
13254 vec_compare);
13255 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13256 vec_compare = vec_compare_name;
13257 }
13258
13259 if (must_invert_cmp_result)
13260 {
13261 tree vec_compare_name = make_ssa_name (vec_cmp_type);
13262 gassign *new_stmt = gimple_build_assign (vec_compare_name,
13263 BIT_NOT_EXPR,
13264 vec_compare);
13265 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13266 vec_compare = vec_compare_name;
13267 }
13268
13269 if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST,
13270 vectype, OPTIMIZE_FOR_SPEED))
13271 {
13272 if (lens)
13273 {
13274 len = vect_get_loop_len (loop_vinfo, gsi, lens,
13275 vec_num * ncopies, vectype, i, 1);
13276 signed char biasval
13277 = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo);
13278 bias = build_int_cst (intQI_type_node, biasval);
13279 }
13280 else
13281 {
13282 len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
13283 bias = build_int_cst (intQI_type_node, 0);
13284 }
13285 }
13286 if (masks)
13287 {
13288 tree loop_mask
13289 = vect_get_loop_mask (loop_vinfo, gsi, masks, vec_num * ncopies,
13290 vectype, i);
13291 tree tmp2 = make_ssa_name (vec_cmp_type);
13292 gassign *g
13293 = gimple_build_assign (tmp2, BIT_AND_EXPR, vec_compare,
13294 loop_mask);
13295 vect_finish_stmt_generation (vinfo, stmt_info, g, gsi);
13296 vec_compare = tmp2;
13297 }
13298 }
13299
13300 gimple *new_stmt;
13301 if (reduction_type == EXTRACT_LAST_REDUCTION)
13302 {
13303 gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt;
13304 tree lhs = gimple_get_lhs (old_stmt);
13305 if ((unsigned)i != vec_oprnds0.length () - 1)
13306 lhs = copy_ssa_name (lhs);
13307 if (len)
13308 new_stmt = gimple_build_call_internal
13309 (IFN_LEN_FOLD_EXTRACT_LAST, 5, vec_else_clause, vec_compare,
13310 vec_then_clause, len, bias);
13311 else
13312 new_stmt = gimple_build_call_internal
13313 (IFN_FOLD_EXTRACT_LAST, 3, vec_else_clause, vec_compare,
13314 vec_then_clause);
13315 gimple_call_set_lhs (new_stmt, lhs);
13316 SSA_NAME_DEF_STMT (lhs) = new_stmt;
13317 if ((unsigned)i != vec_oprnds0.length () - 1)
13318 {
13319 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13320 vec_else_clause = lhs;
13321 }
13322 else if (old_stmt == gsi_stmt (*gsi))
13323 vect_finish_replace_stmt (vinfo, stmt_info, new_stmt);
13324 else
13325 {
13326 /* In this case we're moving the definition to later in the
13327 block. That doesn't matter because the only uses of the
13328 lhs are in phi statements. */
13329 gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt);
13330 gsi_remove (&old_gsi, true);
13331 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13332 }
13333 }
13334 else
13335 {
13336 new_temp = make_ssa_name (vec_dest);
13337 new_stmt = gimple_build_assign (new_temp, VEC_COND_EXPR, vec_compare,
13338 vec_then_clause, vec_else_clause);
13339 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13340 }
13341 if (slp_node)
13342 slp_node->push_vec_def (new_stmt);
13343 else
13344 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
13345 }
13346
13347 if (!slp_node)
13348 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
13349
13350 vec_oprnds0.release ();
13351 vec_oprnds1.release ();
13352 vec_oprnds2.release ();
13353 vec_oprnds3.release ();
13354
13355 return true;
13356 }
13357
13358 /* Helper of vectorizable_comparison.
13359
13360 Check if STMT_INFO is comparison expression CODE that can be vectorized.
13361 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
13362 comparison, put it in VEC_STMT, and insert it at GSI.
13363
13364 Return true if STMT_INFO is vectorizable in this way. */
13365
13366 static bool
13367 vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
13368 stmt_vec_info stmt_info, tree_code code,
13369 gimple_stmt_iterator *gsi, gimple **vec_stmt,
13370 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
13371 {
13372 tree lhs, rhs1, rhs2;
13373 tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
13374 tree vec_rhs1 = NULL_TREE, vec_rhs2 = NULL_TREE;
13375 tree new_temp;
13376 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
13377 enum vect_def_type dts[2] = {vect_unknown_def_type, vect_unknown_def_type};
13378 int ndts = 2;
13379 poly_uint64 nunits;
13380 int ncopies;
13381 enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
13382 int i;
13383 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13384 vec<tree> vec_oprnds0 = vNULL;
13385 vec<tree> vec_oprnds1 = vNULL;
13386 tree mask_type;
13387 tree mask = NULL_TREE;
13388
13389 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
13390 return false;
13391
13392 if (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype))
13393 return false;
13394
13395 mask_type = vectype;
13396 nunits = TYPE_VECTOR_SUBPARTS (vectype);
13397
13398 if (slp_node)
13399 ncopies = 1;
13400 else
13401 ncopies = vect_get_num_copies (loop_vinfo, vectype);
13402
13403 gcc_assert (ncopies >= 1);
13404
13405 if (TREE_CODE_CLASS (code) != tcc_comparison)
13406 return false;
13407
13408 slp_tree slp_rhs1, slp_rhs2;
13409 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
13410 0, &rhs1, &slp_rhs1, &dts[0], &vectype1))
13411 return false;
13412
13413 if (!vect_is_simple_use (vinfo, stmt_info, slp_node,
13414 1, &rhs2, &slp_rhs2, &dts[1], &vectype2))
13415 return false;
13416
13417 if (vectype1 && vectype2
13418 && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
13419 TYPE_VECTOR_SUBPARTS (vectype2)))
13420 return false;
13421
13422 vectype = vectype1 ? vectype1 : vectype2;
13423
13424 /* Invariant comparison. */
13425 if (!vectype)
13426 {
13427 vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs1), slp_node);
13428 if (!vectype || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype), nunits))
13429 return false;
13430 }
13431 else if (maybe_ne (nunits, TYPE_VECTOR_SUBPARTS (vectype)))
13432 return false;
13433
13434 /* Can't compare mask and non-mask types. */
13435 if (vectype1 && vectype2
13436 && (VECTOR_BOOLEAN_TYPE_P (vectype1) ^ VECTOR_BOOLEAN_TYPE_P (vectype2)))
13437 return false;
13438
13439 /* Boolean values may have another representation in vectors
13440 and therefore we prefer bit operations over comparison for
13441 them (which also works for scalar masks). We store opcodes
13442 to use in bitop1 and bitop2. Statement is vectorized as
13443 BITOP2 (rhs1 BITOP1 rhs2) or
13444 rhs1 BITOP2 (BITOP1 rhs2)
13445 depending on bitop1 and bitop2 arity. */
13446 bool swap_p = false;
13447 if (VECTOR_BOOLEAN_TYPE_P (vectype))
13448 {
13449 if (code == GT_EXPR)
13450 {
13451 bitop1 = BIT_NOT_EXPR;
13452 bitop2 = BIT_AND_EXPR;
13453 }
13454 else if (code == GE_EXPR)
13455 {
13456 bitop1 = BIT_NOT_EXPR;
13457 bitop2 = BIT_IOR_EXPR;
13458 }
13459 else if (code == LT_EXPR)
13460 {
13461 bitop1 = BIT_NOT_EXPR;
13462 bitop2 = BIT_AND_EXPR;
13463 swap_p = true;
13464 }
13465 else if (code == LE_EXPR)
13466 {
13467 bitop1 = BIT_NOT_EXPR;
13468 bitop2 = BIT_IOR_EXPR;
13469 swap_p = true;
13470 }
13471 else
13472 {
13473 bitop1 = BIT_XOR_EXPR;
13474 if (code == EQ_EXPR)
13475 bitop2 = BIT_NOT_EXPR;
13476 }
13477 }
13478
13479 if (!vec_stmt)
13480 {
13481 if (bitop1 == NOP_EXPR)
13482 {
13483 if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
13484 return false;
13485 }
13486 else
13487 {
13488 machine_mode mode = TYPE_MODE (vectype);
13489 optab optab;
13490
13491 optab = optab_for_tree_code (bitop1, vectype, optab_default);
13492 if (!optab || !can_implement_p (optab, mode))
13493 return false;
13494
13495 if (bitop2 != NOP_EXPR)
13496 {
13497 optab = optab_for_tree_code (bitop2, vectype, optab_default);
13498 if (!optab || !can_implement_p (optab, mode))
13499 return false;
13500 }
13501 }
13502
13503 /* Put types on constant and invariant SLP children. */
13504 if (slp_node
13505 && (!vect_maybe_update_slp_op_vectype (slp_rhs1, vectype)
13506 || !vect_maybe_update_slp_op_vectype (slp_rhs2, vectype)))
13507 {
13508 if (dump_enabled_p ())
13509 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13510 "incompatible vector types for invariants\n");
13511 return false;
13512 }
13513
13514 vect_model_simple_cost (vinfo, stmt_info,
13515 ncopies * (1 + (bitop2 != NOP_EXPR)),
13516 dts, ndts, slp_node, cost_vec);
13517 return true;
13518 }
13519
13520 /* Transform. */
13521
13522 /* Handle def. */
13523 lhs = gimple_get_lhs (STMT_VINFO_STMT (stmt_info));
13524 if (lhs)
13525 mask = vect_create_destination_var (lhs, mask_type);
13526
13527 vect_get_vec_defs (vinfo, stmt_info, slp_node, ncopies,
13528 rhs1, vectype, &vec_oprnds0,
13529 rhs2, vectype, &vec_oprnds1);
13530 if (swap_p)
13531 std::swap (vec_oprnds0, vec_oprnds1);
13532
13533 /* Arguments are ready. Create the new vector stmt. */
13534 FOR_EACH_VEC_ELT (vec_oprnds0, i, vec_rhs1)
13535 {
13536 gimple *new_stmt;
13537 vec_rhs2 = vec_oprnds1[i];
13538
13539 if (lhs)
13540 new_temp = make_ssa_name (mask);
13541 else
13542 new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
13543 if (bitop1 == NOP_EXPR)
13544 {
13545 new_stmt = gimple_build_assign (new_temp, code,
13546 vec_rhs1, vec_rhs2);
13547 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13548 }
13549 else
13550 {
13551 if (bitop1 == BIT_NOT_EXPR)
13552 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs2);
13553 else
13554 new_stmt = gimple_build_assign (new_temp, bitop1, vec_rhs1,
13555 vec_rhs2);
13556 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13557 if (bitop2 != NOP_EXPR)
13558 {
13559 tree res = make_ssa_name (mask);
13560 if (bitop2 == BIT_NOT_EXPR)
13561 new_stmt = gimple_build_assign (res, bitop2, new_temp);
13562 else
13563 new_stmt = gimple_build_assign (res, bitop2, vec_rhs1,
13564 new_temp);
13565 vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
13566 }
13567 }
13568 if (slp_node)
13569 slp_node->push_vec_def (new_stmt);
13570 else
13571 STMT_VINFO_VEC_STMTS (stmt_info).safe_push (new_stmt);
13572 }
13573
13574 if (!slp_node)
13575 *vec_stmt = STMT_VINFO_VEC_STMTS (stmt_info)[0];
13576
13577 vec_oprnds0.release ();
13578 vec_oprnds1.release ();
13579
13580 return true;
13581 }
13582
13583 /* vectorizable_comparison.
13584
13585 Check if STMT_INFO is comparison expression that can be vectorized.
13586 If VEC_STMT is also passed, vectorize STMT_INFO: create a vectorized
13587 comparison, put it in VEC_STMT, and insert it at GSI.
13588
13589 Return true if STMT_INFO is vectorizable in this way. */
13590
13591 static bool
13592 vectorizable_comparison (vec_info *vinfo,
13593 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
13594 gimple **vec_stmt,
13595 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
13596 {
13597 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13598
13599 if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
13600 return false;
13601
13602 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_internal_def)
13603 return false;
13604
13605 gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt);
13606 if (!stmt)
13607 return false;
13608
13609 enum tree_code code = gimple_assign_rhs_code (stmt);
13610 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
13611 if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
13612 vec_stmt, slp_node, cost_vec))
13613 return false;
13614
13615 if (!vec_stmt)
13616 STMT_VINFO_TYPE (stmt_info) = comparison_vec_info_type;
13617
13618 return true;
13619 }
13620
13621 /* Check to see if the current early break given in STMT_INFO is valid for
13622 vectorization. */
13623
13624 bool
13625 vectorizable_early_exit (vec_info *vinfo, stmt_vec_info stmt_info,
13626 gimple_stmt_iterator *gsi, gimple **vec_stmt,
13627 slp_tree slp_node, stmt_vector_for_cost *cost_vec)
13628 {
13629 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
13630 if (!loop_vinfo
13631 || !is_a <gcond *> (STMT_VINFO_STMT (stmt_info)))
13632 return false;
13633
13634 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_condition_def)
13635 return false;
13636
13637 if (!STMT_VINFO_RELEVANT_P (stmt_info))
13638 return false;
13639
13640 DUMP_VECT_SCOPE ("vectorizable_early_exit");
13641
13642 auto code = gimple_cond_code (STMT_VINFO_STMT (stmt_info));
13643
13644 tree vectype = NULL_TREE;
13645 slp_tree slp_op0;
13646 tree op0;
13647 enum vect_def_type dt0;
13648
13649 /* Early break gcond kind SLP trees can be root only and have no children,
13650 for instance in the case where the argument is an external. If that's
13651 the case there is no operand to analyse use of. */
13652 if ((!slp_node || !SLP_TREE_CHILDREN (slp_node).is_empty ())
13653 && !vect_is_simple_use (vinfo, stmt_info, slp_node, 0, &op0, &slp_op0, &dt0,
13654 &vectype))
13655 {
13656 if (dump_enabled_p ())
13657 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13658 "use not simple.\n");
13659 return false;
13660 }
13661
13662 /* For SLP we don't want to use the type of the operands of the SLP node, when
13663 vectorizing using SLP slp_node will be the children of the gcond and we
13664 want to use the type of the direct children which since the gcond is root
13665 will be the current node, rather than a child node as vect_is_simple_use
13666 assumes. */
13667 if (slp_node)
13668 vectype = SLP_TREE_VECTYPE (slp_node);
13669
13670 if (!vectype)
13671 return false;
13672
13673 machine_mode mode = TYPE_MODE (vectype);
13674 int ncopies, vec_num;
13675
13676 if (slp_node)
13677 {
13678 ncopies = 1;
13679 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
13680 }
13681 else
13682 {
13683 ncopies = vect_get_num_copies (loop_vinfo, vectype);
13684 vec_num = 1;
13685 }
13686
13687 vec_loop_masks *masks = &LOOP_VINFO_MASKS (loop_vinfo);
13688 vec_loop_lens *lens = &LOOP_VINFO_LENS (loop_vinfo);
13689 bool masked_loop_p = LOOP_VINFO_FULLY_MASKED_P (loop_vinfo);
13690 bool len_loop_p = LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo);
13691
13692 /* Now build the new conditional. Pattern gimple_conds get dropped during
13693 codegen so we must replace the original insn. */
13694 gimple *orig_stmt = STMT_VINFO_STMT (vect_orig_stmt (stmt_info));
13695 gcond *cond_stmt = as_a <gcond *>(orig_stmt);
13696
13697 tree cst = build_zero_cst (vectype);
13698 auto bb = gimple_bb (cond_stmt);
13699 edge exit_true_edge = EDGE_SUCC (bb, 0);
13700 if (exit_true_edge->flags & EDGE_FALSE_VALUE)
13701 exit_true_edge = EDGE_SUCC (bb, 1);
13702 gcc_assert (exit_true_edge->flags & EDGE_TRUE_VALUE);
13703
13704 /* When vectorizing we assume that if the branch edge is taken that we're
13705 exiting the loop. This is not however always the case as the compiler will
13706 rewrite conditions to always be a comparison against 0. To do this it
13707 sometimes flips the edges. This is fine for scalar, but for vector we
13708 then have to negate the result of the test, as we're still assuming that if
13709 you take the branch edge that we found the exit condition. i.e. we need to
13710 know whether we are generating a `forall` or an `exist` condition. */
13711 bool flipped = flow_bb_inside_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
13712 exit_true_edge->dest);
13713
13714 /* Analyze only. */
13715 if (!vec_stmt)
13716 {
13717 if (direct_optab_handler (cbranch_optab, mode) == CODE_FOR_nothing)
13718 {
13719 if (dump_enabled_p ())
13720 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13721 "can't vectorize early exit because the "
13722 "target doesn't support flag setting vector "
13723 "comparisons.\n");
13724 return false;
13725 }
13726
13727 if (ncopies > 1
13728 && direct_optab_handler (ior_optab, mode) == CODE_FOR_nothing)
13729 {
13730 if (dump_enabled_p ())
13731 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
13732 "can't vectorize early exit because the "
13733 "target does not support boolean vector IOR "
13734 "for type %T.\n",
13735 vectype);
13736 return false;
13737 }
13738
13739 if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
13740 vec_stmt, slp_node, cost_vec))
13741 return false;
13742
13743 if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
13744 {
13745 if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
13746 OPTIMIZE_FOR_SPEED))
13747 vect_record_loop_len (loop_vinfo, lens, ncopies * vec_num,
13748 vectype, 1);
13749 else
13750 vect_record_loop_mask (loop_vinfo, masks, ncopies * vec_num,
13751 vectype, NULL);
13752 }
13753
13754 return true;
13755 }
13756
13757 /* Tranform. */
13758
13759 tree new_temp = NULL_TREE;
13760 gimple *new_stmt = NULL;
13761
13762 if (dump_enabled_p ())
13763 dump_printf_loc (MSG_NOTE, vect_location, "transform early-exit.\n");
13764
13765 /* For SLP we don't do codegen of the body starting from the gcond, the gconds are
13766 roots and so by the time we get to them we have already codegened the SLP tree
13767 and so we shouldn't try to do so again. The arguments have already been
13768 vectorized. It's not very clean to do this here, But the masking code below is
13769 complex and this keeps it all in one place to ease fixes and backports. Once we
13770 drop the non-SLP loop vect or split vectorizable_* this can be simplified. */
13771 if (!slp_node)
13772 {
13773 if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
13774 vec_stmt, slp_node, cost_vec))
13775 gcc_unreachable ();
13776 }
13777
13778 gimple *stmt = STMT_VINFO_STMT (stmt_info);
13779 basic_block cond_bb = gimple_bb (stmt);
13780 gimple_stmt_iterator cond_gsi = gsi_last_bb (cond_bb);
13781
13782 auto_vec<tree> stmts;
13783
13784 if (slp_node)
13785 stmts.safe_splice (SLP_TREE_VEC_DEFS (slp_node));
13786 else
13787 {
13788 auto vec_stmts = STMT_VINFO_VEC_STMTS (stmt_info);
13789 stmts.reserve_exact (vec_stmts.length ());
13790 for (auto stmt : vec_stmts)
13791 stmts.quick_push (gimple_assign_lhs (stmt));
13792 }
13793
13794 /* If we're comparing against a previous forall we need to negate the resullts
13795 before we do the final comparison or reduction. */
13796 if (flipped)
13797 {
13798 /* Rewrite the if(all(mask)) into if (!all(mask)) which is the same as
13799 if (any(~mask)) by negating the masks and flipping the branches.
13800
13801 1. For unmasked loops we simply reduce the ~mask.
13802 2. For masked loops we reduce (~mask & loop_mask) which is the same as
13803 doing (mask & loop_mask) ^ loop_mask. */
13804 for (unsigned i = 0; i < stmts.length (); i++)
13805 {
13806 tree inv_lhs = make_temp_ssa_name (vectype, NULL, "vexit_inv");
13807 auto inv_stmt = gimple_build_assign (inv_lhs, BIT_NOT_EXPR, stmts[i]);
13808 vect_finish_stmt_generation (loop_vinfo, stmt_info, inv_stmt,
13809 &cond_gsi);
13810 stmts[i] = inv_lhs;
13811 }
13812
13813 EDGE_SUCC (bb, 0)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13814 EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
13815 }
13816
13817 /* Determine if we need to reduce the final value. */
13818 if (stmts.length () > 1)
13819 {
13820 /* We build the reductions in a way to maintain as much parallelism as
13821 possible. */
13822 auto_vec<tree> workset (stmts.length ());
13823
13824 /* Mask the statements as we queue them up. Normally we loop over
13825 vec_num, but since we inspect the exact results of vectorization
13826 we don't need to and instead can just use the stmts themselves. */
13827 if (masked_loop_p)
13828 for (unsigned i = 0; i < stmts.length (); i++)
13829 {
13830 tree stmt_mask
13831 = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies * vec_num,
13832 vectype, i);
13833 stmt_mask
13834 = prepare_vec_mask (loop_vinfo, TREE_TYPE (stmt_mask), stmt_mask,
13835 stmts[i], &cond_gsi);
13836 workset.quick_push (stmt_mask);
13837 }
13838 else if (len_loop_p)
13839 for (unsigned i = 0; i < stmts.length (); i++)
13840 {
13841 tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
13842 lens, ncopies * vec_num,
13843 vectype, stmts[i], i, 1);
13844
13845 workset.quick_push (len_mask);
13846 }
13847 else
13848 workset.splice (stmts);
13849
13850 while (workset.length () > 1)
13851 {
13852 new_temp = make_temp_ssa_name (vectype, NULL, "vexit_reduc");
13853 tree arg0 = workset.pop ();
13854 tree arg1 = workset.pop ();
13855 new_stmt = gimple_build_assign (new_temp, BIT_IOR_EXPR, arg0, arg1);
13856 vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
13857 &cond_gsi);
13858 workset.quick_insert (0, new_temp);
13859 }
13860 }
13861 else
13862 {
13863 new_temp = stmts[0];
13864 if (masked_loop_p)
13865 {
13866 tree mask
13867 = vect_get_loop_mask (loop_vinfo, gsi, masks, ncopies, vectype, 0);
13868 new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
13869 new_temp, &cond_gsi);
13870 }
13871 else if (len_loop_p)
13872 new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
13873 ncopies, vectype, new_temp, 0, 1);
13874 }
13875
13876 gcc_assert (new_temp);
13877
13878 gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
13879 update_stmt (orig_stmt);
13880
13881 if (slp_node)
13882 SLP_TREE_VEC_DEFS (slp_node).truncate (0);
13883 else
13884 STMT_VINFO_VEC_STMTS (stmt_info).truncate (0);
13885
13886 if (!slp_node)
13887 *vec_stmt = orig_stmt;
13888
13889 return true;
13890 }
13891
13892 /* If SLP_NODE is nonnull, return true if vectorizable_live_operation
13893 can handle all live statements in the node. Otherwise return true
13894 if STMT_INFO is not live or if vectorizable_live_operation can handle it.
13895 VEC_STMT_P is as for vectorizable_live_operation. */
13896
13897 static bool
13898 can_vectorize_live_stmts (vec_info *vinfo, stmt_vec_info stmt_info,
13899 slp_tree slp_node, slp_instance slp_node_instance,
13900 bool vec_stmt_p,
13901 stmt_vector_for_cost *cost_vec)
13902 {
13903 loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
13904 if (slp_node)
13905 {
13906 stmt_vec_info slp_stmt_info;
13907 unsigned int i;
13908 FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), i, slp_stmt_info)
13909 {
13910 if (slp_stmt_info
13911 && (STMT_VINFO_LIVE_P (slp_stmt_info)
13912 || (loop_vinfo
13913 && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
13914 && STMT_VINFO_DEF_TYPE (slp_stmt_info)
13915 == vect_induction_def))
13916 && !vectorizable_live_operation (vinfo, slp_stmt_info, slp_node,
13917 slp_node_instance, i,
13918 vec_stmt_p, cost_vec))
13919 return false;
13920 }
13921 }
13922 else if ((STMT_VINFO_LIVE_P (stmt_info)
13923 || (LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
13924 && STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def))
13925 && !vectorizable_live_operation (vinfo, stmt_info,
13926 slp_node, slp_node_instance, -1,
13927 vec_stmt_p, cost_vec))
13928 return false;
13929
13930 return true;
13931 }
13932
13933 /* Make sure the statement is vectorizable. */
13934
13935 opt_result
13936 vect_analyze_stmt (vec_info *vinfo,
13937 stmt_vec_info stmt_info, bool *need_to_vectorize,
13938 slp_tree node, slp_instance node_instance,
13939 stmt_vector_for_cost *cost_vec)
13940 {
13941 bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
13942 enum vect_relevant relevance = STMT_VINFO_RELEVANT (stmt_info);
13943 bool ok;
13944 gimple_seq pattern_def_seq;
13945
13946 if (dump_enabled_p ())
13947 dump_printf_loc (MSG_NOTE, vect_location, "==> examining statement: %G",
13948 stmt_info->stmt);
13949
13950 if (gimple_has_volatile_ops (stmt_info->stmt))
13951 return opt_result::failure_at (stmt_info->stmt,
13952 "not vectorized:"
13953 " stmt has volatile operands: %G\n",
13954 stmt_info->stmt);
13955
13956 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
13957 && node == NULL
13958 && (pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info)))
13959 {
13960 gimple_stmt_iterator si;
13961
13962 for (si = gsi_start (pattern_def_seq); !gsi_end_p (si); gsi_next (&si))
13963 {
13964 stmt_vec_info pattern_def_stmt_info
13965 = vinfo->lookup_stmt (gsi_stmt (si));
13966 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
13967 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
13968 {
13969 /* Analyze def stmt of STMT if it's a pattern stmt. */
13970 if (dump_enabled_p ())
13971 dump_printf_loc (MSG_NOTE, vect_location,
13972 "==> examining pattern def statement: %G",
13973 pattern_def_stmt_info->stmt);
13974
13975 opt_result res
13976 = vect_analyze_stmt (vinfo, pattern_def_stmt_info,
13977 need_to_vectorize, node, node_instance,
13978 cost_vec);
13979 if (!res)
13980 return res;
13981 }
13982 }
13983 }
13984
13985 /* Skip stmts that do not need to be vectorized. In loops this is expected
13986 to include:
13987 - the COND_EXPR which is the loop exit condition
13988 - any LABEL_EXPRs in the loop
13989 - computations that are used only for array indexing or loop control.
13990 In basic blocks we only analyze statements that are a part of some SLP
13991 instance, therefore, all the statements are relevant.
13992
13993 Pattern statement needs to be analyzed instead of the original statement
13994 if the original statement is not relevant. Otherwise, we analyze both
13995 statements. In basic blocks we are called from some SLP instance
13996 traversal, don't analyze pattern stmts instead, the pattern stmts
13997 already will be part of SLP instance. */
13998
13999 stmt_vec_info pattern_stmt_info = STMT_VINFO_RELATED_STMT (stmt_info);
14000 if (!STMT_VINFO_RELEVANT_P (stmt_info)
14001 && !STMT_VINFO_LIVE_P (stmt_info))
14002 {
14003 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
14004 && pattern_stmt_info
14005 && (STMT_VINFO_RELEVANT_P (pattern_stmt_info)
14006 || STMT_VINFO_LIVE_P (pattern_stmt_info)))
14007 {
14008 /* Analyze PATTERN_STMT instead of the original stmt. */
14009 stmt_info = pattern_stmt_info;
14010 if (dump_enabled_p ())
14011 dump_printf_loc (MSG_NOTE, vect_location,
14012 "==> examining pattern statement: %G",
14013 stmt_info->stmt);
14014 }
14015 else
14016 {
14017 if (dump_enabled_p ())
14018 dump_printf_loc (MSG_NOTE, vect_location, "irrelevant.\n");
14019
14020 if (node)
14021 return opt_result::failure_at (stmt_info->stmt,
14022 "not vectorized:"
14023 " irrelevant stmt as SLP node %p "
14024 "representative.\n",
14025 (void *)node);
14026 return opt_result::success ();
14027 }
14028 }
14029 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
14030 && node == NULL
14031 && pattern_stmt_info
14032 && (STMT_VINFO_RELEVANT_P (pattern_stmt_info)
14033 || STMT_VINFO_LIVE_P (pattern_stmt_info)))
14034 {
14035 /* Analyze PATTERN_STMT too. */
14036 if (dump_enabled_p ())
14037 dump_printf_loc (MSG_NOTE, vect_location,
14038 "==> examining pattern statement: %G",
14039 pattern_stmt_info->stmt);
14040
14041 opt_result res
14042 = vect_analyze_stmt (vinfo, pattern_stmt_info, need_to_vectorize, node,
14043 node_instance, cost_vec);
14044 if (!res)
14045 return res;
14046 }
14047
14048 switch (STMT_VINFO_DEF_TYPE (stmt_info))
14049 {
14050 case vect_internal_def:
14051 case vect_condition_def:
14052 break;
14053
14054 case vect_reduction_def:
14055 case vect_nested_cycle:
14056 gcc_assert (!bb_vinfo
14057 && (relevance == vect_used_in_outer
14058 || relevance == vect_used_in_outer_by_reduction
14059 || relevance == vect_used_by_reduction
14060 || relevance == vect_unused_in_scope
14061 || relevance == vect_used_only_live));
14062 break;
14063
14064 case vect_double_reduction_def:
14065 gcc_assert (!bb_vinfo && node);
14066 break;
14067
14068 case vect_induction_def:
14069 case vect_first_order_recurrence:
14070 gcc_assert (!bb_vinfo);
14071 break;
14072
14073 case vect_constant_def:
14074 case vect_external_def:
14075 case vect_unknown_def_type:
14076 default:
14077 gcc_unreachable ();
14078 }
14079
14080 tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
14081 if (node)
14082 STMT_VINFO_VECTYPE (stmt_info) = SLP_TREE_VECTYPE (node);
14083
14084 if (STMT_VINFO_RELEVANT_P (stmt_info))
14085 {
14086 gcall *call = dyn_cast <gcall *> (stmt_info->stmt);
14087 gcc_assert (STMT_VINFO_VECTYPE (stmt_info)
14088 || gimple_code (stmt_info->stmt) == GIMPLE_COND
14089 || (call && gimple_call_lhs (call) == NULL_TREE));
14090 *need_to_vectorize = true;
14091 }
14092
14093 if (PURE_SLP_STMT (stmt_info) && !node)
14094 {
14095 if (dump_enabled_p ())
14096 dump_printf_loc (MSG_NOTE, vect_location,
14097 "handled only by SLP analysis\n");
14098 return opt_result::success ();
14099 }
14100
14101 /* When we arrive here with a non-SLP statement and we are supposed
14102 to use SLP for everything fail vectorization. */
14103 if (!node && param_vect_force_slp)
14104 return opt_result::failure_at (stmt_info->stmt,
14105 "needs non-SLP handling\n");
14106
14107 ok = true;
14108 if (!bb_vinfo
14109 && (STMT_VINFO_RELEVANT_P (stmt_info)
14110 || STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def))
14111 /* Prefer vectorizable_call over vectorizable_simd_clone_call so
14112 -mveclibabi= takes preference over library functions with
14113 the simd attribute. */
14114 ok = (vectorizable_call (vinfo, stmt_info, NULL, NULL, node, cost_vec)
14115 || vectorizable_simd_clone_call (vinfo, stmt_info, NULL, NULL, node,
14116 cost_vec)
14117 || vectorizable_conversion (vinfo, stmt_info,
14118 NULL, NULL, node, cost_vec)
14119 || vectorizable_operation (vinfo, stmt_info,
14120 NULL, NULL, node, cost_vec)
14121 || vectorizable_assignment (vinfo, stmt_info,
14122 NULL, NULL, node, cost_vec)
14123 || vectorizable_load (vinfo, stmt_info, NULL, NULL, node, cost_vec)
14124 || vectorizable_store (vinfo, stmt_info, NULL, NULL, node, cost_vec)
14125 || vectorizable_lane_reducing (as_a <loop_vec_info> (vinfo),
14126 stmt_info, node, cost_vec)
14127 || vectorizable_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
14128 node, node_instance, cost_vec)
14129 || vectorizable_induction (as_a <loop_vec_info> (vinfo), stmt_info,
14130 NULL, node, cost_vec)
14131 || vectorizable_shift (vinfo, stmt_info, NULL, NULL, node, cost_vec)
14132 || vectorizable_condition (vinfo, stmt_info,
14133 NULL, NULL, node, cost_vec)
14134 || vectorizable_comparison (vinfo, stmt_info, NULL, NULL, node,
14135 cost_vec)
14136 || vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
14137 stmt_info, NULL, node)
14138 || vectorizable_recurr (as_a <loop_vec_info> (vinfo),
14139 stmt_info, NULL, node, cost_vec)
14140 || vectorizable_early_exit (vinfo, stmt_info, NULL, NULL, node,
14141 cost_vec));
14142 else
14143 {
14144 if (bb_vinfo)
14145 ok = (vectorizable_call (vinfo, stmt_info, NULL, NULL, node, cost_vec)
14146 || vectorizable_simd_clone_call (vinfo, stmt_info,
14147 NULL, NULL, node, cost_vec)
14148 || vectorizable_conversion (vinfo, stmt_info, NULL, NULL, node,
14149 cost_vec)
14150 || vectorizable_shift (vinfo, stmt_info,
14151 NULL, NULL, node, cost_vec)
14152 || vectorizable_operation (vinfo, stmt_info,
14153 NULL, NULL, node, cost_vec)
14154 || vectorizable_assignment (vinfo, stmt_info, NULL, NULL, node,
14155 cost_vec)
14156 || vectorizable_load (vinfo, stmt_info,
14157 NULL, NULL, node, cost_vec)
14158 || vectorizable_store (vinfo, stmt_info,
14159 NULL, NULL, node, cost_vec)
14160 || vectorizable_condition (vinfo, stmt_info,
14161 NULL, NULL, node, cost_vec)
14162 || vectorizable_comparison (vinfo, stmt_info, NULL, NULL, node,
14163 cost_vec)
14164 || vectorizable_phi (vinfo, stmt_info, NULL, node, cost_vec)
14165 || vectorizable_early_exit (vinfo, stmt_info, NULL, NULL, node,
14166 cost_vec));
14167
14168 }
14169
14170 if (node)
14171 STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
14172
14173 if (!ok)
14174 return opt_result::failure_at (stmt_info->stmt,
14175 "not vectorized:"
14176 " relevant stmt not supported: %G",
14177 stmt_info->stmt);
14178
14179 /* Stmts that are (also) "live" (i.e. - that are used out of the loop)
14180 need extra handling, except for vectorizable reductions. */
14181 if (!bb_vinfo
14182 && STMT_VINFO_TYPE (stmt_info) != reduc_vec_info_type
14183 && STMT_VINFO_TYPE (stmt_info) != lc_phi_info_type
14184 && (!node || !node->ldst_lanes || SLP_TREE_CODE (node) == VEC_PERM_EXPR)
14185 && !can_vectorize_live_stmts (as_a <loop_vec_info> (vinfo),
14186 stmt_info, node, node_instance,
14187 false, cost_vec))
14188 return opt_result::failure_at (stmt_info->stmt,
14189 "not vectorized:"
14190 " live stmt not supported: %G",
14191 stmt_info->stmt);
14192
14193 return opt_result::success ();
14194 }
14195
14196
14197 /* Function vect_transform_stmt.
14198
14199 Create a vectorized stmt to replace STMT_INFO, and insert it at GSI. */
14200
14201 bool
14202 vect_transform_stmt (vec_info *vinfo,
14203 stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
14204 slp_tree slp_node, slp_instance slp_node_instance)
14205 {
14206 bool is_store = false;
14207 gimple *vec_stmt = NULL;
14208 bool done;
14209
14210 gcc_assert (slp_node || !PURE_SLP_STMT (stmt_info));
14211
14212 tree saved_vectype = STMT_VINFO_VECTYPE (stmt_info);
14213 if (slp_node)
14214 STMT_VINFO_VECTYPE (stmt_info) = SLP_TREE_VECTYPE (slp_node);
14215
14216 switch (STMT_VINFO_TYPE (stmt_info))
14217 {
14218 case type_demotion_vec_info_type:
14219 case type_promotion_vec_info_type:
14220 case type_conversion_vec_info_type:
14221 done = vectorizable_conversion (vinfo, stmt_info,
14222 gsi, &vec_stmt, slp_node, NULL);
14223 gcc_assert (done);
14224 break;
14225
14226 case induc_vec_info_type:
14227 done = vectorizable_induction (as_a <loop_vec_info> (vinfo),
14228 stmt_info, &vec_stmt, slp_node,
14229 NULL);
14230 gcc_assert (done);
14231 break;
14232
14233 case shift_vec_info_type:
14234 done = vectorizable_shift (vinfo, stmt_info,
14235 gsi, &vec_stmt, slp_node, NULL);
14236 gcc_assert (done);
14237 break;
14238
14239 case op_vec_info_type:
14240 done = vectorizable_operation (vinfo, stmt_info, gsi, &vec_stmt, slp_node,
14241 NULL);
14242 gcc_assert (done);
14243 break;
14244
14245 case assignment_vec_info_type:
14246 done = vectorizable_assignment (vinfo, stmt_info,
14247 gsi, &vec_stmt, slp_node, NULL);
14248 gcc_assert (done);
14249 break;
14250
14251 case load_vec_info_type:
14252 done = vectorizable_load (vinfo, stmt_info, gsi, &vec_stmt, slp_node,
14253 NULL);
14254 gcc_assert (done);
14255 break;
14256
14257 case store_vec_info_type:
14258 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
14259 && !slp_node
14260 && (++DR_GROUP_STORE_COUNT (DR_GROUP_FIRST_ELEMENT (stmt_info))
14261 < DR_GROUP_SIZE (DR_GROUP_FIRST_ELEMENT (stmt_info))))
14262 /* In case of interleaving, the whole chain is vectorized when the
14263 last store in the chain is reached. Store stmts before the last
14264 one are skipped, and there vec_stmt_info shouldn't be freed
14265 meanwhile. */
14266 ;
14267 else
14268 {
14269 done = vectorizable_store (vinfo, stmt_info,
14270 gsi, &vec_stmt, slp_node, NULL);
14271 gcc_assert (done);
14272 is_store = true;
14273 }
14274 break;
14275
14276 case condition_vec_info_type:
14277 done = vectorizable_condition (vinfo, stmt_info,
14278 gsi, &vec_stmt, slp_node, NULL);
14279 gcc_assert (done);
14280 break;
14281
14282 case comparison_vec_info_type:
14283 done = vectorizable_comparison (vinfo, stmt_info, gsi, &vec_stmt,
14284 slp_node, NULL);
14285 gcc_assert (done);
14286 break;
14287
14288 case call_vec_info_type:
14289 done = vectorizable_call (vinfo, stmt_info,
14290 gsi, &vec_stmt, slp_node, NULL);
14291 break;
14292
14293 case call_simd_clone_vec_info_type:
14294 done = vectorizable_simd_clone_call (vinfo, stmt_info, gsi, &vec_stmt,
14295 slp_node, NULL);
14296 break;
14297
14298 case reduc_vec_info_type:
14299 done = vect_transform_reduction (as_a <loop_vec_info> (vinfo), stmt_info,
14300 gsi, &vec_stmt, slp_node);
14301 gcc_assert (done);
14302 break;
14303
14304 case cycle_phi_info_type:
14305 done = vect_transform_cycle_phi (as_a <loop_vec_info> (vinfo), stmt_info,
14306 &vec_stmt, slp_node, slp_node_instance);
14307 gcc_assert (done);
14308 break;
14309
14310 case lc_phi_info_type:
14311 done = vectorizable_lc_phi (as_a <loop_vec_info> (vinfo),
14312 stmt_info, &vec_stmt, slp_node);
14313 gcc_assert (done);
14314 break;
14315
14316 case recurr_info_type:
14317 done = vectorizable_recurr (as_a <loop_vec_info> (vinfo),
14318 stmt_info, &vec_stmt, slp_node, NULL);
14319 gcc_assert (done);
14320 break;
14321
14322 case phi_info_type:
14323 done = vectorizable_phi (vinfo, stmt_info, &vec_stmt, slp_node, NULL);
14324 gcc_assert (done);
14325 break;
14326
14327 case loop_exit_ctrl_vec_info_type:
14328 done = vectorizable_early_exit (vinfo, stmt_info, gsi, &vec_stmt,
14329 slp_node, NULL);
14330 gcc_assert (done);
14331 break;
14332
14333 default:
14334 if (!STMT_VINFO_LIVE_P (stmt_info))
14335 {
14336 if (dump_enabled_p ())
14337 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
14338 "stmt not supported.\n");
14339 gcc_unreachable ();
14340 }
14341 done = true;
14342 }
14343
14344 if (!slp_node && vec_stmt)
14345 gcc_assert (STMT_VINFO_VEC_STMTS (stmt_info).exists ());
14346
14347 if (STMT_VINFO_TYPE (stmt_info) != store_vec_info_type
14348 && (!slp_node
14349 || !slp_node->ldst_lanes
14350 || SLP_TREE_CODE (slp_node) == VEC_PERM_EXPR))
14351 {
14352 /* Handle stmts whose DEF is used outside the loop-nest that is
14353 being vectorized. */
14354 done = can_vectorize_live_stmts (vinfo, stmt_info, slp_node,
14355 slp_node_instance, true, NULL);
14356 gcc_assert (done);
14357 }
14358
14359 if (slp_node)
14360 STMT_VINFO_VECTYPE (stmt_info) = saved_vectype;
14361
14362 return is_store;
14363 }
14364
14365
14366 /* Remove a group of stores (for SLP or interleaving), free their
14367 stmt_vec_info. */
14368
14369 void
14370 vect_remove_stores (vec_info *vinfo, stmt_vec_info first_stmt_info)
14371 {
14372 stmt_vec_info next_stmt_info = first_stmt_info;
14373
14374 while (next_stmt_info)
14375 {
14376 stmt_vec_info tmp = DR_GROUP_NEXT_ELEMENT (next_stmt_info);
14377 next_stmt_info = vect_orig_stmt (next_stmt_info);
14378 /* Free the attached stmt_vec_info and remove the stmt. */
14379 vinfo->remove_stmt (next_stmt_info);
14380 next_stmt_info = tmp;
14381 }
14382 }
14383
14384 /* If NUNITS is nonzero, return a vector type that contains NUNITS
14385 elements of type SCALAR_TYPE, or null if the target doesn't support
14386 such a type.
14387
14388 If NUNITS is zero, return a vector type that contains elements of
14389 type SCALAR_TYPE, choosing whichever vector size the target prefers.
14390
14391 If PREVAILING_MODE is VOIDmode, we have not yet chosen a vector mode
14392 for this vectorization region and want to "autodetect" the best choice.
14393 Otherwise, PREVAILING_MODE is a previously-chosen vector TYPE_MODE
14394 and we want the new type to be interoperable with it. PREVAILING_MODE
14395 in this case can be a scalar integer mode or a vector mode; when it
14396 is a vector mode, the function acts like a tree-level version of
14397 related_vector_mode. */
14398
14399 tree
14400 get_related_vectype_for_scalar_type (machine_mode prevailing_mode,
14401 tree scalar_type, poly_uint64 nunits)
14402 {
14403 tree orig_scalar_type = scalar_type;
14404 scalar_mode inner_mode;
14405 machine_mode simd_mode;
14406 tree vectype;
14407
14408 if ((!INTEGRAL_TYPE_P (scalar_type)
14409 && !POINTER_TYPE_P (scalar_type)
14410 && !SCALAR_FLOAT_TYPE_P (scalar_type))
14411 || (!is_int_mode (TYPE_MODE (scalar_type), &inner_mode)
14412 && !is_float_mode (TYPE_MODE (scalar_type), &inner_mode)))
14413 return NULL_TREE;
14414
14415 unsigned int nbytes = GET_MODE_SIZE (inner_mode);
14416
14417 /* Interoperability between modes requires one to be a constant multiple
14418 of the other, so that the number of vectors required for each operation
14419 is a compile-time constant. */
14420 if (prevailing_mode != VOIDmode
14421 && !constant_multiple_p (nunits * nbytes,
14422 GET_MODE_SIZE (prevailing_mode))
14423 && !constant_multiple_p (GET_MODE_SIZE (prevailing_mode),
14424 nunits * nbytes))
14425 return NULL_TREE;
14426
14427 /* For vector types of elements whose mode precision doesn't
14428 match their types precision we use a element type of mode
14429 precision. The vectorization routines will have to make sure
14430 they support the proper result truncation/extension.
14431 We also make sure to build vector types with INTEGER_TYPE
14432 component type only. */
14433 if (INTEGRAL_TYPE_P (scalar_type)
14434 && (GET_MODE_BITSIZE (inner_mode) != TYPE_PRECISION (scalar_type)
14435 || TREE_CODE (scalar_type) != INTEGER_TYPE))
14436 scalar_type = build_nonstandard_integer_type (GET_MODE_BITSIZE (inner_mode),
14437 TYPE_UNSIGNED (scalar_type));
14438
14439 /* We shouldn't end up building VECTOR_TYPEs of non-scalar components.
14440 When the component mode passes the above test simply use a type
14441 corresponding to that mode. The theory is that any use that
14442 would cause problems with this will disable vectorization anyway. */
14443 else if (!SCALAR_FLOAT_TYPE_P (scalar_type)
14444 && !INTEGRAL_TYPE_P (scalar_type))
14445 scalar_type = lang_hooks.types.type_for_mode (inner_mode, 1);
14446
14447 /* We can't build a vector type of elements with alignment bigger than
14448 their size. */
14449 else if (nbytes < TYPE_ALIGN_UNIT (scalar_type))
14450 scalar_type = lang_hooks.types.type_for_mode (inner_mode,
14451 TYPE_UNSIGNED (scalar_type));
14452
14453 /* If we felt back to using the mode fail if there was
14454 no scalar type for it. */
14455 if (scalar_type == NULL_TREE)
14456 return NULL_TREE;
14457
14458 /* If no prevailing mode was supplied, use the mode the target prefers.
14459 Otherwise lookup a vector mode based on the prevailing mode. */
14460 if (prevailing_mode == VOIDmode)
14461 {
14462 gcc_assert (known_eq (nunits, 0U));
14463 simd_mode = targetm.vectorize.preferred_simd_mode (inner_mode);
14464 if (SCALAR_INT_MODE_P (simd_mode))
14465 {
14466 /* Traditional behavior is not to take the integer mode
14467 literally, but simply to use it as a way of determining
14468 the vector size. It is up to mode_for_vector to decide
14469 what the TYPE_MODE should be.
14470
14471 Note that nunits == 1 is allowed in order to support single
14472 element vector types. */
14473 if (!multiple_p (GET_MODE_SIZE (simd_mode), nbytes, &nunits)
14474 || !mode_for_vector (inner_mode, nunits).exists (&simd_mode))
14475 return NULL_TREE;
14476 }
14477 }
14478 else if (SCALAR_INT_MODE_P (prevailing_mode)
14479 || !related_vector_mode (prevailing_mode,
14480 inner_mode, nunits).exists (&simd_mode))
14481 {
14482 /* Fall back to using mode_for_vector, mostly in the hope of being
14483 able to use an integer mode. */
14484 if (known_eq (nunits, 0U)
14485 && !multiple_p (GET_MODE_SIZE (prevailing_mode), nbytes, &nunits))
14486 return NULL_TREE;
14487
14488 if (!mode_for_vector (inner_mode, nunits).exists (&simd_mode))
14489 return NULL_TREE;
14490 }
14491
14492 vectype = build_vector_type_for_mode (scalar_type, simd_mode);
14493
14494 /* In cases where the mode was chosen by mode_for_vector, check that
14495 the target actually supports the chosen mode, or that it at least
14496 allows the vector mode to be replaced by a like-sized integer. */
14497 if (!VECTOR_MODE_P (TYPE_MODE (vectype))
14498 && !INTEGRAL_MODE_P (TYPE_MODE (vectype)))
14499 return NULL_TREE;
14500
14501 /* Re-attach the address-space qualifier if we canonicalized the scalar
14502 type. */
14503 if (TYPE_ADDR_SPACE (orig_scalar_type) != TYPE_ADDR_SPACE (vectype))
14504 return build_qualified_type
14505 (vectype, KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (orig_scalar_type)));
14506
14507 return vectype;
14508 }
14509
14510 /* Function get_vectype_for_scalar_type.
14511
14512 Returns the vector type corresponding to SCALAR_TYPE as supported
14513 by the target. If GROUP_SIZE is nonzero and we're performing BB
14514 vectorization, make sure that the number of elements in the vector
14515 is no bigger than GROUP_SIZE. */
14516
14517 tree
14518 get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type,
14519 unsigned int group_size)
14520 {
14521 /* For BB vectorization, we should always have a group size once we've
14522 constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
14523 are tentative requests during things like early data reference
14524 analysis and pattern recognition. */
14525 if (is_a <bb_vec_info> (vinfo))
14526 gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
14527 else
14528 group_size = 0;
14529
14530 tree vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
14531 scalar_type);
14532 if (vectype && vinfo->vector_mode == VOIDmode)
14533 vinfo->vector_mode = TYPE_MODE (vectype);
14534
14535 /* Register the natural choice of vector type, before the group size
14536 has been applied. */
14537 if (vectype)
14538 vinfo->used_vector_modes.add (TYPE_MODE (vectype));
14539
14540 /* If the natural choice of vector type doesn't satisfy GROUP_SIZE,
14541 try again with an explicit number of elements. */
14542 if (vectype
14543 && group_size
14544 && maybe_ge (TYPE_VECTOR_SUBPARTS (vectype), group_size))
14545 {
14546 /* Start with the biggest number of units that fits within
14547 GROUP_SIZE and halve it until we find a valid vector type.
14548 Usually either the first attempt will succeed or all will
14549 fail (in the latter case because GROUP_SIZE is too small
14550 for the target), but it's possible that a target could have
14551 a hole between supported vector types.
14552
14553 If GROUP_SIZE is not a power of 2, this has the effect of
14554 trying the largest power of 2 that fits within the group,
14555 even though the group is not a multiple of that vector size.
14556 The BB vectorizer will then try to carve up the group into
14557 smaller pieces. */
14558 unsigned int nunits = 1 << floor_log2 (group_size);
14559 do
14560 {
14561 vectype = get_related_vectype_for_scalar_type (vinfo->vector_mode,
14562 scalar_type, nunits);
14563 nunits /= 2;
14564 }
14565 while (nunits > 1 && !vectype);
14566 }
14567
14568 return vectype;
14569 }
14570
14571 /* Return the vector type corresponding to SCALAR_TYPE as supported
14572 by the target. NODE, if nonnull, is the SLP tree node that will
14573 use the returned vector type. */
14574
14575 tree
14576 get_vectype_for_scalar_type (vec_info *vinfo, tree scalar_type, slp_tree node)
14577 {
14578 unsigned int group_size = 0;
14579 if (node)
14580 group_size = SLP_TREE_LANES (node);
14581 return get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14582 }
14583
14584 /* Function get_mask_type_for_scalar_type.
14585
14586 Returns the mask type corresponding to a result of comparison
14587 of vectors of specified SCALAR_TYPE as supported by target.
14588 If GROUP_SIZE is nonzero and we're performing BB vectorization,
14589 make sure that the number of elements in the vector is no bigger
14590 than GROUP_SIZE. */
14591
14592 tree
14593 get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
14594 unsigned int group_size)
14595 {
14596 tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
14597
14598 if (!vectype)
14599 return NULL;
14600
14601 return truth_type_for (vectype);
14602 }
14603
14604 /* Function get_mask_type_for_scalar_type.
14605
14606 Returns the mask type corresponding to a result of comparison
14607 of vectors of specified SCALAR_TYPE as supported by target.
14608 NODE, if nonnull, is the SLP tree node that will use the returned
14609 vector type. */
14610
14611 tree
14612 get_mask_type_for_scalar_type (vec_info *vinfo, tree scalar_type,
14613 slp_tree node)
14614 {
14615 tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type, node);
14616
14617 if (!vectype)
14618 return NULL;
14619
14620 return truth_type_for (vectype);
14621 }
14622
14623 /* Function get_same_sized_vectype
14624
14625 Returns a vector type corresponding to SCALAR_TYPE of size
14626 VECTOR_TYPE if supported by the target. */
14627
14628 tree
14629 get_same_sized_vectype (tree scalar_type, tree vector_type)
14630 {
14631 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
14632 return truth_type_for (vector_type);
14633
14634 poly_uint64 nunits;
14635 if (!multiple_p (GET_MODE_SIZE (TYPE_MODE (vector_type)),
14636 GET_MODE_SIZE (TYPE_MODE (scalar_type)), &nunits))
14637 return NULL_TREE;
14638
14639 return get_related_vectype_for_scalar_type (TYPE_MODE (vector_type),
14640 scalar_type, nunits);
14641 }
14642
14643 /* Return true if replacing LOOP_VINFO->vector_mode with VECTOR_MODE
14644 would not change the chosen vector modes. */
14645
14646 bool
14647 vect_chooses_same_modes_p (vec_info *vinfo, machine_mode vector_mode)
14648 {
14649 for (vec_info::mode_set::iterator i = vinfo->used_vector_modes.begin ();
14650 i != vinfo->used_vector_modes.end (); ++i)
14651 if (!VECTOR_MODE_P (*i)
14652 || related_vector_mode (vector_mode, GET_MODE_INNER (*i), 0) != *i)
14653 return false;
14654 return true;
14655 }
14656
14657 /* Function vect_is_simple_use.
14658
14659 Input:
14660 VINFO - the vect info of the loop or basic block that is being vectorized.
14661 OPERAND - operand in the loop or bb.
14662 Output:
14663 DEF_STMT_INFO_OUT (optional) - information about the defining stmt in
14664 case OPERAND is an SSA_NAME that is defined in the vectorizable region
14665 DEF_STMT_OUT (optional) - the defining stmt in case OPERAND is an SSA_NAME;
14666 the definition could be anywhere in the function
14667 DT - the type of definition
14668
14669 Returns whether a stmt with OPERAND can be vectorized.
14670 For loops, supportable operands are constants, loop invariants, and operands
14671 that are defined by the current iteration of the loop. Unsupportable
14672 operands are those that are defined by a previous iteration of the loop (as
14673 is the case in reduction/induction computations).
14674 For basic blocks, supportable operands are constants and bb invariants.
14675 For now, operands defined outside the basic block are not supported. */
14676
14677 bool
14678 vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
14679 stmt_vec_info *def_stmt_info_out, gimple **def_stmt_out)
14680 {
14681 if (def_stmt_info_out)
14682 *def_stmt_info_out = NULL;
14683 if (def_stmt_out)
14684 *def_stmt_out = NULL;
14685 *dt = vect_unknown_def_type;
14686
14687 if (dump_enabled_p ())
14688 {
14689 dump_printf_loc (MSG_NOTE, vect_location,
14690 "vect_is_simple_use: operand ");
14691 if (TREE_CODE (operand) == SSA_NAME
14692 && !SSA_NAME_IS_DEFAULT_DEF (operand))
14693 dump_gimple_expr (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (operand), 0);
14694 else
14695 dump_generic_expr (MSG_NOTE, TDF_SLIM, operand);
14696 }
14697
14698 if (CONSTANT_CLASS_P (operand))
14699 *dt = vect_constant_def;
14700 else if (is_gimple_min_invariant (operand))
14701 *dt = vect_external_def;
14702 else if (TREE_CODE (operand) != SSA_NAME)
14703 *dt = vect_unknown_def_type;
14704 else if (SSA_NAME_IS_DEFAULT_DEF (operand))
14705 *dt = vect_external_def;
14706 else
14707 {
14708 gimple *def_stmt = SSA_NAME_DEF_STMT (operand);
14709 stmt_vec_info stmt_vinfo = vinfo->lookup_def (operand);
14710 if (!stmt_vinfo)
14711 *dt = vect_external_def;
14712 else
14713 {
14714 stmt_vinfo = vect_stmt_to_vectorize (stmt_vinfo);
14715 def_stmt = stmt_vinfo->stmt;
14716 *dt = STMT_VINFO_DEF_TYPE (stmt_vinfo);
14717 if (def_stmt_info_out)
14718 *def_stmt_info_out = stmt_vinfo;
14719 }
14720 if (def_stmt_out)
14721 *def_stmt_out = def_stmt;
14722 }
14723
14724 if (dump_enabled_p ())
14725 {
14726 dump_printf (MSG_NOTE, ", type of def: ");
14727 switch (*dt)
14728 {
14729 case vect_uninitialized_def:
14730 dump_printf (MSG_NOTE, "uninitialized\n");
14731 break;
14732 case vect_constant_def:
14733 dump_printf (MSG_NOTE, "constant\n");
14734 break;
14735 case vect_external_def:
14736 dump_printf (MSG_NOTE, "external\n");
14737 break;
14738 case vect_internal_def:
14739 dump_printf (MSG_NOTE, "internal\n");
14740 break;
14741 case vect_induction_def:
14742 dump_printf (MSG_NOTE, "induction\n");
14743 break;
14744 case vect_reduction_def:
14745 dump_printf (MSG_NOTE, "reduction\n");
14746 break;
14747 case vect_double_reduction_def:
14748 dump_printf (MSG_NOTE, "double reduction\n");
14749 break;
14750 case vect_nested_cycle:
14751 dump_printf (MSG_NOTE, "nested cycle\n");
14752 break;
14753 case vect_first_order_recurrence:
14754 dump_printf (MSG_NOTE, "first order recurrence\n");
14755 break;
14756 case vect_condition_def:
14757 dump_printf (MSG_NOTE, "control flow\n");
14758 break;
14759 case vect_unknown_def_type:
14760 dump_printf (MSG_NOTE, "unknown\n");
14761 break;
14762 }
14763 }
14764
14765 if (*dt == vect_unknown_def_type)
14766 {
14767 if (dump_enabled_p ())
14768 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
14769 "Unsupported pattern.\n");
14770 return false;
14771 }
14772
14773 return true;
14774 }
14775
14776 /* Function vect_is_simple_use.
14777
14778 Same as vect_is_simple_use but also determines the vector operand
14779 type of OPERAND and stores it to *VECTYPE. If the definition of
14780 OPERAND is vect_uninitialized_def, vect_constant_def or
14781 vect_external_def *VECTYPE will be set to NULL_TREE and the caller
14782 is responsible to compute the best suited vector type for the
14783 scalar operand. */
14784
14785 bool
14786 vect_is_simple_use (tree operand, vec_info *vinfo, enum vect_def_type *dt,
14787 tree *vectype, stmt_vec_info *def_stmt_info_out,
14788 gimple **def_stmt_out)
14789 {
14790 stmt_vec_info def_stmt_info;
14791 gimple *def_stmt;
14792 if (!vect_is_simple_use (operand, vinfo, dt, &def_stmt_info, &def_stmt))
14793 return false;
14794
14795 if (def_stmt_out)
14796 *def_stmt_out = def_stmt;
14797 if (def_stmt_info_out)
14798 *def_stmt_info_out = def_stmt_info;
14799
14800 /* Now get a vector type if the def is internal, otherwise supply
14801 NULL_TREE and leave it up to the caller to figure out a proper
14802 type for the use stmt. */
14803 if (*dt == vect_internal_def
14804 || *dt == vect_induction_def
14805 || *dt == vect_reduction_def
14806 || *dt == vect_double_reduction_def
14807 || *dt == vect_nested_cycle
14808 || *dt == vect_first_order_recurrence)
14809 {
14810 *vectype = STMT_VINFO_VECTYPE (def_stmt_info);
14811 gcc_assert (*vectype != NULL_TREE);
14812 if (dump_enabled_p ())
14813 dump_printf_loc (MSG_NOTE, vect_location,
14814 "vect_is_simple_use: vectype %T\n", *vectype);
14815 }
14816 else if (*dt == vect_uninitialized_def
14817 || *dt == vect_constant_def
14818 || *dt == vect_external_def)
14819 *vectype = NULL_TREE;
14820 else
14821 gcc_unreachable ();
14822
14823 return true;
14824 }
14825
14826 /* Function vect_is_simple_use.
14827
14828 Same as vect_is_simple_use but determines the operand by operand
14829 position OPERAND from either STMT or SLP_NODE, filling in *OP
14830 and *SLP_DEF (when SLP_NODE is not NULL). */
14831
14832 bool
14833 vect_is_simple_use (vec_info *vinfo, stmt_vec_info stmt, slp_tree slp_node,
14834 unsigned operand, tree *op, slp_tree *slp_def,
14835 enum vect_def_type *dt,
14836 tree *vectype, stmt_vec_info *def_stmt_info_out)
14837 {
14838 if (slp_node)
14839 {
14840 slp_tree child = SLP_TREE_CHILDREN (slp_node)[operand];
14841 *slp_def = child;
14842 *vectype = SLP_TREE_VECTYPE (child);
14843 if (SLP_TREE_DEF_TYPE (child) == vect_internal_def)
14844 {
14845 /* ??? VEC_PERM nodes might be intermediate and their lane value
14846 have no representative (nor do we build a VEC_PERM stmt for
14847 the actual operation). Note for two-operator nodes we set
14848 a representative but leave scalar stmts empty as we'd only
14849 have one for a subset of lanes. Ideally no caller would
14850 require *op for internal defs. */
14851 if (SLP_TREE_REPRESENTATIVE (child))
14852 {
14853 *op = gimple_get_lhs (SLP_TREE_REPRESENTATIVE (child)->stmt);
14854 return vect_is_simple_use (*op, vinfo, dt, def_stmt_info_out);
14855 }
14856 else
14857 {
14858 gcc_assert (SLP_TREE_CODE (child) == VEC_PERM_EXPR);
14859 *op = error_mark_node;
14860 *dt = vect_internal_def;
14861 if (def_stmt_info_out)
14862 *def_stmt_info_out = NULL;
14863 return true;
14864 }
14865 }
14866 else
14867 {
14868 if (def_stmt_info_out)
14869 *def_stmt_info_out = NULL;
14870 *op = SLP_TREE_SCALAR_OPS (child)[0];
14871 *dt = SLP_TREE_DEF_TYPE (child);
14872 return true;
14873 }
14874 }
14875 else
14876 {
14877 *slp_def = NULL;
14878 if (gassign *ass = dyn_cast <gassign *> (stmt->stmt))
14879 {
14880 if (gimple_assign_rhs_code (ass) == COND_EXPR
14881 && COMPARISON_CLASS_P (gimple_assign_rhs1 (ass)))
14882 gcc_unreachable ();
14883 else if (gimple_assign_rhs_code (ass) == VIEW_CONVERT_EXPR)
14884 *op = TREE_OPERAND (gimple_assign_rhs1 (ass), 0);
14885 else
14886 *op = gimple_op (ass, operand + 1);
14887 }
14888 else if (gcond *cond = dyn_cast <gcond *> (stmt->stmt))
14889 *op = gimple_op (cond, operand);
14890 else if (gcall *call = dyn_cast <gcall *> (stmt->stmt))
14891 *op = gimple_call_arg (call, operand);
14892 else
14893 gcc_unreachable ();
14894 return vect_is_simple_use (*op, vinfo, dt, vectype, def_stmt_info_out);
14895 }
14896 }
14897
14898 /* If OP is not NULL and is external or constant update its vector
14899 type with VECTYPE. Returns true if successful or false if not,
14900 for example when conflicting vector types are present. */
14901
14902 bool
14903 vect_maybe_update_slp_op_vectype (slp_tree op, tree vectype)
14904 {
14905 if (!op || SLP_TREE_DEF_TYPE (op) == vect_internal_def)
14906 return true;
14907 if (SLP_TREE_VECTYPE (op))
14908 return types_compatible_p (SLP_TREE_VECTYPE (op), vectype);
14909 /* For external defs refuse to produce VECTOR_BOOLEAN_TYPE_P, those
14910 should be handled by patters. Allow vect_constant_def for now
14911 as well as the trivial single-lane uniform vect_external_def case
14912 both of which we code-generate reasonably. */
14913 if (VECTOR_BOOLEAN_TYPE_P (vectype)
14914 && SLP_TREE_DEF_TYPE (op) == vect_external_def
14915 && SLP_TREE_LANES (op) > 1)
14916 return false;
14917 SLP_TREE_VECTYPE (op) = vectype;
14918 return true;
14919 }
14920
14921 /* Function supportable_widening_operation
14922
14923 Check whether an operation represented by the code CODE is a
14924 widening operation that is supported by the target platform in
14925 vector form (i.e., when operating on arguments of type VECTYPE_IN
14926 producing a result of type VECTYPE_OUT).
14927
14928 Widening operations we currently support are NOP (CONVERT), FLOAT,
14929 FIX_TRUNC and WIDEN_MULT. This function checks if these operations
14930 are supported by the target platform either directly (via vector
14931 tree-codes), or via target builtins.
14932
14933 When EVENODD_OK then also lane-swizzling operations are considered.
14934
14935 Output:
14936 - CODE1 and CODE2 are codes of vector operations to be used when
14937 vectorizing the operation, if available.
14938 - MULTI_STEP_CVT determines the number of required intermediate steps in
14939 case of multi-step conversion (like char->short->int - in that case
14940 MULTI_STEP_CVT will be 1).
14941 - INTERM_TYPES contains the intermediate type required to perform the
14942 widening operation (short in the above example). */
14943
14944 bool
14945 supportable_widening_operation (code_helper code,
14946 tree vectype_out, tree vectype_in,
14947 bool evenodd_ok,
14948 code_helper *code1,
14949 code_helper *code2,
14950 int *multi_step_cvt,
14951 vec<tree> *interm_types)
14952 {
14953 machine_mode vec_mode;
14954 enum insn_code icode1, icode2;
14955 optab optab1 = unknown_optab, optab2 = unknown_optab;
14956 tree vectype = vectype_in;
14957 tree wide_vectype = vectype_out;
14958 tree_code c1 = MAX_TREE_CODES, c2 = MAX_TREE_CODES;
14959 int i;
14960 tree prev_type, intermediate_type;
14961 machine_mode intermediate_mode, prev_mode;
14962 optab optab3, optab4;
14963
14964 *multi_step_cvt = 0;
14965
14966 switch (code.safe_as_tree_code ())
14967 {
14968 case MAX_TREE_CODES:
14969 /* Don't set c1 and c2 if code is not a tree_code. */
14970 break;
14971
14972 case WIDEN_MULT_EXPR:
14973 /* The result of a vectorized widening operation usually requires
14974 two vectors (because the widened results do not fit into one vector).
14975 The generated vector results would normally be expected to be
14976 generated in the same order as in the original scalar computation,
14977 i.e. if 8 results are generated in each vector iteration, they are
14978 to be organized as follows:
14979 vect1: [res1,res2,res3,res4],
14980 vect2: [res5,res6,res7,res8].
14981
14982 However, in the special case that the result of the widening
14983 operation is used in a reduction computation only, the order doesn't
14984 matter (because when vectorizing a reduction we change the order of
14985 the computation). Some targets can take advantage of this and
14986 generate more efficient code. For example, targets like Altivec,
14987 that support widen_mult using a sequence of {mult_even,mult_odd}
14988 generate the following vectors:
14989 vect1: [res1,res3,res5,res7],
14990 vect2: [res2,res4,res6,res8].
14991
14992 When vectorizing outer-loops, we execute the inner-loop sequentially
14993 (each vectorized inner-loop iteration contributes to VF outer-loop
14994 iterations in parallel). We therefore don't allow to change the
14995 order of the computation in the inner-loop during outer-loop
14996 vectorization. */
14997 /* TODO: Another case in which order doesn't *really* matter is when we
14998 widen and then contract again, e.g. (short)((int)x * y >> 8).
14999 Normally, pack_trunc performs an even/odd permute, whereas the
15000 repack from an even/odd expansion would be an interleave, which
15001 would be significantly simpler for e.g. AVX2. */
15002 /* In any case, in order to avoid duplicating the code below, recurse
15003 on VEC_WIDEN_MULT_EVEN_EXPR. If it succeeds, all the return values
15004 are properly set up for the caller. If we fail, we'll continue with
15005 a VEC_WIDEN_MULT_LO/HI_EXPR check. */
15006 if (evenodd_ok
15007 && supportable_widening_operation (VEC_WIDEN_MULT_EVEN_EXPR,
15008 vectype_out, vectype_in,
15009 evenodd_ok, code1,
15010 code2, multi_step_cvt,
15011 interm_types))
15012 return true;
15013 c1 = VEC_WIDEN_MULT_LO_EXPR;
15014 c2 = VEC_WIDEN_MULT_HI_EXPR;
15015 break;
15016
15017 case DOT_PROD_EXPR:
15018 c1 = DOT_PROD_EXPR;
15019 c2 = DOT_PROD_EXPR;
15020 break;
15021
15022 case SAD_EXPR:
15023 c1 = SAD_EXPR;
15024 c2 = SAD_EXPR;
15025 break;
15026
15027 case VEC_WIDEN_MULT_EVEN_EXPR:
15028 /* Support the recursion induced just above. */
15029 c1 = VEC_WIDEN_MULT_EVEN_EXPR;
15030 c2 = VEC_WIDEN_MULT_ODD_EXPR;
15031 break;
15032
15033 case WIDEN_LSHIFT_EXPR:
15034 c1 = VEC_WIDEN_LSHIFT_LO_EXPR;
15035 c2 = VEC_WIDEN_LSHIFT_HI_EXPR;
15036 break;
15037
15038 CASE_CONVERT:
15039 c1 = VEC_UNPACK_LO_EXPR;
15040 c2 = VEC_UNPACK_HI_EXPR;
15041 break;
15042
15043 case FLOAT_EXPR:
15044 c1 = VEC_UNPACK_FLOAT_LO_EXPR;
15045 c2 = VEC_UNPACK_FLOAT_HI_EXPR;
15046 break;
15047
15048 case FIX_TRUNC_EXPR:
15049 c1 = VEC_UNPACK_FIX_TRUNC_LO_EXPR;
15050 c2 = VEC_UNPACK_FIX_TRUNC_HI_EXPR;
15051 break;
15052
15053 default:
15054 gcc_unreachable ();
15055 }
15056
15057 if (BYTES_BIG_ENDIAN && c1 != VEC_WIDEN_MULT_EVEN_EXPR)
15058 std::swap (c1, c2);
15059
15060 if (code == FIX_TRUNC_EXPR)
15061 {
15062 /* The signedness is determined from output operand. */
15063 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
15064 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
15065 }
15066 else if (CONVERT_EXPR_CODE_P (code.safe_as_tree_code ())
15067 && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
15068 && VECTOR_BOOLEAN_TYPE_P (vectype)
15069 && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
15070 && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
15071 {
15072 /* If the input and result modes are the same, a different optab
15073 is needed where we pass in the number of units in vectype. */
15074 optab1 = vec_unpacks_sbool_lo_optab;
15075 optab2 = vec_unpacks_sbool_hi_optab;
15076 }
15077
15078 vec_mode = TYPE_MODE (vectype);
15079 if (widening_fn_p (code))
15080 {
15081 /* If this is an internal fn then we must check whether the target
15082 supports either a low-high split or an even-odd split. */
15083 internal_fn ifn = as_internal_fn ((combined_fn) code);
15084
15085 internal_fn lo, hi, even, odd;
15086 lookup_hilo_internal_fn (ifn, &lo, &hi);
15087 if (BYTES_BIG_ENDIAN)
15088 std::swap (lo, hi);
15089 *code1 = as_combined_fn (lo);
15090 *code2 = as_combined_fn (hi);
15091 optab1 = direct_internal_fn_optab (lo, {vectype, vectype});
15092 optab2 = direct_internal_fn_optab (hi, {vectype, vectype});
15093
15094 /* If we don't support low-high, then check for even-odd. */
15095 if (!optab1
15096 || (icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
15097 || !optab2
15098 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
15099 {
15100 lookup_evenodd_internal_fn (ifn, &even, &odd);
15101 *code1 = as_combined_fn (even);
15102 *code2 = as_combined_fn (odd);
15103 optab1 = direct_internal_fn_optab (even, {vectype, vectype});
15104 optab2 = direct_internal_fn_optab (odd, {vectype, vectype});
15105 }
15106 }
15107 else if (code.is_tree_code ())
15108 {
15109 if (code == FIX_TRUNC_EXPR)
15110 {
15111 /* The signedness is determined from output operand. */
15112 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
15113 optab2 = optab_for_tree_code (c2, vectype_out, optab_default);
15114 }
15115 else if (CONVERT_EXPR_CODE_P ((tree_code) code.safe_as_tree_code ())
15116 && VECTOR_BOOLEAN_TYPE_P (wide_vectype)
15117 && VECTOR_BOOLEAN_TYPE_P (vectype)
15118 && TYPE_MODE (wide_vectype) == TYPE_MODE (vectype)
15119 && SCALAR_INT_MODE_P (TYPE_MODE (vectype)))
15120 {
15121 /* If the input and result modes are the same, a different optab
15122 is needed where we pass in the number of units in vectype. */
15123 optab1 = vec_unpacks_sbool_lo_optab;
15124 optab2 = vec_unpacks_sbool_hi_optab;
15125 }
15126 else
15127 {
15128 optab1 = optab_for_tree_code (c1, vectype, optab_default);
15129 optab2 = optab_for_tree_code (c2, vectype, optab_default);
15130 }
15131 *code1 = c1;
15132 *code2 = c2;
15133 }
15134
15135 if (!optab1 || !optab2)
15136 return false;
15137
15138 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing
15139 || (icode2 = optab_handler (optab2, vec_mode)) == CODE_FOR_nothing)
15140 return false;
15141
15142
15143 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
15144 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
15145 {
15146 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
15147 return true;
15148 /* For scalar masks we may have different boolean
15149 vector types having the same QImode. Thus we
15150 add additional check for elements number. */
15151 if (known_eq (TYPE_VECTOR_SUBPARTS (vectype),
15152 TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
15153 return true;
15154 }
15155
15156 /* Check if it's a multi-step conversion that can be done using intermediate
15157 types. */
15158
15159 prev_type = vectype;
15160 prev_mode = vec_mode;
15161
15162 if (!CONVERT_EXPR_CODE_P (code.safe_as_tree_code ()))
15163 return false;
15164
15165 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
15166 intermediate steps in promotion sequence. We try
15167 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do
15168 not. */
15169 interm_types->create (MAX_INTERM_CVT_STEPS);
15170 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
15171 {
15172 intermediate_mode = insn_data[icode1].operand[0].mode;
15173 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
15174 intermediate_type
15175 = vect_halve_mask_nunits (prev_type, intermediate_mode);
15176 else if (VECTOR_MODE_P (intermediate_mode))
15177 {
15178 tree intermediate_element_type
15179 = lang_hooks.types.type_for_mode (GET_MODE_INNER (intermediate_mode),
15180 TYPE_UNSIGNED (prev_type));
15181 intermediate_type
15182 = build_vector_type_for_mode (intermediate_element_type,
15183 intermediate_mode);
15184 }
15185 else
15186 intermediate_type
15187 = lang_hooks.types.type_for_mode (intermediate_mode,
15188 TYPE_UNSIGNED (prev_type));
15189
15190 if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
15191 && VECTOR_BOOLEAN_TYPE_P (prev_type)
15192 && intermediate_mode == prev_mode
15193 && SCALAR_INT_MODE_P (prev_mode))
15194 {
15195 /* If the input and result modes are the same, a different optab
15196 is needed where we pass in the number of units in vectype. */
15197 optab3 = vec_unpacks_sbool_lo_optab;
15198 optab4 = vec_unpacks_sbool_hi_optab;
15199 }
15200 else
15201 {
15202 optab3 = optab_for_tree_code (c1, intermediate_type, optab_default);
15203 optab4 = optab_for_tree_code (c2, intermediate_type, optab_default);
15204 }
15205
15206 if (!optab3 || !optab4
15207 || (icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing
15208 || insn_data[icode1].operand[0].mode != intermediate_mode
15209 || (icode2 = optab_handler (optab2, prev_mode)) == CODE_FOR_nothing
15210 || insn_data[icode2].operand[0].mode != intermediate_mode
15211 || ((icode1 = optab_handler (optab3, intermediate_mode))
15212 == CODE_FOR_nothing)
15213 || ((icode2 = optab_handler (optab4, intermediate_mode))
15214 == CODE_FOR_nothing))
15215 break;
15216
15217 interm_types->quick_push (intermediate_type);
15218 (*multi_step_cvt)++;
15219
15220 if (insn_data[icode1].operand[0].mode == TYPE_MODE (wide_vectype)
15221 && insn_data[icode2].operand[0].mode == TYPE_MODE (wide_vectype))
15222 {
15223 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
15224 return true;
15225 if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type),
15226 TYPE_VECTOR_SUBPARTS (wide_vectype) * 2))
15227 return true;
15228 }
15229
15230 prev_type = intermediate_type;
15231 prev_mode = intermediate_mode;
15232 }
15233
15234 interm_types->release ();
15235 return false;
15236 }
15237
15238
15239 /* Function supportable_narrowing_operation
15240
15241 Check whether an operation represented by the code CODE is a
15242 narrowing operation that is supported by the target platform in
15243 vector form (i.e., when operating on arguments of type VECTYPE_IN
15244 and producing a result of type VECTYPE_OUT).
15245
15246 Narrowing operations we currently support are NOP (CONVERT), FIX_TRUNC
15247 and FLOAT. This function checks if these operations are supported by
15248 the target platform directly via vector tree-codes.
15249
15250 Output:
15251 - CODE1 is the code of a vector operation to be used when
15252 vectorizing the operation, if available.
15253 - MULTI_STEP_CVT determines the number of required intermediate steps in
15254 case of multi-step conversion (like int->short->char - in that case
15255 MULTI_STEP_CVT will be 1).
15256 - INTERM_TYPES contains the intermediate type required to perform the
15257 narrowing operation (short in the above example). */
15258
15259 bool
15260 supportable_narrowing_operation (code_helper code,
15261 tree vectype_out, tree vectype_in,
15262 code_helper *code1, int *multi_step_cvt,
15263 vec<tree> *interm_types)
15264 {
15265 machine_mode vec_mode;
15266 enum insn_code icode1;
15267 optab optab1, interm_optab;
15268 tree vectype = vectype_in;
15269 tree narrow_vectype = vectype_out;
15270 enum tree_code c1;
15271 tree intermediate_type, prev_type;
15272 machine_mode intermediate_mode, prev_mode;
15273 int i;
15274 unsigned HOST_WIDE_INT n_elts;
15275 bool uns;
15276
15277 if (!code.is_tree_code ())
15278 return false;
15279
15280 *multi_step_cvt = 0;
15281 switch ((tree_code) code)
15282 {
15283 CASE_CONVERT:
15284 c1 = VEC_PACK_TRUNC_EXPR;
15285 if (VECTOR_BOOLEAN_TYPE_P (narrow_vectype)
15286 && VECTOR_BOOLEAN_TYPE_P (vectype)
15287 && SCALAR_INT_MODE_P (TYPE_MODE (vectype))
15288 && TYPE_VECTOR_SUBPARTS (vectype).is_constant (&n_elts)
15289 && n_elts < BITS_PER_UNIT)
15290 optab1 = vec_pack_sbool_trunc_optab;
15291 else
15292 optab1 = optab_for_tree_code (c1, vectype, optab_default);
15293 break;
15294
15295 case FIX_TRUNC_EXPR:
15296 c1 = VEC_PACK_FIX_TRUNC_EXPR;
15297 /* The signedness is determined from output operand. */
15298 optab1 = optab_for_tree_code (c1, vectype_out, optab_default);
15299 break;
15300
15301 case FLOAT_EXPR:
15302 c1 = VEC_PACK_FLOAT_EXPR;
15303 optab1 = optab_for_tree_code (c1, vectype, optab_default);
15304 break;
15305
15306 default:
15307 gcc_unreachable ();
15308 }
15309
15310 if (!optab1)
15311 return false;
15312
15313 vec_mode = TYPE_MODE (vectype);
15314 if ((icode1 = optab_handler (optab1, vec_mode)) == CODE_FOR_nothing)
15315 return false;
15316
15317 *code1 = c1;
15318
15319 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
15320 {
15321 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
15322 return true;
15323 /* For scalar masks we may have different boolean
15324 vector types having the same QImode. Thus we
15325 add additional check for elements number. */
15326 if (known_eq (TYPE_VECTOR_SUBPARTS (vectype) * 2,
15327 TYPE_VECTOR_SUBPARTS (narrow_vectype)))
15328 return true;
15329 }
15330
15331 if (code == FLOAT_EXPR)
15332 return false;
15333
15334 /* Check if it's a multi-step conversion that can be done using intermediate
15335 types. */
15336 prev_mode = vec_mode;
15337 prev_type = vectype;
15338 if (code == FIX_TRUNC_EXPR)
15339 uns = TYPE_UNSIGNED (vectype_out);
15340 else
15341 uns = TYPE_UNSIGNED (vectype);
15342
15343 /* For multi-step FIX_TRUNC_EXPR prefer signed floating to integer
15344 conversion over unsigned, as unsigned FIX_TRUNC_EXPR is often more
15345 costly than signed. */
15346 if (code == FIX_TRUNC_EXPR && uns)
15347 {
15348 enum insn_code icode2;
15349
15350 intermediate_type
15351 = lang_hooks.types.type_for_mode (TYPE_MODE (vectype_out), 0);
15352 interm_optab
15353 = optab_for_tree_code (c1, intermediate_type, optab_default);
15354 if (interm_optab != unknown_optab
15355 && (icode2 = optab_handler (optab1, vec_mode)) != CODE_FOR_nothing
15356 && insn_data[icode1].operand[0].mode
15357 == insn_data[icode2].operand[0].mode)
15358 {
15359 uns = false;
15360 optab1 = interm_optab;
15361 icode1 = icode2;
15362 }
15363 }
15364
15365 /* We assume here that there will not be more than MAX_INTERM_CVT_STEPS
15366 intermediate steps in promotion sequence. We try
15367 MAX_INTERM_CVT_STEPS to get to NARROW_VECTYPE, and fail if we do not. */
15368 interm_types->create (MAX_INTERM_CVT_STEPS);
15369 for (i = 0; i < MAX_INTERM_CVT_STEPS; i++)
15370 {
15371 intermediate_mode = insn_data[icode1].operand[0].mode;
15372 if (VECTOR_BOOLEAN_TYPE_P (prev_type))
15373 intermediate_type
15374 = vect_double_mask_nunits (prev_type, intermediate_mode);
15375 else
15376 intermediate_type
15377 = lang_hooks.types.type_for_mode (intermediate_mode, uns);
15378 if (VECTOR_BOOLEAN_TYPE_P (intermediate_type)
15379 && VECTOR_BOOLEAN_TYPE_P (prev_type)
15380 && SCALAR_INT_MODE_P (prev_mode)
15381 && TYPE_VECTOR_SUBPARTS (intermediate_type).is_constant (&n_elts)
15382 && n_elts < BITS_PER_UNIT)
15383 interm_optab = vec_pack_sbool_trunc_optab;
15384 else
15385 interm_optab
15386 = optab_for_tree_code (VEC_PACK_TRUNC_EXPR, intermediate_type,
15387 optab_default);
15388 if (!interm_optab
15389 || ((icode1 = optab_handler (optab1, prev_mode)) == CODE_FOR_nothing)
15390 || insn_data[icode1].operand[0].mode != intermediate_mode
15391 || ((icode1 = optab_handler (interm_optab, intermediate_mode))
15392 == CODE_FOR_nothing))
15393 break;
15394
15395 interm_types->quick_push (intermediate_type);
15396 (*multi_step_cvt)++;
15397
15398 if (insn_data[icode1].operand[0].mode == TYPE_MODE (narrow_vectype))
15399 {
15400 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
15401 return true;
15402 if (known_eq (TYPE_VECTOR_SUBPARTS (intermediate_type) * 2,
15403 TYPE_VECTOR_SUBPARTS (narrow_vectype)))
15404 return true;
15405 }
15406
15407 prev_mode = intermediate_mode;
15408 prev_type = intermediate_type;
15409 optab1 = interm_optab;
15410 }
15411
15412 interm_types->release ();
15413 return false;
15414 }
15415
15416 /* Function supportable_indirect_convert_operation
15417
15418 Check whether an operation represented by the code CODE is single or multi
15419 operations that are supported by the target platform in
15420 vector form (i.e., when operating on arguments of type VECTYPE_IN
15421 producing a result of type VECTYPE_OUT).
15422
15423 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
15424 This function checks if these operations are supported
15425 by the target platform directly (via vector tree-codes).
15426
15427 Output:
15428 - converts contains some pairs to perform the convert operation,
15429 the pair's first is the intermediate type, and its second is the code of
15430 a vector operation to be used when converting the operation from the
15431 previous type to the intermediate type. */
15432 bool
15433 supportable_indirect_convert_operation (code_helper code,
15434 tree vectype_out,
15435 tree vectype_in,
15436 vec<std::pair<tree, tree_code> > &converts,
15437 tree op0, slp_tree slp_op0)
15438 {
15439 bool found_mode = false;
15440 scalar_mode lhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_out));
15441 scalar_mode rhs_mode = GET_MODE_INNER (TYPE_MODE (vectype_in));
15442 opt_scalar_mode mode_iter;
15443 tree_code tc1, tc2, code1, code2;
15444
15445 tree cvt_type = NULL_TREE;
15446 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (vectype_in);
15447
15448 if (supportable_convert_operation ((tree_code) code,
15449 vectype_out,
15450 vectype_in,
15451 &tc1))
15452 {
15453 converts.safe_push (std::make_pair (vectype_out, tc1));
15454 return true;
15455 }
15456
15457 /* For conversions between float and integer types try whether
15458 we can use intermediate signed integer types to support the
15459 conversion. */
15460 if (GET_MODE_SIZE (lhs_mode) != GET_MODE_SIZE (rhs_mode)
15461 && (code == FLOAT_EXPR
15462 || (code == FIX_TRUNC_EXPR && !flag_trapping_math)))
15463 {
15464 bool demotion = GET_MODE_SIZE (rhs_mode) > GET_MODE_SIZE (lhs_mode);
15465 bool float_expr_p = code == FLOAT_EXPR;
15466 unsigned short target_size;
15467 scalar_mode intermediate_mode;
15468 if (demotion)
15469 {
15470 intermediate_mode = lhs_mode;
15471 target_size = GET_MODE_SIZE (rhs_mode);
15472 }
15473 else
15474 {
15475 target_size = GET_MODE_SIZE (lhs_mode);
15476 if (!int_mode_for_size
15477 (GET_MODE_BITSIZE (rhs_mode), 0).exists (&intermediate_mode))
15478 return false;
15479 }
15480 code1 = float_expr_p ? (tree_code) code : NOP_EXPR;
15481 code2 = float_expr_p ? NOP_EXPR : (tree_code) code;
15482 opt_scalar_mode mode_iter;
15483 FOR_EACH_2XWIDER_MODE (mode_iter, intermediate_mode)
15484 {
15485 intermediate_mode = mode_iter.require ();
15486
15487 if (GET_MODE_SIZE (intermediate_mode) > target_size)
15488 break;
15489
15490 scalar_mode cvt_mode;
15491 if (!int_mode_for_size
15492 (GET_MODE_BITSIZE (intermediate_mode), 0).exists (&cvt_mode))
15493 break;
15494
15495 cvt_type = build_nonstandard_integer_type
15496 (GET_MODE_BITSIZE (cvt_mode), 0);
15497
15498 /* Check if the intermediate type can hold OP0's range.
15499 When converting from float to integer this is not necessary
15500 because values that do not fit the (smaller) target type are
15501 unspecified anyway. */
15502 if (demotion && float_expr_p)
15503 {
15504 wide_int op_min_value, op_max_value;
15505 /* For vector form, it looks like op0 doesn't have RANGE_INFO.
15506 In the future, if it is supported, changes may need to be made
15507 to this part, such as checking the RANGE of each element
15508 in the vector. */
15509 if (slp_op0)
15510 {
15511 tree def;
15512 /* ??? Merge ranges in case of more than one lane. */
15513 if (SLP_TREE_LANES (slp_op0) != 1
15514 || !(def = vect_get_slp_scalar_def (slp_op0, 0))
15515 || !vect_get_range_info (def,
15516 &op_min_value, &op_max_value))
15517 break;
15518 }
15519 else if (!op0
15520 || TREE_CODE (op0) != SSA_NAME
15521 || !SSA_NAME_RANGE_INFO (op0)
15522 || !vect_get_range_info (op0, &op_min_value,
15523 &op_max_value))
15524 break;
15525
15526 if (cvt_type == NULL_TREE
15527 || (wi::min_precision (op_max_value, SIGNED)
15528 > TYPE_PRECISION (cvt_type))
15529 || (wi::min_precision (op_min_value, SIGNED)
15530 > TYPE_PRECISION (cvt_type)))
15531 continue;
15532 }
15533
15534 cvt_type = get_related_vectype_for_scalar_type (TYPE_MODE (vectype_in),
15535 cvt_type,
15536 nelts);
15537 /* This should only happened for SLP as long as loop vectorizer
15538 only supports same-sized vector. */
15539 if (cvt_type == NULL_TREE
15540 || maybe_ne (TYPE_VECTOR_SUBPARTS (cvt_type), nelts)
15541 || !supportable_convert_operation ((tree_code) code1,
15542 vectype_out,
15543 cvt_type, &tc1)
15544 || !supportable_convert_operation ((tree_code) code2,
15545 cvt_type,
15546 vectype_in, &tc2))
15547 continue;
15548
15549 found_mode = true;
15550 break;
15551 }
15552
15553 if (found_mode)
15554 {
15555 converts.safe_push (std::make_pair (cvt_type, tc2));
15556 if (TYPE_MODE (cvt_type) != TYPE_MODE (vectype_out))
15557 converts.safe_push (std::make_pair (vectype_out, tc1));
15558 return true;
15559 }
15560 }
15561 return false;
15562 }
15563
15564 /* Generate and return a vector mask of MASK_TYPE such that
15565 mask[I] is true iff J + START_INDEX < END_INDEX for all J <= I.
15566 Add the statements to SEQ. */
15567
15568 tree
15569 vect_gen_while (gimple_seq *seq, tree mask_type, tree start_index,
15570 tree end_index, const char *name)
15571 {
15572 tree cmp_type = TREE_TYPE (start_index);
15573 gcc_checking_assert (direct_internal_fn_supported_p (IFN_WHILE_ULT,
15574 cmp_type, mask_type,
15575 OPTIMIZE_FOR_SPEED));
15576 gcall *call = gimple_build_call_internal (IFN_WHILE_ULT, 3,
15577 start_index, end_index,
15578 build_zero_cst (mask_type));
15579 tree tmp;
15580 if (name)
15581 tmp = make_temp_ssa_name (mask_type, NULL, name);
15582 else
15583 tmp = make_ssa_name (mask_type);
15584 gimple_call_set_lhs (call, tmp);
15585 gimple_seq_add_stmt (seq, call);
15586 return tmp;
15587 }
15588
15589 /* Generate a vector mask of type MASK_TYPE for which index I is false iff
15590 J + START_INDEX < END_INDEX for all J <= I. Add the statements to SEQ. */
15591
15592 tree
15593 vect_gen_while_not (gimple_seq *seq, tree mask_type, tree start_index,
15594 tree end_index)
15595 {
15596 tree tmp = vect_gen_while (seq, mask_type, start_index, end_index);
15597 return gimple_build (seq, BIT_NOT_EXPR, mask_type, tmp);
15598 }
15599
15600 /* Try to compute the vector types required to vectorize STMT_INFO,
15601 returning true on success and false if vectorization isn't possible.
15602 If GROUP_SIZE is nonzero and we're performing BB vectorization,
15603 take sure that the number of elements in the vectors is no bigger
15604 than GROUP_SIZE.
15605
15606 On success:
15607
15608 - Set *STMT_VECTYPE_OUT to:
15609 - NULL_TREE if the statement doesn't need to be vectorized;
15610 - the equivalent of STMT_VINFO_VECTYPE otherwise.
15611
15612 - Set *NUNITS_VECTYPE_OUT to the vector type that contains the maximum
15613 number of units needed to vectorize STMT_INFO, or NULL_TREE if the
15614 statement does not help to determine the overall number of units. */
15615
15616 opt_result
15617 vect_get_vector_types_for_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
15618 tree *stmt_vectype_out,
15619 tree *nunits_vectype_out,
15620 unsigned int group_size)
15621 {
15622 gimple *stmt = stmt_info->stmt;
15623
15624 /* For BB vectorization, we should always have a group size once we've
15625 constructed the SLP tree; the only valid uses of zero GROUP_SIZEs
15626 are tentative requests during things like early data reference
15627 analysis and pattern recognition. */
15628 if (is_a <bb_vec_info> (vinfo))
15629 gcc_assert (vinfo->slp_instances.is_empty () || group_size != 0);
15630 else
15631 group_size = 0;
15632
15633 *stmt_vectype_out = NULL_TREE;
15634 *nunits_vectype_out = NULL_TREE;
15635
15636 if (gimple_get_lhs (stmt) == NULL_TREE
15637 /* Allow vector conditionals through here. */
15638 && !is_a <gcond *> (stmt)
15639 /* MASK_STORE and friends have no lhs, but are ok. */
15640 && !(is_gimple_call (stmt)
15641 && gimple_call_internal_p (stmt)
15642 && internal_store_fn_p (gimple_call_internal_fn (stmt))))
15643 {
15644 if (is_a <gcall *> (stmt))
15645 {
15646 /* Ignore calls with no lhs. These must be calls to
15647 #pragma omp simd functions, and what vectorization factor
15648 it really needs can't be determined until
15649 vectorizable_simd_clone_call. */
15650 if (dump_enabled_p ())
15651 dump_printf_loc (MSG_NOTE, vect_location,
15652 "defer to SIMD clone analysis.\n");
15653 return opt_result::success ();
15654 }
15655
15656 return opt_result::failure_at (stmt,
15657 "not vectorized: irregular stmt: %G", stmt);
15658 }
15659
15660 tree vectype;
15661 tree scalar_type = NULL_TREE;
15662 if (group_size == 0 && STMT_VINFO_VECTYPE (stmt_info))
15663 {
15664 vectype = STMT_VINFO_VECTYPE (stmt_info);
15665 if (dump_enabled_p ())
15666 dump_printf_loc (MSG_NOTE, vect_location,
15667 "precomputed vectype: %T\n", vectype);
15668 }
15669 else if (vect_use_mask_type_p (stmt_info))
15670 {
15671 unsigned int precision = stmt_info->mask_precision;
15672 scalar_type = build_nonstandard_integer_type (precision, 1);
15673 vectype = get_mask_type_for_scalar_type (vinfo, scalar_type, group_size);
15674 if (!vectype)
15675 return opt_result::failure_at (stmt, "not vectorized: unsupported"
15676 " data-type %T\n", scalar_type);
15677 if (dump_enabled_p ())
15678 dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
15679 }
15680 else
15681 {
15682 /* If we got here with a gcond it means that the target had no available vector
15683 mode for the scalar type. We can't vectorize so abort. */
15684 if (is_a <gcond *> (stmt))
15685 return opt_result::failure_at (stmt,
15686 "not vectorized:"
15687 " unsupported data-type for gcond %T\n",
15688 scalar_type);
15689
15690 if (data_reference *dr = STMT_VINFO_DATA_REF (stmt_info))
15691 scalar_type = TREE_TYPE (DR_REF (dr));
15692 else
15693 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
15694
15695 if (dump_enabled_p ())
15696 {
15697 if (group_size)
15698 dump_printf_loc (MSG_NOTE, vect_location,
15699 "get vectype for scalar type (group size %d):"
15700 " %T\n", group_size, scalar_type);
15701 else
15702 dump_printf_loc (MSG_NOTE, vect_location,
15703 "get vectype for scalar type: %T\n", scalar_type);
15704 }
15705 vectype = get_vectype_for_scalar_type (vinfo, scalar_type, group_size);
15706 if (!vectype)
15707 return opt_result::failure_at (stmt,
15708 "not vectorized:"
15709 " unsupported data-type %T\n",
15710 scalar_type);
15711
15712 if (dump_enabled_p ())
15713 dump_printf_loc (MSG_NOTE, vect_location, "vectype: %T\n", vectype);
15714 }
15715
15716 if (scalar_type && VECTOR_MODE_P (TYPE_MODE (scalar_type)))
15717 return opt_result::failure_at (stmt,
15718 "not vectorized: vector stmt in loop:%G",
15719 stmt);
15720
15721 *stmt_vectype_out = vectype;
15722
15723 /* Don't try to compute scalar types if the stmt produces a boolean
15724 vector; use the existing vector type instead. */
15725 tree nunits_vectype = vectype;
15726 if (!VECTOR_BOOLEAN_TYPE_P (vectype))
15727 {
15728 /* The number of units is set according to the smallest scalar
15729 type (or the largest vector size, but we only support one
15730 vector size per vectorization). */
15731 scalar_type = vect_get_smallest_scalar_type (stmt_info,
15732 TREE_TYPE (vectype));
15733 if (!types_compatible_p (scalar_type, TREE_TYPE (vectype)))
15734 {
15735 if (dump_enabled_p ())
15736 dump_printf_loc (MSG_NOTE, vect_location,
15737 "get vectype for smallest scalar type: %T\n",
15738 scalar_type);
15739 nunits_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
15740 group_size);
15741 if (!nunits_vectype)
15742 return opt_result::failure_at
15743 (stmt, "not vectorized: unsupported data-type %T\n",
15744 scalar_type);
15745 if (dump_enabled_p ())
15746 dump_printf_loc (MSG_NOTE, vect_location, "nunits vectype: %T\n",
15747 nunits_vectype);
15748 }
15749 }
15750
15751 if (!multiple_p (TYPE_VECTOR_SUBPARTS (nunits_vectype),
15752 TYPE_VECTOR_SUBPARTS (*stmt_vectype_out)))
15753 return opt_result::failure_at (stmt,
15754 "Not vectorized: Incompatible number "
15755 "of vector subparts between %T and %T\n",
15756 nunits_vectype, *stmt_vectype_out);
15757
15758 if (dump_enabled_p ())
15759 {
15760 dump_printf_loc (MSG_NOTE, vect_location, "nunits = ");
15761 dump_dec (MSG_NOTE, TYPE_VECTOR_SUBPARTS (nunits_vectype));
15762 dump_printf (MSG_NOTE, "\n");
15763 }
15764
15765 *nunits_vectype_out = nunits_vectype;
15766 return opt_result::success ();
15767 }
15768
15769 /* Generate and return statement sequence that sets vector length LEN that is:
15770
15771 min_of_start_and_end = min (START_INDEX, END_INDEX);
15772 left_len = END_INDEX - min_of_start_and_end;
15773 rhs = min (left_len, LEN_LIMIT);
15774 LEN = rhs;
15775
15776 Note: the cost of the code generated by this function is modeled
15777 by vect_estimate_min_profitable_iters, so changes here may need
15778 corresponding changes there. */
15779
15780 gimple_seq
15781 vect_gen_len (tree len, tree start_index, tree end_index, tree len_limit)
15782 {
15783 gimple_seq stmts = NULL;
15784 tree len_type = TREE_TYPE (len);
15785 gcc_assert (TREE_TYPE (start_index) == len_type);
15786
15787 tree min = gimple_build (&stmts, MIN_EXPR, len_type, start_index, end_index);
15788 tree left_len = gimple_build (&stmts, MINUS_EXPR, len_type, end_index, min);
15789 tree rhs = gimple_build (&stmts, MIN_EXPR, len_type, left_len, len_limit);
15790 gimple* stmt = gimple_build_assign (len, rhs);
15791 gimple_seq_add_stmt (&stmts, stmt);
15792
15793 return stmts;
15794 }
15795