invoke.texi: Add documentation for the new option.
[gcc.git] / gcc / tree-vectorizer.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 /* Loop and basic block vectorizer.
23
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
29
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)
41
42 Here's a poor attempt at illustrating that:
43
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
56 */
57
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"
70
71 /* Loop or bb location. */
72 LOC vect_location;
73
74 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
75 VEC(vec_void_p,heap) *stmt_vec_info_vec;
76
77 \f
78 /* Function vectorize_loops.
79
80 Entry point to loop vectorization phase. */
81
82 unsigned
83 vectorize_loops (void)
84 {
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;
90
91 vect_loops_num = number_of_loops ();
92
93 /* Bail out if there are no loops. */
94 if (vect_loops_num <= 1)
95 return 0;
96
97 init_stmt_vec_info_vec ();
98
99 /* ----------- Analyze loops. ----------- */
100
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))
106 {
107 loop_vec_info loop_vinfo;
108 vect_location = find_loop_location (loop);
109 if (vect_location != UNKNOWN_LOC && dump_kind_p (MSG_ALL))
110 dump_printf (MSG_ALL, "\nAnalyzing loop at %s:%d\n",
111 LOC_FILE (vect_location), LOC_LINE (vect_location));
112
113 loop_vinfo = vect_analyze_loop (loop);
114 loop->aux = loop_vinfo;
115
116 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
117 continue;
118
119 if (vect_location != UNKNOWN_LOC && dump_kind_p (MSG_ALL))
120 dump_printf (MSG_ALL, "\n\nVectorizing loop at %s:%d\n",
121 LOC_FILE (vect_location), LOC_LINE (vect_location));
122 vect_transform_loop (loop_vinfo);
123 num_vectorized_loops++;
124 }
125
126 vect_location = UNKNOWN_LOC;
127
128 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
129 if (dump_kind_p (MSG_ALL)
130 || (num_vectorized_loops > 0 && dump_kind_p (MSG_ALL)))
131 dump_printf_loc (MSG_ALL, vect_location,
132 "vectorized %u loops in function.\n",
133 num_vectorized_loops);
134
135 /* ----------- Finalize. ----------- */
136
137 for (i = 1; i < vect_loops_num; i++)
138 {
139 loop_vec_info loop_vinfo;
140
141 loop = get_loop (i);
142 if (!loop)
143 continue;
144 loop_vinfo = (loop_vec_info) loop->aux;
145 destroy_loop_vec_info (loop_vinfo, true);
146 loop->aux = NULL;
147 }
148
149 free_stmt_vec_info_vec ();
150
151 return num_vectorized_loops > 0 ? TODO_cleanup_cfg : 0;
152 }
153
154
155 /* Entry point to basic block SLP phase. */
156
157 static unsigned int
158 execute_vect_slp (void)
159 {
160 basic_block bb;
161
162 init_stmt_vec_info_vec ();
163
164 FOR_EACH_BB (bb)
165 {
166 vect_location = find_bb_location (bb);
167
168 if (vect_slp_analyze_bb (bb))
169 {
170 vect_slp_transform_bb (bb);
171 if (dump_kind_p (MSG_OPTIMIZED_LOCATIONS))
172 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
173 "basic block vectorized using SLP\n");
174 }
175 }
176
177 free_stmt_vec_info_vec ();
178 return 0;
179 }
180
181 static bool
182 gate_vect_slp (void)
183 {
184 /* Apply SLP either if the vectorizer is on and the user didn't specify
185 whether to run SLP or not, or if the SLP flag was set by the user. */
186 return ((flag_tree_vectorize != 0 && flag_tree_slp_vectorize != 0)
187 || flag_tree_slp_vectorize == 1);
188 }
189
190 struct gimple_opt_pass pass_slp_vectorize =
191 {
192 {
193 GIMPLE_PASS,
194 "slp", /* name */
195 gate_vect_slp, /* gate */
196 execute_vect_slp, /* execute */
197 NULL, /* sub */
198 NULL, /* next */
199 0, /* static_pass_number */
200 TV_TREE_SLP_VECTORIZATION, /* tv_id */
201 PROP_ssa | PROP_cfg, /* properties_required */
202 0, /* properties_provided */
203 0, /* properties_destroyed */
204 0, /* todo_flags_start */
205 TODO_ggc_collect
206 | TODO_verify_ssa
207 | TODO_update_ssa
208 | TODO_verify_stmts /* todo_flags_finish */
209 }
210 };
211
212
213 /* Increase alignment of global arrays to improve vectorization potential.
214 TODO:
215 - Consider also structs that have an array field.
216 - Use ipa analysis to prune arrays that can't be vectorized?
217 This should involve global alignment analysis and in the future also
218 array padding. */
219
220 static unsigned int
221 increase_alignment (void)
222 {
223 struct varpool_node *vnode;
224
225 /* Increase the alignment of all global arrays for vectorization. */
226 FOR_EACH_DEFINED_VARIABLE (vnode)
227 {
228 tree vectype, decl = vnode->symbol.decl;
229 tree t;
230 unsigned int alignment;
231
232 t = TREE_TYPE(decl);
233 if (TREE_CODE (t) != ARRAY_TYPE)
234 continue;
235 vectype = get_vectype_for_scalar_type (strip_array_types (t));
236 if (!vectype)
237 continue;
238 alignment = TYPE_ALIGN (vectype);
239 if (DECL_ALIGN (decl) >= alignment)
240 continue;
241
242 if (vect_can_force_dr_alignment_p (decl, alignment))
243 {
244 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
245 DECL_USER_ALIGN (decl) = 1;
246 dump_printf (MSG_NOTE, "Increasing alignment of decl: ");
247 dump_generic_expr (MSG_NOTE, TDF_SLIM, decl);
248 dump_printf (MSG_NOTE, "\n");
249 }
250 }
251 return 0;
252 }
253
254
255 static bool
256 gate_increase_alignment (void)
257 {
258 return flag_section_anchors && flag_tree_vectorize;
259 }
260
261
262 struct simple_ipa_opt_pass pass_ipa_increase_alignment =
263 {
264 {
265 SIMPLE_IPA_PASS,
266 "increase_alignment", /* name */
267 gate_increase_alignment, /* gate */
268 increase_alignment, /* execute */
269 NULL, /* sub */
270 NULL, /* next */
271 0, /* static_pass_number */
272 TV_IPA_OPT, /* tv_id */
273 0, /* properties_required */
274 0, /* properties_provided */
275 0, /* properties_destroyed */
276 0, /* todo_flags_start */
277 0 /* todo_flags_finish */
278 }
279 };