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