* passes.c (init_optimization_passes): Remove two copies of ehcleanup
[official-gcc/constexpr.git] / gcc / tree-vectorizer.c
blob0636c6adbc67a9b3fbac3e536c01687b188385f0
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"
72 /* vect_dump will be set to stderr or dump_file if exist. */
73 FILE *vect_dump;
75 /* vect_verbosity_level set to an invalid value
76 to mark that it's uninitialized. */
77 enum verbosity_levels vect_verbosity_level = MAX_VERBOSITY_LEVEL;
79 /* Loop location. */
80 LOC vect_loop_location;
82 /* Bitmap of virtual variables to be renamed. */
83 bitmap vect_memsyms_to_rename;
85 /* Vector mapping GIMPLE stmt to stmt_vec_info. */
86 VEC(vec_void_p,heap) *stmt_vec_info_vec;
90 /* Function vect_set_verbosity_level.
92 Called from toplev.c upon detection of the
93 -ftree-vectorizer-verbose=N option. */
95 void
96 vect_set_verbosity_level (const char *val)
98 unsigned int vl;
100 vl = atoi (val);
101 if (vl < MAX_VERBOSITY_LEVEL)
102 vect_verbosity_level = vl;
103 else
104 vect_verbosity_level = MAX_VERBOSITY_LEVEL - 1;
108 /* Function vect_set_dump_settings.
110 Fix the verbosity level of the vectorizer if the
111 requested level was not set explicitly using the flag
112 -ftree-vectorizer-verbose=N.
113 Decide where to print the debugging information (dump_file/stderr).
114 If the user defined the verbosity level, but there is no dump file,
115 print to stderr, otherwise print to the dump file. */
117 static void
118 vect_set_dump_settings (void)
120 vect_dump = dump_file;
122 /* Check if the verbosity level was defined by the user: */
123 if (vect_verbosity_level != MAX_VERBOSITY_LEVEL)
125 /* If there is no dump file, print to stderr. */
126 if (!dump_file)
127 vect_dump = stderr;
128 return;
131 /* User didn't specify verbosity level: */
132 if (dump_file && (dump_flags & TDF_DETAILS))
133 vect_verbosity_level = REPORT_DETAILS;
134 else if (dump_file && (dump_flags & TDF_STATS))
135 vect_verbosity_level = REPORT_UNVECTORIZED_LOOPS;
136 else
137 vect_verbosity_level = REPORT_NONE;
139 gcc_assert (dump_file || vect_verbosity_level == REPORT_NONE);
143 /* Function debug_loop_details.
145 For vectorization debug dumps. */
147 bool
148 vect_print_dump_info (enum verbosity_levels vl)
150 if (vl > vect_verbosity_level)
151 return false;
153 if (!current_function_decl || !vect_dump)
154 return false;
156 if (vect_loop_location == UNKNOWN_LOC)
157 fprintf (vect_dump, "\n%s:%d: note: ",
158 DECL_SOURCE_FILE (current_function_decl),
159 DECL_SOURCE_LINE (current_function_decl));
160 else
161 fprintf (vect_dump, "\n%s:%d: note: ",
162 LOC_FILE (vect_loop_location), LOC_LINE (vect_loop_location));
164 return true;
168 /* Function vectorize_loops.
170 Entry Point to loop vectorization phase. */
172 unsigned
173 vectorize_loops (void)
175 unsigned int i;
176 unsigned int num_vectorized_loops = 0;
177 unsigned int vect_loops_num;
178 loop_iterator li;
179 struct loop *loop;
181 vect_loops_num = number_of_loops ();
183 /* Bail out if there are no loops. */
184 if (vect_loops_num <= 1)
185 return 0;
187 /* Fix the verbosity level if not defined explicitly by the user. */
188 vect_set_dump_settings ();
190 /* Allocate the bitmap that records which virtual variables that
191 need to be renamed. */
192 vect_memsyms_to_rename = BITMAP_ALLOC (NULL);
194 init_stmt_vec_info_vec ();
196 /* ----------- Analyze loops. ----------- */
198 /* If some loop was duplicated, it gets bigger number
199 than all previously defined loops. This fact allows us to run
200 only over initial loops skipping newly generated ones. */
201 FOR_EACH_LOOP (li, loop, 0)
202 if (optimize_loop_nest_for_speed_p (loop))
204 loop_vec_info loop_vinfo;
206 vect_loop_location = find_loop_location (loop);
207 loop_vinfo = vect_analyze_loop (loop);
208 loop->aux = loop_vinfo;
210 if (!loop_vinfo || !LOOP_VINFO_VECTORIZABLE_P (loop_vinfo))
211 continue;
213 vect_transform_loop (loop_vinfo);
214 num_vectorized_loops++;
216 vect_loop_location = UNKNOWN_LOC;
218 statistics_counter_event (cfun, "Vectorized loops", num_vectorized_loops);
219 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS)
220 || (vect_print_dump_info (REPORT_VECTORIZED_LOOPS)
221 && num_vectorized_loops > 0))
222 fprintf (vect_dump, "vectorized %u loops in function.\n",
223 num_vectorized_loops);
225 /* ----------- Finalize. ----------- */
227 BITMAP_FREE (vect_memsyms_to_rename);
229 for (i = 1; i < vect_loops_num; i++)
231 loop_vec_info loop_vinfo;
233 loop = get_loop (i);
234 if (!loop)
235 continue;
236 loop_vinfo = (loop_vec_info) loop->aux;
237 destroy_loop_vec_info (loop_vinfo, true);
238 loop->aux = NULL;
241 free_stmt_vec_info_vec ();
243 return num_vectorized_loops > 0 ? TODO_cleanup_cfg : 0;
247 /* Increase alignment of global arrays to improve vectorization potential.
248 TODO:
249 - Consider also structs that have an array field.
250 - Use ipa analysis to prune arrays that can't be vectorized?
251 This should involve global alignment analysis and in the future also
252 array padding. */
254 static unsigned int
255 increase_alignment (void)
257 struct varpool_node *vnode;
259 /* Increase the alignment of all global arrays for vectorization. */
260 for (vnode = varpool_nodes_queue;
261 vnode;
262 vnode = vnode->next_needed)
264 tree vectype, decl = vnode->decl;
265 unsigned int alignment;
267 if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
268 continue;
269 vectype = get_vectype_for_scalar_type (TREE_TYPE (TREE_TYPE (decl)));
270 if (!vectype)
271 continue;
272 alignment = TYPE_ALIGN (vectype);
273 if (DECL_ALIGN (decl) >= alignment)
274 continue;
276 if (vect_can_force_dr_alignment_p (decl, alignment))
278 DECL_ALIGN (decl) = TYPE_ALIGN (vectype);
279 DECL_USER_ALIGN (decl) = 1;
280 if (dump_file)
282 fprintf (dump_file, "Increasing alignment of decl: ");
283 print_generic_expr (dump_file, decl, TDF_SLIM);
287 return 0;
291 static bool
292 gate_increase_alignment (void)
294 return flag_section_anchors && flag_tree_vectorize;
298 struct simple_ipa_opt_pass pass_ipa_increase_alignment =
301 SIMPLE_IPA_PASS,
302 "increase_alignment", /* name */
303 gate_increase_alignment, /* gate */
304 increase_alignment, /* execute */
305 NULL, /* sub */
306 NULL, /* next */
307 0, /* static_pass_number */
308 0, /* tv_id */
309 0, /* properties_required */
310 0, /* properties_provided */
311 0, /* properties_destroyed */
312 0, /* todo_flags_start */
313 0 /* todo_flags_finish */