OSDN Git Service

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