* config/arm/neon-gen.ml: Include vxWorks.h rather than stdint.h
[official-gcc.git] / gcc / tree-vectorizer.c
blob4ab8547dee202745eacf0823db3399b133efc6e8
1 /* Vectorizer
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software
3 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 "tm.h"
62 #include "ggc.h"
63 #include "tree.h"
64 #include "diagnostic.h"
65 #include "tree-flow.h"
66 #include "tree-dump.h"
67 #include "cfgloop.h"
68 #include "cfglayout.h"
69 #include "tree-vectorizer.h"
70 #include "tree-pass.h"
71 #include "timevar.h"
73 /* vect_dump will be set to stderr or dump_file if exist. */
74 FILE *vect_dump;
76 /* vect_verbosity_level set to an invalid value
77 to mark that it's uninitialized. */
78 static enum verbosity_levels vect_verbosity_level = MAX_VERBOSITY_LEVEL;
79 static enum verbosity_levels user_vect_verbosity_level = MAX_VERBOSITY_LEVEL;
81 /* Loop or bb location. */
82 LOC vect_location;
84 /* Bitmap of virtual variables to be renamed. */
85 bitmap vect_memsyms_to_rename;
87 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
88 VEC(vec_void_p,heap) *stmt_vec_info_vec;
92 /* Function vect_set_verbosity_level.
94 Called from opts.c upon detection of the
95 -ftree-vectorizer-verbose=N option. */
97 void
98 vect_set_verbosity_level (const char *val)
100 unsigned int vl;
102 vl = atoi (val);
103 if (vl < MAX_VERBOSITY_LEVEL)
104 user_vect_verbosity_level = (enum verbosity_levels) vl;
105 else
106 user_vect_verbosity_level
107 = (enum verbosity_levels) (MAX_VERBOSITY_LEVEL - 1);
111 /* Function vect_set_dump_settings.
113 Fix the verbosity level of the vectorizer if the
114 requested level was not set explicitly using the flag
115 -ftree-vectorizer-verbose=N.
116 Decide where to print the debugging information (dump_file/stderr).
117 If the user defined the verbosity level, but there is no dump file,
118 print to stderr, otherwise print to the dump file. */
120 static void
121 vect_set_dump_settings (bool slp)
123 vect_dump = dump_file;
125 /* Check if the verbosity level was defined by the user: */
126 if (user_vect_verbosity_level != MAX_VERBOSITY_LEVEL)
128 vect_verbosity_level = user_vect_verbosity_level;
129 /* Ignore user defined verbosity if dump flags require higher level of
130 verbosity. */
131 if (dump_file)
133 if (((dump_flags & TDF_DETAILS)
134 && vect_verbosity_level >= REPORT_DETAILS)
135 || ((dump_flags & TDF_STATS)
136 && vect_verbosity_level >= REPORT_UNVECTORIZED_LOCATIONS))
137 return;
139 else
141 /* If there is no dump file, print to stderr in case of loop
142 vectorization. */
143 if (!slp)
144 vect_dump = stderr;
146 return;
150 /* User didn't specify verbosity level: */
151 if (dump_file && (dump_flags & TDF_DETAILS))
152 vect_verbosity_level = REPORT_DETAILS;
153 else if (dump_file && (dump_flags & TDF_STATS))
154 vect_verbosity_level = REPORT_UNVECTORIZED_LOCATIONS;
155 else
156 vect_verbosity_level = REPORT_NONE;
158 gcc_assert (dump_file || vect_verbosity_level == REPORT_NONE);
162 /* Function debug_loop_details.
164 For vectorization debug dumps. */
166 bool
167 vect_print_dump_info (enum verbosity_levels vl)
169 if (vl > vect_verbosity_level)
170 return false;
172 if (!current_function_decl || !vect_dump)
173 return false;
175 if (vect_location == UNKNOWN_LOC)
176 fprintf (vect_dump, "\n%s:%d: note: ",
177 DECL_SOURCE_FILE (current_function_decl),
178 DECL_SOURCE_LINE (current_function_decl));
179 else
180 fprintf (vect_dump, "\n%s:%d: note: ",
181 LOC_FILE (vect_location), LOC_LINE (vect_location));
183 return true;
187 /* Function vectorize_loops.
189 Entry point to loop vectorization phase. */
191 unsigned
192 vectorize_loops (void)
194 unsigned int i;
195 unsigned int num_vectorized_loops = 0;
196 unsigned int vect_loops_num;
197 loop_iterator li;
198 struct loop *loop;
200 vect_loops_num = number_of_loops ();
202 /* Bail out if there are no loops. */
203 if (vect_loops_num <= 1)
204 return 0;
206 /* Fix the verbosity level if not defined explicitly by the user. */
207 vect_set_dump_settings (false);
209 /* Allocate the bitmap that records which virtual variables
210 need to be renamed. */
211 vect_memsyms_to_rename = BITMAP_ALLOC (NULL);
213 init_stmt_vec_info_vec ();
215 /* ----------- Analyze loops. ----------- */
217 /* If some loop was duplicated, it gets bigger number
218 than all previously defined loops. This fact allows us to run
219 only over initial loops skipping newly generated ones. */
220 FOR_EACH_LOOP (li, loop, 0)
221 if (optimize_loop_nest_for_speed_p (loop))
223 loop_vec_info loop_vinfo;
225 vect_location = find_loop_location (loop);
226 loop_vinfo = vect_analyze_loop (loop);
227 loop->aux = loop_vinfo;
229 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
230 continue;
232 vect_transform_loop (loop_vinfo);
233 num_vectorized_loops++;
236 vect_location = UNKNOWN_LOC;
238 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
239 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS)
240 || (vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS)
241 && num_vectorized_loops > 0))
242 fprintf (vect_dump, "vectorized %u loops in function.\n",
243 num_vectorized_loops);
245 /* ----------- Finalize. ----------- */
247 BITMAP_FREE (vect_memsyms_to_rename);
249 for (i = 1; i < vect_loops_num; i++)
251 loop_vec_info loop_vinfo;
253 loop = get_loop (i);
254 if (!loop)
255 continue;
256 loop_vinfo = (loop_vec_info) loop->aux;
257 destroy_loop_vec_info (loop_vinfo, true);
258 loop->aux = NULL;
261 free_stmt_vec_info_vec ();
263 return num_vectorized_loops > 0 ? TODO_cleanup_cfg : 0;
267 /* Entry point to basic block SLP phase. */
269 static unsigned int
270 execute_vect_slp (void)
272 basic_block bb;
274 /* Fix the verbosity level if not defined explicitly by the user. */
275 vect_set_dump_settings (true);
277 init_stmt_vec_info_vec ();
279 FOR_EACH_BB (bb)
281 vect_location = find_bb_location (bb);
283 if (vect_slp_analyze_bb (bb))
285 vect_slp_transform_bb (bb);
287 if (vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS))
288 fprintf (vect_dump, "basic block vectorized using SLP\n");
292 free_stmt_vec_info_vec ();
293 return 0;
296 static bool
297 gate_vect_slp (void)
299 /* Apply SLP either if the vectorizer is on and the user didn't specify
300 whether to run SLP or not, or if the SLP flag was set by the user. */
301 return ((flag_tree_vectorize != 0 && flag_tree_slp_vectorize != 0)
302 || flag_tree_slp_vectorize == 1);
305 struct gimple_opt_pass pass_slp_vectorize =
308 GIMPLE_PASS,
309 "slp", /* name */
310 gate_vect_slp, /* gate */
311 execute_vect_slp, /* execute */
312 NULL, /* sub */
313 NULL, /* next */
314 0, /* static_pass_number */
315 TV_TREE_SLP_VECTORIZATION, /* tv_id */
316 PROP_ssa | PROP_cfg, /* properties_required */
317 0, /* properties_provided */
318 0, /* properties_destroyed */
319 0, /* todo_flags_start */
320 TODO_ggc_collect
321 | TODO_verify_ssa
322 | TODO_dump_func
323 | TODO_update_ssa
324 | TODO_verify_stmts /* todo_flags_finish */
329 /* Increase alignment of global arrays to improve vectorization potential.
330 TODO:
331 - Consider also structs that have an array field.
332 - Use ipa analysis to prune arrays that can't be vectorized?
333 This should involve global alignment analysis and in the future also
334 array padding. */
336 static unsigned int
337 increase_alignment (void)
339 struct varpool_node *vnode;
341 /* Increase the alignment of all global arrays for vectorization. */
342 for (vnode = varpool_nodes_queue;
343 vnode;
344 vnode = vnode->next_needed)
346 tree vectype, decl = vnode->decl;
347 unsigned int alignment;
349 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
350 continue;
351 vectype = get_vectype_for_scalar_type (TREE_TYPE (TREE_TYPE (decl)));
352 if (!vectype)
353 continue;
354 alignment = TYPE_ALIGN (vectype);
355 if (DECL_ALIGN (decl) >= alignment)
356 continue;
358 if (vect_can_force_dr_alignment_p (decl, alignment))
360 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
361 DECL_USER_ALIGN (decl) = 1;
362 if (dump_file)
364 fprintf (dump_file, "Increasing alignment of decl: ");
365 print_generic_expr (dump_file, decl, TDF_SLIM);
369 return 0;
373 static bool
374 gate_increase_alignment (void)
376 return flag_section_anchors && flag_tree_vectorize;
380 struct simple_ipa_opt_pass pass_ipa_increase_alignment =
383 SIMPLE_IPA_PASS,
384 "increase_alignment", /* name */
385 gate_increase_alignment, /* gate */
386 increase_alignment, /* execute */
387 NULL, /* sub */
388 NULL, /* next */
389 0, /* static_pass_number */
390 TV_NONE, /* tv_id */
391 0, /* properties_required */
392 0, /* properties_provided */
393 0, /* properties_destroyed */
394 0, /* todo_flags_start */
395 0 /* todo_flags_finish */