Line data Source code
1 : /* Vectorizer
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
3 : Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : /* Loop and basic block vectorizer.
22 :
23 : This file contains drivers for the three vectorizers:
24 : (1) loop vectorizer (inter-iteration parallelism),
25 : (2) loop-aware SLP (intra-iteration parallelism) (invoked by the loop
26 : vectorizer)
27 : (3) BB vectorizer (out-of-loops), aka SLP
28 :
29 : The rest of the vectorizer's code is organized as follows:
30 : - tree-vect-loop.cc - loop specific parts such as reductions, etc. These are
31 : used by drivers (1) and (2).
32 : - tree-vect-loop-manip.cc - vectorizer's loop control-flow utilities, used by
33 : drivers (1) and (2).
34 : - tree-vect-slp.cc - BB vectorization specific analysis and transformation,
35 : used by drivers (2) and (3).
36 : - tree-vect-stmts.cc - statements analysis and transformation (used by all).
37 : - tree-vect-data-refs.cc - vectorizer specific data-refs analysis and
38 : manipulations (used by all).
39 : - tree-vect-patterns.cc - vectorizable code patterns detector (used by all)
40 :
41 : Here's a poor attempt at illustrating that:
42 :
43 : tree-vectorizer.cc:
44 : loop_vect() loop_aware_slp() slp_vect()
45 : | / \ /
46 : | / \ /
47 : tree-vect-loop.cc tree-vect-slp.cc
48 : | \ \ / / |
49 : | \ \/ / |
50 : | \ /\ / |
51 : | \ / \ / |
52 : tree-vect-stmts.cc tree-vect-data-refs.cc
53 : \ /
54 : tree-vect-patterns.cc
55 : */
56 :
57 : #include "config.h"
58 : #include "system.h"
59 : #include "coretypes.h"
60 : #include "backend.h"
61 : #include "tree.h"
62 : #include "gimple.h"
63 : #include "predict.h"
64 : #include "tree-pass.h"
65 : #include "ssa.h"
66 : #include "cgraph.h"
67 : #include "fold-const.h"
68 : #include "stor-layout.h"
69 : #include "gimple-iterator.h"
70 : #include "gimple-walk.h"
71 : #include "tree-ssa-loop-manip.h"
72 : #include "tree-ssa-loop-niter.h"
73 : #include "tree-cfg.h"
74 : #include "cfgloop.h"
75 : #include "tree-vectorizer.h"
76 : #include "tree-ssa-propagate.h"
77 : #include "dbgcnt.h"
78 : #include "tree-scalar-evolution.h"
79 : #include "stringpool.h"
80 : #include "attribs.h"
81 : #include "gimple-pretty-print.h"
82 : #include "opt-problem.h"
83 : #include "internal-fn.h"
84 : #include "tree-ssa-sccvn.h"
85 : #include "tree-into-ssa.h"
86 : #include "gimple-range.h"
87 :
88 : /* Loop or bb location, with hotness information. */
89 : dump_user_location_t vect_location;
90 :
91 : /* auto_purge_vect_location's dtor: reset the vect_location
92 : global, to avoid stale location_t values that could reference
93 : GC-ed blocks. */
94 :
95 1419630 : auto_purge_vect_location::~auto_purge_vect_location ()
96 : {
97 1419630 : vect_location = dump_user_location_t ();
98 1419630 : }
99 :
100 : /* Dump a cost entry according to args to F. */
101 :
102 : void
103 238838 : dump_stmt_cost (FILE *f, int count, enum vect_cost_for_stmt kind,
104 : stmt_vec_info stmt_info, slp_tree node, tree,
105 : int misalign, unsigned cost,
106 : enum vect_cost_model_location where)
107 : {
108 238838 : if (stmt_info)
109 : {
110 215708 : print_gimple_expr (f, STMT_VINFO_STMT (stmt_info), 0, TDF_SLIM);
111 215708 : fprintf (f, " ");
112 : }
113 23130 : else if (node)
114 22424 : fprintf (f, "node %p ", (void *)node);
115 : else
116 706 : fprintf (f, "<unknown> ");
117 238838 : fprintf (f, "%d times ", count);
118 238838 : const char *ks = "unknown";
119 238838 : switch (kind)
120 : {
121 54750 : case scalar_stmt:
122 54750 : ks = "scalar_stmt";
123 54750 : break;
124 39746 : case scalar_load:
125 39746 : ks = "scalar_load";
126 39746 : break;
127 29650 : case scalar_store:
128 29650 : ks = "scalar_store";
129 29650 : break;
130 38491 : case vector_stmt:
131 38491 : ks = "vector_stmt";
132 38491 : break;
133 23761 : case vector_load:
134 23761 : ks = "vector_load";
135 23761 : break;
136 0 : case vector_gather_load:
137 0 : ks = "vector_gather_load";
138 0 : break;
139 8493 : case unaligned_load:
140 8493 : ks = "unaligned_load";
141 8493 : break;
142 5157 : case unaligned_store:
143 5157 : ks = "unaligned_store";
144 5157 : break;
145 9354 : case vector_store:
146 9354 : ks = "vector_store";
147 9354 : break;
148 0 : case vector_scatter_store:
149 0 : ks = "vector_scatter_store";
150 0 : break;
151 3828 : case vec_to_scalar:
152 3828 : ks = "vec_to_scalar";
153 3828 : break;
154 10064 : case scalar_to_vec:
155 10064 : ks = "scalar_to_vec";
156 10064 : break;
157 18 : case cond_branch_not_taken:
158 18 : ks = "cond_branch_not_taken";
159 18 : break;
160 656 : case cond_branch_taken:
161 656 : ks = "cond_branch_taken";
162 656 : break;
163 6777 : case vec_perm:
164 6777 : ks = "vec_perm";
165 6777 : break;
166 5651 : case vec_promote_demote:
167 5651 : ks = "vec_promote_demote";
168 5651 : break;
169 1247 : case vec_construct:
170 1247 : ks = "vec_construct";
171 1247 : break;
172 1195 : case vec_deconstruct:
173 1195 : ks = "vec_deconstruct";
174 1195 : break;
175 : }
176 238838 : fprintf (f, "%s ", ks);
177 238838 : if (kind == unaligned_load || kind == unaligned_store)
178 13650 : fprintf (f, "(misalign %d) ", misalign);
179 238838 : fprintf (f, "costs %u ", cost);
180 238838 : const char *ws = "unknown";
181 238838 : switch (where)
182 : {
183 25370 : case vect_prologue:
184 25370 : ws = "prologue";
185 25370 : break;
186 203947 : case vect_body:
187 203947 : ws = "body";
188 203947 : break;
189 9521 : case vect_epilogue:
190 9521 : ws = "epilogue";
191 9521 : break;
192 : }
193 238838 : fprintf (f, "in %s\n", ws);
194 238838 : }
195 :
196 : /* For mapping simduid to vectorization factor. */
197 :
198 : class simduid_to_vf : public free_ptr_hash<simduid_to_vf>
199 : {
200 : public:
201 : unsigned int simduid;
202 : poly_uint64 vf;
203 :
204 : /* hash_table support. */
205 : static inline hashval_t hash (const simduid_to_vf *);
206 : static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
207 : };
208 :
209 : inline hashval_t
210 8112 : simduid_to_vf::hash (const simduid_to_vf *p)
211 : {
212 8112 : return p->simduid;
213 : }
214 :
215 : inline int
216 15918 : simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
217 : {
218 15918 : return p1->simduid == p2->simduid;
219 : }
220 :
221 : /* This hash maps the OMP simd array to the corresponding simduid used
222 : to index into it. Like thus,
223 :
224 : _7 = GOMP_SIMD_LANE (simduid.0)
225 : ...
226 : ...
227 : D.1737[_7] = stuff;
228 :
229 :
230 : This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
231 : simduid.0. */
232 :
233 : struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
234 : {
235 : tree decl;
236 : unsigned int simduid;
237 :
238 : /* hash_table support. */
239 : static inline hashval_t hash (const simd_array_to_simduid *);
240 : static inline int equal (const simd_array_to_simduid *,
241 : const simd_array_to_simduid *);
242 : };
243 :
244 : inline hashval_t
245 27536 : simd_array_to_simduid::hash (const simd_array_to_simduid *p)
246 : {
247 27536 : return DECL_UID (p->decl);
248 : }
249 :
250 : inline int
251 18856 : simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
252 : const simd_array_to_simduid *p2)
253 : {
254 18856 : return p1->decl == p2->decl;
255 : }
256 :
257 : /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LAST_LANE,
258 : into their corresponding constants and remove
259 : IFN_GOMP_SIMD_ORDERED_{START,END}. */
260 :
261 : static void
262 8818 : adjust_simduid_builtins (hash_table<simduid_to_vf> *htab, function *fun)
263 : {
264 8818 : basic_block bb;
265 :
266 140234 : FOR_EACH_BB_FN (bb, fun)
267 : {
268 131416 : gimple_stmt_iterator i;
269 :
270 860209 : for (i = gsi_start_bb (bb); !gsi_end_p (i); )
271 : {
272 597377 : poly_uint64 vf = 1;
273 597377 : enum internal_fn ifn;
274 597377 : gimple *stmt = gsi_stmt (i);
275 597377 : tree t;
276 597377 : if (!is_gimple_call (stmt)
277 597377 : || !gimple_call_internal_p (stmt))
278 : {
279 588242 : gsi_next (&i);
280 589354 : continue;
281 : }
282 9135 : ifn = gimple_call_internal_fn (stmt);
283 9135 : switch (ifn)
284 : {
285 8023 : case IFN_GOMP_SIMD_LANE:
286 8023 : case IFN_GOMP_SIMD_VF:
287 8023 : case IFN_GOMP_SIMD_LAST_LANE:
288 8023 : break;
289 162 : case IFN_GOMP_SIMD_ORDERED_START:
290 162 : case IFN_GOMP_SIMD_ORDERED_END:
291 162 : if (integer_onep (gimple_call_arg (stmt, 0)))
292 : {
293 6 : enum built_in_function bcode
294 : = (ifn == IFN_GOMP_SIMD_ORDERED_START
295 12 : ? BUILT_IN_GOMP_ORDERED_START
296 : : BUILT_IN_GOMP_ORDERED_END);
297 12 : gimple *g
298 12 : = gimple_build_call (builtin_decl_explicit (bcode), 0);
299 12 : gimple_move_vops (g, stmt);
300 12 : gsi_replace (&i, g, true);
301 12 : continue;
302 12 : }
303 150 : gsi_remove (&i, true);
304 150 : unlink_stmt_vdef (stmt);
305 150 : continue;
306 950 : default:
307 950 : gsi_next (&i);
308 950 : continue;
309 1100 : }
310 8023 : tree arg = gimple_call_arg (stmt, 0);
311 8023 : gcc_assert (arg != NULL_TREE);
312 8023 : gcc_assert (TREE_CODE (arg) == SSA_NAME);
313 8023 : simduid_to_vf *p = NULL, data;
314 8023 : data.simduid = DECL_UID (SSA_NAME_VAR (arg));
315 : /* Need to nullify loop safelen field since it's value is not
316 : valid after transformation. */
317 8023 : if (bb->loop_father && bb->loop_father->safelen > 0)
318 2813 : bb->loop_father->safelen = 0;
319 8023 : if (htab)
320 : {
321 5851 : p = htab->find (&data);
322 5851 : if (p)
323 5802 : vf = p->vf;
324 : }
325 8023 : switch (ifn)
326 : {
327 : case IFN_GOMP_SIMD_VF:
328 1248 : t = build_int_cst (unsigned_type_node, vf);
329 1248 : break;
330 4268 : case IFN_GOMP_SIMD_LANE:
331 4268 : t = build_int_cst (unsigned_type_node, 0);
332 4268 : break;
333 2507 : case IFN_GOMP_SIMD_LAST_LANE:
334 2507 : t = gimple_call_arg (stmt, 1);
335 2507 : break;
336 : default:
337 : gcc_unreachable ();
338 : }
339 8023 : tree lhs = gimple_call_lhs (stmt);
340 8023 : if (lhs)
341 7971 : replace_uses_by (lhs, t);
342 8023 : release_defs (stmt);
343 8023 : gsi_remove (&i, true);
344 : }
345 : }
346 8818 : }
347 :
348 : /* Helper structure for note_simd_array_uses. */
349 :
350 : struct note_simd_array_uses_struct
351 : {
352 : hash_table<simd_array_to_simduid> **htab;
353 : unsigned int simduid;
354 : };
355 :
356 : /* Callback for note_simd_array_uses, called through walk_gimple_op. */
357 :
358 : static tree
359 76548 : note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
360 : {
361 76548 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
362 76548 : struct note_simd_array_uses_struct *ns
363 : = (struct note_simd_array_uses_struct *) wi->info;
364 :
365 76548 : if (TYPE_P (*tp))
366 0 : *walk_subtrees = 0;
367 76548 : else if (VAR_P (*tp)
368 14331 : && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
369 90879 : && DECL_CONTEXT (*tp) == current_function_decl)
370 : {
371 14331 : simd_array_to_simduid data;
372 14331 : if (!*ns->htab)
373 2760 : *ns->htab = new hash_table<simd_array_to_simduid> (15);
374 14331 : data.decl = *tp;
375 14331 : data.simduid = ns->simduid;
376 14331 : simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
377 14331 : if (*slot == NULL)
378 : {
379 6436 : simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
380 6436 : *p = data;
381 6436 : *slot = p;
382 : }
383 7895 : else if ((*slot)->simduid != ns->simduid)
384 0 : (*slot)->simduid = -1U;
385 14331 : *walk_subtrees = 0;
386 : }
387 76548 : return NULL_TREE;
388 : }
389 :
390 : /* Find "omp simd array" temporaries and map them to corresponding
391 : simduid. */
392 :
393 : static void
394 8818 : note_simd_array_uses (hash_table<simd_array_to_simduid> **htab, function *fun)
395 : {
396 8818 : basic_block bb;
397 8818 : gimple_stmt_iterator gsi;
398 8818 : struct walk_stmt_info wi;
399 8818 : struct note_simd_array_uses_struct ns;
400 :
401 8818 : memset (&wi, 0, sizeof (wi));
402 8818 : wi.info = &ns;
403 8818 : ns.htab = htab;
404 :
405 120737 : FOR_EACH_BB_FN (bb, fun)
406 677506 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
407 : {
408 453668 : gimple *stmt = gsi_stmt (gsi);
409 453668 : if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
410 445880 : continue;
411 9053 : switch (gimple_call_internal_fn (stmt))
412 : {
413 7819 : case IFN_GOMP_SIMD_LANE:
414 7819 : case IFN_GOMP_SIMD_VF:
415 7819 : case IFN_GOMP_SIMD_LAST_LANE:
416 7819 : break;
417 1234 : default:
418 1234 : continue;
419 : }
420 7819 : tree lhs = gimple_call_lhs (stmt);
421 7819 : if (lhs == NULL_TREE)
422 31 : continue;
423 7788 : imm_use_iterator use_iter;
424 7788 : gimple *use_stmt;
425 7788 : ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
426 29289 : FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
427 21501 : if (!is_gimple_debug (use_stmt))
428 29193 : walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
429 : }
430 8818 : }
431 :
432 : /* Shrink arrays with "omp simd array" attribute to the corresponding
433 : vectorization factor. */
434 :
435 : static void
436 2760 : shrink_simd_arrays
437 : (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
438 : hash_table<simduid_to_vf> *simduid_to_vf_htab)
439 : {
440 6436 : for (hash_table<simd_array_to_simduid>::iterator iter
441 2760 : = simd_array_to_simduid_htab->begin ();
442 9196 : iter != simd_array_to_simduid_htab->end (); ++iter)
443 6436 : if ((*iter)->simduid != -1U)
444 : {
445 6436 : tree decl = (*iter)->decl;
446 6436 : poly_uint64 vf = 1;
447 6436 : if (simduid_to_vf_htab)
448 : {
449 5324 : simduid_to_vf *p = NULL, data;
450 5324 : data.simduid = (*iter)->simduid;
451 5324 : p = simduid_to_vf_htab->find (&data);
452 5324 : if (p)
453 5286 : vf = p->vf;
454 : }
455 6436 : tree atype
456 6436 : = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
457 6436 : TREE_TYPE (decl) = atype;
458 6436 : relayout_decl (decl);
459 : }
460 :
461 2760 : delete simd_array_to_simduid_htab;
462 2760 : }
463 :
464 : /* Initialize the vec_info with kind KIND_IN and target cost data
465 : TARGET_COST_DATA_IN. */
466 :
467 2864999 : vec_info::vec_info (vec_info::vec_kind kind_in, vec_info_shared *shared_)
468 2864999 : : kind (kind_in),
469 2864999 : shared (shared_),
470 2864999 : stmt_vec_info_ro (false),
471 2864999 : bbs (NULL),
472 2864999 : nbbs (0),
473 2864999 : inv_pattern_def_seq (NULL),
474 2864999 : vectorization_factor (1),
475 2864999 : rgroup_compare_type (NULL_TREE),
476 2864999 : rgroup_iv_type (NULL_TREE),
477 2864999 : partial_vector_style (vect_partial_vectors_none),
478 2864999 : partial_load_store_bias (0)
479 : {
480 2864999 : stmt_vec_infos.create (50);
481 2864999 : }
482 :
483 2864999 : vec_info::~vec_info ()
484 : {
485 5238828 : for (slp_instance &instance : slp_instances)
486 1197291 : vect_free_slp_instance (instance);
487 :
488 2864999 : free_stmt_vec_infos ();
489 :
490 2864999 : release_vec_loop_controls (&masks.rgc_vec);
491 2864999 : release_vec_loop_controls (&lens);
492 5729998 : }
493 :
494 2434858 : vec_info_shared::vec_info_shared ()
495 2434858 : : datarefs (vNULL),
496 2434858 : datarefs_copy (vNULL),
497 2434858 : ddrs (vNULL)
498 : {
499 2434858 : }
500 :
501 2434858 : vec_info_shared::~vec_info_shared ()
502 : {
503 2434858 : free_data_refs (datarefs);
504 2434858 : free_dependence_relations (ddrs);
505 2434858 : datarefs_copy.release ();
506 2434858 : }
507 :
508 : void
509 2189117 : vec_info_shared::save_datarefs ()
510 : {
511 2189117 : if (!flag_checking)
512 : return;
513 3279898 : datarefs_copy.reserve_exact (datarefs.length ());
514 15492389 : for (unsigned i = 0; i < datarefs.length (); ++i)
515 11114189 : datarefs_copy.quick_push (*datarefs[i]);
516 : }
517 :
518 : void
519 1200230 : vec_info_shared::check_datarefs ()
520 : {
521 1200230 : if (!flag_checking)
522 : return;
523 3548978 : gcc_assert (datarefs.length () == datarefs_copy.length ());
524 14130670 : for (unsigned i = 0; i < datarefs.length (); ++i)
525 12930444 : if (memcmp (&datarefs_copy[i], datarefs[i],
526 : offsetof (data_reference, alt_indices)) != 0)
527 0 : gcc_unreachable ();
528 : }
529 :
530 : /* Record that STMT belongs to the vectorizable region. Create and return
531 : an associated stmt_vec_info. */
532 :
533 : stmt_vec_info
534 64416256 : vec_info::add_stmt (gimple *stmt)
535 : {
536 64416256 : stmt_vec_info res = new_stmt_vec_info (stmt);
537 64416256 : set_vinfo_for_stmt (stmt, res);
538 64416256 : return res;
539 : }
540 :
541 : /* Record that STMT belongs to the vectorizable region. Create a new
542 : stmt_vec_info and mark VECINFO as being related and return the new
543 : stmt_vec_info. */
544 :
545 : stmt_vec_info
546 1112 : vec_info::add_pattern_stmt (gimple *stmt, stmt_vec_info stmt_info)
547 : {
548 1112 : stmt_vec_info res = new_stmt_vec_info (stmt);
549 1112 : res->pattern_stmt_p = true;
550 1112 : set_vinfo_for_stmt (stmt, res, false);
551 1112 : STMT_VINFO_RELATED_STMT (res) = stmt_info;
552 1112 : return res;
553 : }
554 :
555 : /* If STMT was previously associated with a stmt_vec_info and STMT now resides
556 : at a different address than before (e.g., because STMT is a phi node that has
557 : been resized), update the stored address to match the new one. It is not
558 : possible to use lookup_stmt () to perform this task, because that function
559 : returns NULL if the stored stmt pointer does not match the one being looked
560 : up. */
561 :
562 : stmt_vec_info
563 11353 : vec_info::resync_stmt_addr (gimple *stmt)
564 : {
565 11353 : unsigned int uid = gimple_uid (stmt);
566 11353 : if (uid > 0 && uid - 1 < stmt_vec_infos.length ())
567 : {
568 11353 : stmt_vec_info res = stmt_vec_infos[uid - 1];
569 11353 : if (res && res->stmt)
570 : {
571 11353 : res->stmt = stmt;
572 11353 : return res;
573 : }
574 : }
575 : return nullptr;
576 : }
577 :
578 : /* If STMT has an associated stmt_vec_info, return that vec_info, otherwise
579 : return null. It is safe to call this function on any statement, even if
580 : it might not be part of the vectorizable region. */
581 :
582 : stmt_vec_info
583 520134368 : vec_info::lookup_stmt (gimple *stmt)
584 : {
585 520134368 : unsigned int uid = gimple_uid (stmt);
586 520134368 : if (uid > 0 && uid - 1 < stmt_vec_infos.length ())
587 : {
588 324503989 : stmt_vec_info res = stmt_vec_infos[uid - 1];
589 324503989 : if (res && res->stmt == stmt)
590 324158896 : return res;
591 : }
592 : return NULL;
593 : }
594 :
595 : /* If NAME is an SSA_NAME and its definition has an associated stmt_vec_info,
596 : return that stmt_vec_info, otherwise return null. It is safe to call
597 : this on arbitrary operands. */
598 :
599 : stmt_vec_info
600 61367153 : vec_info::lookup_def (tree name)
601 : {
602 61367153 : if (TREE_CODE (name) == SSA_NAME
603 61367153 : && !SSA_NAME_IS_DEFAULT_DEF (name))
604 55455392 : return lookup_stmt (SSA_NAME_DEF_STMT (name));
605 : return NULL;
606 : }
607 :
608 : /* See whether there is a single non-debug statement that uses LHS and
609 : whether that statement has an associated stmt_vec_info. Return the
610 : stmt_vec_info if so, otherwise return null. */
611 :
612 : stmt_vec_info
613 2044 : vec_info::lookup_single_use (tree lhs)
614 : {
615 2044 : use_operand_p dummy;
616 2044 : gimple *use_stmt;
617 2044 : if (single_imm_use (lhs, &dummy, &use_stmt))
618 1901 : return lookup_stmt (use_stmt);
619 : return NULL;
620 : }
621 :
622 : /* Return vectorization information about DR. */
623 :
624 : dr_vec_info *
625 53683869 : vec_info::lookup_dr (data_reference *dr)
626 : {
627 53683869 : stmt_vec_info stmt_info = lookup_stmt (DR_STMT (dr));
628 : /* DR_STMT should never refer to a stmt in a pattern replacement. */
629 53683869 : gcc_checking_assert (!is_pattern_stmt_p (stmt_info));
630 53683869 : return STMT_VINFO_DR_INFO (stmt_info->dr_aux.stmt);
631 : }
632 :
633 : /* Record that NEW_STMT_INFO now implements the same data reference
634 : as OLD_STMT_INFO. */
635 :
636 : void
637 6553 : vec_info::move_dr (stmt_vec_info new_stmt_info, stmt_vec_info old_stmt_info)
638 : {
639 6553 : gcc_assert (!is_pattern_stmt_p (old_stmt_info));
640 6553 : STMT_VINFO_DR_INFO (old_stmt_info)->stmt = new_stmt_info;
641 6553 : new_stmt_info->dr_aux = old_stmt_info->dr_aux;
642 6553 : STMT_VINFO_DR_WRT_VEC_LOOP (new_stmt_info)
643 6553 : = STMT_VINFO_DR_WRT_VEC_LOOP (old_stmt_info);
644 6553 : STMT_VINFO_GATHER_SCATTER_P (new_stmt_info)
645 6553 : = STMT_VINFO_GATHER_SCATTER_P (old_stmt_info);
646 6553 : STMT_VINFO_STRIDED_P (new_stmt_info)
647 6553 : = STMT_VINFO_STRIDED_P (old_stmt_info);
648 6553 : STMT_VINFO_SIMD_LANE_ACCESS_P (new_stmt_info)
649 6553 : = STMT_VINFO_SIMD_LANE_ACCESS_P (old_stmt_info);
650 6553 : }
651 :
652 : /* Permanently remove the statement described by STMT_INFO from the
653 : function. */
654 :
655 : void
656 1524112 : vec_info::remove_stmt (stmt_vec_info stmt_info)
657 : {
658 1524112 : gcc_assert (!stmt_info->pattern_stmt_p);
659 1524112 : set_vinfo_for_stmt (stmt_info->stmt, NULL);
660 1524112 : unlink_stmt_vdef (stmt_info->stmt);
661 1524112 : gimple_stmt_iterator si = gsi_for_stmt (stmt_info->stmt);
662 1524112 : gsi_remove (&si, true);
663 1524112 : release_defs (stmt_info->stmt);
664 1524112 : free_stmt_vec_info (stmt_info);
665 1524112 : }
666 :
667 : /* Replace the statement at GSI by NEW_STMT, both the vectorization
668 : information and the function itself. STMT_INFO describes the statement
669 : at GSI. */
670 :
671 : void
672 5777 : vec_info::replace_stmt (gimple_stmt_iterator *gsi, stmt_vec_info stmt_info,
673 : gimple *new_stmt)
674 : {
675 5777 : gimple *old_stmt = stmt_info->stmt;
676 5777 : gcc_assert (!stmt_info->pattern_stmt_p && old_stmt == gsi_stmt (*gsi));
677 5777 : gimple_set_uid (new_stmt, gimple_uid (old_stmt));
678 5777 : stmt_info->stmt = new_stmt;
679 5777 : gsi_replace (gsi, new_stmt, true);
680 5777 : }
681 :
682 : /* Insert stmts in SEQ on the VEC_INFO region entry. If CONTEXT is
683 : not NULL it specifies whether to use the sub-region entry
684 : determined by it, currently used for loop vectorization to insert
685 : on the inner loop entry vs. the outer loop entry. */
686 :
687 : void
688 107156 : vec_info::insert_seq_on_entry (stmt_vec_info context, gimple_seq seq)
689 : {
690 107156 : if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (this))
691 : {
692 21231 : class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
693 21231 : basic_block new_bb;
694 21231 : edge pe;
695 :
696 21231 : if (context && nested_in_vect_loop_p (loop, context))
697 : loop = loop->inner;
698 :
699 21231 : pe = loop_preheader_edge (loop);
700 21231 : new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
701 21231 : gcc_assert (!new_bb);
702 : }
703 : else
704 : {
705 85925 : gimple_stmt_iterator gsi_region_begin = gsi_after_labels (bbs[0]);
706 : /* If the regions starts with a returns-twice call insert afte that. */
707 85925 : if (gcall *call = safe_dyn_cast <gcall *> (gsi_stmt (gsi_region_begin)))
708 4046 : if (gimple_call_flags (call) & ECF_RETURNS_TWICE)
709 2 : gsi_next (&gsi_region_begin);
710 85925 : gsi_insert_seq_before (&gsi_region_begin, seq, GSI_SAME_STMT);
711 : }
712 107156 : }
713 :
714 : /* Like insert_seq_on_entry but just inserts the single stmt NEW_STMT. */
715 :
716 : void
717 3538 : vec_info::insert_on_entry (stmt_vec_info context, gimple *new_stmt)
718 : {
719 3538 : gimple_seq seq = NULL;
720 3538 : gimple_stmt_iterator gsi = gsi_start (seq);
721 3538 : gsi_insert_before_without_update (&gsi, new_stmt, GSI_SAME_STMT);
722 3538 : insert_seq_on_entry (context, seq);
723 3538 : }
724 :
725 : /* Create and initialize a new stmt_vec_info struct for STMT. */
726 :
727 : stmt_vec_info
728 64417368 : vec_info::new_stmt_vec_info (gimple *stmt)
729 : {
730 64417368 : stmt_vec_info res = XCNEW (class _stmt_vec_info);
731 64417368 : res->stmt = stmt;
732 :
733 64417368 : STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
734 64417368 : STMT_VINFO_VECTORIZABLE (res) = true;
735 64417368 : STMT_VINFO_REDUC_TYPE (res) = TREE_CODE_REDUCTION;
736 64417368 : STMT_VINFO_REDUC_CODE (res) = ERROR_MARK;
737 64417368 : STMT_VINFO_REDUC_IDX (res) = -1;
738 64417368 : STMT_VINFO_REDUC_DEF (res) = NULL;
739 64417368 : STMT_VINFO_SLP_VECT_ONLY (res) = false;
740 :
741 64417368 : if (is_a <loop_vec_info> (this)
742 8299095 : && gimple_code (stmt) == GIMPLE_PHI
743 65865321 : && is_loop_header_bb_p (gimple_bb (stmt)))
744 1438950 : STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
745 : else
746 62978418 : STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
747 :
748 64417368 : STMT_SLP_TYPE (res) = not_vect;
749 :
750 : /* This is really "uninitialized" until vect_compute_data_ref_alignment. */
751 64417368 : res->dr_aux.misalignment = DR_MISALIGNMENT_UNINITIALIZED;
752 :
753 64417368 : return res;
754 : }
755 :
756 : /* Associate STMT with INFO. */
757 :
758 : void
759 65941480 : vec_info::set_vinfo_for_stmt (gimple *stmt, stmt_vec_info info, bool check_ro)
760 : {
761 65941480 : unsigned int uid = gimple_uid (stmt);
762 65941480 : if (uid == 0)
763 : {
764 64417368 : gcc_assert (!check_ro || !stmt_vec_info_ro);
765 64417368 : gcc_checking_assert (info);
766 64417368 : uid = stmt_vec_infos.length () + 1;
767 64417368 : gimple_set_uid (stmt, uid);
768 64417368 : stmt_vec_infos.safe_push (info);
769 : }
770 : else
771 : {
772 1524112 : gcc_checking_assert (info == NULL);
773 1524112 : stmt_vec_infos[uid - 1] = info;
774 : }
775 65941480 : }
776 :
777 : /* Free the contents of stmt_vec_infos. */
778 :
779 : void
780 2864999 : vec_info::free_stmt_vec_infos (void)
781 : {
782 73012365 : for (stmt_vec_info &info : stmt_vec_infos)
783 64417368 : if (info != NULL)
784 62893256 : free_stmt_vec_info (info);
785 2864999 : stmt_vec_infos.release ();
786 2864999 : }
787 :
788 : /* Free STMT_INFO. */
789 :
790 : void
791 64417368 : vec_info::free_stmt_vec_info (stmt_vec_info stmt_info)
792 : {
793 64417368 : if (stmt_info->pattern_stmt_p)
794 : {
795 2439704 : gimple_set_bb (stmt_info->stmt, NULL);
796 2439704 : tree lhs = gimple_get_lhs (stmt_info->stmt);
797 2439704 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
798 2039616 : release_ssa_name (lhs);
799 : }
800 :
801 64417368 : free (stmt_info);
802 64417368 : }
803 :
804 : /* Returns true if S1 dominates S2. */
805 :
806 : bool
807 1736601 : vect_stmt_dominates_stmt_p (gimple *s1, gimple *s2)
808 : {
809 1736601 : basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
810 :
811 : /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
812 : SSA_NAME. Assume it lives at the beginning of function and
813 : thus dominates everything. */
814 1736601 : if (!bb1 || s1 == s2)
815 : return true;
816 :
817 : /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
818 1117195 : if (!bb2)
819 : return false;
820 :
821 1117195 : if (bb1 != bb2)
822 344834 : return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
823 :
824 : /* PHIs in the same basic block are assumed to be
825 : executed all in parallel, if only one stmt is a PHI,
826 : it dominates the other stmt in the same basic block. */
827 772361 : if (gimple_code (s1) == GIMPLE_PHI)
828 : return true;
829 :
830 651613 : if (gimple_code (s2) == GIMPLE_PHI)
831 : return false;
832 :
833 : /* Inserted vectorized stmts all have UID 0 while the original stmts
834 : in the IL have UID increasing within a BB. Walk from both sides
835 : until we find the other stmt or a stmt with UID != 0. */
836 628668 : gimple_stmt_iterator gsi1 = gsi_for_stmt (s1);
837 1204975 : while (gimple_uid (gsi_stmt (gsi1)) == 0)
838 : {
839 699165 : gsi_next (&gsi1);
840 699165 : if (gsi_end_p (gsi1))
841 : return false;
842 697469 : if (gsi_stmt (gsi1) == s2)
843 : return true;
844 : }
845 505810 : if (gimple_uid (gsi_stmt (gsi1)) == -1u)
846 : return false;
847 :
848 505810 : gimple_stmt_iterator gsi2 = gsi_for_stmt (s2);
849 1152393 : while (gimple_uid (gsi_stmt (gsi2)) == 0)
850 : {
851 660710 : gsi_prev (&gsi2);
852 660710 : if (gsi_end_p (gsi2))
853 : return false;
854 646591 : if (gsi_stmt (gsi2) == s1)
855 : return true;
856 : }
857 491683 : if (gimple_uid (gsi_stmt (gsi2)) == -1u)
858 : return false;
859 :
860 491683 : if (gimple_uid (gsi_stmt (gsi1)) <= gimple_uid (gsi_stmt (gsi2)))
861 387154 : return true;
862 : return false;
863 : }
864 :
865 : /* A helper function to free scev and LOOP niter information, as well as
866 : clear loop constraint LOOP_C_FINITE. */
867 :
868 : void
869 53653 : vect_free_loop_info_assumptions (class loop *loop)
870 : {
871 53653 : scev_reset_htab ();
872 : /* We need to explicitly reset upper bound information since they are
873 : used even after free_numbers_of_iterations_estimates. */
874 53653 : loop->any_upper_bound = false;
875 53653 : loop->any_likely_upper_bound = false;
876 53653 : free_numbers_of_iterations_estimates (loop);
877 53653 : loop_constraint_clear (loop, LOOP_C_FINITE);
878 53653 : }
879 :
880 : /* If LOOP has been versioned during ifcvt, return the internal call
881 : guarding it. */
882 :
883 : gimple *
884 538386 : vect_loop_vectorized_call (class loop *loop, gcond **cond)
885 : {
886 538386 : basic_block bb = loop_preheader_edge (loop)->src;
887 994669 : gimple *g;
888 1450952 : do
889 : {
890 994669 : g = *gsi_last_bb (bb);
891 630551 : if ((g && gimple_code (g) == GIMPLE_COND)
892 2134746 : || !single_succ_p (bb))
893 : break;
894 601691 : if (!single_pred_p (bb))
895 : break;
896 456283 : bb = single_pred (bb);
897 : }
898 : while (1);
899 538386 : if (g && gimple_code (g) == GIMPLE_COND)
900 : {
901 385923 : if (cond)
902 0 : *cond = as_a <gcond *> (g);
903 385923 : gimple_stmt_iterator gsi = gsi_for_stmt (g);
904 385923 : gsi_prev (&gsi);
905 385923 : if (!gsi_end_p (gsi))
906 : {
907 351954 : g = gsi_stmt (gsi);
908 351954 : if (gimple_call_internal_p (g, IFN_LOOP_VECTORIZED)
909 351954 : && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
910 26979 : || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
911 54270 : return g;
912 : }
913 : }
914 : return NULL;
915 : }
916 :
917 : /* If LOOP has been versioned during loop distribution, return the gurading
918 : internal call. */
919 :
920 : static gimple *
921 504575 : vect_loop_dist_alias_call (class loop *loop, function *fun)
922 : {
923 504575 : basic_block bb;
924 504575 : basic_block entry;
925 504575 : class loop *outer, *orig;
926 :
927 504575 : if (loop->orig_loop_num == 0)
928 : return NULL;
929 :
930 152 : orig = get_loop (fun, loop->orig_loop_num);
931 152 : if (orig == NULL)
932 : {
933 : /* The original loop is somehow destroyed. Clear the information. */
934 0 : loop->orig_loop_num = 0;
935 0 : return NULL;
936 : }
937 :
938 152 : if (loop != orig)
939 85 : bb = nearest_common_dominator (CDI_DOMINATORS, loop->header, orig->header);
940 : else
941 67 : bb = loop_preheader_edge (loop)->src;
942 :
943 152 : outer = bb->loop_father;
944 152 : entry = ENTRY_BLOCK_PTR_FOR_FN (fun);
945 :
946 : /* Look upward in dominance tree. */
947 707 : for (; bb != entry && flow_bb_inside_loop_p (outer, bb);
948 555 : bb = get_immediate_dominator (CDI_DOMINATORS, bb))
949 : {
950 666 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
951 666 : if (!safe_is_a <gcond *> (*gsi))
952 179 : continue;
953 :
954 487 : gsi_prev (&gsi);
955 487 : if (gsi_end_p (gsi))
956 7 : continue;
957 :
958 480 : gimple *g = gsi_stmt (gsi);
959 : /* The guarding internal function call must have the same distribution
960 : alias id. */
961 480 : if (gimple_call_internal_p (g, IFN_LOOP_DIST_ALIAS)
962 480 : && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->orig_loop_num))
963 504575 : return g;
964 : }
965 : return NULL;
966 : }
967 :
968 : /* Set the uids of all the statements in basic blocks inside loop
969 : represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
970 : call guarding the loop which has been if converted. */
971 : static void
972 7139 : set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call,
973 : function *fun)
974 : {
975 7139 : tree arg = gimple_call_arg (loop_vectorized_call, 1);
976 7139 : basic_block *bbs;
977 7139 : unsigned int i;
978 7139 : class loop *scalar_loop = get_loop (fun, tree_to_shwi (arg));
979 :
980 7139 : LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
981 7139 : LOOP_VINFO_SCALAR_MAIN_EXIT (loop_vinfo)
982 7139 : = vec_init_loop_exit_info (scalar_loop);
983 7139 : gcc_checking_assert (vect_loop_vectorized_call (scalar_loop)
984 : == loop_vectorized_call);
985 : /* If we are going to vectorize outer loop, prevent vectorization
986 : of the inner loop in the scalar loop - either the scalar loop is
987 : thrown away, so it is a wasted work, or is used only for
988 : a few iterations. */
989 7139 : if (scalar_loop->inner)
990 : {
991 100 : gimple *g = vect_loop_vectorized_call (scalar_loop->inner);
992 100 : if (g)
993 : {
994 100 : arg = gimple_call_arg (g, 0);
995 100 : get_loop (fun, tree_to_shwi (arg))->dont_vectorize = true;
996 100 : fold_loop_internal_call (g, boolean_false_node);
997 : }
998 : }
999 7139 : bbs = get_loop_body (scalar_loop);
1000 46354 : for (i = 0; i < scalar_loop->num_nodes; i++)
1001 : {
1002 32076 : basic_block bb = bbs[i];
1003 32076 : gimple_stmt_iterator gsi;
1004 62632 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1005 : {
1006 30556 : gimple *phi = gsi_stmt (gsi);
1007 30556 : gimple_set_uid (phi, 0);
1008 : }
1009 151335 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1010 : {
1011 87183 : gimple *stmt = gsi_stmt (gsi);
1012 87183 : gimple_set_uid (stmt, 0);
1013 : }
1014 : }
1015 7139 : free (bbs);
1016 7139 : }
1017 :
1018 : /* Generate vectorized code for LOOP and its epilogues. */
1019 :
1020 : static unsigned
1021 66514 : vect_transform_loops (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1022 : loop_p loop, gimple *loop_vectorized_call,
1023 : function *fun)
1024 : {
1025 66514 : loop_vec_info loop_vinfo = loop_vec_info_for_loop (loop);
1026 :
1027 66514 : if (loop_vectorized_call)
1028 7139 : set_uid_loop_bbs (loop_vinfo, loop_vectorized_call, fun);
1029 :
1030 66514 : unsigned HOST_WIDE_INT bytes;
1031 66514 : if (dump_enabled_p ())
1032 : {
1033 23420 : if (GET_MODE_SIZE (loop_vinfo->vector_mode).is_constant (&bytes))
1034 11710 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1035 : "%sloop vectorized using %s%wu byte vectors and"
1036 : " unroll factor %u\n",
1037 11710 : LOOP_VINFO_EPILOGUE_P (loop_vinfo)
1038 : ? "epilogue " : "",
1039 11710 : LOOP_VINFO_USING_PARTIAL_VECTORS_P (loop_vinfo)
1040 : ? "masked " : "", bytes,
1041 : (unsigned int) LOOP_VINFO_VECT_FACTOR
1042 11710 : (loop_vinfo).to_constant ());
1043 : else
1044 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1045 : "%sloop vectorized using variable length vectors\n",
1046 : LOOP_VINFO_EPILOGUE_P (loop_vinfo)
1047 : ? "epilogue " : "");
1048 : }
1049 :
1050 66514 : loop_p new_loop = vect_transform_loop (loop_vinfo,
1051 : loop_vectorized_call);
1052 : /* Now that the loop has been vectorized, allow it to be unrolled
1053 : etc. */
1054 66514 : loop->force_vectorize = false;
1055 :
1056 66514 : if (loop->simduid)
1057 : {
1058 2346 : simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
1059 2346 : if (!simduid_to_vf_htab)
1060 1981 : simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
1061 2346 : simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
1062 2346 : simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
1063 2346 : *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
1064 2346 : = simduid_to_vf_data;
1065 : }
1066 :
1067 : /* We should not have to update virtual SSA form here but some
1068 : transforms involve creating new virtual definitions which makes
1069 : updating difficult.
1070 : We delay the actual update to the end of the pass but avoid
1071 : confusing ourselves by forcing need_ssa_update_p () to false. */
1072 66514 : unsigned todo = 0;
1073 66514 : if (need_ssa_update_p (cfun))
1074 : {
1075 126 : gcc_assert (loop_vinfo->any_known_not_updated_vssa);
1076 126 : fun->gimple_df->ssa_renaming_needed = false;
1077 126 : todo |= TODO_update_ssa_only_virtuals;
1078 : }
1079 66514 : gcc_assert (!need_ssa_update_p (cfun));
1080 :
1081 : /* Epilogue of vectorized loop must be vectorized too. */
1082 66514 : if (new_loop)
1083 7377 : todo |= vect_transform_loops (simduid_to_vf_htab, new_loop, NULL, fun);
1084 :
1085 66514 : return todo;
1086 : }
1087 :
1088 : /* Try to vectorize LOOP. */
1089 :
1090 : static unsigned
1091 488891 : try_vectorize_loop_1 (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1092 : unsigned *num_vectorized_loops, loop_p loop,
1093 : gimple *loop_vectorized_call,
1094 : gimple *loop_dist_alias_call,
1095 : function *fun)
1096 : {
1097 488891 : unsigned ret = 0;
1098 488891 : vec_info_shared shared;
1099 488891 : auto_purge_vect_location sentinel;
1100 488891 : vect_location = find_loop_location (loop);
1101 :
1102 488891 : if (LOCATION_LOCUS (vect_location.get_location_t ()) != UNKNOWN_LOCATION
1103 488891 : && dump_enabled_p ())
1104 15870 : dump_printf (MSG_NOTE | MSG_PRIORITY_INTERNALS,
1105 : "\nAnalyzing loop at %s:%d\n",
1106 15870 : LOCATION_FILE (vect_location.get_location_t ()),
1107 31740 : LOCATION_LINE (vect_location.get_location_t ()));
1108 :
1109 : /* Try to analyze the loop, retaining an opt_problem if dump_enabled_p. */
1110 488891 : opt_loop_vec_info loop_vinfo = vect_analyze_loop (loop, loop_vectorized_call,
1111 : &shared);
1112 488891 : loop->aux = loop_vinfo;
1113 :
1114 488891 : if (!loop_vinfo)
1115 429750 : if (dump_enabled_p ())
1116 6097 : if (opt_problem *problem = loop_vinfo.get_problem ())
1117 : {
1118 6097 : dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1119 : "couldn't vectorize loop\n");
1120 6097 : problem->emit_and_clear ();
1121 : }
1122 :
1123 488891 : if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
1124 : {
1125 : /* Free existing information if loop is analyzed with some
1126 : assumptions. */
1127 429750 : if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1128 7347 : vect_free_loop_info_assumptions (loop);
1129 :
1130 : /* If we applied if-conversion then try to vectorize the
1131 : BB of innermost loops.
1132 : ??? Ideally BB vectorization would learn to vectorize
1133 : control flow by applying if-conversion on-the-fly, the
1134 : following retains the if-converted loop body even when
1135 : only non-if-converted parts took part in BB vectorization. */
1136 429750 : if (flag_tree_slp_vectorize != 0
1137 428626 : && loop_vectorized_call
1138 18444 : && ! loop->inner)
1139 : {
1140 17620 : basic_block bb = loop->header;
1141 17620 : bool require_loop_vectorize = false;
1142 35240 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1143 635141 : !gsi_end_p (gsi); gsi_next (&gsi))
1144 : {
1145 618307 : gimple *stmt = gsi_stmt (gsi);
1146 618307 : gcall *call = dyn_cast <gcall *> (stmt);
1147 1248 : if (call && gimple_call_internal_p (call))
1148 : {
1149 1170 : internal_fn ifn = gimple_call_internal_fn (call);
1150 1170 : if (ifn == IFN_MASK_LOAD
1151 1170 : || ifn == IFN_MASK_STORE
1152 794 : || ifn == IFN_MASK_CALL
1153 : /* Don't keep the if-converted parts when the ifn with
1154 : specific type is not supported by the backend. */
1155 1945 : || (direct_internal_fn_p (ifn)
1156 416 : && !direct_internal_fn_supported_p
1157 416 : (call, OPTIMIZE_FOR_SPEED)))
1158 : {
1159 : require_loop_vectorize = true;
1160 : break;
1161 : }
1162 : }
1163 617521 : gimple_set_uid (stmt, -1);
1164 617521 : gimple_set_visited (stmt, false);
1165 : }
1166 17620 : if (!require_loop_vectorize)
1167 : {
1168 16834 : tree arg = gimple_call_arg (loop_vectorized_call, 1);
1169 16834 : class loop *scalar_loop = get_loop (fun, tree_to_shwi (arg));
1170 16834 : if (vect_slp_if_converted_bb (bb, scalar_loop))
1171 : {
1172 64 : fold_loop_internal_call (loop_vectorized_call,
1173 : boolean_true_node);
1174 64 : loop_vectorized_call = NULL;
1175 64 : ret |= TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
1176 : }
1177 : }
1178 : }
1179 : /* If outer loop vectorization fails for LOOP_VECTORIZED guarded
1180 : loop, don't vectorize its inner loop; we'll attempt to
1181 : vectorize LOOP_VECTORIZED guarded inner loop of the scalar
1182 : loop version. */
1183 19568 : if (loop_vectorized_call && loop->inner)
1184 826 : loop->inner->dont_vectorize = true;
1185 : return ret;
1186 : }
1187 :
1188 59141 : if (!dbg_cnt (vect_loop))
1189 : {
1190 : /* Free existing information if loop is analyzed with some
1191 : assumptions. */
1192 4 : if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1193 0 : vect_free_loop_info_assumptions (loop);
1194 : return ret;
1195 : }
1196 :
1197 59137 : (*num_vectorized_loops)++;
1198 : /* Transform LOOP and its epilogues. */
1199 59137 : ret |= vect_transform_loops (simduid_to_vf_htab, loop,
1200 : loop_vectorized_call, fun);
1201 :
1202 59137 : if (loop_vectorized_call)
1203 : {
1204 7139 : fold_loop_internal_call (loop_vectorized_call, boolean_true_node);
1205 7139 : ret |= TODO_cleanup_cfg;
1206 : }
1207 59137 : if (loop_dist_alias_call)
1208 : {
1209 3 : tree value = gimple_call_arg (loop_dist_alias_call, 1);
1210 3 : fold_loop_internal_call (loop_dist_alias_call, value);
1211 3 : ret |= TODO_cleanup_cfg;
1212 : }
1213 :
1214 : return ret;
1215 488891 : }
1216 :
1217 : /* Try to vectorize LOOP. */
1218 :
1219 : static unsigned
1220 521924 : try_vectorize_loop (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1221 : unsigned *num_vectorized_loops, loop_p loop,
1222 : function *fun)
1223 : {
1224 521924 : if (!((flag_tree_loop_vectorize
1225 517081 : && optimize_loop_nest_for_speed_p (loop))
1226 34934 : || loop->force_vectorize))
1227 : return 0;
1228 :
1229 488891 : return try_vectorize_loop_1 (simduid_to_vf_htab, num_vectorized_loops, loop,
1230 : vect_loop_vectorized_call (loop),
1231 488891 : vect_loop_dist_alias_call (loop, fun), fun);
1232 : }
1233 :
1234 :
1235 : /* Loop autovectorization. */
1236 :
1237 : namespace {
1238 :
1239 : const pass_data pass_data_vectorize =
1240 : {
1241 : GIMPLE_PASS, /* type */
1242 : "vect", /* name */
1243 : OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1244 : TV_TREE_VECTORIZATION, /* tv_id */
1245 : ( PROP_cfg | PROP_ssa ), /* properties_required */
1246 : 0, /* properties_provided */
1247 : 0, /* properties_destroyed */
1248 : 0, /* todo_flags_start */
1249 : 0, /* todo_flags_finish */
1250 : };
1251 :
1252 : class pass_vectorize : public gimple_opt_pass
1253 : {
1254 : public:
1255 302985 : pass_vectorize (gcc::context *ctxt)
1256 605970 : : gimple_opt_pass (pass_data_vectorize, ctxt)
1257 : {}
1258 :
1259 : /* opt_pass methods: */
1260 251589 : bool gate (function *fun) final override
1261 : {
1262 251589 : return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
1263 : }
1264 :
1265 : unsigned int execute (function *) final override;
1266 :
1267 : }; // class pass_vectorize
1268 :
1269 : /* Function vectorize_loops.
1270 :
1271 : Entry point to loop vectorization phase. */
1272 :
1273 : unsigned
1274 216965 : pass_vectorize::execute (function *fun)
1275 : {
1276 216965 : unsigned int i;
1277 216965 : unsigned int num_vectorized_loops = 0;
1278 216965 : unsigned int vect_loops_num;
1279 216965 : hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
1280 216965 : hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1281 216965 : bool any_ifcvt_loops = false;
1282 216965 : unsigned ret = 0;
1283 :
1284 216965 : vect_loops_num = number_of_loops (fun);
1285 :
1286 : /* Bail out if there are no loops. */
1287 216965 : if (vect_loops_num <= 1)
1288 : return 0;
1289 :
1290 216965 : vect_slp_init ();
1291 :
1292 216965 : if (fun->has_simduid_loops)
1293 6610 : note_simd_array_uses (&simd_array_to_simduid_htab, fun);
1294 :
1295 : /* ----------- Analyze loops. ----------- */
1296 216965 : enable_ranger (fun);
1297 :
1298 : /* If some loop was duplicated, it gets bigger number
1299 : than all previously defined loops. This fact allows us to run
1300 : only over initial loops skipping newly generated ones. */
1301 1204611 : for (auto loop : loops_list (fun, 0))
1302 553716 : if (loop->dont_vectorize)
1303 : {
1304 32718 : any_ifcvt_loops = true;
1305 : /* If-conversion sometimes versions both the outer loop
1306 : (for the case when outer loop vectorization might be
1307 : desirable) as well as the inner loop in the scalar version
1308 : of the loop. So we have:
1309 : if (LOOP_VECTORIZED (1, 3))
1310 : {
1311 : loop1
1312 : loop2
1313 : }
1314 : else
1315 : loop3 (copy of loop1)
1316 : if (LOOP_VECTORIZED (4, 5))
1317 : loop4 (copy of loop2)
1318 : else
1319 : loop5 (copy of loop4)
1320 : If loops' iteration gives us loop3 first (which has
1321 : dont_vectorize set), make sure to process loop1 before loop4;
1322 : so that we can prevent vectorization of loop4 if loop1
1323 : is successfully vectorized. */
1324 32718 : if (loop->inner)
1325 : {
1326 2183 : gimple *loop_vectorized_call
1327 2183 : = vect_loop_vectorized_call (loop);
1328 2183 : if (loop_vectorized_call
1329 2183 : && vect_loop_vectorized_call (loop->inner))
1330 : {
1331 926 : tree arg = gimple_call_arg (loop_vectorized_call, 0);
1332 926 : class loop *vector_loop
1333 926 : = get_loop (fun, tree_to_shwi (arg));
1334 926 : if (vector_loop && vector_loop != loop)
1335 : {
1336 : /* Make sure we don't vectorize it twice. */
1337 926 : vector_loop->dont_vectorize = true;
1338 926 : ret |= try_vectorize_loop (simduid_to_vf_htab,
1339 : &num_vectorized_loops,
1340 : vector_loop, fun);
1341 : }
1342 : }
1343 : }
1344 : }
1345 : else
1346 520998 : ret |= try_vectorize_loop (simduid_to_vf_htab, &num_vectorized_loops,
1347 216965 : loop, fun);
1348 :
1349 216965 : vect_location = dump_user_location_t ();
1350 :
1351 216965 : statistics_counter_event (fun, "Vectorized loops", num_vectorized_loops);
1352 216965 : if (dump_enabled_p ()
1353 216965 : || (num_vectorized_loops > 0 && dump_enabled_p ()))
1354 12417 : dump_printf_loc (MSG_NOTE, vect_location,
1355 : "vectorized %u loops in function.\n",
1356 : num_vectorized_loops);
1357 :
1358 : /* ----------- Finalize. ----------- */
1359 216965 : disable_ranger (fun);
1360 :
1361 216965 : if (any_ifcvt_loops)
1362 292348 : for (i = 1; i < number_of_loops (fun); i++)
1363 : {
1364 125316 : class loop *loop = get_loop (fun, i);
1365 125316 : if (loop && loop->dont_vectorize)
1366 : {
1367 34398 : gimple *g = vect_loop_vectorized_call (loop);
1368 34398 : if (g)
1369 : {
1370 18714 : fold_loop_internal_call (g, boolean_false_node);
1371 18714 : loop->dont_vectorize = false;
1372 18714 : ret |= TODO_cleanup_cfg;
1373 18714 : g = NULL;
1374 : }
1375 : else
1376 15684 : g = vect_loop_dist_alias_call (loop, fun);
1377 :
1378 34398 : if (g)
1379 : {
1380 30 : fold_loop_internal_call (g, boolean_false_node);
1381 30 : loop->dont_vectorize = false;
1382 30 : ret |= TODO_cleanup_cfg;
1383 : }
1384 : }
1385 : }
1386 :
1387 : /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1388 216965 : if (fun->has_simduid_loops)
1389 : {
1390 6610 : adjust_simduid_builtins (simduid_to_vf_htab, fun);
1391 : /* Avoid stale SCEV cache entries for the SIMD_LANE defs. */
1392 6610 : scev_reset ();
1393 : }
1394 : /* Shrink any "omp array simd" temporary arrays to the
1395 : actual vectorization factors. */
1396 216965 : if (simd_array_to_simduid_htab)
1397 2756 : shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
1398 216965 : delete simduid_to_vf_htab;
1399 216965 : fun->has_simduid_loops = false;
1400 :
1401 216965 : if (num_vectorized_loops > 0)
1402 : {
1403 : /* We are collecting some corner cases where we need to update
1404 : virtual SSA form via the TODO but delete the queued update-SSA
1405 : state. Force renaming if we think that might be necessary. */
1406 40055 : if (ret & TODO_update_ssa_only_virtuals)
1407 94 : mark_virtual_operands_for_renaming (cfun);
1408 : /* If we vectorized any loop only virtual SSA form needs to be updated.
1409 : ??? Also while we try hard to update loop-closed SSA form we fail
1410 : to properly do this in some corner-cases (see PR56286). */
1411 40055 : rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
1412 40055 : ret |= TODO_cleanup_cfg;
1413 : }
1414 :
1415 1943814 : for (i = 1; i < number_of_loops (fun); i++)
1416 : {
1417 754942 : loop_vec_info loop_vinfo;
1418 754942 : bool has_mask_store;
1419 :
1420 754942 : class loop *loop = get_loop (fun, i);
1421 754942 : if (!loop || !loop->aux)
1422 688424 : continue;
1423 66518 : loop_vinfo = (loop_vec_info) loop->aux;
1424 66518 : has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
1425 66518 : delete loop_vinfo;
1426 66518 : if (has_mask_store
1427 66518 : && targetm.vectorize.empty_mask_is_expensive (IFN_MASK_STORE))
1428 509 : optimize_mask_stores (loop);
1429 :
1430 66518 : auto_bitmap exit_bbs;
1431 : /* Perform local CSE, this esp. helps because we emit code for
1432 : predicates that need to be shared for optimal predicate usage.
1433 : However reassoc will re-order them and prevent CSE from working
1434 : as it should. CSE only the loop body, not the entry. */
1435 66518 : auto_vec<edge> exits = get_loop_exit_edges (loop);
1436 267697 : for (edge exit : exits)
1437 68143 : bitmap_set_bit (exit_bbs, exit->dest->index);
1438 :
1439 66518 : edge entry = EDGE_PRED (loop_preheader_edge (loop)->src, 0);
1440 66518 : do_rpo_vn (fun, entry, exit_bbs);
1441 :
1442 66518 : loop->aux = NULL;
1443 66518 : }
1444 :
1445 216965 : vect_slp_fini ();
1446 :
1447 216965 : return ret;
1448 : }
1449 :
1450 : } // anon namespace
1451 :
1452 : gimple_opt_pass *
1453 302985 : make_pass_vectorize (gcc::context *ctxt)
1454 : {
1455 302985 : return new pass_vectorize (ctxt);
1456 : }
1457 :
1458 : /* Entry point to the simduid cleanup pass. */
1459 :
1460 : namespace {
1461 :
1462 : const pass_data pass_data_simduid_cleanup =
1463 : {
1464 : GIMPLE_PASS, /* type */
1465 : "simduid", /* name */
1466 : OPTGROUP_NONE, /* optinfo_flags */
1467 : TV_NONE, /* tv_id */
1468 : ( PROP_ssa | PROP_cfg ), /* properties_required */
1469 : 0, /* properties_provided */
1470 : 0, /* properties_destroyed */
1471 : 0, /* todo_flags_start */
1472 : 0, /* todo_flags_finish */
1473 : };
1474 :
1475 : class pass_simduid_cleanup : public gimple_opt_pass
1476 : {
1477 : public:
1478 605970 : pass_simduid_cleanup (gcc::context *ctxt)
1479 1211940 : : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
1480 : {}
1481 :
1482 : /* opt_pass methods: */
1483 302985 : opt_pass * clone () final override
1484 : {
1485 302985 : return new pass_simduid_cleanup (m_ctxt);
1486 : }
1487 2600200 : bool gate (function *fun) final override { return fun->has_simduid_loops; }
1488 : unsigned int execute (function *) final override;
1489 :
1490 : }; // class pass_simduid_cleanup
1491 :
1492 : unsigned int
1493 2208 : pass_simduid_cleanup::execute (function *fun)
1494 : {
1495 2208 : hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1496 :
1497 2208 : note_simd_array_uses (&simd_array_to_simduid_htab, fun);
1498 :
1499 : /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1500 2208 : adjust_simduid_builtins (NULL, fun);
1501 :
1502 : /* Shrink any "omp array simd" temporary arrays to the
1503 : actual vectorization factors. */
1504 2208 : if (simd_array_to_simduid_htab)
1505 4 : shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
1506 2208 : fun->has_simduid_loops = false;
1507 2208 : return 0;
1508 : }
1509 :
1510 : } // anon namespace
1511 :
1512 : gimple_opt_pass *
1513 302985 : make_pass_simduid_cleanup (gcc::context *ctxt)
1514 : {
1515 302985 : return new pass_simduid_cleanup (ctxt);
1516 : }
1517 :
1518 :
1519 : /* Entry point to basic block SLP phase. */
1520 :
1521 : namespace {
1522 :
1523 : const pass_data pass_data_slp_vectorize =
1524 : {
1525 : GIMPLE_PASS, /* type */
1526 : "slp", /* name */
1527 : OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1528 : TV_TREE_SLP_VECTORIZATION, /* tv_id */
1529 : ( PROP_ssa | PROP_cfg ), /* properties_required */
1530 : 0, /* properties_provided */
1531 : 0, /* properties_destroyed */
1532 : 0, /* todo_flags_start */
1533 : TODO_update_ssa, /* todo_flags_finish */
1534 : };
1535 :
1536 : class pass_slp_vectorize : public gimple_opt_pass
1537 : {
1538 : public:
1539 605970 : pass_slp_vectorize (gcc::context *ctxt)
1540 1211940 : : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
1541 : {}
1542 :
1543 : /* opt_pass methods: */
1544 302985 : opt_pass * clone () final override { return new pass_slp_vectorize (m_ctxt); }
1545 1068306 : bool gate (function *) final override { return flag_tree_slp_vectorize != 0; }
1546 : unsigned int execute (function *) final override;
1547 :
1548 : }; // class pass_slp_vectorize
1549 :
1550 : unsigned int
1551 930230 : pass_slp_vectorize::execute (function *fun)
1552 : {
1553 930230 : auto_purge_vect_location sentinel;
1554 930230 : basic_block bb;
1555 :
1556 930230 : bool in_loop_pipeline = scev_initialized_p ();
1557 930230 : if (!in_loop_pipeline)
1558 : {
1559 715027 : loop_optimizer_init (LOOPS_NORMAL);
1560 715027 : scev_initialize ();
1561 : }
1562 :
1563 : /* Mark all stmts as not belonging to the current region and unvisited. */
1564 11911006 : FOR_EACH_BB_FN (bb, fun)
1565 : {
1566 15895357 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1567 4914581 : gsi_next (&gsi))
1568 : {
1569 4914581 : gphi *stmt = gsi.phi ();
1570 4914581 : gimple_set_uid (stmt, -1);
1571 4914581 : gimple_set_visited (stmt, false);
1572 : }
1573 106315325 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1574 84353773 : gsi_next (&gsi))
1575 : {
1576 84353773 : gimple *stmt = gsi_stmt (gsi);
1577 84353773 : gimple_set_uid (stmt, -1);
1578 84353773 : gimple_set_visited (stmt, false);
1579 : }
1580 : }
1581 :
1582 930230 : vect_slp_init ();
1583 :
1584 930230 : vect_slp_function (fun);
1585 :
1586 930230 : vect_slp_fini ();
1587 :
1588 930230 : if (!in_loop_pipeline)
1589 : {
1590 715027 : scev_finalize ();
1591 715027 : loop_optimizer_finalize ();
1592 : }
1593 :
1594 1860460 : return 0;
1595 930230 : }
1596 :
1597 : } // anon namespace
1598 :
1599 : gimple_opt_pass *
1600 302985 : make_pass_slp_vectorize (gcc::context *ctxt)
1601 : {
1602 302985 : return new pass_slp_vectorize (ctxt);
1603 : }
1604 :
1605 :
1606 : /* Increase alignment of global arrays to improve vectorization potential.
1607 : TODO:
1608 : - Consider also structs that have an array field.
1609 : - Use ipa analysis to prune arrays that can't be vectorized?
1610 : This should involve global alignment analysis and in the future also
1611 : array padding. */
1612 :
1613 : static unsigned get_vec_alignment_for_type (tree);
1614 : static hash_map<tree, unsigned> *type_align_map;
1615 :
1616 : /* Return alignment of array's vector type corresponding to scalar type.
1617 : 0 if no vector type exists. */
1618 : static unsigned
1619 0 : get_vec_alignment_for_array_type (tree type)
1620 : {
1621 0 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1622 0 : poly_uint64 array_size, vector_size;
1623 :
1624 0 : tree scalar_type = strip_array_types (type);
1625 0 : tree vectype = get_related_vectype_for_scalar_type (VOIDmode, scalar_type);
1626 0 : if (!vectype
1627 0 : || !poly_int_tree_p (TYPE_SIZE (type), &array_size)
1628 0 : || !poly_int_tree_p (TYPE_SIZE (vectype), &vector_size)
1629 0 : || maybe_lt (array_size, vector_size))
1630 : return 0;
1631 :
1632 0 : return TYPE_ALIGN (vectype);
1633 : }
1634 :
1635 : /* Return alignment of field having maximum alignment of vector type
1636 : corresponding to it's scalar type. For now, we only consider fields whose
1637 : offset is a multiple of it's vector alignment.
1638 : 0 if no suitable field is found. */
1639 : static unsigned
1640 0 : get_vec_alignment_for_record_type (tree type)
1641 : {
1642 0 : gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1643 :
1644 0 : unsigned max_align = 0, alignment;
1645 0 : HOST_WIDE_INT offset;
1646 0 : tree offset_tree;
1647 :
1648 0 : if (TYPE_PACKED (type))
1649 : return 0;
1650 :
1651 0 : unsigned *slot = type_align_map->get (type);
1652 0 : if (slot)
1653 0 : return *slot;
1654 :
1655 0 : for (tree field = first_field (type);
1656 0 : field != NULL_TREE;
1657 0 : field = DECL_CHAIN (field))
1658 : {
1659 : /* Skip if not FIELD_DECL or if alignment is set by user. */
1660 0 : if (TREE_CODE (field) != FIELD_DECL
1661 0 : || DECL_USER_ALIGN (field)
1662 0 : || DECL_ARTIFICIAL (field))
1663 0 : continue;
1664 :
1665 : /* We don't need to process the type further if offset is variable,
1666 : since the offsets of remaining members will also be variable. */
1667 0 : if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
1668 0 : || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
1669 : break;
1670 :
1671 : /* Similarly stop processing the type if offset_tree
1672 : does not fit in unsigned HOST_WIDE_INT. */
1673 0 : offset_tree = bit_position (field);
1674 0 : if (!tree_fits_uhwi_p (offset_tree))
1675 : break;
1676 :
1677 0 : offset = tree_to_uhwi (offset_tree);
1678 0 : alignment = get_vec_alignment_for_type (TREE_TYPE (field));
1679 :
1680 : /* Get maximum alignment of vectorized field/array among those members
1681 : whose offset is multiple of the vector alignment. */
1682 0 : if (alignment
1683 0 : && (offset % alignment == 0)
1684 0 : && (alignment > max_align))
1685 0 : max_align = alignment;
1686 : }
1687 :
1688 0 : type_align_map->put (type, max_align);
1689 0 : return max_align;
1690 : }
1691 :
1692 : /* Return alignment of vector type corresponding to decl's scalar type
1693 : or 0 if it doesn't exist or the vector alignment is lesser than
1694 : decl's alignment. */
1695 : static unsigned
1696 0 : get_vec_alignment_for_type (tree type)
1697 : {
1698 0 : if (type == NULL_TREE)
1699 : return 0;
1700 :
1701 0 : gcc_assert (TYPE_P (type));
1702 :
1703 0 : static unsigned alignment = 0;
1704 0 : switch (TREE_CODE (type))
1705 : {
1706 0 : case ARRAY_TYPE:
1707 0 : alignment = get_vec_alignment_for_array_type (type);
1708 0 : break;
1709 0 : case RECORD_TYPE:
1710 0 : alignment = get_vec_alignment_for_record_type (type);
1711 0 : break;
1712 0 : default:
1713 0 : alignment = 0;
1714 0 : break;
1715 : }
1716 :
1717 0 : return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
1718 : }
1719 :
1720 : /* Entry point to increase_alignment pass. */
1721 : static unsigned int
1722 0 : increase_alignment (void)
1723 : {
1724 0 : varpool_node *vnode;
1725 :
1726 0 : vect_location = dump_user_location_t ();
1727 0 : type_align_map = new hash_map<tree, unsigned>;
1728 :
1729 : /* Increase the alignment of all global arrays for vectorization. */
1730 0 : FOR_EACH_DEFINED_VARIABLE (vnode)
1731 : {
1732 0 : tree decl = vnode->decl;
1733 0 : unsigned int alignment;
1734 :
1735 0 : if ((decl_in_symtab_p (decl)
1736 0 : && !symtab_node::get (decl)->can_increase_alignment_p ())
1737 0 : || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
1738 0 : continue;
1739 :
1740 0 : alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
1741 0 : if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
1742 : {
1743 0 : vnode->increase_alignment (alignment);
1744 0 : if (dump_enabled_p ())
1745 0 : dump_printf (MSG_NOTE, "Increasing alignment of decl: %T\n", decl);
1746 : }
1747 : }
1748 :
1749 0 : delete type_align_map;
1750 0 : return 0;
1751 : }
1752 :
1753 :
1754 : namespace {
1755 :
1756 : const pass_data pass_data_ipa_increase_alignment =
1757 : {
1758 : SIMPLE_IPA_PASS, /* type */
1759 : "increase_alignment", /* name */
1760 : OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1761 : TV_IPA_OPT, /* tv_id */
1762 : 0, /* properties_required */
1763 : 0, /* properties_provided */
1764 : 0, /* properties_destroyed */
1765 : 0, /* todo_flags_start */
1766 : 0, /* todo_flags_finish */
1767 : };
1768 :
1769 : class pass_ipa_increase_alignment : public simple_ipa_opt_pass
1770 : {
1771 : public:
1772 302985 : pass_ipa_increase_alignment (gcc::context *ctxt)
1773 605970 : : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
1774 : {}
1775 :
1776 : /* opt_pass methods: */
1777 243276 : bool gate (function *) final override
1778 : {
1779 243276 : return flag_section_anchors && flag_tree_loop_vectorize;
1780 : }
1781 :
1782 0 : unsigned int execute (function *) final override
1783 : {
1784 0 : return increase_alignment ();
1785 : }
1786 :
1787 : }; // class pass_ipa_increase_alignment
1788 :
1789 : } // anon namespace
1790 :
1791 : simple_ipa_opt_pass *
1792 302985 : make_pass_ipa_increase_alignment (gcc::context *ctxt)
1793 : {
1794 302985 : return new pass_ipa_increase_alignment (ctxt);
1795 : }
1796 :
1797 : /* If the condition represented by T is a comparison or the SSA name
1798 : result of a comparison, extract the comparison's operands. Represent
1799 : T as NE_EXPR <T, 0> otherwise. */
1800 :
1801 : void
1802 58373 : scalar_cond_masked_key::get_cond_ops_from_tree (tree t)
1803 : {
1804 58373 : if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_comparison)
1805 : {
1806 0 : this->code = TREE_CODE (t);
1807 0 : this->op0 = TREE_OPERAND (t, 0);
1808 0 : this->op1 = TREE_OPERAND (t, 1);
1809 0 : this->inverted_p = false;
1810 0 : return;
1811 : }
1812 :
1813 58373 : if (TREE_CODE (t) == SSA_NAME)
1814 25064 : if (gassign *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (t)))
1815 : {
1816 25064 : tree_code code = gimple_assign_rhs_code (stmt);
1817 25064 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1818 : {
1819 18117 : this->code = code;
1820 18117 : this->op0 = gimple_assign_rhs1 (stmt);
1821 18117 : this->op1 = gimple_assign_rhs2 (stmt);
1822 18117 : this->inverted_p = false;
1823 18117 : return;
1824 : }
1825 6947 : else if (code == BIT_NOT_EXPR)
1826 : {
1827 2943 : tree n_op = gimple_assign_rhs1 (stmt);
1828 2943 : if ((stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (n_op))))
1829 : {
1830 2943 : code = gimple_assign_rhs_code (stmt);
1831 2943 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1832 : {
1833 2907 : this->code = code;
1834 2907 : this->op0 = gimple_assign_rhs1 (stmt);
1835 2907 : this->op1 = gimple_assign_rhs2 (stmt);
1836 2907 : this->inverted_p = true;
1837 2907 : return;
1838 : }
1839 : }
1840 : }
1841 : }
1842 :
1843 37349 : this->code = NE_EXPR;
1844 37349 : this->op0 = t;
1845 37349 : this->op1 = build_zero_cst (TREE_TYPE (t));
1846 37349 : this->inverted_p = false;
1847 : }
1848 :
1849 : /* See the comment above the declaration for details. */
1850 :
1851 : unsigned int
1852 0 : vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind,
1853 : stmt_vec_info stmt_info, slp_tree,
1854 : tree vectype, int misalign,
1855 : vect_cost_model_location where)
1856 : {
1857 0 : unsigned int cost
1858 0 : = builtin_vectorization_cost (kind, vectype, misalign) * count;
1859 0 : return record_stmt_cost (stmt_info, where, cost);
1860 : }
1861 :
1862 : unsigned int
1863 2833395 : vector_costs::add_slp_cost (slp_tree,
1864 : const array_slice<stmt_info_for_cost> &cost_vec)
1865 : {
1866 2833395 : unsigned int sum = 0;
1867 6396164 : for (auto item : cost_vec)
1868 3562769 : sum += ::add_stmt_cost (this, item.count, item.kind, item.stmt_info,
1869 : item.node, item.vectype, item.misalign, item.where);
1870 2833395 : return sum;
1871 : }
1872 :
1873 : /* See the comment above the declaration for details. */
1874 :
1875 : void
1876 1965345 : vector_costs::finish_cost (const vector_costs *)
1877 : {
1878 1965345 : gcc_assert (!m_finished);
1879 1965345 : m_finished = true;
1880 1965345 : }
1881 :
1882 : /* Record a base cost of COST units against WHERE. If STMT_INFO is
1883 : nonnull, use it to adjust the cost based on execution frequency
1884 : (where appropriate). */
1885 :
1886 : unsigned int
1887 0 : vector_costs::record_stmt_cost (stmt_vec_info stmt_info,
1888 : vect_cost_model_location where,
1889 : unsigned int cost)
1890 : {
1891 0 : cost = adjust_cost_for_freq (stmt_info, where, cost);
1892 0 : m_costs[where] += cost;
1893 0 : return cost;
1894 : }
1895 :
1896 : /* COST is the base cost we have calculated for an operation in location WHERE.
1897 : If STMT_INFO is nonnull, use it to adjust the cost based on execution
1898 : frequency (where appropriate). Return the adjusted cost. */
1899 :
1900 : unsigned int
1901 8120578 : vector_costs::adjust_cost_for_freq (stmt_vec_info stmt_info,
1902 : vect_cost_model_location where,
1903 : unsigned int cost)
1904 : {
1905 : /* Statements in an inner loop relative to the loop being
1906 : vectorized are weighted more heavily. The value here is
1907 : arbitrary and could potentially be improved with analysis. */
1908 8120578 : if (where == vect_body
1909 8120578 : && stmt_info
1910 8120578 : && stmt_in_inner_loop_p (m_vinfo, stmt_info))
1911 : {
1912 11504 : loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
1913 11504 : cost *= LOOP_VINFO_INNER_LOOP_COST_FACTOR (loop_vinfo);
1914 : }
1915 8120578 : return cost;
1916 : }
1917 :
1918 : /* See the comment above the declaration for details. */
1919 :
1920 : bool
1921 34905 : vector_costs::better_main_loop_than_p (const vector_costs *other) const
1922 : {
1923 34905 : int diff = compare_inside_loop_cost (other);
1924 34905 : if (diff != 0)
1925 34698 : return diff < 0;
1926 :
1927 : /* If there's nothing to choose between the loop bodies, see whether
1928 : there's a difference in the prologue and epilogue costs. */
1929 207 : diff = compare_outside_loop_cost (other);
1930 207 : if (diff != 0)
1931 141 : return diff < 0;
1932 :
1933 : return false;
1934 : }
1935 :
1936 :
1937 : /* See the comment above the declaration for details. */
1938 :
1939 : bool
1940 1506 : vector_costs::better_epilogue_loop_than_p (const vector_costs *other,
1941 : loop_vec_info main_loop) const
1942 : {
1943 1506 : loop_vec_info this_loop_vinfo = as_a<loop_vec_info> (this->m_vinfo);
1944 1506 : loop_vec_info other_loop_vinfo = as_a<loop_vec_info> (other->m_vinfo);
1945 :
1946 1506 : poly_int64 this_vf = LOOP_VINFO_VECT_FACTOR (this_loop_vinfo);
1947 1506 : poly_int64 other_vf = LOOP_VINFO_VECT_FACTOR (other_loop_vinfo);
1948 :
1949 1506 : poly_uint64 main_poly_vf = LOOP_VINFO_VECT_FACTOR (main_loop);
1950 1506 : unsigned HOST_WIDE_INT main_vf;
1951 1506 : unsigned HOST_WIDE_INT other_factor, this_factor, other_cost, this_cost;
1952 : /* If we can determine how many iterations are left for the epilogue
1953 : loop, that is if both the main loop's vectorization factor and number
1954 : of iterations are constant, then we use them to calculate the cost of
1955 : the epilogue loop together with a 'likely value' for the epilogues
1956 : vectorization factor. Otherwise we use the main loop's vectorization
1957 : factor and the maximum poly value for the epilogue's. If the target
1958 : has not provided with a sensible upper bound poly vectorization
1959 : factors are likely to be favored over constant ones. */
1960 1506 : if (main_poly_vf.is_constant (&main_vf)
1961 1506 : && LOOP_VINFO_NITERS_KNOWN_P (main_loop))
1962 : {
1963 93 : unsigned HOST_WIDE_INT niters
1964 93 : = LOOP_VINFO_INT_NITERS (main_loop) % main_vf;
1965 93 : HOST_WIDE_INT other_likely_vf
1966 93 : = estimated_poly_value (other_vf, POLY_VALUE_LIKELY);
1967 93 : HOST_WIDE_INT this_likely_vf
1968 93 : = estimated_poly_value (this_vf, POLY_VALUE_LIKELY);
1969 :
1970 : /* If the epilogue is using partial vectors we account for the
1971 : partial iteration here too. */
1972 93 : other_factor = niters / other_likely_vf;
1973 93 : if (LOOP_VINFO_USING_PARTIAL_VECTORS_P (other_loop_vinfo)
1974 0 : && niters % other_likely_vf != 0)
1975 0 : other_factor++;
1976 :
1977 93 : this_factor = niters / this_likely_vf;
1978 93 : if (LOOP_VINFO_USING_PARTIAL_VECTORS_P (this_loop_vinfo)
1979 0 : && niters % this_likely_vf != 0)
1980 0 : this_factor++;
1981 : }
1982 : else
1983 : {
1984 : unsigned HOST_WIDE_INT main_vf_max
1985 1413 : = estimated_poly_value (main_poly_vf, POLY_VALUE_MAX);
1986 1413 : unsigned HOST_WIDE_INT other_vf_max
1987 1413 : = estimated_poly_value (other_vf, POLY_VALUE_MAX);
1988 1413 : unsigned HOST_WIDE_INT this_vf_max
1989 1413 : = estimated_poly_value (this_vf, POLY_VALUE_MAX);
1990 :
1991 1413 : other_factor = CEIL (main_vf_max, other_vf_max);
1992 1413 : this_factor = CEIL (main_vf_max, this_vf_max);
1993 :
1994 : /* If the loop is not using partial vectors then it will iterate one
1995 : time less than one that does. It is safe to subtract one here,
1996 : because the main loop's vf is always at least 2x bigger than that
1997 : of an epilogue. */
1998 1413 : if (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (other_loop_vinfo))
1999 1398 : other_factor -= 1;
2000 1413 : if (!LOOP_VINFO_USING_PARTIAL_VECTORS_P (this_loop_vinfo))
2001 1412 : this_factor -= 1;
2002 : }
2003 :
2004 : /* Compute the costs by multiplying the inside costs with the factor and
2005 : add the outside costs for a more complete picture. The factor is the
2006 : amount of times we are expecting to iterate this epilogue. */
2007 1506 : other_cost = other->body_cost () * other_factor;
2008 1506 : this_cost = this->body_cost () * this_factor;
2009 1506 : other_cost += other->outside_cost ();
2010 1506 : this_cost += this->outside_cost ();
2011 1506 : return this_cost < other_cost;
2012 : }
2013 :
2014 : /* A <=>-style subroutine of better_main_loop_than_p. Check whether we can
2015 : determine the return value of better_main_loop_than_p by comparing the
2016 : inside (loop body) costs of THIS and OTHER. Return:
2017 :
2018 : * -1 if better_main_loop_than_p should return true.
2019 : * 1 if better_main_loop_than_p should return false.
2020 : * 0 if we can't decide. */
2021 :
2022 : int
2023 34905 : vector_costs::compare_inside_loop_cost (const vector_costs *other) const
2024 : {
2025 34905 : loop_vec_info this_loop_vinfo = as_a<loop_vec_info> (this->m_vinfo);
2026 34905 : loop_vec_info other_loop_vinfo = as_a<loop_vec_info> (other->m_vinfo);
2027 :
2028 34905 : struct loop *loop = LOOP_VINFO_LOOP (this_loop_vinfo);
2029 34905 : gcc_assert (LOOP_VINFO_LOOP (other_loop_vinfo) == loop);
2030 :
2031 34905 : poly_int64 this_vf = LOOP_VINFO_VECT_FACTOR (this_loop_vinfo);
2032 34905 : poly_int64 other_vf = LOOP_VINFO_VECT_FACTOR (other_loop_vinfo);
2033 :
2034 : /* Limit the VFs to what is likely to be the maximum number of iterations,
2035 : to handle cases in which at least one loop_vinfo is fully-masked. */
2036 34905 : HOST_WIDE_INT estimated_max_niter = likely_max_stmt_executions_int (loop);
2037 34905 : if (estimated_max_niter != -1)
2038 : {
2039 33819 : if (estimated_poly_value (this_vf, POLY_VALUE_MIN)
2040 : >= estimated_max_niter)
2041 : this_vf = estimated_max_niter;
2042 33819 : if (estimated_poly_value (other_vf, POLY_VALUE_MIN)
2043 : >= estimated_max_niter)
2044 : other_vf = estimated_max_niter;
2045 : }
2046 :
2047 : /* Check whether the (fractional) cost per scalar iteration is lower or
2048 : higher: this_inside_cost / this_vf vs. other_inside_cost / other_vf. */
2049 34905 : poly_int64 rel_this = this_loop_vinfo->vector_costs->body_cost () * other_vf;
2050 34905 : poly_int64 rel_other
2051 34905 : = other_loop_vinfo->vector_costs->body_cost () * this_vf;
2052 :
2053 34905 : HOST_WIDE_INT est_rel_this_min
2054 34905 : = estimated_poly_value (rel_this, POLY_VALUE_MIN);
2055 34905 : HOST_WIDE_INT est_rel_this_max
2056 34905 : = estimated_poly_value (rel_this, POLY_VALUE_MAX);
2057 :
2058 34905 : HOST_WIDE_INT est_rel_other_min
2059 34905 : = estimated_poly_value (rel_other, POLY_VALUE_MIN);
2060 34905 : HOST_WIDE_INT est_rel_other_max
2061 34905 : = estimated_poly_value (rel_other, POLY_VALUE_MAX);
2062 :
2063 : /* Check first if we can make out an unambiguous total order from the minimum
2064 : and maximum estimates. */
2065 34905 : if (est_rel_this_min < est_rel_other_min
2066 : && est_rel_this_max < est_rel_other_max)
2067 : return -1;
2068 :
2069 33779 : if (est_rel_other_min < est_rel_this_min
2070 : && est_rel_other_max < est_rel_this_max)
2071 33572 : return 1;
2072 :
2073 : /* When other_loop_vinfo uses a variable vectorization factor,
2074 : we know that it has a lower cost for at least one runtime VF.
2075 : However, we don't know how likely that VF is.
2076 :
2077 : One option would be to compare the costs for the estimated VFs.
2078 : The problem is that that can put too much pressure on the cost
2079 : model. E.g. if the estimated VF is also the lowest possible VF,
2080 : and if other_loop_vinfo is 1 unit worse than this_loop_vinfo
2081 : for the estimated VF, we'd then choose this_loop_vinfo even
2082 : though (a) this_loop_vinfo might not actually be better than
2083 : other_loop_vinfo for that VF and (b) it would be significantly
2084 : worse at larger VFs.
2085 :
2086 : Here we go for a hacky compromise: pick this_loop_vinfo if it is
2087 : no more expensive than other_loop_vinfo even after doubling the
2088 : estimated other_loop_vinfo VF. For all but trivial loops, this
2089 : ensures that we only pick this_loop_vinfo if it is significantly
2090 : better than other_loop_vinfo at the estimated VF. */
2091 : if (est_rel_other_min != est_rel_this_min
2092 : || est_rel_other_max != est_rel_this_max)
2093 : {
2094 : HOST_WIDE_INT est_rel_this_likely
2095 : = estimated_poly_value (rel_this, POLY_VALUE_LIKELY);
2096 : HOST_WIDE_INT est_rel_other_likely
2097 : = estimated_poly_value (rel_other, POLY_VALUE_LIKELY);
2098 :
2099 : return est_rel_this_likely * 2 <= est_rel_other_likely ? -1 : 1;
2100 : }
2101 :
2102 : return 0;
2103 : }
2104 :
2105 : /* A <=>-style subroutine of better_main_loop_than_p, used when there is
2106 : nothing to choose between the inside (loop body) costs of THIS and OTHER.
2107 : Check whether we can determine the return value of better_main_loop_than_p
2108 : by comparing the outside (prologue and epilogue) costs of THIS and OTHER.
2109 : Return:
2110 :
2111 : * -1 if better_main_loop_than_p should return true.
2112 : * 1 if better_main_loop_than_p should return false.
2113 : * 0 if we can't decide. */
2114 :
2115 : int
2116 207 : vector_costs::compare_outside_loop_cost (const vector_costs *other) const
2117 : {
2118 207 : auto this_outside_cost = this->outside_cost ();
2119 207 : auto other_outside_cost = other->outside_cost ();
2120 207 : if (this_outside_cost != other_outside_cost)
2121 141 : return this_outside_cost < other_outside_cost ? -1 : 1;
2122 :
2123 : return 0;
2124 : }
|