Move PREFERRED_DEBUGGING_TYPE define in pa64-hpux.h to pa.h
[official-gcc.git] / gcc / tree-vectorizer.c
bloba2e13acb6d223b4db16356e4167a4e66536878a3
1 /* Vectorizer
2 Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 /* Loop and basic block vectorizer.
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
29 The rest of the vectorizer's code is organized as follows:
30 - tree-vect-loop.c - loop specific parts such as reductions, etc. These are
31 used by drivers (1) and (2).
32 - tree-vect-loop-manip.c - vectorizer's loop control-flow utilities, used by
33 drivers (1) and (2).
34 - tree-vect-slp.c - BB vectorization specific analysis and transformation,
35 used by drivers (2) and (3).
36 - tree-vect-stmts.c - statements analysis and transformation (used by all).
37 - tree-vect-data-refs.c - vectorizer specific data-refs analysis and
38 manipulations (used by all).
39 - tree-vect-patterns.c - vectorizable code patterns detector (used by all)
41 Here's a poor attempt at illustrating that:
43 tree-vectorizer.c:
44 loop_vect() loop_aware_slp() slp_vect()
45 | / \ /
46 | / \ /
47 tree-vect-loop.c tree-vect-slp.c
48 | \ \ / / |
49 | \ \/ / |
50 | \ /\ / |
51 | \ / \ / |
52 tree-vect-stmts.c tree-vect-data-refs.c
53 \ /
54 tree-vect-patterns.c
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"
86 /* Loop or bb location, with hotness information. */
87 dump_user_location_t vect_location;
89 /* auto_purge_vect_location's dtor: reset the vect_location
90 global, to avoid stale location_t values that could reference
91 GC-ed blocks. */
93 auto_purge_vect_location::~auto_purge_vect_location ()
95 vect_location = dump_user_location_t ();
98 /* Dump a cost entry according to args to F. */
100 void
101 dump_stmt_cost (FILE *f, int count, enum vect_cost_for_stmt kind,
102 stmt_vec_info stmt_info, tree, int misalign, unsigned cost,
103 enum vect_cost_model_location where)
105 if (stmt_info)
107 print_gimple_expr (f, STMT_VINFO_STMT (stmt_info), 0, TDF_SLIM);
108 fprintf (f, " ");
110 else
111 fprintf (f, "<unknown> ");
112 fprintf (f, "%d times ", count);
113 const char *ks = "unknown";
114 switch (kind)
116 case scalar_stmt:
117 ks = "scalar_stmt";
118 break;
119 case scalar_load:
120 ks = "scalar_load";
121 break;
122 case scalar_store:
123 ks = "scalar_store";
124 break;
125 case vector_stmt:
126 ks = "vector_stmt";
127 break;
128 case vector_load:
129 ks = "vector_load";
130 break;
131 case vector_gather_load:
132 ks = "vector_gather_load";
133 break;
134 case unaligned_load:
135 ks = "unaligned_load";
136 break;
137 case unaligned_store:
138 ks = "unaligned_store";
139 break;
140 case vector_store:
141 ks = "vector_store";
142 break;
143 case vector_scatter_store:
144 ks = "vector_scatter_store";
145 break;
146 case vec_to_scalar:
147 ks = "vec_to_scalar";
148 break;
149 case scalar_to_vec:
150 ks = "scalar_to_vec";
151 break;
152 case cond_branch_not_taken:
153 ks = "cond_branch_not_taken";
154 break;
155 case cond_branch_taken:
156 ks = "cond_branch_taken";
157 break;
158 case vec_perm:
159 ks = "vec_perm";
160 break;
161 case vec_promote_demote:
162 ks = "vec_promote_demote";
163 break;
164 case vec_construct:
165 ks = "vec_construct";
166 break;
168 fprintf (f, "%s ", ks);
169 if (kind == unaligned_load || kind == unaligned_store)
170 fprintf (f, "(misalign %d) ", misalign);
171 fprintf (f, "costs %u ", cost);
172 const char *ws = "unknown";
173 switch (where)
175 case vect_prologue:
176 ws = "prologue";
177 break;
178 case vect_body:
179 ws = "body";
180 break;
181 case vect_epilogue:
182 ws = "epilogue";
183 break;
185 fprintf (f, "in %s\n", ws);
188 /* For mapping simduid to vectorization factor. */
190 class simduid_to_vf : public free_ptr_hash<simduid_to_vf>
192 public:
193 unsigned int simduid;
194 poly_uint64 vf;
196 /* hash_table support. */
197 static inline hashval_t hash (const simduid_to_vf *);
198 static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
201 inline hashval_t
202 simduid_to_vf::hash (const simduid_to_vf *p)
204 return p->simduid;
207 inline int
208 simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
210 return p1->simduid == p2->simduid;
213 /* This hash maps the OMP simd array to the corresponding simduid used
214 to index into it. Like thus,
216 _7 = GOMP_SIMD_LANE (simduid.0)
219 D.1737[_7] = stuff;
222 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
223 simduid.0. */
225 struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
227 tree decl;
228 unsigned int simduid;
230 /* hash_table support. */
231 static inline hashval_t hash (const simd_array_to_simduid *);
232 static inline int equal (const simd_array_to_simduid *,
233 const simd_array_to_simduid *);
236 inline hashval_t
237 simd_array_to_simduid::hash (const simd_array_to_simduid *p)
239 return DECL_UID (p->decl);
242 inline int
243 simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
244 const simd_array_to_simduid *p2)
246 return p1->decl == p2->decl;
249 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LAST_LANE,
250 into their corresponding constants and remove
251 IFN_GOMP_SIMD_ORDERED_{START,END}. */
253 static void
254 adjust_simduid_builtins (hash_table<simduid_to_vf> *htab)
256 basic_block bb;
258 FOR_EACH_BB_FN (bb, cfun)
260 gimple_stmt_iterator i;
262 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
264 poly_uint64 vf = 1;
265 enum internal_fn ifn;
266 gimple *stmt = gsi_stmt (i);
267 tree t;
268 if (!is_gimple_call (stmt)
269 || !gimple_call_internal_p (stmt))
271 gsi_next (&i);
272 continue;
274 ifn = gimple_call_internal_fn (stmt);
275 switch (ifn)
277 case IFN_GOMP_SIMD_LANE:
278 case IFN_GOMP_SIMD_VF:
279 case IFN_GOMP_SIMD_LAST_LANE:
280 break;
281 case IFN_GOMP_SIMD_ORDERED_START:
282 case IFN_GOMP_SIMD_ORDERED_END:
283 if (integer_onep (gimple_call_arg (stmt, 0)))
285 enum built_in_function bcode
286 = (ifn == IFN_GOMP_SIMD_ORDERED_START
287 ? BUILT_IN_GOMP_ORDERED_START
288 : BUILT_IN_GOMP_ORDERED_END);
289 gimple *g
290 = gimple_build_call (builtin_decl_explicit (bcode), 0);
291 gimple_move_vops (g, stmt);
292 gsi_replace (&i, g, true);
293 continue;
295 gsi_remove (&i, true);
296 unlink_stmt_vdef (stmt);
297 continue;
298 default:
299 gsi_next (&i);
300 continue;
302 tree arg = gimple_call_arg (stmt, 0);
303 gcc_assert (arg != NULL_TREE);
304 gcc_assert (TREE_CODE (arg) == SSA_NAME);
305 simduid_to_vf *p = NULL, data;
306 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
307 /* Need to nullify loop safelen field since it's value is not
308 valid after transformation. */
309 if (bb->loop_father && bb->loop_father->safelen > 0)
310 bb->loop_father->safelen = 0;
311 if (htab)
313 p = htab->find (&data);
314 if (p)
315 vf = p->vf;
317 switch (ifn)
319 case IFN_GOMP_SIMD_VF:
320 t = build_int_cst (unsigned_type_node, vf);
321 break;
322 case IFN_GOMP_SIMD_LANE:
323 t = build_int_cst (unsigned_type_node, 0);
324 break;
325 case IFN_GOMP_SIMD_LAST_LANE:
326 t = gimple_call_arg (stmt, 1);
327 break;
328 default:
329 gcc_unreachable ();
331 tree lhs = gimple_call_lhs (stmt);
332 if (lhs)
333 replace_uses_by (lhs, t);
334 release_defs (stmt);
335 gsi_remove (&i, true);
340 /* Helper structure for note_simd_array_uses. */
342 struct note_simd_array_uses_struct
344 hash_table<simd_array_to_simduid> **htab;
345 unsigned int simduid;
348 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
350 static tree
351 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
353 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
354 struct note_simd_array_uses_struct *ns
355 = (struct note_simd_array_uses_struct *) wi->info;
357 if (TYPE_P (*tp))
358 *walk_subtrees = 0;
359 else if (VAR_P (*tp)
360 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
361 && DECL_CONTEXT (*tp) == current_function_decl)
363 simd_array_to_simduid data;
364 if (!*ns->htab)
365 *ns->htab = new hash_table<simd_array_to_simduid> (15);
366 data.decl = *tp;
367 data.simduid = ns->simduid;
368 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
369 if (*slot == NULL)
371 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
372 *p = data;
373 *slot = p;
375 else if ((*slot)->simduid != ns->simduid)
376 (*slot)->simduid = -1U;
377 *walk_subtrees = 0;
379 return NULL_TREE;
382 /* Find "omp simd array" temporaries and map them to corresponding
383 simduid. */
385 static void
386 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
388 basic_block bb;
389 gimple_stmt_iterator gsi;
390 struct walk_stmt_info wi;
391 struct note_simd_array_uses_struct ns;
393 memset (&wi, 0, sizeof (wi));
394 wi.info = &ns;
395 ns.htab = htab;
397 FOR_EACH_BB_FN (bb, cfun)
398 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
400 gimple *stmt = gsi_stmt (gsi);
401 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
402 continue;
403 switch (gimple_call_internal_fn (stmt))
405 case IFN_GOMP_SIMD_LANE:
406 case IFN_GOMP_SIMD_VF:
407 case IFN_GOMP_SIMD_LAST_LANE:
408 break;
409 default:
410 continue;
412 tree lhs = gimple_call_lhs (stmt);
413 if (lhs == NULL_TREE)
414 continue;
415 imm_use_iterator use_iter;
416 gimple *use_stmt;
417 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
418 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
419 if (!is_gimple_debug (use_stmt))
420 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
424 /* Shrink arrays with "omp simd array" attribute to the corresponding
425 vectorization factor. */
427 static void
428 shrink_simd_arrays
429 (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
430 hash_table<simduid_to_vf> *simduid_to_vf_htab)
432 for (hash_table<simd_array_to_simduid>::iterator iter
433 = simd_array_to_simduid_htab->begin ();
434 iter != simd_array_to_simduid_htab->end (); ++iter)
435 if ((*iter)->simduid != -1U)
437 tree decl = (*iter)->decl;
438 poly_uint64 vf = 1;
439 if (simduid_to_vf_htab)
441 simduid_to_vf *p = NULL, data;
442 data.simduid = (*iter)->simduid;
443 p = simduid_to_vf_htab->find (&data);
444 if (p)
445 vf = p->vf;
447 tree atype
448 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
449 TREE_TYPE (decl) = atype;
450 relayout_decl (decl);
453 delete simd_array_to_simduid_htab;
456 /* Initialize the vec_info with kind KIND_IN and target cost data
457 TARGET_COST_DATA_IN. */
459 vec_info::vec_info (vec_info::vec_kind kind_in, vec_info_shared *shared_)
460 : kind (kind_in),
461 shared (shared_),
462 stmt_vec_info_ro (false),
463 target_cost_data (nullptr)
465 stmt_vec_infos.create (50);
468 vec_info::~vec_info ()
470 for (slp_instance &instance : slp_instances)
471 vect_free_slp_instance (instance);
473 delete target_cost_data;
474 free_stmt_vec_infos ();
477 vec_info_shared::vec_info_shared ()
478 : n_stmts (0),
479 datarefs (vNULL),
480 datarefs_copy (vNULL),
481 ddrs (vNULL)
485 vec_info_shared::~vec_info_shared ()
487 free_data_refs (datarefs);
488 free_dependence_relations (ddrs);
489 datarefs_copy.release ();
492 void
493 vec_info_shared::save_datarefs ()
495 if (!flag_checking)
496 return;
497 datarefs_copy.reserve_exact (datarefs.length ());
498 for (unsigned i = 0; i < datarefs.length (); ++i)
499 datarefs_copy.quick_push (*datarefs[i]);
502 void
503 vec_info_shared::check_datarefs ()
505 if (!flag_checking)
506 return;
507 gcc_assert (datarefs.length () == datarefs_copy.length ());
508 for (unsigned i = 0; i < datarefs.length (); ++i)
509 if (memcmp (&datarefs_copy[i], datarefs[i],
510 offsetof (data_reference, alt_indices)) != 0)
511 gcc_unreachable ();
514 /* Record that STMT belongs to the vectorizable region. Create and return
515 an associated stmt_vec_info. */
517 stmt_vec_info
518 vec_info::add_stmt (gimple *stmt)
520 stmt_vec_info res = new_stmt_vec_info (stmt);
521 set_vinfo_for_stmt (stmt, res);
522 return res;
525 /* Record that STMT belongs to the vectorizable region. Create a new
526 stmt_vec_info and mark VECINFO as being related and return the new
527 stmt_vec_info. */
529 stmt_vec_info
530 vec_info::add_pattern_stmt (gimple *stmt, stmt_vec_info stmt_info)
532 stmt_vec_info res = new_stmt_vec_info (stmt);
533 set_vinfo_for_stmt (stmt, res, false);
534 STMT_VINFO_RELATED_STMT (res) = stmt_info;
535 return res;
538 /* If STMT has an associated stmt_vec_info, return that vec_info, otherwise
539 return null. It is safe to call this function on any statement, even if
540 it might not be part of the vectorizable region. */
542 stmt_vec_info
543 vec_info::lookup_stmt (gimple *stmt)
545 unsigned int uid = gimple_uid (stmt);
546 if (uid > 0 && uid - 1 < stmt_vec_infos.length ())
548 stmt_vec_info res = stmt_vec_infos[uid - 1];
549 if (res && res->stmt == stmt)
550 return res;
552 return NULL;
555 /* If NAME is an SSA_NAME and its definition has an associated stmt_vec_info,
556 return that stmt_vec_info, otherwise return null. It is safe to call
557 this on arbitrary operands. */
559 stmt_vec_info
560 vec_info::lookup_def (tree name)
562 if (TREE_CODE (name) == SSA_NAME
563 && !SSA_NAME_IS_DEFAULT_DEF (name))
564 return lookup_stmt (SSA_NAME_DEF_STMT (name));
565 return NULL;
568 /* See whether there is a single non-debug statement that uses LHS and
569 whether that statement has an associated stmt_vec_info. Return the
570 stmt_vec_info if so, otherwise return null. */
572 stmt_vec_info
573 vec_info::lookup_single_use (tree lhs)
575 use_operand_p dummy;
576 gimple *use_stmt;
577 if (single_imm_use (lhs, &dummy, &use_stmt))
578 return lookup_stmt (use_stmt);
579 return NULL;
582 /* Return vectorization information about DR. */
584 dr_vec_info *
585 vec_info::lookup_dr (data_reference *dr)
587 stmt_vec_info stmt_info = lookup_stmt (DR_STMT (dr));
588 /* DR_STMT should never refer to a stmt in a pattern replacement. */
589 gcc_checking_assert (!is_pattern_stmt_p (stmt_info));
590 return STMT_VINFO_DR_INFO (stmt_info->dr_aux.stmt);
593 /* Record that NEW_STMT_INFO now implements the same data reference
594 as OLD_STMT_INFO. */
596 void
597 vec_info::move_dr (stmt_vec_info new_stmt_info, stmt_vec_info old_stmt_info)
599 gcc_assert (!is_pattern_stmt_p (old_stmt_info));
600 STMT_VINFO_DR_INFO (old_stmt_info)->stmt = new_stmt_info;
601 new_stmt_info->dr_aux = old_stmt_info->dr_aux;
602 STMT_VINFO_DR_WRT_VEC_LOOP (new_stmt_info)
603 = STMT_VINFO_DR_WRT_VEC_LOOP (old_stmt_info);
604 STMT_VINFO_GATHER_SCATTER_P (new_stmt_info)
605 = STMT_VINFO_GATHER_SCATTER_P (old_stmt_info);
608 /* Permanently remove the statement described by STMT_INFO from the
609 function. */
611 void
612 vec_info::remove_stmt (stmt_vec_info stmt_info)
614 gcc_assert (!stmt_info->pattern_stmt_p);
615 set_vinfo_for_stmt (stmt_info->stmt, NULL);
616 unlink_stmt_vdef (stmt_info->stmt);
617 gimple_stmt_iterator si = gsi_for_stmt (stmt_info->stmt);
618 gsi_remove (&si, true);
619 release_defs (stmt_info->stmt);
620 free_stmt_vec_info (stmt_info);
623 /* Replace the statement at GSI by NEW_STMT, both the vectorization
624 information and the function itself. STMT_INFO describes the statement
625 at GSI. */
627 void
628 vec_info::replace_stmt (gimple_stmt_iterator *gsi, stmt_vec_info stmt_info,
629 gimple *new_stmt)
631 gimple *old_stmt = stmt_info->stmt;
632 gcc_assert (!stmt_info->pattern_stmt_p && old_stmt == gsi_stmt (*gsi));
633 gimple_set_uid (new_stmt, gimple_uid (old_stmt));
634 stmt_info->stmt = new_stmt;
635 gsi_replace (gsi, new_stmt, true);
638 /* Insert stmts in SEQ on the VEC_INFO region entry. If CONTEXT is
639 not NULL it specifies whether to use the sub-region entry
640 determined by it, currently used for loop vectorization to insert
641 on the inner loop entry vs. the outer loop entry. */
643 void
644 vec_info::insert_seq_on_entry (stmt_vec_info context, gimple_seq seq)
646 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (this))
648 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
649 basic_block new_bb;
650 edge pe;
652 if (context && nested_in_vect_loop_p (loop, context))
653 loop = loop->inner;
655 pe = loop_preheader_edge (loop);
656 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
657 gcc_assert (!new_bb);
659 else
661 bb_vec_info bb_vinfo = as_a <bb_vec_info> (this);
662 gimple_stmt_iterator gsi_region_begin
663 = gsi_after_labels (bb_vinfo->bbs[0]);
664 gsi_insert_seq_before (&gsi_region_begin, seq, GSI_SAME_STMT);
668 /* Like insert_seq_on_entry but just inserts the single stmt NEW_STMT. */
670 void
671 vec_info::insert_on_entry (stmt_vec_info context, gimple *new_stmt)
673 gimple_seq seq = NULL;
674 gimple_stmt_iterator gsi = gsi_start (seq);
675 gsi_insert_before_without_update (&gsi, new_stmt, GSI_SAME_STMT);
676 insert_seq_on_entry (context, seq);
679 /* Create and initialize a new stmt_vec_info struct for STMT. */
681 stmt_vec_info
682 vec_info::new_stmt_vec_info (gimple *stmt)
684 stmt_vec_info res = XCNEW (class _stmt_vec_info);
685 res->stmt = stmt;
687 STMT_VINFO_TYPE (res) = undef_vec_info_type;
688 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
689 STMT_VINFO_VECTORIZABLE (res) = true;
690 STMT_VINFO_REDUC_TYPE (res) = TREE_CODE_REDUCTION;
691 STMT_VINFO_REDUC_CODE (res) = ERROR_MARK;
692 STMT_VINFO_REDUC_FN (res) = IFN_LAST;
693 STMT_VINFO_REDUC_IDX (res) = -1;
694 STMT_VINFO_SLP_VECT_ONLY (res) = false;
695 STMT_VINFO_SLP_VECT_ONLY_PATTERN (res) = false;
696 STMT_VINFO_VEC_STMTS (res) = vNULL;
697 res->reduc_initial_values = vNULL;
698 res->reduc_scalar_results = vNULL;
700 if (is_a <loop_vec_info> (this)
701 && gimple_code (stmt) == GIMPLE_PHI
702 && is_loop_header_bb_p (gimple_bb (stmt)))
703 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
704 else
705 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
707 STMT_SLP_TYPE (res) = loop_vect;
709 /* This is really "uninitialized" until vect_compute_data_ref_alignment. */
710 res->dr_aux.misalignment = DR_MISALIGNMENT_UNINITIALIZED;
712 return res;
715 /* Associate STMT with INFO. */
717 void
718 vec_info::set_vinfo_for_stmt (gimple *stmt, stmt_vec_info info, bool check_ro)
720 unsigned int uid = gimple_uid (stmt);
721 if (uid == 0)
723 gcc_assert (!check_ro || !stmt_vec_info_ro);
724 gcc_checking_assert (info);
725 uid = stmt_vec_infos.length () + 1;
726 gimple_set_uid (stmt, uid);
727 stmt_vec_infos.safe_push (info);
729 else
731 gcc_checking_assert (info == NULL);
732 stmt_vec_infos[uid - 1] = info;
736 /* Free the contents of stmt_vec_infos. */
738 void
739 vec_info::free_stmt_vec_infos (void)
741 for (stmt_vec_info &info : stmt_vec_infos)
742 if (info != NULL)
743 free_stmt_vec_info (info);
744 stmt_vec_infos.release ();
747 /* Free STMT_INFO. */
749 void
750 vec_info::free_stmt_vec_info (stmt_vec_info stmt_info)
752 if (stmt_info->pattern_stmt_p)
754 gimple_set_bb (stmt_info->stmt, NULL);
755 tree lhs = gimple_get_lhs (stmt_info->stmt);
756 if (lhs && TREE_CODE (lhs) == SSA_NAME)
757 release_ssa_name (lhs);
760 stmt_info->reduc_initial_values.release ();
761 stmt_info->reduc_scalar_results.release ();
762 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
763 STMT_VINFO_VEC_STMTS (stmt_info).release ();
764 free (stmt_info);
767 /* Returns true if S1 dominates S2. */
769 bool
770 vect_stmt_dominates_stmt_p (gimple *s1, gimple *s2)
772 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
774 /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
775 SSA_NAME. Assume it lives at the beginning of function and
776 thus dominates everything. */
777 if (!bb1 || s1 == s2)
778 return true;
780 /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
781 if (!bb2)
782 return false;
784 if (bb1 != bb2)
785 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
787 /* PHIs in the same basic block are assumed to be
788 executed all in parallel, if only one stmt is a PHI,
789 it dominates the other stmt in the same basic block. */
790 if (gimple_code (s1) == GIMPLE_PHI)
791 return true;
793 if (gimple_code (s2) == GIMPLE_PHI)
794 return false;
796 /* Inserted vectorized stmts all have UID 0 while the original stmts
797 in the IL have UID increasing within a BB. Walk from both sides
798 until we find the other stmt or a stmt with UID != 0. */
799 gimple_stmt_iterator gsi1 = gsi_for_stmt (s1);
800 while (gimple_uid (gsi_stmt (gsi1)) == 0)
802 gsi_next (&gsi1);
803 if (gsi_end_p (gsi1))
804 return false;
805 if (gsi_stmt (gsi1) == s2)
806 return true;
808 if (gimple_uid (gsi_stmt (gsi1)) == -1u)
809 return false;
811 gimple_stmt_iterator gsi2 = gsi_for_stmt (s2);
812 while (gimple_uid (gsi_stmt (gsi2)) == 0)
814 gsi_prev (&gsi2);
815 if (gsi_end_p (gsi2))
816 return false;
817 if (gsi_stmt (gsi2) == s1)
818 return true;
820 if (gimple_uid (gsi_stmt (gsi2)) == -1u)
821 return false;
823 if (gimple_uid (gsi_stmt (gsi1)) <= gimple_uid (gsi_stmt (gsi2)))
824 return true;
825 return false;
828 /* A helper function to free scev and LOOP niter information, as well as
829 clear loop constraint LOOP_C_FINITE. */
831 void
832 vect_free_loop_info_assumptions (class loop *loop)
834 scev_reset_htab ();
835 /* We need to explicitly reset upper bound information since they are
836 used even after free_numbers_of_iterations_estimates. */
837 loop->any_upper_bound = false;
838 loop->any_likely_upper_bound = false;
839 free_numbers_of_iterations_estimates (loop);
840 loop_constraint_clear (loop, LOOP_C_FINITE);
843 /* If LOOP has been versioned during ifcvt, return the internal call
844 guarding it. */
846 gimple *
847 vect_loop_vectorized_call (class loop *loop, gcond **cond)
849 basic_block bb = loop_preheader_edge (loop)->src;
850 gimple *g;
853 g = last_stmt (bb);
854 if ((g && gimple_code (g) == GIMPLE_COND)
855 || !single_succ_p (bb))
856 break;
857 if (!single_pred_p (bb))
858 break;
859 bb = single_pred (bb);
861 while (1);
862 if (g && gimple_code (g) == GIMPLE_COND)
864 if (cond)
865 *cond = as_a <gcond *> (g);
866 gimple_stmt_iterator gsi = gsi_for_stmt (g);
867 gsi_prev (&gsi);
868 if (!gsi_end_p (gsi))
870 g = gsi_stmt (gsi);
871 if (gimple_call_internal_p (g, IFN_LOOP_VECTORIZED)
872 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
873 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
874 return g;
877 return NULL;
880 /* If LOOP has been versioned during loop distribution, return the gurading
881 internal call. */
883 static gimple *
884 vect_loop_dist_alias_call (class loop *loop)
886 basic_block bb;
887 basic_block entry;
888 class loop *outer, *orig;
889 gimple_stmt_iterator gsi;
890 gimple *g;
892 if (loop->orig_loop_num == 0)
893 return NULL;
895 orig = get_loop (cfun, loop->orig_loop_num);
896 if (orig == NULL)
898 /* The original loop is somehow destroyed. Clear the information. */
899 loop->orig_loop_num = 0;
900 return NULL;
903 if (loop != orig)
904 bb = nearest_common_dominator (CDI_DOMINATORS, loop->header, orig->header);
905 else
906 bb = loop_preheader_edge (loop)->src;
908 outer = bb->loop_father;
909 entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
911 /* Look upward in dominance tree. */
912 for (; bb != entry && flow_bb_inside_loop_p (outer, bb);
913 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
915 g = last_stmt (bb);
916 if (g == NULL || gimple_code (g) != GIMPLE_COND)
917 continue;
919 gsi = gsi_for_stmt (g);
920 gsi_prev (&gsi);
921 if (gsi_end_p (gsi))
922 continue;
924 g = gsi_stmt (gsi);
925 /* The guarding internal function call must have the same distribution
926 alias id. */
927 if (gimple_call_internal_p (g, IFN_LOOP_DIST_ALIAS)
928 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->orig_loop_num))
929 return g;
931 return NULL;
934 /* Set the uids of all the statements in basic blocks inside loop
935 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
936 call guarding the loop which has been if converted. */
937 static void
938 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
940 tree arg = gimple_call_arg (loop_vectorized_call, 1);
941 basic_block *bbs;
942 unsigned int i;
943 class loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
945 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
946 gcc_checking_assert (vect_loop_vectorized_call (scalar_loop)
947 == loop_vectorized_call);
948 /* If we are going to vectorize outer loop, prevent vectorization
949 of the inner loop in the scalar loop - either the scalar loop is
950 thrown away, so it is a wasted work, or is used only for
951 a few iterations. */
952 if (scalar_loop->inner)
954 gimple *g = vect_loop_vectorized_call (scalar_loop->inner);
955 if (g)
957 arg = gimple_call_arg (g, 0);
958 get_loop (cfun, tree_to_shwi (arg))->dont_vectorize = true;
959 fold_loop_internal_call (g, boolean_false_node);
962 bbs = get_loop_body (scalar_loop);
963 for (i = 0; i < scalar_loop->num_nodes; i++)
965 basic_block bb = bbs[i];
966 gimple_stmt_iterator gsi;
967 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
969 gimple *phi = gsi_stmt (gsi);
970 gimple_set_uid (phi, 0);
972 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
974 gimple *stmt = gsi_stmt (gsi);
975 gimple_set_uid (stmt, 0);
978 free (bbs);
981 /* Generate vectorized code for LOOP and its epilogues. */
983 static void
984 vect_transform_loops (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
985 loop_p loop, gimple *loop_vectorized_call)
987 loop_vec_info loop_vinfo = loop_vec_info_for_loop (loop);
989 if (loop_vectorized_call)
990 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
992 unsigned HOST_WIDE_INT bytes;
993 if (dump_enabled_p ())
995 if (GET_MODE_SIZE (loop_vinfo->vector_mode).is_constant (&bytes))
996 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
997 "loop vectorized using %wu byte vectors\n", bytes);
998 else
999 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1000 "loop vectorized using variable length vectors\n");
1003 loop_p new_loop = vect_transform_loop (loop_vinfo,
1004 loop_vectorized_call);
1005 /* Now that the loop has been vectorized, allow it to be unrolled
1006 etc. */
1007 loop->force_vectorize = false;
1009 if (loop->simduid)
1011 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
1012 if (!simduid_to_vf_htab)
1013 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
1014 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
1015 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
1016 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
1017 = simduid_to_vf_data;
1020 /* Epilogue of vectorized loop must be vectorized too. */
1021 if (new_loop)
1022 vect_transform_loops (simduid_to_vf_htab, new_loop, NULL);
1025 /* Try to vectorize LOOP. */
1027 static unsigned
1028 try_vectorize_loop_1 (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1029 unsigned *num_vectorized_loops, loop_p loop,
1030 gimple *loop_vectorized_call,
1031 gimple *loop_dist_alias_call)
1033 unsigned ret = 0;
1034 vec_info_shared shared;
1035 auto_purge_vect_location sentinel;
1036 vect_location = find_loop_location (loop);
1038 if (LOCATION_LOCUS (vect_location.get_location_t ()) != UNKNOWN_LOCATION
1039 && dump_enabled_p ())
1040 dump_printf (MSG_NOTE | MSG_PRIORITY_INTERNALS,
1041 "\nAnalyzing loop at %s:%d\n",
1042 LOCATION_FILE (vect_location.get_location_t ()),
1043 LOCATION_LINE (vect_location.get_location_t ()));
1045 /* Try to analyze the loop, retaining an opt_problem if dump_enabled_p. */
1046 opt_loop_vec_info loop_vinfo = vect_analyze_loop (loop, &shared);
1047 loop->aux = loop_vinfo;
1049 if (!loop_vinfo)
1050 if (dump_enabled_p ())
1051 if (opt_problem *problem = loop_vinfo.get_problem ())
1053 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1054 "couldn't vectorize loop\n");
1055 problem->emit_and_clear ();
1058 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
1060 /* Free existing information if loop is analyzed with some
1061 assumptions. */
1062 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1063 vect_free_loop_info_assumptions (loop);
1065 /* If we applied if-conversion then try to vectorize the
1066 BB of innermost loops.
1067 ??? Ideally BB vectorization would learn to vectorize
1068 control flow by applying if-conversion on-the-fly, the
1069 following retains the if-converted loop body even when
1070 only non-if-converted parts took part in BB vectorization. */
1071 if (flag_tree_slp_vectorize != 0
1072 && loop_vectorized_call
1073 && ! loop->inner)
1075 basic_block bb = loop->header;
1076 bool require_loop_vectorize = false;
1077 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1078 !gsi_end_p (gsi); gsi_next (&gsi))
1080 gimple *stmt = gsi_stmt (gsi);
1081 gcall *call = dyn_cast <gcall *> (stmt);
1082 if (call && gimple_call_internal_p (call))
1084 internal_fn ifn = gimple_call_internal_fn (call);
1085 if (ifn == IFN_MASK_LOAD || ifn == IFN_MASK_STORE
1086 /* Don't keep the if-converted parts when the ifn with
1087 specifc type is not supported by the backend. */
1088 || (direct_internal_fn_p (ifn)
1089 && !direct_internal_fn_supported_p
1090 (call, OPTIMIZE_FOR_SPEED)))
1092 require_loop_vectorize = true;
1093 break;
1096 gimple_set_uid (stmt, -1);
1097 gimple_set_visited (stmt, false);
1099 if (!require_loop_vectorize)
1101 tree arg = gimple_call_arg (loop_vectorized_call, 1);
1102 class loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
1103 if (vect_slp_if_converted_bb (bb, scalar_loop))
1105 fold_loop_internal_call (loop_vectorized_call,
1106 boolean_true_node);
1107 loop_vectorized_call = NULL;
1108 ret |= TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
1112 /* If outer loop vectorization fails for LOOP_VECTORIZED guarded
1113 loop, don't vectorize its inner loop; we'll attempt to
1114 vectorize LOOP_VECTORIZED guarded inner loop of the scalar
1115 loop version. */
1116 if (loop_vectorized_call && loop->inner)
1117 loop->inner->dont_vectorize = true;
1118 return ret;
1121 if (!dbg_cnt (vect_loop))
1123 /* Free existing information if loop is analyzed with some
1124 assumptions. */
1125 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1126 vect_free_loop_info_assumptions (loop);
1127 return ret;
1130 (*num_vectorized_loops)++;
1131 /* Transform LOOP and its epilogues. */
1132 vect_transform_loops (simduid_to_vf_htab, loop, loop_vectorized_call);
1134 if (loop_vectorized_call)
1136 fold_loop_internal_call (loop_vectorized_call, boolean_true_node);
1137 ret |= TODO_cleanup_cfg;
1139 if (loop_dist_alias_call)
1141 tree value = gimple_call_arg (loop_dist_alias_call, 1);
1142 fold_loop_internal_call (loop_dist_alias_call, value);
1143 ret |= TODO_cleanup_cfg;
1146 return ret;
1149 /* Try to vectorize LOOP. */
1151 static unsigned
1152 try_vectorize_loop (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1153 unsigned *num_vectorized_loops, loop_p loop)
1155 if (!((flag_tree_loop_vectorize
1156 && optimize_loop_nest_for_speed_p (loop))
1157 || loop->force_vectorize))
1158 return 0;
1160 return try_vectorize_loop_1 (simduid_to_vf_htab, num_vectorized_loops, loop,
1161 vect_loop_vectorized_call (loop),
1162 vect_loop_dist_alias_call (loop));
1166 /* Function vectorize_loops.
1168 Entry point to loop vectorization phase. */
1170 unsigned
1171 vectorize_loops (void)
1173 unsigned int i;
1174 unsigned int num_vectorized_loops = 0;
1175 unsigned int vect_loops_num;
1176 class loop *loop;
1177 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
1178 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1179 bool any_ifcvt_loops = false;
1180 unsigned ret = 0;
1182 vect_loops_num = number_of_loops (cfun);
1184 /* Bail out if there are no loops. */
1185 if (vect_loops_num <= 1)
1186 return 0;
1188 vect_slp_init ();
1190 if (cfun->has_simduid_loops)
1191 note_simd_array_uses (&simd_array_to_simduid_htab);
1193 /* ----------- Analyze loops. ----------- */
1195 /* If some loop was duplicated, it gets bigger number
1196 than all previously defined loops. This fact allows us to run
1197 only over initial loops skipping newly generated ones. */
1198 for (auto loop : loops_list (cfun, 0))
1199 if (loop->dont_vectorize)
1201 any_ifcvt_loops = true;
1202 /* If-conversion sometimes versions both the outer loop
1203 (for the case when outer loop vectorization might be
1204 desirable) as well as the inner loop in the scalar version
1205 of the loop. So we have:
1206 if (LOOP_VECTORIZED (1, 3))
1208 loop1
1209 loop2
1211 else
1212 loop3 (copy of loop1)
1213 if (LOOP_VECTORIZED (4, 5))
1214 loop4 (copy of loop2)
1215 else
1216 loop5 (copy of loop4)
1217 If loops' iteration gives us loop3 first (which has
1218 dont_vectorize set), make sure to process loop1 before loop4;
1219 so that we can prevent vectorization of loop4 if loop1
1220 is successfully vectorized. */
1221 if (loop->inner)
1223 gimple *loop_vectorized_call
1224 = vect_loop_vectorized_call (loop);
1225 if (loop_vectorized_call
1226 && vect_loop_vectorized_call (loop->inner))
1228 tree arg = gimple_call_arg (loop_vectorized_call, 0);
1229 class loop *vector_loop
1230 = get_loop (cfun, tree_to_shwi (arg));
1231 if (vector_loop && vector_loop != loop)
1233 /* Make sure we don't vectorize it twice. */
1234 vector_loop->dont_vectorize = true;
1235 ret |= try_vectorize_loop (simduid_to_vf_htab,
1236 &num_vectorized_loops,
1237 vector_loop);
1242 else
1243 ret |= try_vectorize_loop (simduid_to_vf_htab, &num_vectorized_loops,
1244 loop);
1246 vect_location = dump_user_location_t ();
1248 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
1249 if (dump_enabled_p ()
1250 || (num_vectorized_loops > 0 && dump_enabled_p ()))
1251 dump_printf_loc (MSG_NOTE, vect_location,
1252 "vectorized %u loops in function.\n",
1253 num_vectorized_loops);
1255 /* ----------- Finalize. ----------- */
1257 if (any_ifcvt_loops)
1258 for (i = 1; i < number_of_loops (cfun); i++)
1260 loop = get_loop (cfun, i);
1261 if (loop && loop->dont_vectorize)
1263 gimple *g = vect_loop_vectorized_call (loop);
1264 if (g)
1266 fold_loop_internal_call (g, boolean_false_node);
1267 ret |= TODO_cleanup_cfg;
1268 g = NULL;
1270 else
1271 g = vect_loop_dist_alias_call (loop);
1273 if (g)
1275 fold_loop_internal_call (g, boolean_false_node);
1276 ret |= TODO_cleanup_cfg;
1281 for (i = 1; i < number_of_loops (cfun); i++)
1283 loop_vec_info loop_vinfo;
1284 bool has_mask_store;
1286 loop = get_loop (cfun, i);
1287 if (!loop || !loop->aux)
1288 continue;
1289 loop_vinfo = (loop_vec_info) loop->aux;
1290 has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
1291 delete loop_vinfo;
1292 if (has_mask_store
1293 && targetm.vectorize.empty_mask_is_expensive (IFN_MASK_STORE))
1294 optimize_mask_stores (loop);
1295 loop->aux = NULL;
1298 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1299 if (cfun->has_simduid_loops)
1301 adjust_simduid_builtins (simduid_to_vf_htab);
1302 /* Avoid stale SCEV cache entries for the SIMD_LANE defs. */
1303 scev_reset ();
1306 /* Shrink any "omp array simd" temporary arrays to the
1307 actual vectorization factors. */
1308 if (simd_array_to_simduid_htab)
1309 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
1310 delete simduid_to_vf_htab;
1311 cfun->has_simduid_loops = false;
1312 vect_slp_fini ();
1314 if (num_vectorized_loops > 0)
1316 /* If we vectorized any loop only virtual SSA form needs to be updated.
1317 ??? Also while we try hard to update loop-closed SSA form we fail
1318 to properly do this in some corner-cases (see PR56286). */
1319 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
1320 return TODO_cleanup_cfg;
1323 return ret;
1327 /* Entry point to the simduid cleanup pass. */
1329 namespace {
1331 const pass_data pass_data_simduid_cleanup =
1333 GIMPLE_PASS, /* type */
1334 "simduid", /* name */
1335 OPTGROUP_NONE, /* optinfo_flags */
1336 TV_NONE, /* tv_id */
1337 ( PROP_ssa | PROP_cfg ), /* properties_required */
1338 0, /* properties_provided */
1339 0, /* properties_destroyed */
1340 0, /* todo_flags_start */
1341 0, /* todo_flags_finish */
1344 class pass_simduid_cleanup : public gimple_opt_pass
1346 public:
1347 pass_simduid_cleanup (gcc::context *ctxt)
1348 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
1351 /* opt_pass methods: */
1352 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
1353 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
1354 virtual unsigned int execute (function *);
1356 }; // class pass_simduid_cleanup
1358 unsigned int
1359 pass_simduid_cleanup::execute (function *fun)
1361 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1363 note_simd_array_uses (&simd_array_to_simduid_htab);
1365 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1366 adjust_simduid_builtins (NULL);
1368 /* Shrink any "omp array simd" temporary arrays to the
1369 actual vectorization factors. */
1370 if (simd_array_to_simduid_htab)
1371 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
1372 fun->has_simduid_loops = false;
1373 return 0;
1376 } // anon namespace
1378 gimple_opt_pass *
1379 make_pass_simduid_cleanup (gcc::context *ctxt)
1381 return new pass_simduid_cleanup (ctxt);
1385 /* Entry point to basic block SLP phase. */
1387 namespace {
1389 const pass_data pass_data_slp_vectorize =
1391 GIMPLE_PASS, /* type */
1392 "slp", /* name */
1393 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1394 TV_TREE_SLP_VECTORIZATION, /* tv_id */
1395 ( PROP_ssa | PROP_cfg ), /* properties_required */
1396 0, /* properties_provided */
1397 0, /* properties_destroyed */
1398 0, /* todo_flags_start */
1399 TODO_update_ssa, /* todo_flags_finish */
1402 class pass_slp_vectorize : public gimple_opt_pass
1404 public:
1405 pass_slp_vectorize (gcc::context *ctxt)
1406 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
1409 /* opt_pass methods: */
1410 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
1411 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
1412 virtual unsigned int execute (function *);
1414 }; // class pass_slp_vectorize
1416 unsigned int
1417 pass_slp_vectorize::execute (function *fun)
1419 auto_purge_vect_location sentinel;
1420 basic_block bb;
1422 bool in_loop_pipeline = scev_initialized_p ();
1423 if (!in_loop_pipeline)
1425 loop_optimizer_init (LOOPS_NORMAL);
1426 scev_initialize ();
1429 /* Mark all stmts as not belonging to the current region and unvisited. */
1430 FOR_EACH_BB_FN (bb, fun)
1432 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1433 gsi_next (&gsi))
1435 gphi *stmt = gsi.phi ();
1436 gimple_set_uid (stmt, -1);
1437 gimple_set_visited (stmt, false);
1439 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1440 gsi_next (&gsi))
1442 gimple *stmt = gsi_stmt (gsi);
1443 gimple_set_uid (stmt, -1);
1444 gimple_set_visited (stmt, false);
1448 vect_slp_init ();
1450 vect_slp_function (fun);
1452 vect_slp_fini ();
1454 if (!in_loop_pipeline)
1456 scev_finalize ();
1457 loop_optimizer_finalize ();
1460 return 0;
1463 } // anon namespace
1465 gimple_opt_pass *
1466 make_pass_slp_vectorize (gcc::context *ctxt)
1468 return new pass_slp_vectorize (ctxt);
1472 /* Increase alignment of global arrays to improve vectorization potential.
1473 TODO:
1474 - Consider also structs that have an array field.
1475 - Use ipa analysis to prune arrays that can't be vectorized?
1476 This should involve global alignment analysis and in the future also
1477 array padding. */
1479 static unsigned get_vec_alignment_for_type (tree);
1480 static hash_map<tree, unsigned> *type_align_map;
1482 /* Return alignment of array's vector type corresponding to scalar type.
1483 0 if no vector type exists. */
1484 static unsigned
1485 get_vec_alignment_for_array_type (tree type)
1487 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1488 poly_uint64 array_size, vector_size;
1490 tree scalar_type = strip_array_types (type);
1491 tree vectype = get_related_vectype_for_scalar_type (VOIDmode, scalar_type);
1492 if (!vectype
1493 || !poly_int_tree_p (TYPE_SIZE (type), &array_size)
1494 || !poly_int_tree_p (TYPE_SIZE (vectype), &vector_size)
1495 || maybe_lt (array_size, vector_size))
1496 return 0;
1498 return TYPE_ALIGN (vectype);
1501 /* Return alignment of field having maximum alignment of vector type
1502 corresponding to it's scalar type. For now, we only consider fields whose
1503 offset is a multiple of it's vector alignment.
1504 0 if no suitable field is found. */
1505 static unsigned
1506 get_vec_alignment_for_record_type (tree type)
1508 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1510 unsigned max_align = 0, alignment;
1511 HOST_WIDE_INT offset;
1512 tree offset_tree;
1514 if (TYPE_PACKED (type))
1515 return 0;
1517 unsigned *slot = type_align_map->get (type);
1518 if (slot)
1519 return *slot;
1521 for (tree field = first_field (type);
1522 field != NULL_TREE;
1523 field = DECL_CHAIN (field))
1525 /* Skip if not FIELD_DECL or if alignment is set by user. */
1526 if (TREE_CODE (field) != FIELD_DECL
1527 || DECL_USER_ALIGN (field)
1528 || DECL_ARTIFICIAL (field))
1529 continue;
1531 /* We don't need to process the type further if offset is variable,
1532 since the offsets of remaining members will also be variable. */
1533 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
1534 || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
1535 break;
1537 /* Similarly stop processing the type if offset_tree
1538 does not fit in unsigned HOST_WIDE_INT. */
1539 offset_tree = bit_position (field);
1540 if (!tree_fits_uhwi_p (offset_tree))
1541 break;
1543 offset = tree_to_uhwi (offset_tree);
1544 alignment = get_vec_alignment_for_type (TREE_TYPE (field));
1546 /* Get maximum alignment of vectorized field/array among those members
1547 whose offset is multiple of the vector alignment. */
1548 if (alignment
1549 && (offset % alignment == 0)
1550 && (alignment > max_align))
1551 max_align = alignment;
1554 type_align_map->put (type, max_align);
1555 return max_align;
1558 /* Return alignment of vector type corresponding to decl's scalar type
1559 or 0 if it doesn't exist or the vector alignment is lesser than
1560 decl's alignment. */
1561 static unsigned
1562 get_vec_alignment_for_type (tree type)
1564 if (type == NULL_TREE)
1565 return 0;
1567 gcc_assert (TYPE_P (type));
1569 static unsigned alignment = 0;
1570 switch (TREE_CODE (type))
1572 case ARRAY_TYPE:
1573 alignment = get_vec_alignment_for_array_type (type);
1574 break;
1575 case RECORD_TYPE:
1576 alignment = get_vec_alignment_for_record_type (type);
1577 break;
1578 default:
1579 alignment = 0;
1580 break;
1583 return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
1586 /* Entry point to increase_alignment pass. */
1587 static unsigned int
1588 increase_alignment (void)
1590 varpool_node *vnode;
1592 vect_location = dump_user_location_t ();
1593 type_align_map = new hash_map<tree, unsigned>;
1595 /* Increase the alignment of all global arrays for vectorization. */
1596 FOR_EACH_DEFINED_VARIABLE (vnode)
1598 tree decl = vnode->decl;
1599 unsigned int alignment;
1601 if ((decl_in_symtab_p (decl)
1602 && !symtab_node::get (decl)->can_increase_alignment_p ())
1603 || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
1604 continue;
1606 alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
1607 if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
1609 vnode->increase_alignment (alignment);
1610 if (dump_enabled_p ())
1611 dump_printf (MSG_NOTE, "Increasing alignment of decl: %T\n", decl);
1615 delete type_align_map;
1616 return 0;
1620 namespace {
1622 const pass_data pass_data_ipa_increase_alignment =
1624 SIMPLE_IPA_PASS, /* type */
1625 "increase_alignment", /* name */
1626 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1627 TV_IPA_OPT, /* tv_id */
1628 0, /* properties_required */
1629 0, /* properties_provided */
1630 0, /* properties_destroyed */
1631 0, /* todo_flags_start */
1632 0, /* todo_flags_finish */
1635 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
1637 public:
1638 pass_ipa_increase_alignment (gcc::context *ctxt)
1639 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
1642 /* opt_pass methods: */
1643 virtual bool gate (function *)
1645 return flag_section_anchors && flag_tree_loop_vectorize;
1648 virtual unsigned int execute (function *) { return increase_alignment (); }
1650 }; // class pass_ipa_increase_alignment
1652 } // anon namespace
1654 simple_ipa_opt_pass *
1655 make_pass_ipa_increase_alignment (gcc::context *ctxt)
1657 return new pass_ipa_increase_alignment (ctxt);
1660 /* If the condition represented by T is a comparison or the SSA name
1661 result of a comparison, extract the comparison's operands. Represent
1662 T as NE_EXPR <T, 0> otherwise. */
1664 void
1665 scalar_cond_masked_key::get_cond_ops_from_tree (tree t)
1667 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_comparison)
1669 this->code = TREE_CODE (t);
1670 this->op0 = TREE_OPERAND (t, 0);
1671 this->op1 = TREE_OPERAND (t, 1);
1672 return;
1675 if (TREE_CODE (t) == SSA_NAME)
1676 if (gassign *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (t)))
1678 tree_code code = gimple_assign_rhs_code (stmt);
1679 if (TREE_CODE_CLASS (code) == tcc_comparison)
1681 this->code = code;
1682 this->op0 = gimple_assign_rhs1 (stmt);
1683 this->op1 = gimple_assign_rhs2 (stmt);
1684 return;
1688 this->code = NE_EXPR;
1689 this->op0 = t;
1690 this->op1 = build_zero_cst (TREE_TYPE (t));
1693 /* See the comment above the declaration for details. */
1695 unsigned int
1696 vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind,
1697 stmt_vec_info stmt_info, tree vectype,
1698 int misalign, vect_cost_model_location where)
1700 unsigned int cost
1701 = builtin_vectorization_cost (kind, vectype, misalign) * count;
1702 return record_stmt_cost (stmt_info, where, cost);
1705 /* See the comment above the declaration for details. */
1707 void
1708 vector_costs::finish_cost ()
1710 gcc_assert (!m_finished);
1711 m_finished = true;
1714 /* Record a base cost of COST units against WHERE. If STMT_INFO is
1715 nonnull, use it to adjust the cost based on execution frequency
1716 (where appropriate). */
1718 unsigned int
1719 vector_costs::record_stmt_cost (stmt_vec_info stmt_info,
1720 vect_cost_model_location where,
1721 unsigned int cost)
1723 cost = adjust_cost_for_freq (stmt_info, where, cost);
1724 m_costs[where] += cost;
1725 return cost;
1728 /* COST is the base cost we have calculated for an operation in location WHERE.
1729 If STMT_INFO is nonnull, use it to adjust the cost based on execution
1730 frequency (where appropriate). Return the adjusted cost. */
1732 unsigned int
1733 vector_costs::adjust_cost_for_freq (stmt_vec_info stmt_info,
1734 vect_cost_model_location where,
1735 unsigned int cost)
1737 /* Statements in an inner loop relative to the loop being
1738 vectorized are weighted more heavily. The value here is
1739 arbitrary and could potentially be improved with analysis. */
1740 if (where == vect_body
1741 && stmt_info
1742 && stmt_in_inner_loop_p (m_vinfo, stmt_info))
1744 loop_vec_info loop_vinfo = as_a<loop_vec_info> (m_vinfo);
1745 cost *= LOOP_VINFO_INNER_LOOP_COST_FACTOR (loop_vinfo);
1747 return cost;