2016-07-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / tree-vectorizer.c
blob9fbd1836ecb905e228147d8ea3eab61a51ad737a
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 /* Need to nullify loop safelen field since it's value is not
208 valid after transformation. */
209 if (bb->loop_father && bb->loop_father->safelen > 0)
210 bb->loop_father->safelen = 0;
211 if (htab)
213 p = htab->find (&data);
214 if (p)
215 vf = p->vf;
217 switch (ifn)
219 case IFN_GOMP_SIMD_VF:
220 t = build_int_cst (unsigned_type_node, vf);
221 break;
222 case IFN_GOMP_SIMD_LANE:
223 t = build_int_cst (unsigned_type_node, 0);
224 break;
225 case IFN_GOMP_SIMD_LAST_LANE:
226 t = gimple_call_arg (stmt, 1);
227 break;
228 default:
229 gcc_unreachable ();
231 update_call_from_tree (&i, t);
232 gsi_next (&i);
237 /* Helper structure for note_simd_array_uses. */
239 struct note_simd_array_uses_struct
241 hash_table<simd_array_to_simduid> **htab;
242 unsigned int simduid;
245 /* Callback for note_simd_array_uses, called through walk_gimple_op. */
247 static tree
248 note_simd_array_uses_cb (tree *tp, int *walk_subtrees, void *data)
250 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
251 struct note_simd_array_uses_struct *ns
252 = (struct note_simd_array_uses_struct *) wi->info;
254 if (TYPE_P (*tp))
255 *walk_subtrees = 0;
256 else if (VAR_P (*tp)
257 && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (*tp))
258 && DECL_CONTEXT (*tp) == current_function_decl)
260 simd_array_to_simduid data;
261 if (!*ns->htab)
262 *ns->htab = new hash_table<simd_array_to_simduid> (15);
263 data.decl = *tp;
264 data.simduid = ns->simduid;
265 simd_array_to_simduid **slot = (*ns->htab)->find_slot (&data, INSERT);
266 if (*slot == NULL)
268 simd_array_to_simduid *p = XNEW (simd_array_to_simduid);
269 *p = data;
270 *slot = p;
272 else if ((*slot)->simduid != ns->simduid)
273 (*slot)->simduid = -1U;
274 *walk_subtrees = 0;
276 return NULL_TREE;
279 /* Find "omp simd array" temporaries and map them to corresponding
280 simduid. */
282 static void
283 note_simd_array_uses (hash_table<simd_array_to_simduid> **htab)
285 basic_block bb;
286 gimple_stmt_iterator gsi;
287 struct walk_stmt_info wi;
288 struct note_simd_array_uses_struct ns;
290 memset (&wi, 0, sizeof (wi));
291 wi.info = &ns;
292 ns.htab = htab;
294 FOR_EACH_BB_FN (bb, cfun)
295 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
297 gimple *stmt = gsi_stmt (gsi);
298 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
299 continue;
300 switch (gimple_call_internal_fn (stmt))
302 case IFN_GOMP_SIMD_LANE:
303 case IFN_GOMP_SIMD_VF:
304 case IFN_GOMP_SIMD_LAST_LANE:
305 break;
306 default:
307 continue;
309 tree lhs = gimple_call_lhs (stmt);
310 if (lhs == NULL_TREE)
311 continue;
312 imm_use_iterator use_iter;
313 gimple *use_stmt;
314 ns.simduid = DECL_UID (SSA_NAME_VAR (gimple_call_arg (stmt, 0)));
315 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, lhs)
316 if (!is_gimple_debug (use_stmt))
317 walk_gimple_op (use_stmt, note_simd_array_uses_cb, &wi);
321 /* Shrink arrays with "omp simd array" attribute to the corresponding
322 vectorization factor. */
324 static void
325 shrink_simd_arrays
326 (hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab,
327 hash_table<simduid_to_vf> *simduid_to_vf_htab)
329 for (hash_table<simd_array_to_simduid>::iterator iter
330 = simd_array_to_simduid_htab->begin ();
331 iter != simd_array_to_simduid_htab->end (); ++iter)
332 if ((*iter)->simduid != -1U)
334 tree decl = (*iter)->decl;
335 int vf = 1;
336 if (simduid_to_vf_htab)
338 simduid_to_vf *p = NULL, data;
339 data.simduid = (*iter)->simduid;
340 p = simduid_to_vf_htab->find (&data);
341 if (p)
342 vf = p->vf;
344 tree atype
345 = build_array_type_nelts (TREE_TYPE (TREE_TYPE (decl)), vf);
346 TREE_TYPE (decl) = atype;
347 relayout_decl (decl);
350 delete simd_array_to_simduid_htab;
353 /* A helper function to free data refs. */
355 void
356 vect_destroy_datarefs (vec_info *vinfo)
358 struct data_reference *dr;
359 unsigned int i;
361 FOR_EACH_VEC_ELT (vinfo->datarefs, i, dr)
362 if (dr->aux)
364 free (dr->aux);
365 dr->aux = NULL;
368 free_data_refs (vinfo->datarefs);
372 /* Return whether STMT is inside the region we try to vectorize. */
374 bool
375 vect_stmt_in_region_p (vec_info *vinfo, gimple *stmt)
377 if (!gimple_bb (stmt))
378 return false;
380 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
382 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
383 if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
384 return false;
386 else
388 bb_vec_info bb_vinfo = as_a <bb_vec_info> (vinfo);
389 if (gimple_bb (stmt) != BB_VINFO_BB (bb_vinfo)
390 || gimple_uid (stmt) == -1U
391 || gimple_code (stmt) == GIMPLE_PHI)
392 return false;
395 return true;
399 /* If LOOP has been versioned during ifcvt, return the internal call
400 guarding it. */
402 static gimple *
403 vect_loop_vectorized_call (struct loop *loop)
405 basic_block bb = loop_preheader_edge (loop)->src;
406 gimple *g;
409 g = last_stmt (bb);
410 if (g)
411 break;
412 if (!single_pred_p (bb))
413 break;
414 bb = single_pred (bb);
416 while (1);
417 if (g && gimple_code (g) == GIMPLE_COND)
419 gimple_stmt_iterator gsi = gsi_for_stmt (g);
420 gsi_prev (&gsi);
421 if (!gsi_end_p (gsi))
423 g = gsi_stmt (gsi);
424 if (is_gimple_call (g)
425 && gimple_call_internal_p (g)
426 && gimple_call_internal_fn (g) == IFN_LOOP_VECTORIZED
427 && (tree_to_shwi (gimple_call_arg (g, 0)) == loop->num
428 || tree_to_shwi (gimple_call_arg (g, 1)) == loop->num))
429 return g;
432 return NULL;
435 /* Fold LOOP_VECTORIZED internal call G to VALUE and
436 update any immediate uses of it's LHS. */
438 static void
439 fold_loop_vectorized_call (gimple *g, tree value)
441 tree lhs = gimple_call_lhs (g);
442 use_operand_p use_p;
443 imm_use_iterator iter;
444 gimple *use_stmt;
445 gimple_stmt_iterator gsi = gsi_for_stmt (g);
447 update_call_from_tree (&gsi, value);
448 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
450 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
451 SET_USE (use_p, value);
452 update_stmt (use_stmt);
455 /* Set the uids of all the statements in basic blocks inside loop
456 represented by LOOP_VINFO. LOOP_VECTORIZED_CALL is the internal
457 call guarding the loop which has been if converted. */
458 static void
459 set_uid_loop_bbs (loop_vec_info loop_vinfo, gimple *loop_vectorized_call)
461 tree arg = gimple_call_arg (loop_vectorized_call, 1);
462 basic_block *bbs;
463 unsigned int i;
464 struct loop *scalar_loop = get_loop (cfun, tree_to_shwi (arg));
466 LOOP_VINFO_SCALAR_LOOP (loop_vinfo) = scalar_loop;
467 gcc_checking_assert (vect_loop_vectorized_call
468 (LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
469 == loop_vectorized_call);
470 bbs = get_loop_body (scalar_loop);
471 for (i = 0; i < scalar_loop->num_nodes; i++)
473 basic_block bb = bbs[i];
474 gimple_stmt_iterator gsi;
475 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
477 gimple *phi = gsi_stmt (gsi);
478 gimple_set_uid (phi, 0);
480 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
482 gimple *stmt = gsi_stmt (gsi);
483 gimple_set_uid (stmt, 0);
486 free (bbs);
489 /* Function vectorize_loops.
491 Entry point to loop vectorization phase. */
493 unsigned
494 vectorize_loops (void)
496 unsigned int i;
497 unsigned int num_vectorized_loops = 0;
498 unsigned int vect_loops_num;
499 struct loop *loop;
500 hash_table<simduid_to_vf> *simduid_to_vf_htab = NULL;
501 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
502 bool any_ifcvt_loops = false;
503 unsigned ret = 0;
505 vect_loops_num = number_of_loops (cfun);
507 /* Bail out if there are no loops. */
508 if (vect_loops_num <= 1)
509 return 0;
511 if (cfun->has_simduid_loops)
512 note_simd_array_uses (&simd_array_to_simduid_htab);
514 init_stmt_vec_info_vec ();
516 /* ----------- Analyze loops. ----------- */
518 /* If some loop was duplicated, it gets bigger number
519 than all previously defined loops. This fact allows us to run
520 only over initial loops skipping newly generated ones. */
521 FOR_EACH_LOOP (loop, 0)
522 if (loop->dont_vectorize)
523 any_ifcvt_loops = true;
524 else if ((flag_tree_loop_vectorize
525 && optimize_loop_nest_for_speed_p (loop))
526 || loop->force_vectorize)
528 loop_vec_info loop_vinfo;
529 vect_location = find_loop_location (loop);
530 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
531 && dump_enabled_p ())
532 dump_printf (MSG_NOTE, "\nAnalyzing loop at %s:%d\n",
533 LOCATION_FILE (vect_location),
534 LOCATION_LINE (vect_location));
536 loop_vinfo = vect_analyze_loop (loop);
537 loop->aux = loop_vinfo;
539 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
540 continue;
542 if (!dbg_cnt (vect_loop))
544 /* We may miss some if-converted loops due to
545 debug counter. Set any_ifcvt_loops to visit
546 them at finalization. */
547 any_ifcvt_loops = true;
548 break;
551 gimple *loop_vectorized_call = vect_loop_vectorized_call (loop);
552 if (loop_vectorized_call)
553 set_uid_loop_bbs (loop_vinfo, loop_vectorized_call);
554 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOCATION
555 && dump_enabled_p ())
556 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
557 "loop vectorized\n");
558 vect_transform_loop (loop_vinfo);
559 num_vectorized_loops++;
560 /* Now that the loop has been vectorized, allow it to be unrolled
561 etc. */
562 loop->force_vectorize = false;
564 if (loop->simduid)
566 simduid_to_vf *simduid_to_vf_data = XNEW (simduid_to_vf);
567 if (!simduid_to_vf_htab)
568 simduid_to_vf_htab = new hash_table<simduid_to_vf> (15);
569 simduid_to_vf_data->simduid = DECL_UID (loop->simduid);
570 simduid_to_vf_data->vf = loop_vinfo->vectorization_factor;
571 *simduid_to_vf_htab->find_slot (simduid_to_vf_data, INSERT)
572 = simduid_to_vf_data;
575 if (loop_vectorized_call)
577 fold_loop_vectorized_call (loop_vectorized_call, boolean_true_node);
578 ret |= TODO_cleanup_cfg;
582 vect_location = UNKNOWN_LOCATION;
584 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
585 if (dump_enabled_p ()
586 || (num_vectorized_loops > 0 && dump_enabled_p ()))
587 dump_printf_loc (MSG_NOTE, vect_location,
588 "vectorized %u loops in function.\n",
589 num_vectorized_loops);
591 /* ----------- Finalize. ----------- */
593 if (any_ifcvt_loops)
594 for (i = 1; i < vect_loops_num; i++)
596 loop = get_loop (cfun, i);
597 if (loop && loop->dont_vectorize)
599 gimple *g = vect_loop_vectorized_call (loop);
600 if (g)
602 fold_loop_vectorized_call (g, boolean_false_node);
603 ret |= TODO_cleanup_cfg;
608 for (i = 1; i < vect_loops_num; i++)
610 loop_vec_info loop_vinfo;
611 bool has_mask_store;
613 loop = get_loop (cfun, i);
614 if (!loop)
615 continue;
616 loop_vinfo = (loop_vec_info) loop->aux;
617 has_mask_store = false;
618 if (loop_vinfo)
619 has_mask_store = LOOP_VINFO_HAS_MASK_STORE (loop_vinfo);
620 destroy_loop_vec_info (loop_vinfo, true);
621 if (has_mask_store)
622 optimize_mask_stores (loop);
623 loop->aux = NULL;
626 free_stmt_vec_info_vec ();
628 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
629 if (cfun->has_simduid_loops)
630 adjust_simduid_builtins (simduid_to_vf_htab);
632 /* Shrink any "omp array simd" temporary arrays to the
633 actual vectorization factors. */
634 if (simd_array_to_simduid_htab)
635 shrink_simd_arrays (simd_array_to_simduid_htab, simduid_to_vf_htab);
636 delete simduid_to_vf_htab;
637 cfun->has_simduid_loops = false;
639 if (num_vectorized_loops > 0)
641 /* If we vectorized any loop only virtual SSA form needs to be updated.
642 ??? Also while we try hard to update loop-closed SSA form we fail
643 to properly do this in some corner-cases (see PR56286). */
644 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_only_virtuals);
645 return TODO_cleanup_cfg;
648 return ret;
652 /* Entry point to the simduid cleanup pass. */
654 namespace {
656 const pass_data pass_data_simduid_cleanup =
658 GIMPLE_PASS, /* type */
659 "simduid", /* name */
660 OPTGROUP_NONE, /* optinfo_flags */
661 TV_NONE, /* tv_id */
662 ( PROP_ssa | PROP_cfg ), /* properties_required */
663 0, /* properties_provided */
664 0, /* properties_destroyed */
665 0, /* todo_flags_start */
666 0, /* todo_flags_finish */
669 class pass_simduid_cleanup : public gimple_opt_pass
671 public:
672 pass_simduid_cleanup (gcc::context *ctxt)
673 : gimple_opt_pass (pass_data_simduid_cleanup, ctxt)
676 /* opt_pass methods: */
677 opt_pass * clone () { return new pass_simduid_cleanup (m_ctxt); }
678 virtual bool gate (function *fun) { return fun->has_simduid_loops; }
679 virtual unsigned int execute (function *);
681 }; // class pass_simduid_cleanup
683 unsigned int
684 pass_simduid_cleanup::execute (function *fun)
686 hash_table<simd_array_to_simduid> *simd_array_to_simduid_htab = NULL;
688 note_simd_array_uses (&simd_array_to_simduid_htab);
690 /* Fold IFN_GOMP_SIMD_{VF,LANE,LAST_LANE,ORDERED_{START,END}} builtins. */
691 adjust_simduid_builtins (NULL);
693 /* Shrink any "omp array simd" temporary arrays to the
694 actual vectorization factors. */
695 if (simd_array_to_simduid_htab)
696 shrink_simd_arrays (simd_array_to_simduid_htab, NULL);
697 fun->has_simduid_loops = false;
698 return 0;
701 } // anon namespace
703 gimple_opt_pass *
704 make_pass_simduid_cleanup (gcc::context *ctxt)
706 return new pass_simduid_cleanup (ctxt);
710 /* Entry point to basic block SLP phase. */
712 namespace {
714 const pass_data pass_data_slp_vectorize =
716 GIMPLE_PASS, /* type */
717 "slp", /* name */
718 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
719 TV_TREE_SLP_VECTORIZATION, /* tv_id */
720 ( PROP_ssa | PROP_cfg ), /* properties_required */
721 0, /* properties_provided */
722 0, /* properties_destroyed */
723 0, /* todo_flags_start */
724 TODO_update_ssa, /* todo_flags_finish */
727 class pass_slp_vectorize : public gimple_opt_pass
729 public:
730 pass_slp_vectorize (gcc::context *ctxt)
731 : gimple_opt_pass (pass_data_slp_vectorize, ctxt)
734 /* opt_pass methods: */
735 opt_pass * clone () { return new pass_slp_vectorize (m_ctxt); }
736 virtual bool gate (function *) { return flag_tree_slp_vectorize != 0; }
737 virtual unsigned int execute (function *);
739 }; // class pass_slp_vectorize
741 unsigned int
742 pass_slp_vectorize::execute (function *fun)
744 basic_block bb;
746 bool in_loop_pipeline = scev_initialized_p ();
747 if (!in_loop_pipeline)
749 loop_optimizer_init (LOOPS_NORMAL);
750 scev_initialize ();
753 /* Mark all stmts as not belonging to the current region and unvisited. */
754 FOR_EACH_BB_FN (bb, fun)
756 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
757 gsi_next (&gsi))
759 gimple *stmt = gsi_stmt (gsi);
760 gimple_set_uid (stmt, -1);
761 gimple_set_visited (stmt, false);
765 init_stmt_vec_info_vec ();
767 FOR_EACH_BB_FN (bb, fun)
769 if (vect_slp_bb (bb))
770 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
771 "basic block vectorized\n");
774 free_stmt_vec_info_vec ();
776 if (!in_loop_pipeline)
778 scev_finalize ();
779 loop_optimizer_finalize ();
782 return 0;
785 } // anon namespace
787 gimple_opt_pass *
788 make_pass_slp_vectorize (gcc::context *ctxt)
790 return new pass_slp_vectorize (ctxt);
794 /* Increase alignment of global arrays to improve vectorization potential.
795 TODO:
796 - Consider also structs that have an array field.
797 - Use ipa analysis to prune arrays that can't be vectorized?
798 This should involve global alignment analysis and in the future also
799 array padding. */
801 static unsigned get_vec_alignment_for_type (tree);
802 static hash_map<tree, unsigned> *type_align_map;
804 /* Return alignment of array's vector type corresponding to scalar type.
805 0 if no vector type exists. */
806 static unsigned
807 get_vec_alignment_for_array_type (tree type)
809 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
811 tree vectype = get_vectype_for_scalar_type (strip_array_types (type));
812 if (!vectype
813 || !TYPE_SIZE (type)
814 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
815 || tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (vectype)))
816 return 0;
818 return TYPE_ALIGN (vectype);
821 /* Return alignment of field having maximum alignment of vector type
822 corresponding to it's scalar type. For now, we only consider fields whose
823 offset is a multiple of it's vector alignment.
824 0 if no suitable field is found. */
825 static unsigned
826 get_vec_alignment_for_record_type (tree type)
828 gcc_assert (TREE_CODE (type) == RECORD_TYPE);
830 unsigned max_align = 0, alignment;
831 HOST_WIDE_INT offset;
832 tree offset_tree;
834 if (TYPE_PACKED (type))
835 return 0;
837 unsigned *slot = type_align_map->get (type);
838 if (slot)
839 return *slot;
841 for (tree field = first_field (type);
842 field != NULL_TREE;
843 field = DECL_CHAIN (field))
845 /* Skip if not FIELD_DECL or if alignment is set by user. */
846 if (TREE_CODE (field) != FIELD_DECL
847 || DECL_USER_ALIGN (field)
848 || DECL_ARTIFICIAL (field))
849 continue;
851 /* We don't need to process the type further if offset is variable,
852 since the offsets of remaining members will also be variable. */
853 if (TREE_CODE (DECL_FIELD_OFFSET (field)) != INTEGER_CST
854 || TREE_CODE (DECL_FIELD_BIT_OFFSET (field)) != INTEGER_CST)
855 break;
857 /* Similarly stop processing the type if offset_tree
858 does not fit in unsigned HOST_WIDE_INT. */
859 offset_tree = bit_position (field);
860 if (!tree_fits_uhwi_p (offset_tree))
861 break;
863 offset = tree_to_uhwi (offset_tree);
864 alignment = get_vec_alignment_for_type (TREE_TYPE (field));
866 /* Get maximum alignment of vectorized field/array among those members
867 whose offset is multiple of the vector alignment. */
868 if (alignment
869 && (offset % alignment == 0)
870 && (alignment > max_align))
871 max_align = alignment;
874 type_align_map->put (type, max_align);
875 return max_align;
878 /* Return alignment of vector type corresponding to decl's scalar type
879 or 0 if it doesn't exist or the vector alignment is lesser than
880 decl's alignment. */
881 static unsigned
882 get_vec_alignment_for_type (tree type)
884 if (type == NULL_TREE)
885 return 0;
887 gcc_assert (TYPE_P (type));
889 static unsigned alignment = 0;
890 switch (TREE_CODE (type))
892 case ARRAY_TYPE:
893 alignment = get_vec_alignment_for_array_type (type);
894 break;
895 case RECORD_TYPE:
896 alignment = get_vec_alignment_for_record_type (type);
897 break;
898 default:
899 alignment = 0;
900 break;
903 return (alignment > TYPE_ALIGN (type)) ? alignment : 0;
906 /* Entry point to increase_alignment pass. */
907 static unsigned int
908 increase_alignment (void)
910 varpool_node *vnode;
912 vect_location = UNKNOWN_LOCATION;
913 type_align_map = new hash_map<tree, unsigned>;
915 /* Increase the alignment of all global arrays for vectorization. */
916 FOR_EACH_DEFINED_VARIABLE (vnode)
918 tree decl = vnode->decl;
919 unsigned int alignment;
921 if ((decl_in_symtab_p (decl)
922 && !symtab_node::get (decl)->can_increase_alignment_p ())
923 || DECL_USER_ALIGN (decl) || DECL_ARTIFICIAL (decl))
924 continue;
926 alignment = get_vec_alignment_for_type (TREE_TYPE (decl));
927 if (alignment && vect_can_force_dr_alignment_p (decl, alignment))
929 vnode->increase_alignment (alignment);
930 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
931 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
932 dump_printf (MSG_NOTE, "\n");
936 delete type_align_map;
937 return 0;
941 namespace {
943 const pass_data pass_data_ipa_increase_alignment =
945 SIMPLE_IPA_PASS, /* type */
946 "increase_alignment", /* name */
947 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
948 TV_IPA_OPT, /* tv_id */
949 0, /* properties_required */
950 0, /* properties_provided */
951 0, /* properties_destroyed */
952 0, /* todo_flags_start */
953 0, /* todo_flags_finish */
956 class pass_ipa_increase_alignment : public simple_ipa_opt_pass
958 public:
959 pass_ipa_increase_alignment (gcc::context *ctxt)
960 : simple_ipa_opt_pass (pass_data_ipa_increase_alignment, ctxt)
963 /* opt_pass methods: */
964 virtual bool gate (function *)
966 return flag_section_anchors && flag_tree_loop_vectorize;
969 virtual unsigned int execute (function *) { return increase_alignment (); }
971 }; // class pass_ipa_increase_alignment
973 } // anon namespace
975 simple_ipa_opt_pass *
976 make_pass_ipa_increase_alignment (gcc::context *ctxt)
978 return new pass_ipa_increase_alignment (ctxt);