missing Changelog
[official-gcc.git] / gcc / tree-vectorizer.c
blobec99663ec7fdc665fb0f1dcec828165440f8d8b9
1 /* Vectorizer
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Dorit Naishlos <dorit@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Loop and basic block vectorizer.
24 This file contains drivers for the three vectorizers:
25 (1) loop vectorizer (inter-iteration parallelism),
26 (2) loop-aware SLP (intra-iteration parallelism) (invoked by the loop
27 vectorizer)
28 (3) BB vectorizer (out-of-loops), aka SLP
30 The rest of the vectorizer's code is organized as follows:
31 - tree-vect-loop.c - loop specific parts such as reductions, etc. These are
32 used by drivers (1) and (2).
33 - tree-vect-loop-manip.c - vectorizer's loop control-flow utilities, used by
34 drivers (1) and (2).
35 - tree-vect-slp.c - BB vectorization specific analysis and transformation,
36 used by drivers (2) and (3).
37 - tree-vect-stmts.c - statements analysis and transformation (used by all).
38 - tree-vect-data-refs.c - vectorizer specific data-refs analysis and
39 manipulations (used by all).
40 - tree-vect-patterns.c - vectorizable code patterns detector (used by all)
42 Here's a poor attempt at illustrating that:
44 tree-vectorizer.c:
45 loop_vect() loop_aware_slp() slp_vect()
46 | / \ /
47 | / \ /
48 tree-vect-loop.c tree-vect-slp.c
49 | \ \ / / |
50 | \ \/ / |
51 | \ /\ / |
52 | \ / \ / |
53 tree-vect-stmts.c tree-vect-data-refs.c
54 \ /
55 tree-vect-patterns.c
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "dumpfile.h"
62 #include "tm.h"
63 #include "ggc.h"
64 #include "tree.h"
65 #include "tree-pretty-print.h"
66 #include "tree-flow.h"
67 #include "cfgloop.h"
68 #include "tree-vectorizer.h"
69 #include "tree-pass.h"
71 /* Loop or bb location. */
72 LOC vect_location;
74 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
75 vec<vec_void_p> stmt_vec_info_vec;
78 /* Function vectorize_loops.
80 Entry point to loop vectorization phase. */
82 unsigned
83 vectorize_loops (void)
85 unsigned int i;
86 unsigned int num_vectorized_loops = 0;
87 unsigned int vect_loops_num;
88 loop_iterator li;
89 struct loop *loop;
91 vect_loops_num = number_of_loops ();
93 /* Bail out if there are no loops. */
94 if (vect_loops_num <= 1)
95 return 0;
97 init_stmt_vec_info_vec ();
99 /* ----------- Analyze loops. ----------- */
101 /* If some loop was duplicated, it gets bigger number
102 than all previously defined loops. This fact allows us to run
103 only over initial loops skipping newly generated ones. */
104 FOR_EACH_LOOP (li, loop, 0)
105 if (optimize_loop_nest_for_speed_p (loop))
107 loop_vec_info loop_vinfo;
108 vect_location = find_loop_location (loop);
109 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
110 && dump_enabled_p ())
111 dump_printf (MSG_ALL, "\nAnalyzing loop at %s:%d\n",
112 LOC_FILE (vect_location), LOC_LINE (vect_location));
114 loop_vinfo = vect_analyze_loop (loop);
115 loop->aux = loop_vinfo;
117 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
118 continue;
120 if (LOCATION_LOCUS (vect_location) != UNKNOWN_LOC
121 && dump_enabled_p ())
122 dump_printf (MSG_ALL, "\n\nVectorizing loop at %s:%d\n",
123 LOC_FILE (vect_location), LOC_LINE (vect_location));
124 vect_transform_loop (loop_vinfo);
125 num_vectorized_loops++;
128 vect_location = UNKNOWN_LOC;
130 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
131 if (dump_enabled_p ()
132 || (num_vectorized_loops > 0 && dump_enabled_p ()))
133 dump_printf_loc (MSG_ALL, vect_location,
134 "vectorized %u loops in function.\n",
135 num_vectorized_loops);
137 /* ----------- Finalize. ----------- */
139 for (i = 1; i < vect_loops_num; i++)
141 loop_vec_info loop_vinfo;
143 loop = get_loop (i);
144 if (!loop)
145 continue;
146 loop_vinfo = (loop_vec_info) loop->aux;
147 destroy_loop_vec_info (loop_vinfo, true);
148 loop->aux = NULL;
151 free_stmt_vec_info_vec ();
153 return num_vectorized_loops > 0 ? TODO_cleanup_cfg : 0;
157 /* Entry point to basic block SLP phase. */
159 static unsigned int
160 execute_vect_slp (void)
162 basic_block bb;
164 init_stmt_vec_info_vec ();
166 FOR_EACH_BB (bb)
168 vect_location = find_bb_location (bb);
170 if (vect_slp_analyze_bb (bb))
172 vect_slp_transform_bb (bb);
173 if (dump_enabled_p ())
174 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
175 "basic block vectorized using SLP\n");
179 free_stmt_vec_info_vec ();
180 return 0;
183 static bool
184 gate_vect_slp (void)
186 /* Apply SLP either if the vectorizer is on and the user didn't specify
187 whether to run SLP or not, or if the SLP flag was set by the user. */
188 return ((flag_tree_vectorize != 0 && flag_tree_slp_vectorize != 0)
189 || flag_tree_slp_vectorize == 1);
192 struct gimple_opt_pass pass_slp_vectorize =
195 GIMPLE_PASS,
196 "slp", /* name */
197 OPTGROUP_LOOP
198 | OPTGROUP_VEC, /* optinfo_flags */
199 gate_vect_slp, /* gate */
200 execute_vect_slp, /* execute */
201 NULL, /* sub */
202 NULL, /* next */
203 0, /* static_pass_number */
204 TV_TREE_SLP_VECTORIZATION, /* tv_id */
205 PROP_ssa | PROP_cfg, /* properties_required */
206 0, /* properties_provided */
207 0, /* properties_destroyed */
208 0, /* todo_flags_start */
209 TODO_ggc_collect
210 | TODO_verify_ssa
211 | TODO_update_ssa
212 | TODO_verify_stmts /* todo_flags_finish */
217 /* Increase alignment of global arrays to improve vectorization potential.
218 TODO:
219 - Consider also structs that have an array field.
220 - Use ipa analysis to prune arrays that can't be vectorized?
221 This should involve global alignment analysis and in the future also
222 array padding. */
224 static unsigned int
225 increase_alignment (void)
227 struct varpool_node *vnode;
229 /* Increase the alignment of all global arrays for vectorization. */
230 FOR_EACH_DEFINED_VARIABLE (vnode)
232 tree vectype, decl = vnode->symbol.decl;
233 tree t;
234 unsigned int alignment;
236 t = TREE_TYPE(decl);
237 if (TREE_CODE (t) != ARRAY_TYPE)
238 continue;
239 vectype = get_vectype_for_scalar_type (strip_array_types (t));
240 if (!vectype)
241 continue;
242 alignment = TYPE_ALIGN (vectype);
243 if (DECL_ALIGN (decl) >= alignment)
244 continue;
246 if (vect_can_force_dr_alignment_p (decl, alignment))
248 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
249 DECL_USER_ALIGN (decl) = 1;
250 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
251 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
252 dump_printf (MSG_NOTE, "\n");
255 return 0;
259 static bool
260 gate_increase_alignment (void)
262 return flag_section_anchors && flag_tree_vectorize;
266 struct simple_ipa_opt_pass pass_ipa_increase_alignment =
269 SIMPLE_IPA_PASS,
270 "increase_alignment", /* name */
271 OPTGROUP_LOOP
272 | OPTGROUP_VEC, /* optinfo_flags */
273 gate_increase_alignment, /* gate */
274 increase_alignment, /* execute */
275 NULL, /* sub */
276 NULL, /* next */
277 0, /* static_pass_number */
278 TV_IPA_OPT, /* tv_id */
279 0, /* properties_required */
280 0, /* properties_provided */
281 0, /* properties_destroyed */
282 0, /* todo_flags_start */
283 0 /* todo_flags_finish */