[RS6000] Power10 ICE running gcc.target/powerpc/ppc-ne0-1.c
[official-gcc.git] / gcc / tree-vectorizer.c
blob778177a583b45f13dd92284e26a3bdb710d8d6de
1 /* Vectorizer
2 Copyright (C) 2003-2020 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, void *data, 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 fprintf (f, "%p ", data);
106 if (stmt_info)
108 print_gimple_expr (f, STMT_VINFO_STMT (stmt_info), 0, TDF_SLIM);
109 fprintf (f, " ");
111 else
112 fprintf (f, "<unknown> ");
113 fprintf (f, "%d times ", count);
114 const char *ks = "unknown";
115 switch (kind)
117 case scalar_stmt:
118 ks = "scalar_stmt";
119 break;
120 case scalar_load:
121 ks = "scalar_load";
122 break;
123 case scalar_store:
124 ks = "scalar_store";
125 break;
126 case vector_stmt:
127 ks = "vector_stmt";
128 break;
129 case vector_load:
130 ks = "vector_load";
131 break;
132 case vector_gather_load:
133 ks = "vector_gather_load";
134 break;
135 case unaligned_load:
136 ks = "unaligned_load";
137 break;
138 case unaligned_store:
139 ks = "unaligned_store";
140 break;
141 case vector_store:
142 ks = "vector_store";
143 break;
144 case vector_scatter_store:
145 ks = "vector_scatter_store";
146 break;
147 case vec_to_scalar:
148 ks = "vec_to_scalar";
149 break;
150 case scalar_to_vec:
151 ks = "scalar_to_vec";
152 break;
153 case cond_branch_not_taken:
154 ks = "cond_branch_not_taken";
155 break;
156 case cond_branch_taken:
157 ks = "cond_branch_taken";
158 break;
159 case vec_perm:
160 ks = "vec_perm";
161 break;
162 case vec_promote_demote:
163 ks = "vec_promote_demote";
164 break;
165 case vec_construct:
166 ks = "vec_construct";
167 break;
169 fprintf (f, "%s ", ks);
170 if (kind == unaligned_load || kind == unaligned_store)
171 fprintf (f, "(misalign %d) ", misalign);
172 fprintf (f, "costs %u ", cost);
173 const char *ws = "unknown";
174 switch (where)
176 case vect_prologue:
177 ws = "prologue";
178 break;
179 case vect_body:
180 ws = "body";
181 break;
182 case vect_epilogue:
183 ws = "epilogue";
184 break;
186 fprintf (f, "in %s\n", ws);
189 /* For mapping simduid to vectorization factor. */
191 class simduid_to_vf : public free_ptr_hash<simduid_to_vf>
193 public:
194 unsigned int simduid;
195 poly_uint64 vf;
197 /* hash_table support. */
198 static inline hashval_t hash (const simduid_to_vf *);
199 static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
202 inline hashval_t
203 simduid_to_vf::hash (const simduid_to_vf *p)
205 return p->simduid;
208 inline int
209 simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
211 return p1->simduid == p2->simduid;
214 /* This hash maps the OMP simd array to the corresponding simduid used
215 to index into it. Like thus,
217 _7 = GOMP_SIMD_LANE (simduid.0)
220 D.1737[_7] = stuff;
223 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
224 simduid.0. */
226 struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
228 tree decl;
229 unsigned int simduid;
231 /* hash_table support. */
232 static inline hashval_t hash (const simd_array_to_simduid *);
233 static inline int equal (const simd_array_to_simduid *,
234 const simd_array_to_simduid *);
237 inline hashval_t
238 simd_array_to_simduid::hash (const simd_array_to_simduid *p)
240 return DECL_UID (p->decl);
243 inline int
244 simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
245 const simd_array_to_simduid *p2)
247 return p1->decl == p2->decl;
250 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LAST_LANE,
251 into their corresponding constants and remove
252 IFN_GOMP_SIMD_ORDERED_{START,END}. */
254 static void
255 adjust_simduid_builtins (hash_table<simduid_to_vf> *htab)
257 basic_block bb;
259 FOR_EACH_BB_FN (bb, cfun)
261 gimple_stmt_iterator i;
263 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
265 poly_uint64 vf = 1;
266 enum internal_fn ifn;
267 gimple *stmt = gsi_stmt (i);
268 tree t;
269 if (!is_gimple_call (stmt)
270 || !gimple_call_internal_p (stmt))
272 gsi_next (&i);
273 continue;
275 ifn = gimple_call_internal_fn (stmt);
276 switch (ifn)
278 case IFN_GOMP_SIMD_LANE:
279 case IFN_GOMP_SIMD_VF:
280 case IFN_GOMP_SIMD_LAST_LANE:
281 break;
282 case IFN_GOMP_SIMD_ORDERED_START:
283 case IFN_GOMP_SIMD_ORDERED_END:
284 if (integer_onep (gimple_call_arg (stmt, 0)))
286 enum built_in_function bcode
287 = (ifn == IFN_GOMP_SIMD_ORDERED_START
288 ? BUILT_IN_GOMP_ORDERED_START
289 : BUILT_IN_GOMP_ORDERED_END);
290 gimple *g
291 = gimple_build_call (builtin_decl_explicit (bcode), 0);
292 gimple_move_vops (g, stmt);
293 gsi_replace (&i, g, true);
294 continue;
296 gsi_remove (&i, true);
297 unlink_stmt_vdef (stmt);
298 continue;
299 default:
300 gsi_next (&i);
301 continue;
303 tree arg = gimple_call_arg (stmt, 0);
304 gcc_assert (arg != NULL_TREE);
305 gcc_assert (TREE_CODE (arg) == SSA_NAME);
306 simduid_to_vf *p = NULL, data;
307 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
308 /* Need to nullify loop safelen field since it's value is not
309 valid after transformation. */
310 if (bb->loop_father && bb->loop_father->safelen > 0)
311 bb->loop_father->safelen = 0;
312 if (htab)
314 p = htab->find (&data);
315 if (p)
316 vf = p->vf;
318 switch (ifn)
320 case IFN_GOMP_SIMD_VF:
321 t = build_int_cst (unsigned_type_node, vf);
322 break;
323 case IFN_GOMP_SIMD_LANE:
324 t = build_int_cst (unsigned_type_node, 0);
325 break;
326 case IFN_GOMP_SIMD_LAST_LANE:
327 t = gimple_call_arg (stmt, 1);
328 break;
329 default:
330 gcc_unreachable ();
332 tree lhs = gimple_call_lhs (stmt);
333 if (lhs)
334 replace_uses_by (lhs, t);
335 release_defs (stmt);
336 gsi_remove (&i, true);
341 /* Helper structure for note_simd_array_uses. */
343 struct note_simd_array_uses_struct
345 hash_table<simd_array_to_simduid> **htab;
346 unsigned int simduid;
349 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
351 static tree
352 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
354 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
355 struct note_simd_array_uses_struct *ns
356 = (struct note_simd_array_uses_struct *) wi->info;
358 if (TYPE_P (*tp))
359 *walk_subtrees = 0;
360 else if (VAR_P (*tp)
361 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
362 && DECL_CONTEXT (*tp) == current_function_decl)
364 simd_array_to_simduid data;
365 if (!*ns->htab)
366 *ns->htab = new hash_table<simd_array_to_simduid> (15);
367 data.decl = *tp;
368 data.simduid = ns->simduid;
369 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
370 if (*slot == NULL)
372 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
373 *p = data;
374 *slot = p;
376 else if ((*slot)->simduid != ns->simduid)
377 (*slot)->simduid = -1U;
378 *walk_subtrees = 0;
380 return NULL_TREE;
383 /* Find "omp simd array" temporaries and map them to corresponding
384 simduid. */
386 static void
387 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
389 basic_block bb;
390 gimple_stmt_iterator gsi;
391 struct walk_stmt_info wi;
392 struct note_simd_array_uses_struct ns;
394 memset (&wi, 0, sizeof (wi));
395 wi.info = &ns;
396 ns.htab = htab;
398 FOR_EACH_BB_FN (bb, cfun)
399 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
401 gimple *stmt = gsi_stmt (gsi);
402 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
403 continue;
404 switch (gimple_call_internal_fn (stmt))
406 case IFN_GOMP_SIMD_LANE:
407 case IFN_GOMP_SIMD_VF:
408 case IFN_GOMP_SIMD_LAST_LANE:
409 break;
410 default:
411 continue;
413 tree lhs = gimple_call_lhs (stmt);
414 if (lhs == NULL_TREE)
415 continue;
416 imm_use_iterator use_iter;
417 gimple *use_stmt;
418 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
419 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
420 if (!is_gimple_debug (use_stmt))
421 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
425 /* Shrink arrays with "omp simd array" attribute to the corresponding
426 vectorization factor. */
428 static void
429 shrink_simd_arrays
430 (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
431 hash_table<simduid_to_vf> *simduid_to_vf_htab)
433 for (hash_table<simd_array_to_simduid>::iterator iter
434 = simd_array_to_simduid_htab->begin ();
435 iter != simd_array_to_simduid_htab->end (); ++iter)
436 if ((*iter)->simduid != -1U)
438 tree decl = (*iter)->decl;
439 poly_uint64 vf = 1;
440 if (simduid_to_vf_htab)
442 simduid_to_vf *p = NULL, data;
443 data.simduid = (*iter)->simduid;
444 p = simduid_to_vf_htab->find (&data);
445 if (p)
446 vf = p->vf;
448 tree atype
449 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
450 TREE_TYPE (decl) = atype;
451 relayout_decl (decl);
454 delete simd_array_to_simduid_htab;
457 /* Initialize the vec_info with kind KIND_IN and target cost data
458 TARGET_COST_DATA_IN. */
460 vec_info::vec_info (vec_info::vec_kind kind_in, void *target_cost_data_in,
461 vec_info_shared *shared_)
462 : kind (kind_in),
463 shared (shared_),
464 stmt_vec_info_ro (false),
465 target_cost_data (target_cost_data_in)
467 stmt_vec_infos.create (50);
470 vec_info::~vec_info ()
472 slp_instance instance;
473 unsigned int i;
475 FOR_EACH_VEC_ELT (slp_instances, i, instance)
476 vect_free_slp_instance (instance);
478 destroy_cost_data (target_cost_data);
479 free_stmt_vec_infos ();
482 vec_info_shared::vec_info_shared ()
483 : datarefs (vNULL),
484 datarefs_copy (vNULL),
485 ddrs (vNULL)
489 vec_info_shared::~vec_info_shared ()
491 free_data_refs (datarefs);
492 free_dependence_relations (ddrs);
493 datarefs_copy.release ();
496 void
497 vec_info_shared::save_datarefs ()
499 if (!flag_checking)
500 return;
501 datarefs_copy.reserve_exact (datarefs.length ());
502 for (unsigned i = 0; i < datarefs.length (); ++i)
503 datarefs_copy.quick_push (*datarefs[i]);
506 void
507 vec_info_shared::check_datarefs ()
509 if (!flag_checking)
510 return;
511 gcc_assert (datarefs.length () == datarefs_copy.length ());
512 for (unsigned i = 0; i < datarefs.length (); ++i)
513 if (memcmp (&datarefs_copy[i], datarefs[i], sizeof (data_reference)) != 0)
514 gcc_unreachable ();
517 /* Record that STMT belongs to the vectorizable region. Create and return
518 an associated stmt_vec_info. */
520 stmt_vec_info
521 vec_info::add_stmt (gimple *stmt)
523 stmt_vec_info res = new_stmt_vec_info (stmt);
524 set_vinfo_for_stmt (stmt, res);
525 return res;
528 /* If STMT has an associated stmt_vec_info, return that vec_info, otherwise
529 return null. It is safe to call this function on any statement, even if
530 it might not be part of the vectorizable region. */
532 stmt_vec_info
533 vec_info::lookup_stmt (gimple *stmt)
535 unsigned int uid = gimple_uid (stmt);
536 if (uid > 0 && uid - 1 < stmt_vec_infos.length ())
538 stmt_vec_info res = stmt_vec_infos[uid - 1];
539 if (res && res->stmt == stmt)
540 return res;
542 return NULL;
545 /* If NAME is an SSA_NAME and its definition has an associated stmt_vec_info,
546 return that stmt_vec_info, otherwise return null. It is safe to call
547 this on arbitrary operands. */
549 stmt_vec_info
550 vec_info::lookup_def (tree name)
552 if (TREE_CODE (name) == SSA_NAME
553 && !SSA_NAME_IS_DEFAULT_DEF (name))
554 return lookup_stmt (SSA_NAME_DEF_STMT (name));
555 return NULL;
558 /* See whether there is a single non-debug statement that uses LHS and
559 whether that statement has an associated stmt_vec_info. Return the
560 stmt_vec_info if so, otherwise return null. */
562 stmt_vec_info
563 vec_info::lookup_single_use (tree lhs)
565 use_operand_p dummy;
566 gimple *use_stmt;
567 if (single_imm_use (lhs, &dummy, &use_stmt))
568 return lookup_stmt (use_stmt);
569 return NULL;
572 /* Return vectorization information about DR. */
574 dr_vec_info *
575 vec_info::lookup_dr (data_reference *dr)
577 stmt_vec_info stmt_info = lookup_stmt (DR_STMT (dr));
578 /* DR_STMT should never refer to a stmt in a pattern replacement. */
579 gcc_checking_assert (!is_pattern_stmt_p (stmt_info));
580 return STMT_VINFO_DR_INFO (stmt_info->dr_aux.stmt);
583 /* Record that NEW_STMT_INFO now implements the same data reference
584 as OLD_STMT_INFO. */
586 void
587 vec_info::move_dr (stmt_vec_info new_stmt_info, stmt_vec_info old_stmt_info)
589 gcc_assert (!is_pattern_stmt_p (old_stmt_info));
590 STMT_VINFO_DR_INFO (old_stmt_info)->stmt = new_stmt_info;
591 new_stmt_info->dr_aux = old_stmt_info->dr_aux;
592 STMT_VINFO_DR_WRT_VEC_LOOP (new_stmt_info)
593 = STMT_VINFO_DR_WRT_VEC_LOOP (old_stmt_info);
594 STMT_VINFO_GATHER_SCATTER_P (new_stmt_info)
595 = STMT_VINFO_GATHER_SCATTER_P (old_stmt_info);
598 /* Permanently remove the statement described by STMT_INFO from the
599 function. */
601 void
602 vec_info::remove_stmt (stmt_vec_info stmt_info)
604 gcc_assert (!stmt_info->pattern_stmt_p);
605 set_vinfo_for_stmt (stmt_info->stmt, NULL);
606 unlink_stmt_vdef (stmt_info->stmt);
607 gimple_stmt_iterator si = gsi_for_stmt (stmt_info->stmt);
608 gsi_remove (&si, true);
609 release_defs (stmt_info->stmt);
610 free_stmt_vec_info (stmt_info);
613 /* Replace the statement at GSI by NEW_STMT, both the vectorization
614 information and the function itself. STMT_INFO describes the statement
615 at GSI. */
617 void
618 vec_info::replace_stmt (gimple_stmt_iterator *gsi, stmt_vec_info stmt_info,
619 gimple *new_stmt)
621 gimple *old_stmt = stmt_info->stmt;
622 gcc_assert (!stmt_info->pattern_stmt_p && old_stmt == gsi_stmt (*gsi));
623 gimple_set_uid (new_stmt, gimple_uid (old_stmt));
624 stmt_info->stmt = new_stmt;
625 gsi_replace (gsi, new_stmt, true);
628 /* Insert stmts in SEQ on the VEC_INFO region entry. If CONTEXT is
629 not NULL it specifies whether to use the sub-region entry
630 determined by it, currently used for loop vectorization to insert
631 on the inner loop entry vs. the outer loop entry. */
633 void
634 vec_info::insert_seq_on_entry (stmt_vec_info context, gimple_seq seq)
636 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (this))
638 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
639 basic_block new_bb;
640 edge pe;
642 if (context && nested_in_vect_loop_p (loop, context))
643 loop = loop->inner;
645 pe = loop_preheader_edge (loop);
646 new_bb = gsi_insert_seq_on_edge_immediate (pe, seq);
647 gcc_assert (!new_bb);
649 else
651 bb_vec_info bb_vinfo = as_a <bb_vec_info> (this);
652 gimple_stmt_iterator gsi_region_begin
653 = gsi_after_labels (bb_vinfo->bbs[0]);
654 gsi_insert_seq_before (&gsi_region_begin, seq, GSI_SAME_STMT);
658 /* Like insert_seq_on_entry but just inserts the single stmt NEW_STMT. */
660 void
661 vec_info::insert_on_entry (stmt_vec_info context, gimple *new_stmt)
663 gimple_seq seq = NULL;
664 gimple_stmt_iterator gsi = gsi_start (seq);
665 gsi_insert_before_without_update (&gsi, new_stmt, GSI_SAME_STMT);
666 insert_seq_on_entry (context, seq);
669 /* Create and initialize a new stmt_vec_info struct for STMT. */
671 stmt_vec_info
672 vec_info::new_stmt_vec_info (gimple *stmt)
674 stmt_vec_info res = XCNEW (class _stmt_vec_info);
675 res->stmt = stmt;
677 STMT_VINFO_TYPE (res) = undef_vec_info_type;
678 STMT_VINFO_RELEVANT (res) = vect_unused_in_scope;
679 STMT_VINFO_VECTORIZABLE (res) = true;
680 STMT_VINFO_REDUC_TYPE (res) = TREE_CODE_REDUCTION;
681 STMT_VINFO_REDUC_CODE (res) = ERROR_MARK;
682 STMT_VINFO_REDUC_FN (res) = IFN_LAST;
683 STMT_VINFO_REDUC_IDX (res) = -1;
684 STMT_VINFO_SLP_VECT_ONLY (res) = false;
685 STMT_VINFO_VEC_STMTS (res) = vNULL;
687 if (gimple_code (stmt) == GIMPLE_PHI
688 && is_loop_header_bb_p (gimple_bb (stmt)))
689 STMT_VINFO_DEF_TYPE (res) = vect_unknown_def_type;
690 else
691 STMT_VINFO_DEF_TYPE (res) = vect_internal_def;
693 STMT_SLP_TYPE (res) = loop_vect;
695 /* This is really "uninitialized" until vect_compute_data_ref_alignment. */
696 res->dr_aux.misalignment = DR_MISALIGNMENT_UNINITIALIZED;
698 return res;
701 /* Associate STMT with INFO. */
703 void
704 vec_info::set_vinfo_for_stmt (gimple *stmt, stmt_vec_info info)
706 unsigned int uid = gimple_uid (stmt);
707 if (uid == 0)
709 gcc_assert (!stmt_vec_info_ro);
710 gcc_checking_assert (info);
711 uid = stmt_vec_infos.length () + 1;
712 gimple_set_uid (stmt, uid);
713 stmt_vec_infos.safe_push (info);
715 else
717 gcc_checking_assert (info == NULL);
718 stmt_vec_infos[uid - 1] = info;
722 /* Free the contents of stmt_vec_infos. */
724 void
725 vec_info::free_stmt_vec_infos (void)
727 unsigned int i;
728 stmt_vec_info info;
729 FOR_EACH_VEC_ELT (stmt_vec_infos, i, info)
730 if (info != NULL)
731 free_stmt_vec_info (info);
732 stmt_vec_infos.release ();
735 /* Free STMT_INFO. */
737 void
738 vec_info::free_stmt_vec_info (stmt_vec_info stmt_info)
740 if (stmt_info->pattern_stmt_p)
742 gimple_set_bb (stmt_info->stmt, NULL);
743 tree lhs = gimple_get_lhs (stmt_info->stmt);
744 if (lhs && TREE_CODE (lhs) == SSA_NAME)
745 release_ssa_name (lhs);
748 STMT_VINFO_SIMD_CLONE_INFO (stmt_info).release ();
749 STMT_VINFO_VEC_STMTS (stmt_info).release ();
750 free (stmt_info);
753 /* Returns true if S1 dominates S2. */
755 bool
756 vect_stmt_dominates_stmt_p (gimple *s1, gimple *s2)
758 basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
760 /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
761 SSA_NAME. Assume it lives at the beginning of function and
762 thus dominates everything. */
763 if (!bb1 || s1 == s2)
764 return true;
766 /* If bb2 is NULL, it doesn't dominate any stmt with a bb. */
767 if (!bb2)
768 return false;
770 if (bb1 != bb2)
771 return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
773 /* PHIs in the same basic block are assumed to be
774 executed all in parallel, if only one stmt is a PHI,
775 it dominates the other stmt in the same basic block. */
776 if (gimple_code (s1) == GIMPLE_PHI)
777 return true;
779 if (gimple_code (s2) == GIMPLE_PHI)
780 return false;
782 /* Inserted vectorized stmts all have UID 0 while the original stmts
783 in the IL have UID increasing within a BB. Walk from both sides
784 until we find the other stmt or a stmt with UID != 0. */
785 gimple_stmt_iterator gsi1 = gsi_for_stmt (s1);
786 while (gimple_uid (gsi_stmt (gsi1)) == 0)
788 gsi_next (&gsi1);
789 if (gsi_end_p (gsi1))
790 return false;
791 if (gsi_stmt (gsi1) == s2)
792 return true;
794 if (gimple_uid (gsi_stmt (gsi1)) == -1u)
795 return false;
797 gimple_stmt_iterator gsi2 = gsi_for_stmt (s2);
798 while (gimple_uid (gsi_stmt (gsi2)) == 0)
800 gsi_prev (&gsi2);
801 if (gsi_end_p (gsi2))
802 return false;
803 if (gsi_stmt (gsi2) == s1)
804 return true;
806 if (gimple_uid (gsi_stmt (gsi2)) == -1u)
807 return false;
809 if (gimple_uid (gsi_stmt (gsi1)) <= gimple_uid (gsi_stmt (gsi2)))
810 return true;
811 return false;
814 /* A helper function to free scev and LOOP niter information, as well as
815 clear loop constraint LOOP_C_FINITE. */
817 void
818 vect_free_loop_info_assumptions (class loop *loop)
820 scev_reset_htab ();
821 /* We need to explicitly reset upper bound information since they are
822 used even after free_numbers_of_iterations_estimates. */
823 loop->any_upper_bound = false;
824 loop->any_likely_upper_bound = false;
825 free_numbers_of_iterations_estimates (loop);
826 loop_constraint_clear (loop, LOOP_C_FINITE);
829 /* If LOOP has been versioned during ifcvt, return the internal call
830 guarding it. */
832 gimple *
833 vect_loop_vectorized_call (class loop *loop, gcond **cond)
835 basic_block bb = loop_preheader_edge (loop)->src;
836 gimple *g;
839 g = last_stmt (bb);
840 if (g)
841 break;
842 if (!single_pred_p (bb))
843 break;
844 bb = single_pred (bb);
846 while (1);
847 if (g && gimple_code (g) == GIMPLE_COND)
849 if (cond)
850 *cond = as_a <gcond *> (g);
851 gimple_stmt_iterator gsi = gsi_for_stmt (g);
852 gsi_prev (&gsi);
853 if (!gsi_end_p (gsi))
855 g = gsi_stmt (gsi);
856 if (gimple_call_internal_p (g, IFN_LOOP_VECTORIZED)
857 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
858 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
859 return g;
862 return NULL;
865 /* If LOOP has been versioned during loop distribution, return the gurading
866 internal call. */
868 static gimple *
869 vect_loop_dist_alias_call (class loop *loop)
871 basic_block bb;
872 basic_block entry;
873 class loop *outer, *orig;
874 gimple_stmt_iterator gsi;
875 gimple *g;
877 if (loop->orig_loop_num == 0)
878 return NULL;
880 orig = get_loop (cfun, loop->orig_loop_num);
881 if (orig == NULL)
883 /* The original loop is somehow destroyed. Clear the information. */
884 loop->orig_loop_num = 0;
885 return NULL;
888 if (loop != orig)
889 bb = nearest_common_dominator (CDI_DOMINATORS, loop->header, orig->header);
890 else
891 bb = loop_preheader_edge (loop)->src;
893 outer = bb->loop_father;
894 entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
896 /* Look upward in dominance tree. */
897 for (; bb != entry && flow_bb_inside_loop_p (outer, bb);
898 bb = get_immediate_dominator (CDI_DOMINATORS, bb))
900 g = last_stmt (bb);
901 if (g == NULL || gimple_code (g) != GIMPLE_COND)
902 continue;
904 gsi = gsi_for_stmt (g);
905 gsi_prev (&gsi);
906 if (gsi_end_p (gsi))
907 continue;
909 g = gsi_stmt (gsi);
910 /* The guarding internal function call must have the same distribution
911 alias id. */
912 if (gimple_call_internal_p (g, IFN_LOOP_DIST_ALIAS)
913 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->orig_loop_num))
914 return g;
916 return NULL;
919 /* Set the uids of all the statements in basic blocks inside loop
920 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
921 call guarding the loop which has been if converted. */
922 static void
923 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
925 tree arg = gimple_call_arg (loop_vectorized_call, 1);
926 basic_block *bbs;
927 unsigned int i;
928 class loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
930 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
931 gcc_checking_assert (vect_loop_vectorized_call (scalar_loop)
932 == loop_vectorized_call);
933 /* If we are going to vectorize outer loop, prevent vectorization
934 of the inner loop in the scalar loop - either the scalar loop is
935 thrown away, so it is a wasted work, or is used only for
936 a few iterations. */
937 if (scalar_loop->inner)
939 gimple *g = vect_loop_vectorized_call (scalar_loop->inner);
940 if (g)
942 arg = gimple_call_arg (g, 0);
943 get_loop (cfun, tree_to_shwi (arg))->dont_vectorize = true;
944 fold_loop_internal_call (g, boolean_false_node);
947 bbs = get_loop_body (scalar_loop);
948 for (i = 0; i < scalar_loop->num_nodes; i++)
950 basic_block bb = bbs[i];
951 gimple_stmt_iterator gsi;
952 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
954 gimple *phi = gsi_stmt (gsi);
955 gimple_set_uid (phi, 0);
957 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
959 gimple *stmt = gsi_stmt (gsi);
960 gimple_set_uid (stmt, 0);
963 free (bbs);
966 /* Try to vectorize LOOP. */
968 static unsigned
969 try_vectorize_loop_1 (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
970 unsigned *num_vectorized_loops, loop_p loop,
971 gimple *loop_vectorized_call,
972 gimple *loop_dist_alias_call)
974 unsigned ret = 0;
975 vec_info_shared shared;
976 auto_purge_vect_location sentinel;
977 vect_location = find_loop_location (loop);
979 if (LOCATION_LOCUS (vect_location.get_location_t ()) != UNKNOWN_LOCATION
980 && dump_enabled_p ())
981 dump_printf (MSG_NOTE | MSG_PRIORITY_INTERNALS,
982 "\nAnalyzing loop at %s:%d\n",
983 LOCATION_FILE (vect_location.get_location_t ()),
984 LOCATION_LINE (vect_location.get_location_t ()));
986 opt_loop_vec_info loop_vinfo = opt_loop_vec_info::success (NULL);
987 /* In the case of epilogue vectorization the loop already has its
988 loop_vec_info set, we do not require to analyze the loop in this case. */
989 if (loop_vec_info vinfo = loop_vec_info_for_loop (loop))
990 loop_vinfo = opt_loop_vec_info::success (vinfo);
991 else
993 /* Try to analyze the loop, retaining an opt_problem if dump_enabled_p. */
994 loop_vinfo = vect_analyze_loop (loop, &shared);
995 loop->aux = loop_vinfo;
998 if (!loop_vinfo)
999 if (dump_enabled_p ())
1000 if (opt_problem *problem = loop_vinfo.get_problem ())
1002 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1003 "couldn't vectorize loop\n");
1004 problem->emit_and_clear ();
1007 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
1009 /* Free existing information if loop is analyzed with some
1010 assumptions. */
1011 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1012 vect_free_loop_info_assumptions (loop);
1014 /* If we applied if-conversion then try to vectorize the
1015 BB of innermost loops.
1016 ??? Ideally BB vectorization would learn to vectorize
1017 control flow by applying if-conversion on-the-fly, the
1018 following retains the if-converted loop body even when
1019 only non-if-converted parts took part in BB vectorization. */
1020 if (flag_tree_slp_vectorize != 0
1021 && loop_vectorized_call
1022 && ! loop->inner)
1024 basic_block bb = loop->header;
1025 bool require_loop_vectorize = false;
1026 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
1027 !gsi_end_p (gsi); gsi_next (&gsi))
1029 gimple *stmt = gsi_stmt (gsi);
1030 gcall *call = dyn_cast <gcall *> (stmt);
1031 if (call && gimple_call_internal_p (call))
1033 internal_fn ifn = gimple_call_internal_fn (call);
1034 if (ifn == IFN_MASK_LOAD || ifn == IFN_MASK_STORE
1035 /* Don't keep the if-converted parts when the ifn with
1036 specifc type is not supported by the backend. */
1037 || (direct_internal_fn_p (ifn)
1038 && !direct_internal_fn_supported_p
1039 (call, OPTIMIZE_FOR_SPEED)))
1041 require_loop_vectorize = true;
1042 break;
1045 gimple_set_uid (stmt, -1);
1046 gimple_set_visited (stmt, false);
1048 if (!require_loop_vectorize && vect_slp_bb (bb))
1050 fold_loop_internal_call (loop_vectorized_call,
1051 boolean_true_node);
1052 loop_vectorized_call = NULL;
1053 ret |= TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
1056 /* If outer loop vectorization fails for LOOP_VECTORIZED guarded
1057 loop, don't vectorize its inner loop; we'll attempt to
1058 vectorize LOOP_VECTORIZED guarded inner loop of the scalar
1059 loop version. */
1060 if (loop_vectorized_call && loop->inner)
1061 loop->inner->dont_vectorize = true;
1062 return ret;
1065 /* Only count the original scalar loops. */
1066 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo) && !dbg_cnt (vect_loop))
1068 /* Free existing information if loop is analyzed with some
1069 assumptions. */
1070 if (loop_constraint_set_p (loop, LOOP_C_FINITE))
1071 vect_free_loop_info_assumptions (loop);
1072 return ret;
1075 if (loop_vectorized_call)
1076 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
1078 unsigned HOST_WIDE_INT bytes;
1079 if (dump_enabled_p ())
1081 if (GET_MODE_SIZE (loop_vinfo->vector_mode).is_constant (&bytes))
1082 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1083 "loop vectorized using %wu byte vectors\n", bytes);
1084 else
1085 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
1086 "loop vectorized using variable length vectors\n");
1089 loop_p new_loop = vect_transform_loop (loop_vinfo,
1090 loop_vectorized_call);
1091 (*num_vectorized_loops)++;
1092 /* Now that the loop has been vectorized, allow it to be unrolled
1093 etc. */
1094 loop->force_vectorize = false;
1096 if (loop->simduid)
1098 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
1099 if (!simduid_to_vf_htab)
1100 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
1101 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
1102 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
1103 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
1104 = simduid_to_vf_data;
1107 if (loop_vectorized_call)
1109 fold_loop_internal_call (loop_vectorized_call, boolean_true_node);
1110 loop_vectorized_call = NULL;
1111 ret |= TODO_cleanup_cfg;
1113 if (loop_dist_alias_call)
1115 tree value = gimple_call_arg (loop_dist_alias_call, 1);
1116 fold_loop_internal_call (loop_dist_alias_call, value);
1117 loop_dist_alias_call = NULL;
1118 ret |= TODO_cleanup_cfg;
1121 /* Epilogue of vectorized loop must be vectorized too. */
1122 if (new_loop)
1124 /* Don't include vectorized epilogues in the "vectorized loops" count.
1126 unsigned dont_count = *num_vectorized_loops;
1127 ret |= try_vectorize_loop_1 (simduid_to_vf_htab, &dont_count,
1128 new_loop, NULL, NULL);
1131 return ret;
1134 /* Try to vectorize LOOP. */
1136 static unsigned
1137 try_vectorize_loop (hash_table<simduid_to_vf> *&simduid_to_vf_htab,
1138 unsigned *num_vectorized_loops, loop_p loop)
1140 if (!((flag_tree_loop_vectorize
1141 && optimize_loop_nest_for_speed_p (loop))
1142 || loop->force_vectorize))
1143 return 0;
1145 return try_vectorize_loop_1 (simduid_to_vf_htab, num_vectorized_loops, loop,
1146 vect_loop_vectorized_call (loop),
1147 vect_loop_dist_alias_call (loop));
1151 /* Function vectorize_loops.
1153 Entry point to loop vectorization phase. */
1155 unsigned
1156 vectorize_loops (void)
1158 unsigned int i;
1159 unsigned int num_vectorized_loops = 0;
1160 unsigned int vect_loops_num;
1161 class loop *loop;
1162 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
1163 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1164 bool any_ifcvt_loops = false;
1165 unsigned ret = 0;
1167 vect_loops_num = number_of_loops (cfun);
1169 /* Bail out if there are no loops. */
1170 if (vect_loops_num <= 1)
1171 return 0;
1173 if (cfun->has_simduid_loops)
1174 note_simd_array_uses (&simd_array_to_simduid_htab);
1176 /* ----------- Analyze loops. ----------- */
1178 /* If some loop was duplicated, it gets bigger number
1179 than all previously defined loops. This fact allows us to run
1180 only over initial loops skipping newly generated ones. */
1181 FOR_EACH_LOOP (loop, 0)
1182 if (loop->dont_vectorize)
1184 any_ifcvt_loops = true;
1185 /* If-conversion sometimes versions both the outer loop
1186 (for the case when outer loop vectorization might be
1187 desirable) as well as the inner loop in the scalar version
1188 of the loop. So we have:
1189 if (LOOP_VECTORIZED (1, 3))
1191 loop1
1192 loop2
1194 else
1195 loop3 (copy of loop1)
1196 if (LOOP_VECTORIZED (4, 5))
1197 loop4 (copy of loop2)
1198 else
1199 loop5 (copy of loop4)
1200 If FOR_EACH_LOOP gives us loop3 first (which has
1201 dont_vectorize set), make sure to process loop1 before loop4;
1202 so that we can prevent vectorization of loop4 if loop1
1203 is successfully vectorized. */
1204 if (loop->inner)
1206 gimple *loop_vectorized_call
1207 = vect_loop_vectorized_call (loop);
1208 if (loop_vectorized_call
1209 && vect_loop_vectorized_call (loop->inner))
1211 tree arg = gimple_call_arg (loop_vectorized_call, 0);
1212 class loop *vector_loop
1213 = get_loop (cfun, tree_to_shwi (arg));
1214 if (vector_loop && vector_loop != loop)
1216 /* Make sure we don't vectorize it twice. */
1217 vector_loop->dont_vectorize = true;
1218 ret |= try_vectorize_loop (simduid_to_vf_htab,
1219 &num_vectorized_loops,
1220 vector_loop);
1225 else
1226 ret |= try_vectorize_loop (simduid_to_vf_htab, &num_vectorized_loops,
1227 loop);
1229 vect_location = dump_user_location_t ();
1231 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
1232 if (dump_enabled_p ()
1233 || (num_vectorized_loops > 0 && dump_enabled_p ()))
1234 dump_printf_loc (MSG_NOTE, vect_location,
1235 "vectorized %u loops in function.\n",
1236 num_vectorized_loops);
1238 /* ----------- Finalize. ----------- */
1240 if (any_ifcvt_loops)
1241 for (i = 1; i < number_of_loops (cfun); i++)
1243 loop = get_loop (cfun, i);
1244 if (loop && loop->dont_vectorize)
1246 gimple *g = vect_loop_vectorized_call (loop);
1247 if (g)
1249 fold_loop_internal_call (g, boolean_false_node);
1250 ret |= TODO_cleanup_cfg;
1251 g = NULL;
1253 else
1254 g = vect_loop_dist_alias_call (loop);
1256 if (g)
1258 fold_loop_internal_call (g, boolean_false_node);
1259 ret |= TODO_cleanup_cfg;
1264 for (i = 1; i < number_of_loops (cfun); i++)
1266 loop_vec_info loop_vinfo;
1267 bool has_mask_store;
1269 loop = get_loop (cfun, i);
1270 if (!loop || !loop->aux)
1271 continue;
1272 loop_vinfo = (loop_vec_info) loop->aux;
1273 has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
1274 delete loop_vinfo;
1275 if (has_mask_store
1276 && targetm.vectorize.empty_mask_is_expensive (IFN_MASK_STORE))
1277 optimize_mask_stores (loop);
1278 loop->aux = NULL;
1281 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1282 if (cfun->has_simduid_loops)
1284 adjust_simduid_builtins (simduid_to_vf_htab);
1285 /* Avoid stale SCEV cache entries for the SIMD_LANE defs. */
1286 scev_reset ();
1289 /* Shrink any "omp array simd" temporary arrays to the
1290 actual vectorization factors. */
1291 if (simd_array_to_simduid_htab)
1292 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
1293 delete simduid_to_vf_htab;
1294 cfun->has_simduid_loops = false;
1296 if (num_vectorized_loops > 0)
1298 /* If we vectorized any loop only virtual SSA form needs to be updated.
1299 ??? Also while we try hard to update loop-closed SSA form we fail
1300 to properly do this in some corner-cases (see PR56286). */
1301 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
1302 return TODO_cleanup_cfg;
1305 return ret;
1309 /* Entry point to the simduid cleanup pass. */
1311 namespace {
1313 const pass_data pass_data_simduid_cleanup =
1315 GIMPLE_PASS, /* type */
1316 "simduid", /* name */
1317 OPTGROUP_NONE, /* optinfo_flags */
1318 TV_NONE, /* tv_id */
1319 ( PROP_ssa | PROP_cfg ), /* properties_required */
1320 0, /* properties_provided */
1321 0, /* properties_destroyed */
1322 0, /* todo_flags_start */
1323 0, /* todo_flags_finish */
1326 class pass_simduid_cleanup : public gimple_opt_pass
1328 public:
1329 pass_simduid_cleanup (gcc::context *ctxt)
1330 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
1333 /* opt_pass methods: */
1334 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
1335 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
1336 virtual unsigned int execute (function *);
1338 }; // class pass_simduid_cleanup
1340 unsigned int
1341 pass_simduid_cleanup::execute (function *fun)
1343 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
1345 note_simd_array_uses (&simd_array_to_simduid_htab);
1347 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
1348 adjust_simduid_builtins (NULL);
1350 /* Shrink any "omp array simd" temporary arrays to the
1351 actual vectorization factors. */
1352 if (simd_array_to_simduid_htab)
1353 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
1354 fun->has_simduid_loops = false;
1355 return 0;
1358 } // anon namespace
1360 gimple_opt_pass *
1361 make_pass_simduid_cleanup (gcc::context *ctxt)
1363 return new pass_simduid_cleanup (ctxt);
1367 /* Entry point to basic block SLP phase. */
1369 namespace {
1371 const pass_data pass_data_slp_vectorize =
1373 GIMPLE_PASS, /* type */
1374 "slp", /* name */
1375 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1376 TV_TREE_SLP_VECTORIZATION, /* tv_id */
1377 ( PROP_ssa | PROP_cfg ), /* properties_required */
1378 0, /* properties_provided */
1379 0, /* properties_destroyed */
1380 0, /* todo_flags_start */
1381 TODO_update_ssa, /* todo_flags_finish */
1384 class pass_slp_vectorize : public gimple_opt_pass
1386 public:
1387 pass_slp_vectorize (gcc::context *ctxt)
1388 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
1391 /* opt_pass methods: */
1392 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
1393 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
1394 virtual unsigned int execute (function *);
1396 }; // class pass_slp_vectorize
1398 unsigned int
1399 pass_slp_vectorize::execute (function *fun)
1401 auto_purge_vect_location sentinel;
1402 basic_block bb;
1404 bool in_loop_pipeline = scev_initialized_p ();
1405 if (!in_loop_pipeline)
1407 loop_optimizer_init (LOOPS_NORMAL);
1408 scev_initialize ();
1411 /* Mark all stmts as not belonging to the current region and unvisited. */
1412 FOR_EACH_BB_FN (bb, fun)
1414 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1415 gsi_next (&gsi))
1417 gphi *stmt = gsi.phi ();
1418 gimple_set_uid (stmt, -1);
1419 gimple_set_visited (stmt, false);
1421 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1422 gsi_next (&gsi))
1424 gimple *stmt = gsi_stmt (gsi);
1425 gimple_set_uid (stmt, -1);
1426 gimple_set_visited (stmt, false);
1430 vect_slp_function (fun);
1432 if (!in_loop_pipeline)
1434 scev_finalize ();
1435 loop_optimizer_finalize ();
1438 return 0;
1441 } // anon namespace
1443 gimple_opt_pass *
1444 make_pass_slp_vectorize (gcc::context *ctxt)
1446 return new pass_slp_vectorize (ctxt);
1450 /* Increase alignment of global arrays to improve vectorization potential.
1451 TODO:
1452 - Consider also structs that have an array field.
1453 - Use ipa analysis to prune arrays that can't be vectorized?
1454 This should involve global alignment analysis and in the future also
1455 array padding. */
1457 static unsigned get_vec_alignment_for_type (tree);
1458 static hash_map<tree, unsigned> *type_align_map;
1460 /* Return alignment of array's vector type corresponding to scalar type.
1461 0 if no vector type exists. */
1462 static unsigned
1463 get_vec_alignment_for_array_type (tree type)
1465 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1466 poly_uint64 array_size, vector_size;
1468 tree scalar_type = strip_array_types (type);
1469 tree vectype = get_related_vectype_for_scalar_type (VOIDmode, scalar_type);
1470 if (!vectype
1471 || !poly_int_tree_p (TYPE_SIZE (type), &array_size)
1472 || !poly_int_tree_p (TYPE_SIZE (vectype), &vector_size)
1473 || maybe_lt (array_size, vector_size))
1474 return 0;
1476 return TYPE_ALIGN (vectype);
1479 /* Return alignment of field having maximum alignment of vector type
1480 corresponding to it's scalar type. For now, we only consider fields whose
1481 offset is a multiple of it's vector alignment.
1482 0 if no suitable field is found. */
1483 static unsigned
1484 get_vec_alignment_for_record_type (tree type)
1486 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1488 unsigned max_align = 0, alignment;
1489 HOST_WIDE_INT offset;
1490 tree offset_tree;
1492 if (TYPE_PACKED (type))
1493 return 0;
1495 unsigned *slot = type_align_map->get (type);
1496 if (slot)
1497 return *slot;
1499 for (tree field = first_field (type);
1500 field != NULL_TREE;
1501 field = DECL_CHAIN (field))
1503 /* Skip if not FIELD_DECL or if alignment is set by user. */
1504 if (TREE_CODE (field) != FIELD_DECL
1505 || DECL_USER_ALIGN (field)
1506 || DECL_ARTIFICIAL (field))
1507 continue;
1509 /* We don't need to process the type further if offset is variable,
1510 since the offsets of remaining members will also be variable. */
1511 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
1512 || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
1513 break;
1515 /* Similarly stop processing the type if offset_tree
1516 does not fit in unsigned HOST_WIDE_INT. */
1517 offset_tree = bit_position (field);
1518 if (!tree_fits_uhwi_p (offset_tree))
1519 break;
1521 offset = tree_to_uhwi (offset_tree);
1522 alignment = get_vec_alignment_for_type (TREE_TYPE (field));
1524 /* Get maximum alignment of vectorized field/array among those members
1525 whose offset is multiple of the vector alignment. */
1526 if (alignment
1527 && (offset % alignment == 0)
1528 && (alignment > max_align))
1529 max_align = alignment;
1532 type_align_map->put (type, max_align);
1533 return max_align;
1536 /* Return alignment of vector type corresponding to decl's scalar type
1537 or 0 if it doesn't exist or the vector alignment is lesser than
1538 decl's alignment. */
1539 static unsigned
1540 get_vec_alignment_for_type (tree type)
1542 if (type == NULL_TREE)
1543 return 0;
1545 gcc_assert (TYPE_P (type));
1547 static unsigned alignment = 0;
1548 switch (TREE_CODE (type))
1550 case ARRAY_TYPE:
1551 alignment = get_vec_alignment_for_array_type (type);
1552 break;
1553 case RECORD_TYPE:
1554 alignment = get_vec_alignment_for_record_type (type);
1555 break;
1556 default:
1557 alignment = 0;
1558 break;
1561 return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
1564 /* Entry point to increase_alignment pass. */
1565 static unsigned int
1566 increase_alignment (void)
1568 varpool_node *vnode;
1570 vect_location = dump_user_location_t ();
1571 type_align_map = new hash_map<tree, unsigned>;
1573 /* Increase the alignment of all global arrays for vectorization. */
1574 FOR_EACH_DEFINED_VARIABLE (vnode)
1576 tree decl = vnode->decl;
1577 unsigned int alignment;
1579 if ((decl_in_symtab_p (decl)
1580 && !symtab_node::get (decl)->can_increase_alignment_p ())
1581 || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
1582 continue;
1584 alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
1585 if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
1587 vnode->increase_alignment (alignment);
1588 if (dump_enabled_p ())
1589 dump_printf (MSG_NOTE, "Increasing alignment of decl: %T\n", decl);
1593 delete type_align_map;
1594 return 0;
1598 namespace {
1600 const pass_data pass_data_ipa_increase_alignment =
1602 SIMPLE_IPA_PASS, /* type */
1603 "increase_alignment", /* name */
1604 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
1605 TV_IPA_OPT, /* tv_id */
1606 0, /* properties_required */
1607 0, /* properties_provided */
1608 0, /* properties_destroyed */
1609 0, /* todo_flags_start */
1610 0, /* todo_flags_finish */
1613 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
1615 public:
1616 pass_ipa_increase_alignment (gcc::context *ctxt)
1617 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
1620 /* opt_pass methods: */
1621 virtual bool gate (function *)
1623 return flag_section_anchors && flag_tree_loop_vectorize;
1626 virtual unsigned int execute (function *) { return increase_alignment (); }
1628 }; // class pass_ipa_increase_alignment
1630 } // anon namespace
1632 simple_ipa_opt_pass *
1633 make_pass_ipa_increase_alignment (gcc::context *ctxt)
1635 return new pass_ipa_increase_alignment (ctxt);
1638 /* If the condition represented by T is a comparison or the SSA name
1639 result of a comparison, extract the comparison's operands. Represent
1640 T as NE_EXPR <T, 0> otherwise. */
1642 void
1643 scalar_cond_masked_key::get_cond_ops_from_tree (tree t)
1645 if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_comparison)
1647 this->code = TREE_CODE (t);
1648 this->op0 = TREE_OPERAND (t, 0);
1649 this->op1 = TREE_OPERAND (t, 1);
1650 return;
1653 if (TREE_CODE (t) == SSA_NAME)
1654 if (gassign *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (t)))
1656 tree_code code = gimple_assign_rhs_code (stmt);
1657 if (TREE_CODE_CLASS (code) == tcc_comparison)
1659 this->code = code;
1660 this->op0 = gimple_assign_rhs1 (stmt);
1661 this->op1 = gimple_assign_rhs2 (stmt);
1662 return;
1666 this->code = NE_EXPR;
1667 this->op0 = t;
1668 this->op1 = build_zero_cst (TREE_TYPE (t));