gensupport.c (init_rtx_reader_args_cb): Start counting code generating patterns from...
[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 "tm.h"
62 #include "ggc.h"
63 #include "tree.h"
64 #include "tree-pretty-print.h"
65 #include "tree-flow.h"
66 #include "tree-dump.h"
67 #include "cfgloop.h"
68 #include "tree-vectorizer.h"
69 #include "tree-pass.h"
70 #include "timevar.h"
71
72 /* vect_dump will be set to stderr or dump_file if exist. */
73 FILE *vect_dump;
74
75 /* vect_verbosity_level set to an invalid value
76 to mark that it's uninitialized. */
77 static enum vect_verbosity_levels vect_verbosity_level = MAX_VERBOSITY_LEVEL;
78
79 /* Loop or bb location. */
80 LOC vect_location;
81
82 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
83 VEC(vec_void_p,heap) *stmt_vec_info_vec;
84
85 \f
86
87 /* Function vect_set_dump_settings.
88
89 Fix the verbosity level of the vectorizer if the
90 requested level was not set explicitly using the flag
91 -ftree-vectorizer-verbose=N.
92 Decide where to print the debugging information (dump_file/stderr).
93 If the user defined the verbosity level, but there is no dump file,
94 print to stderr, otherwise print to the dump file. */
95
96 static void
97 vect_set_dump_settings (bool slp)
98 {
99 vect_dump = dump_file;
100
101 /* Check if the verbosity level was defined by the user: */
102 if (user_vect_verbosity_level != MAX_VERBOSITY_LEVEL)
103 {
104 vect_verbosity_level = user_vect_verbosity_level;
105 /* Ignore user defined verbosity if dump flags require higher level of
106 verbosity. */
107 if (dump_file)
108 {
109 if (((dump_flags & TDF_DETAILS)
110 && vect_verbosity_level >= REPORT_DETAILS)
111 || ((dump_flags & TDF_STATS)
112 && vect_verbosity_level >= REPORT_UNVECTORIZED_LOCATIONS))
113 return;
114 }
115 else
116 {
117 /* If there is no dump file, print to stderr in case of loop
118 vectorization. */
119 if (!slp)
120 vect_dump = stderr;
121
122 return;
123 }
124 }
125
126 /* User didn't specify verbosity level: */
127 if (dump_file && (dump_flags & TDF_DETAILS))
128 vect_verbosity_level = REPORT_DETAILS;
129 else if (dump_file && (dump_flags & TDF_STATS))
130 vect_verbosity_level = REPORT_UNVECTORIZED_LOCATIONS;
131 else
132 vect_verbosity_level = REPORT_NONE;
133
134 gcc_assert (dump_file || vect_verbosity_level == REPORT_NONE);
135 }
136
137
138 /* Function debug_loop_details.
139
140 For vectorization debug dumps. */
141
142 bool
143 vect_print_dump_info (enum vect_verbosity_levels vl)
144 {
145 if (vl > vect_verbosity_level)
146 return false;
147
148 if (!current_function_decl || !vect_dump)
149 return false;
150
151 if (vect_location == UNKNOWN_LOC)
152 fprintf (vect_dump, "\n%s:%d: note: ",
153 DECL_SOURCE_FILE (current_function_decl),
154 DECL_SOURCE_LINE (current_function_decl));
155 else
156 fprintf (vect_dump, "\n%d: ", LOC_LINE (vect_location));
157
158 return true;
159 }
160
161
162 /* Function vectorize_loops.
163
164 Entry point to loop vectorization phase. */
165
166 unsigned
167 vectorize_loops (void)
168 {
169 unsigned int i;
170 unsigned int num_vectorized_loops = 0;
171 unsigned int vect_loops_num;
172 loop_iterator li;
173 struct loop *loop;
174
175 vect_loops_num = number_of_loops ();
176
177 /* Bail out if there are no loops. */
178 if (vect_loops_num <= 1)
179 return 0;
180
181 /* Fix the verbosity level if not defined explicitly by the user. */
182 vect_set_dump_settings (false);
183
184 init_stmt_vec_info_vec ();
185
186 /* ----------- Analyze loops. ----------- */
187
188 /* If some loop was duplicated, it gets bigger number
189 than all previously defined loops. This fact allows us to run
190 only over initial loops skipping newly generated ones. */
191 FOR_EACH_LOOP (li, loop, 0)
192 if (optimize_loop_nest_for_speed_p (loop))
193 {
194 loop_vec_info loop_vinfo;
195
196 vect_location = find_loop_location (loop);
197 if (vect_location != UNKNOWN_LOC
198 && vect_verbosity_level > REPORT_NONE)
199 fprintf (vect_dump, "\nAnalyzing loop at %s:%d\n",
200 LOC_FILE (vect_location), LOC_LINE (vect_location));
201
202 loop_vinfo = vect_analyze_loop (loop);
203 loop->aux = loop_vinfo;
204
205 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
206 continue;
207
208 if (vect_location != UNKNOWN_LOC
209 && vect_verbosity_level > REPORT_NONE)
210 fprintf (vect_dump, "\n\nVectorizing loop at %s:%d\n",
211 LOC_FILE (vect_location), LOC_LINE (vect_location));
212
213 vect_transform_loop (loop_vinfo);
214 num_vectorized_loops++;
215 }
216
217 vect_location = UNKNOWN_LOC;
218
219 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
220 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS)
221 || (num_vectorized_loops > 0
222 && vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS)))
223 fprintf (vect_dump, "vectorized %u loops in function.\n",
224 num_vectorized_loops);
225
226 /* ----------- Finalize. ----------- */
227
228 mark_sym_for_renaming (gimple_vop (cfun));
229
230 for (i = 1; i < vect_loops_num; i++)
231 {
232 loop_vec_info loop_vinfo;
233
234 loop = get_loop (i);
235 if (!loop)
236 continue;
237 loop_vinfo = (loop_vec_info) loop->aux;
238 destroy_loop_vec_info (loop_vinfo, true);
239 loop->aux = NULL;
240 }
241
242 free_stmt_vec_info_vec ();
243
244 return num_vectorized_loops > 0 ? TODO_cleanup_cfg : 0;
245 }
246
247
248 /* Entry point to basic block SLP phase. */
249
250 static unsigned int
251 execute_vect_slp (void)
252 {
253 basic_block bb;
254
255 /* Fix the verbosity level if not defined explicitly by the user. */
256 vect_set_dump_settings (true);
257
258 init_stmt_vec_info_vec ();
259
260 FOR_EACH_BB (bb)
261 {
262 vect_location = find_bb_location (bb);
263
264 if (vect_slp_analyze_bb (bb))
265 {
266 vect_slp_transform_bb (bb);
267
268 if (vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS))
269 fprintf (vect_dump, "basic block vectorized using SLP\n");
270 }
271 }
272
273 free_stmt_vec_info_vec ();
274 return 0;
275 }
276
277 static bool
278 gate_vect_slp (void)
279 {
280 /* Apply SLP either if the vectorizer is on and the user didn't specify
281 whether to run SLP or not, or if the SLP flag was set by the user. */
282 return ((flag_tree_vectorize != 0 && flag_tree_slp_vectorize != 0)
283 || flag_tree_slp_vectorize == 1);
284 }
285
286 struct gimple_opt_pass pass_slp_vectorize =
287 {
288 {
289 GIMPLE_PASS,
290 "slp", /* name */
291 gate_vect_slp, /* gate */
292 execute_vect_slp, /* execute */
293 NULL, /* sub */
294 NULL, /* next */
295 0, /* static_pass_number */
296 TV_TREE_SLP_VECTORIZATION, /* tv_id */
297 PROP_ssa | PROP_cfg, /* properties_required */
298 0, /* properties_provided */
299 0, /* properties_destroyed */
300 0, /* todo_flags_start */
301 TODO_ggc_collect
302 | TODO_verify_ssa
303 | TODO_update_ssa
304 | TODO_verify_stmts /* todo_flags_finish */
305 }
306 };
307
308
309 /* Increase alignment of global arrays to improve vectorization potential.
310 TODO:
311 - Consider also structs that have an array field.
312 - Use ipa analysis to prune arrays that can't be vectorized?
313 This should involve global alignment analysis and in the future also
314 array padding. */
315
316 static unsigned int
317 increase_alignment (void)
318 {
319 struct varpool_node *vnode;
320
321 /* Increase the alignment of all global arrays for vectorization. */
322 FOR_EACH_DEFINED_VARIABLE (vnode)
323 {
324 tree vectype, decl = vnode->symbol.decl;
325 tree t;
326 unsigned int alignment;
327
328 t = TREE_TYPE(decl);
329 if (TREE_CODE (t) != ARRAY_TYPE)
330 continue;
331 vectype = get_vectype_for_scalar_type (strip_array_types (t));
332 if (!vectype)
333 continue;
334 alignment = TYPE_ALIGN (vectype);
335 if (DECL_ALIGN (decl) >= alignment)
336 continue;
337
338 if (vect_can_force_dr_alignment_p (decl, alignment))
339 {
340 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
341 DECL_USER_ALIGN (decl) = 1;
342 if (dump_file)
343 {
344 fprintf (dump_file, "Increasing alignment of decl: ");
345 print_generic_expr (dump_file, decl, TDF_SLIM);
346 fprintf (dump_file, "\n");
347 }
348 }
349 }
350 return 0;
351 }
352
353
354 static bool
355 gate_increase_alignment (void)
356 {
357 return flag_section_anchors && flag_tree_vectorize;
358 }
359
360
361 struct simple_ipa_opt_pass pass_ipa_increase_alignment =
362 {
363 {
364 SIMPLE_IPA_PASS,
365 "increase_alignment", /* name */
366 gate_increase_alignment, /* gate */
367 increase_alignment, /* execute */
368 NULL, /* sub */
369 NULL, /* next */
370 0, /* static_pass_number */
371 TV_IPA_OPT, /* tv_id */
372 0, /* properties_required */
373 0, /* properties_provided */
374 0, /* properties_destroyed */
375 0, /* todo_flags_start */
376 0 /* todo_flags_finish */
377 }
378 };