re PR libfortran/68744 (FAIL: gfortran.dg/backtrace_1.f90 -O0 execution test)
[official-gcc.git] / gcc / tree-vectorizer.c
blobe1c2ced8efb4b38af536e5ef1c438965cd9436b1
1 /* Vectorizer
2 Copyright (C) 2003-2016 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-cfg.h"
73 #include "cfgloop.h"
74 #include "tree-vectorizer.h"
75 #include "tree-ssa-propagate.h"
76 #include "dbgcnt.h"
77 #include "tree-scalar-evolution.h"
80 /* Loop or bb location. */
81 source_location vect_location;
83 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
84 vec<stmt_vec_info> stmt_vec_info_vec;
86 /* For mapping simduid to vectorization factor. */
88 struct simduid_to_vf : free_ptr_hash<simduid_to_vf>
90 unsigned int simduid;
91 int vf;
93 /* hash_table support. */
94 static inline hashval_t hash (const simduid_to_vf *);
95 static inline int equal (const simduid_to_vf *, const simduid_to_vf *);
98 inline hashval_t
99 simduid_to_vf::hash (const simduid_to_vf *p)
101 return p->simduid;
104 inline int
105 simduid_to_vf::equal (const simduid_to_vf *p1, const simduid_to_vf *p2)
107 return p1->simduid == p2->simduid;
110 /* This hash maps the OMP simd array to the corresponding simduid used
111 to index into it. Like thus,
113 _7 = GOMP_SIMD_LANE (simduid.0)
116 D.1737[_7] = stuff;
119 This hash maps from the OMP simd array (D.1737[]) to DECL_UID of
120 simduid.0. */
122 struct simd_array_to_simduid : free_ptr_hash<simd_array_to_simduid>
124 tree decl;
125 unsigned int simduid;
127 /* hash_table support. */
128 static inline hashval_t hash (const simd_array_to_simduid *);
129 static inline int equal (const simd_array_to_simduid *,
130 const simd_array_to_simduid *);
133 inline hashval_t
134 simd_array_to_simduid::hash (const simd_array_to_simduid *p)
136 return DECL_UID (p->decl);
139 inline int
140 simd_array_to_simduid::equal (const simd_array_to_simduid *p1,
141 const simd_array_to_simduid *p2)
143 return p1->decl == p2->decl;
146 /* Fold IFN_GOMP_SIMD_LANE, IFN_GOMP_SIMD_VF, IFN_GOMP_SIMD_LAST_LANE,
147 into their corresponding constants and remove
148 IFN_GOMP_SIMD_ORDERED_{START,END}. */
150 static void
151 adjust_simduid_builtins (hash_table<simduid_to_vf> *htab)
153 basic_block bb;
155 FOR_EACH_BB_FN (bb, cfun)
157 gimple_stmt_iterator i;
159 for (i = gsi_start_bb (bb); !gsi_end_p (i); )
161 unsigned int vf = 1;
162 enum internal_fn ifn;
163 gimple *stmt = gsi_stmt (i);
164 tree t;
165 if (!is_gimple_call (stmt)
166 || !gimple_call_internal_p (stmt))
168 gsi_next (&i);
169 continue;
171 ifn = gimple_call_internal_fn (stmt);
172 switch (ifn)
174 case IFN_GOMP_SIMD_LANE:
175 case IFN_GOMP_SIMD_VF:
176 case IFN_GOMP_SIMD_LAST_LANE:
177 break;
178 case IFN_GOMP_SIMD_ORDERED_START:
179 case IFN_GOMP_SIMD_ORDERED_END:
180 if (integer_onep (gimple_call_arg (stmt, 0)))
182 enum built_in_function bcode
183 = (ifn == IFN_GOMP_SIMD_ORDERED_START
184 ? BUILT_IN_GOMP_ORDERED_START
185 : BUILT_IN_GOMP_ORDERED_END);
186 gimple *g
187 = gimple_build_call (builtin_decl_explicit (bcode), 0);
188 tree vdef = gimple_vdef (stmt);
189 gimple_set_vdef (g, vdef);
190 SSA_NAME_DEF_STMT (vdef) = g;
191 gimple_set_vuse (g, gimple_vuse (stmt));
192 gsi_replace (&i, g, true);
193 continue;
195 gsi_remove (&i, true);
196 unlink_stmt_vdef (stmt);
197 continue;
198 default:
199 gsi_next (&i);
200 continue;
202 tree arg = gimple_call_arg (stmt, 0);
203 gcc_assert (arg != NULL_TREE);
204 gcc_assert (TREE_CODE (arg) == SSA_NAME);
205 simduid_to_vf *p = NULL, data;
206 data.simduid = DECL_UID (SSA_NAME_VAR (arg));
207 if (htab)
209 p = htab->find (&data);
210 if (p)
211 vf = p->vf;
213 switch (ifn)
215 case IFN_GOMP_SIMD_VF:
216 t = build_int_cst (unsigned_type_node, vf);
217 break;
218 case IFN_GOMP_SIMD_LANE:
219 t = build_int_cst (unsigned_type_node, 0);
220 break;
221 case IFN_GOMP_SIMD_LAST_LANE:
222 t = gimple_call_arg (stmt, 1);
223 break;
224 default:
225 gcc_unreachable ();
227 update_call_from_tree (&i, t);
228 gsi_next (&i);
233 /* Helper structure for note_simd_array_uses. */
235 struct note_simd_array_uses_struct
237 hash_table<simd_array_to_simduid> **htab;
238 unsigned int simduid;
241 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
243 static tree
244 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
246 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
247 struct note_simd_array_uses_struct *ns
248 = (struct note_simd_array_uses_struct *) wi->info;
250 if (TYPE_P (*tp))
251 *walk_subtrees = 0;
252 else if (VAR_P (*tp)
253 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
254 && DECL_CONTEXT (*tp) == current_function_decl)
256 simd_array_to_simduid data;
257 if (!*ns->htab)
258 *ns->htab = new hash_table<simd_array_to_simduid> (15);
259 data.decl = *tp;
260 data.simduid = ns->simduid;
261 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
262 if (*slot == NULL)
264 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
265 *p = data;
266 *slot = p;
268 else if ((*slot)->simduid != ns->simduid)
269 (*slot)->simduid = -1U;
270 *walk_subtrees = 0;
272 return NULL_TREE;
275 /* Find "omp simd array" temporaries and map them to corresponding
276 simduid. */
278 static void
279 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
281 basic_block bb;
282 gimple_stmt_iterator gsi;
283 struct walk_stmt_info wi;
284 struct note_simd_array_uses_struct ns;
286 memset (&wi, 0, sizeof (wi));
287 wi.info = &ns;
288 ns.htab = htab;
290 FOR_EACH_BB_FN (bb, cfun)
291 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
293 gimple *stmt = gsi_stmt (gsi);
294 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
295 continue;
296 switch (gimple_call_internal_fn (stmt))
298 case IFN_GOMP_SIMD_LANE:
299 case IFN_GOMP_SIMD_VF:
300 case IFN_GOMP_SIMD_LAST_LANE:
301 break;
302 default:
303 continue;
305 tree lhs = gimple_call_lhs (stmt);
306 if (lhs == NULL_TREE)
307 continue;
308 imm_use_iterator use_iter;
309 gimple *use_stmt;
310 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
311 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
312 if (!is_gimple_debug (use_stmt))
313 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
317 /* Shrink arrays with "omp simd array" attribute to the corresponding
318 vectorization factor. */
320 static void
321 shrink_simd_arrays
322 (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
323 hash_table<simduid_to_vf> *simduid_to_vf_htab)
325 for (hash_table<simd_array_to_simduid>::iterator iter
326 = simd_array_to_simduid_htab->begin ();
327 iter != simd_array_to_simduid_htab->end (); ++iter)
328 if ((*iter)->simduid != -1U)
330 tree decl = (*iter)->decl;
331 int vf = 1;
332 if (simduid_to_vf_htab)
334 simduid_to_vf *p = NULL, data;
335 data.simduid = (*iter)->simduid;
336 p = simduid_to_vf_htab->find (&data);
337 if (p)
338 vf = p->vf;
340 tree atype
341 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
342 TREE_TYPE (decl) = atype;
343 relayout_decl (decl);
346 delete simd_array_to_simduid_htab;
349 /* A helper function to free data refs. */
351 void
352 vect_destroy_datarefs (vec_info *vinfo)
354 struct data_reference *dr;
355 unsigned int i;
357 FOR_EACH_VEC_ELT (vinfo->datarefs, i, dr)
358 if (dr->aux)
360 free (dr->aux);
361 dr->aux = NULL;
364 free_data_refs (vinfo->datarefs);
368 /* Return whether STMT is inside the region we try to vectorize. */
370 bool
371 vect_stmt_in_region_p (vec_info *vinfo, gimple *stmt)
373 if (!gimple_bb (stmt))
374 return false;
376 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
378 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
379 if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
380 return false;
382 else
384 bb_vec_info bb_vinfo = as_a <bb_vec_info> (vinfo);
385 if (gimple_bb (stmt) != BB_VINFO_BB (bb_vinfo)
386 || gimple_uid (stmt) == -1U
387 || gimple_code (stmt) == GIMPLE_PHI)
388 return false;
391 return true;
395 /* If LOOP has been versioned during ifcvt, return the internal call
396 guarding it. */
398 static gimple *
399 vect_loop_vectorized_call (struct loop *loop)
401 basic_block bb = loop_preheader_edge (loop)->src;
402 gimple *g;
405 g = last_stmt (bb);
406 if (g)
407 break;
408 if (!single_pred_p (bb))
409 break;
410 bb = single_pred (bb);
412 while (1);
413 if (g && gimple_code (g) == GIMPLE_COND)
415 gimple_stmt_iterator gsi = gsi_for_stmt (g);
416 gsi_prev (&gsi);
417 if (!gsi_end_p (gsi))
419 g = gsi_stmt (gsi);
420 if (is_gimple_call (g)
421 && gimple_call_internal_p (g)
422 && gimple_call_internal_fn (g) == IFN_LOOP_VECTORIZED
423 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
424 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
425 return g;
428 return NULL;
431 /* Fold LOOP_VECTORIZED internal call G to VALUE and
432 update any immediate uses of it's LHS. */
434 static void
435 fold_loop_vectorized_call (gimple *g, tree value)
437 tree lhs = gimple_call_lhs (g);
438 use_operand_p use_p;
439 imm_use_iterator iter;
440 gimple *use_stmt;
441 gimple_stmt_iterator gsi = gsi_for_stmt (g);
443 update_call_from_tree (&gsi, value);
444 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
446 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
447 SET_USE (use_p, value);
448 update_stmt (use_stmt);
451 /* Set the uids of all the statements in basic blocks inside loop
452 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
453 call guarding the loop which has been if converted. */
454 static void
455 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
457 tree arg = gimple_call_arg (loop_vectorized_call, 1);
458 basic_block *bbs;
459 unsigned int i;
460 struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
462 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
463 gcc_checking_assert (vect_loop_vectorized_call
464 (LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
465 == loop_vectorized_call);
466 bbs = get_loop_body (scalar_loop);
467 for (i = 0; i < scalar_loop->num_nodes; i++)
469 basic_block bb = bbs[i];
470 gimple_stmt_iterator gsi;
471 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
473 gimple *phi = gsi_stmt (gsi);
474 gimple_set_uid (phi, 0);
476 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
478 gimple *stmt = gsi_stmt (gsi);
479 gimple_set_uid (stmt, 0);
482 free (bbs);
485 /* Function vectorize_loops.
487 Entry point to loop vectorization phase. */
489 unsigned
490 vectorize_loops (void)
492 unsigned int i;
493 unsigned int num_vectorized_loops = 0;
494 unsigned int vect_loops_num;
495 struct loop *loop;
496 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
497 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
498 bool any_ifcvt_loops = false;
499 unsigned ret = 0;
501 vect_loops_num = number_of_loops (cfun);
503 /* Bail out if there are no loops. */
504 if (vect_loops_num <= 1)
505 return 0;
507 if (cfun->has_simduid_loops)
508 note_simd_array_uses (&simd_array_to_simduid_htab);
510 init_stmt_vec_info_vec ();
512 /* ----------- Analyze loops. ----------- */
514 /* If some loop was duplicated, it gets bigger number
515 than all previously defined loops. This fact allows us to run
516 only over initial loops skipping newly generated ones. */
517 FOR_EACH_LOOP (loop, 0)
518 if (loop->dont_vectorize)
519 any_ifcvt_loops = true;
520 else if ((flag_tree_loop_vectorize
521 && optimize_loop_nest_for_speed_p (loop))
522 || loop->force_vectorize)
524 loop_vec_info loop_vinfo;
525 vect_location = find_loop_location (loop);
526 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
527 && dump_enabled_p ())
528 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
529 LOCATION_FILE (vect_location),
530 LOCATION_LINE (vect_location));
532 loop_vinfo = vect_analyze_loop (loop);
533 loop->aux = loop_vinfo;
535 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
536 continue;
538 if (!dbg_cnt (vect_loop))
540 /* We may miss some if-converted loops due to
541 debug counter. Set any_ifcvt_loops to visit
542 them at finalization. */
543 any_ifcvt_loops = true;
544 break;
547 gimple *loop_vectorized_call = vect_loop_vectorized_call (loop);
548 if (loop_vectorized_call)
549 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
550 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
551 && dump_enabled_p ())
552 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
553 "loop vectorized\n");
554 vect_transform_loop (loop_vinfo);
555 num_vectorized_loops++;
556 /* Now that the loop has been vectorized, allow it to be unrolled
557 etc. */
558 loop->force_vectorize = false;
560 if (loop->simduid)
562 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
563 if (!simduid_to_vf_htab)
564 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
565 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
566 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
567 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
568 = simduid_to_vf_data;
571 if (loop_vectorized_call)
573 fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
574 ret |= TODO_cleanup_cfg;
578 vect_location = UNKNOWN_LOCATION;
580 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
581 if (dump_enabled_p ()
582 || (num_vectorized_loops > 0 && dump_enabled_p ()))
583 dump_printf_loc (MSG_NOTE, vect_location,
584 "vectorized %u loops in function.\n",
585 num_vectorized_loops);
587 /* ----------- Finalize. ----------- */
589 if (any_ifcvt_loops)
590 for (i = 1; i < vect_loops_num; i++)
592 loop = get_loop (cfun, i);
593 if (loop && loop->dont_vectorize)
595 gimple *g = vect_loop_vectorized_call (loop);
596 if (g)
598 fold_loop_vectorized_call (g, boolean_false_node);
599 ret |= TODO_cleanup_cfg;
604 for (i = 1; i < vect_loops_num; i++)
606 loop_vec_info loop_vinfo;
608 loop = get_loop (cfun, i);
609 if (!loop)
610 continue;
611 loop_vinfo = (loop_vec_info) loop->aux;
612 destroy_loop_vec_info (loop_vinfo, true);
613 loop->aux = NULL;
616 free_stmt_vec_info_vec ();
618 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
619 if (cfun->has_simduid_loops)
620 adjust_simduid_builtins (simduid_to_vf_htab);
622 /* Shrink any "omp array simd" temporary arrays to the
623 actual vectorization factors. */
624 if (simd_array_to_simduid_htab)
625 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
626 delete simduid_to_vf_htab;
627 cfun->has_simduid_loops = false;
629 if (num_vectorized_loops > 0)
631 /* If we vectorized any loop only virtual SSA form needs to be updated.
632 ??? Also while we try hard to update loop-closed SSA form we fail
633 to properly do this in some corner-cases (see PR56286). */
634 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
635 return TODO_cleanup_cfg;
638 return ret;
642 /* Entry point to the simduid cleanup pass. */
644 namespace {
646 const pass_data pass_data_simduid_cleanup =
648 GIMPLE_PASS, /* type */
649 "simduid", /* name */
650 OPTGROUP_NONE, /* optinfo_flags */
651 TV_NONE, /* tv_id */
652 ( PROP_ssa | PROP_cfg ), /* properties_required */
653 0, /* properties_provided */
654 0, /* properties_destroyed */
655 0, /* todo_flags_start */
656 0, /* todo_flags_finish */
659 class pass_simduid_cleanup : public gimple_opt_pass
661 public:
662 pass_simduid_cleanup (gcc::context *ctxt)
663 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
666 /* opt_pass methods: */
667 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
668 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
669 virtual unsigned int execute (function *);
671 }; // class pass_simduid_cleanup
673 unsigned int
674 pass_simduid_cleanup::execute (function *fun)
676 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
678 note_simd_array_uses (&simd_array_to_simduid_htab);
680 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
681 adjust_simduid_builtins (NULL);
683 /* Shrink any "omp array simd" temporary arrays to the
684 actual vectorization factors. */
685 if (simd_array_to_simduid_htab)
686 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
687 fun->has_simduid_loops = false;
688 return 0;
691 } // anon namespace
693 gimple_opt_pass *
694 make_pass_simduid_cleanup (gcc::context *ctxt)
696 return new pass_simduid_cleanup (ctxt);
700 /* Entry point to basic block SLP phase. */
702 namespace {
704 const pass_data pass_data_slp_vectorize =
706 GIMPLE_PASS, /* type */
707 "slp", /* name */
708 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
709 TV_TREE_SLP_VECTORIZATION, /* tv_id */
710 ( PROP_ssa | PROP_cfg ), /* properties_required */
711 0, /* properties_provided */
712 0, /* properties_destroyed */
713 0, /* todo_flags_start */
714 TODO_update_ssa, /* todo_flags_finish */
717 class pass_slp_vectorize : public gimple_opt_pass
719 public:
720 pass_slp_vectorize (gcc::context *ctxt)
721 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
724 /* opt_pass methods: */
725 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
726 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
727 virtual unsigned int execute (function *);
729 }; // class pass_slp_vectorize
731 unsigned int
732 pass_slp_vectorize::execute (function *fun)
734 basic_block bb;
736 bool in_loop_pipeline = scev_initialized_p ();
737 if (!in_loop_pipeline)
739 loop_optimizer_init (LOOPS_NORMAL);
740 scev_initialize ();
743 /* Mark all stmts as not belonging to the current region and unvisited. */
744 FOR_EACH_BB_FN (bb, fun)
746 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
747 gsi_next (&gsi))
749 gimple *stmt = gsi_stmt (gsi);
750 gimple_set_uid (stmt, -1);
751 gimple_set_visited (stmt, false);
755 init_stmt_vec_info_vec ();
757 FOR_EACH_BB_FN (bb, fun)
759 if (vect_slp_bb (bb))
760 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
761 "basic block vectorized\n");
764 free_stmt_vec_info_vec ();
766 if (!in_loop_pipeline)
768 scev_finalize ();
769 loop_optimizer_finalize ();
772 return 0;
775 } // anon namespace
777 gimple_opt_pass *
778 make_pass_slp_vectorize (gcc::context *ctxt)
780 return new pass_slp_vectorize (ctxt);
784 /* Increase alignment of global arrays to improve vectorization potential.
785 TODO:
786 - Consider also structs that have an array field.
787 - Use ipa analysis to prune arrays that can't be vectorized?
788 This should involve global alignment analysis and in the future also
789 array padding. */
791 static unsigned int
792 increase_alignment (void)
794 varpool_node *vnode;
796 vect_location = UNKNOWN_LOCATION;
798 /* Increase the alignment of all global arrays for vectorization. */
799 FOR_EACH_DEFINED_VARIABLE (vnode)
801 tree vectype, decl = vnode->decl;
802 tree t;
803 unsigned int alignment;
805 t = TREE_TYPE (decl);
806 if (TREE_CODE (t) != ARRAY_TYPE)
807 continue;
808 vectype = get_vectype_for_scalar_type (strip_array_types (t));
809 if (!vectype)
810 continue;
811 alignment = TYPE_ALIGN (vectype);
812 if (DECL_ALIGN (decl) >= alignment)
813 continue;
815 if (vect_can_force_dr_alignment_p (decl, alignment))
817 vnode->increase_alignment (TYPE_ALIGN (vectype));
818 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
819 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
820 dump_printf (MSG_NOTE, "\n");
823 return 0;
827 namespace {
829 const pass_data pass_data_ipa_increase_alignment =
831 SIMPLE_IPA_PASS, /* type */
832 "increase_alignment", /* name */
833 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
834 TV_IPA_OPT, /* tv_id */
835 0, /* properties_required */
836 0, /* properties_provided */
837 0, /* properties_destroyed */
838 0, /* todo_flags_start */
839 0, /* todo_flags_finish */
842 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
844 public:
845 pass_ipa_increase_alignment (gcc::context *ctxt)
846 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
849 /* opt_pass methods: */
850 virtual bool gate (function *)
852 return flag_section_anchors && flag_tree_loop_vectorize;
855 virtual unsigned int execute (function *) { return increase_alignment (); }
857 }; // class pass_ipa_increase_alignment
859 } // anon namespace
861 simple_ipa_opt_pass *
862 make_pass_ipa_increase_alignment (gcc::context *ctxt)
864 return new pass_ipa_increase_alignment (ctxt);