OSDN Git Service

* cp-tree.h (struct tinst_level): Add chain_next GTY
[pf3gnuchains/gcc-fork.git] / gcc / tree-optimize.c
1 /* Top-level control of tree optimizations.
2    Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License 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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "output.h"
30 #include "flags.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "function.h"
35 #include "langhooks.h"
36 #include "diagnostic-core.h"
37 #include "toplev.h"
38 #include "flags.h"
39 #include "cgraph.h"
40 #include "tree-inline.h"
41 #include "tree-mudflap.h"
42 #include "tree-pass.h"
43 #include "ggc.h"
44 #include "cgraph.h"
45 #include "graph.h"
46 #include "cfgloop.h"
47 #include "except.h"
48 #include "plugin.h"
49 #include "regset.h"     /* FIXME: For reg_obstack.  */
50
51 /* Gate: execute, or not, all of the non-trivial optimizations.  */
52
53 static bool
54 gate_all_optimizations (void)
55 {
56   return (optimize >= 1
57           /* Don't bother doing anything if the program has errors.
58              We have to pass down the queue if we already went into SSA */
59           && (!seen_error () || gimple_in_ssa_p (cfun)));
60 }
61
62 struct gimple_opt_pass pass_all_optimizations =
63 {
64  {
65   GIMPLE_PASS,
66   "*all_optimizations",                 /* name */
67   gate_all_optimizations,               /* gate */
68   NULL,                                 /* execute */
69   NULL,                                 /* sub */
70   NULL,                                 /* next */
71   0,                                    /* static_pass_number */
72   TV_OPTIMIZE,                          /* tv_id */
73   0,                                    /* properties_required */
74   0,                                    /* properties_provided */
75   0,                                    /* properties_destroyed */
76   0,                                    /* todo_flags_start */
77   0                                     /* todo_flags_finish */
78  }
79 };
80
81 /* Gate: execute, or not, all of the non-trivial optimizations.  */
82
83 static bool
84 gate_all_early_local_passes (void)
85 {
86           /* Don't bother doing anything if the program has errors.  */
87   return (!seen_error () && !in_lto_p);
88 }
89
90 static unsigned int
91 execute_all_early_local_passes (void)
92 {
93   /* Once this pass (and its sub-passes) are complete, all functions
94      will be in SSA form.  Technically this state change is happening
95      a tad early, since the sub-passes have not yet run, but since
96      none of the sub-passes are IPA passes and do not create new
97      functions, this is ok.  We're setting this value for the benefit
98      of IPA passes that follow.  */
99   if (cgraph_state < CGRAPH_STATE_IPA_SSA)
100     cgraph_state = CGRAPH_STATE_IPA_SSA;
101   return 0;
102 }
103
104 struct simple_ipa_opt_pass pass_early_local_passes =
105 {
106  {
107   SIMPLE_IPA_PASS,
108   "early_local_cleanups",               /* name */
109   gate_all_early_local_passes,          /* gate */
110   execute_all_early_local_passes,       /* execute */
111   NULL,                                 /* sub */
112   NULL,                                 /* next */
113   0,                                    /* static_pass_number */
114   TV_EARLY_LOCAL,                       /* tv_id */
115   0,                                    /* properties_required */
116   0,                                    /* properties_provided */
117   0,                                    /* properties_destroyed */
118   0,                                    /* todo_flags_start */
119   TODO_remove_functions                 /* todo_flags_finish */
120  }
121 };
122
123 /* Gate: execute, or not, all of the non-trivial optimizations.  */
124
125 static bool
126 gate_all_early_optimizations (void)
127 {
128   return (optimize >= 1
129           /* Don't bother doing anything if the program has errors.  */
130           && !seen_error ());
131 }
132
133 struct gimple_opt_pass pass_all_early_optimizations =
134 {
135  {
136   GIMPLE_PASS,
137   "early_optimizations",                /* name */
138   gate_all_early_optimizations,         /* gate */
139   NULL,                                 /* execute */
140   NULL,                                 /* sub */
141   NULL,                                 /* next */
142   0,                                    /* static_pass_number */
143   TV_NONE,                              /* tv_id */
144   0,                                    /* properties_required */
145   0,                                    /* properties_provided */
146   0,                                    /* properties_destroyed */
147   0,                                    /* todo_flags_start */
148   0                                     /* todo_flags_finish */
149  }
150 };
151
152
153 /* Pass: cleanup the CFG just before expanding trees to RTL.
154    This is just a round of label cleanups and case node grouping
155    because after the tree optimizers have run such cleanups may
156    be necessary.  */
157
158 static unsigned int
159 execute_cleanup_cfg_post_optimizing (void)
160 {
161   cleanup_tree_cfg ();
162   cleanup_dead_labels ();
163   group_case_labels ();
164   if ((flag_compare_debug_opt || flag_compare_debug)
165       && flag_dump_final_insns)
166     {
167       FILE *final_output = fopen (flag_dump_final_insns, "a");
168
169       if (!final_output)
170         {
171           error ("could not open final insn dump file %qs: %m",
172                  flag_dump_final_insns);
173           flag_dump_final_insns = NULL;
174         }
175       else
176         {
177           int save_unnumbered = flag_dump_unnumbered;
178           int save_noaddr = flag_dump_noaddr;
179
180           flag_dump_noaddr = flag_dump_unnumbered = 1;
181           fprintf (final_output, "\n");
182           dump_enumerated_decls (final_output, dump_flags | TDF_NOUID);
183           flag_dump_noaddr = save_noaddr;
184           flag_dump_unnumbered = save_unnumbered;
185           if (fclose (final_output))
186             {
187               error ("could not close final insn dump file %qs: %m",
188                      flag_dump_final_insns);
189               flag_dump_final_insns = NULL;
190             }
191         }
192     }
193   return 0;
194 }
195
196 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing =
197 {
198  {
199   GIMPLE_PASS,
200   "optimized",                          /* name */
201   NULL,                                 /* gate */
202   execute_cleanup_cfg_post_optimizing,  /* execute */
203   NULL,                                 /* sub */
204   NULL,                                 /* next */
205   0,                                    /* static_pass_number */
206   TV_TREE_CLEANUP_CFG,                  /* tv_id */
207   PROP_cfg,                             /* properties_required */
208   0,                                    /* properties_provided */
209   0,                                    /* properties_destroyed */
210   0,                                    /* todo_flags_start */
211   TODO_dump_func                        /* todo_flags_finish */
212     | TODO_remove_unused_locals
213  }
214 };
215
216 /* Pass: do the actions required to finish with tree-ssa optimization
217    passes.  */
218
219 unsigned int
220 execute_free_datastructures (void)
221 {
222   free_dominance_info (CDI_DOMINATORS);
223   free_dominance_info (CDI_POST_DOMINATORS);
224
225   /* And get rid of annotations we no longer need.  */
226   delete_tree_cfg_annotations ();
227
228   return 0;
229 }
230
231 /* IPA passes, compilation of earlier functions or inlining
232    might have changed some properties, such as marked functions nothrow,
233    pure, const or noreturn.
234    Remove redundant edges and basic blocks, and create new ones if necessary.
235
236    This pass can't be executed as stand alone pass from pass manager, because
237    in between inlining and this fixup the verify_flow_info would fail.  */
238
239 unsigned int
240 execute_fixup_cfg (void)
241 {
242   basic_block bb;
243   gimple_stmt_iterator gsi;
244   int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0;
245   gcov_type count_scale;
246   edge e;
247   edge_iterator ei;
248
249   if (ENTRY_BLOCK_PTR->count)
250     count_scale = ((cgraph_get_node (current_function_decl)->count
251                     * REG_BR_PROB_BASE + ENTRY_BLOCK_PTR->count / 2)
252                    / ENTRY_BLOCK_PTR->count);
253   else
254     count_scale = REG_BR_PROB_BASE;
255
256   ENTRY_BLOCK_PTR->count = cgraph_get_node (current_function_decl)->count;
257   EXIT_BLOCK_PTR->count = (EXIT_BLOCK_PTR->count * count_scale
258                            + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
259
260   FOR_EACH_BB (bb)
261     {
262       bb->count = (bb->count * count_scale
263                    + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
264       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
265         {
266           gimple stmt = gsi_stmt (gsi);
267           tree decl = is_gimple_call (stmt)
268                       ? gimple_call_fndecl (stmt)
269                       : NULL;
270           if (decl)
271             {
272               int flags = gimple_call_flags (stmt);
273               if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
274                 {
275                   if (gimple_purge_dead_abnormal_call_edges (bb))
276                     todo |= TODO_cleanup_cfg;
277
278                   if (gimple_in_ssa_p (cfun))
279                     {
280                       todo |= TODO_update_ssa | TODO_cleanup_cfg;
281                       update_stmt (stmt);
282                     }
283                 }
284
285               if (flags & ECF_NORETURN
286                   && fixup_noreturn_call (stmt))
287                 todo |= TODO_cleanup_cfg;
288              }
289
290           if (maybe_clean_eh_stmt (stmt)
291               && gimple_purge_dead_eh_edges (bb))
292             todo |= TODO_cleanup_cfg;
293         }
294
295       FOR_EACH_EDGE (e, ei, bb->succs)
296         e->count = (e->count * count_scale
297                     + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
298     }
299   if (count_scale != REG_BR_PROB_BASE)
300     compute_function_frequency ();
301
302   /* We just processed all calls.  */
303   if (cfun->gimple_df)
304     {
305       VEC_free (gimple, gc, MODIFIED_NORETURN_CALLS (cfun));
306       MODIFIED_NORETURN_CALLS (cfun) = NULL;
307     }
308
309   /* Dump a textual representation of the flowgraph.  */
310   if (dump_file)
311     gimple_dump_cfg (dump_file, dump_flags);
312
313   return todo;
314 }
315
316 struct gimple_opt_pass pass_fixup_cfg =
317 {
318  {
319   GIMPLE_PASS,
320   "*free_cfg_annotations",              /* name */
321   NULL,                                 /* gate */
322   execute_fixup_cfg,                    /* execute */
323   NULL,                                 /* sub */
324   NULL,                                 /* next */
325   0,                                    /* static_pass_number */
326   TV_NONE,                              /* tv_id */
327   PROP_cfg,                             /* properties_required */
328   0,                                    /* properties_provided */
329   0,                                    /* properties_destroyed */
330   0,                                    /* todo_flags_start */
331   0                                     /* todo_flags_finish */
332  }
333 };
334
335 /* Do the actions required to initialize internal data structures used
336    in tree-ssa optimization passes.  */
337
338 static unsigned int
339 execute_init_datastructures (void)
340 {
341   /* Allocate hash tables, arrays and other structures.  */
342   init_tree_ssa (cfun);
343   return 0;
344 }
345
346 struct gimple_opt_pass pass_init_datastructures =
347 {
348  {
349   GIMPLE_PASS,
350   "*init_datastructures",               /* name */
351   NULL,                                 /* gate */
352   execute_init_datastructures,          /* execute */
353   NULL,                                 /* sub */
354   NULL,                                 /* next */
355   0,                                    /* static_pass_number */
356   TV_NONE,                              /* tv_id */
357   PROP_cfg,                             /* properties_required */
358   0,                                    /* properties_provided */
359   0,                                    /* properties_destroyed */
360   0,                                    /* todo_flags_start */
361   0                                     /* todo_flags_finish */
362  }
363 };
364
365 void
366 tree_lowering_passes (tree fn)
367 {
368   tree saved_current_function_decl = current_function_decl;
369
370   current_function_decl = fn;
371   push_cfun (DECL_STRUCT_FUNCTION (fn));
372   gimple_register_cfg_hooks ();
373   bitmap_obstack_initialize (NULL);
374   execute_pass_list (all_lowering_passes);
375   if (optimize && cgraph_global_info_ready)
376     execute_pass_list (pass_early_local_passes.pass.sub);
377   free_dominance_info (CDI_POST_DOMINATORS);
378   free_dominance_info (CDI_DOMINATORS);
379   compact_blocks ();
380   current_function_decl = saved_current_function_decl;
381   bitmap_obstack_release (NULL);
382   pop_cfun ();
383 }
384 \f
385 /* For functions-as-trees languages, this performs all optimization and
386    compilation for FNDECL.  */
387
388 void
389 tree_rest_of_compilation (tree fndecl)
390 {
391   location_t saved_loc;
392
393   timevar_push (TV_REST_OF_COMPILATION);
394
395   gcc_assert (cgraph_global_info_ready);
396
397   /* Initialize the default bitmap obstack.  */
398   bitmap_obstack_initialize (NULL);
399
400   /* Initialize the RTL code for the function.  */
401   current_function_decl = fndecl;
402   saved_loc = input_location;
403   input_location = DECL_SOURCE_LOCATION (fndecl);
404   init_function_start (fndecl);
405
406   gimple_register_cfg_hooks ();
407
408   bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
409
410   execute_all_ipa_transforms ();
411
412   /* Perform all tree transforms and optimizations.  */
413
414   /* Signal the start of passes.  */
415   invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
416
417   execute_pass_list (all_passes);
418
419   /* Signal the end of passes.  */
420   invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
421
422   bitmap_obstack_release (&reg_obstack);
423
424   /* Release the default bitmap obstack.  */
425   bitmap_obstack_release (NULL);
426
427   set_cfun (NULL);
428
429   /* If requested, warn about function definitions where the function will
430      return a value (usually of some struct or union type) which itself will
431      take up a lot of stack space.  */
432   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
433     {
434       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
435
436       if (ret_type && TYPE_SIZE_UNIT (ret_type)
437           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
438           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
439                                    larger_than_size))
440         {
441           unsigned int size_as_int
442             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
443
444           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
445             warning (OPT_Wlarger_than_, "size of return value of %q+D is %u bytes",
446                      fndecl, size_as_int);
447           else
448             warning (OPT_Wlarger_than_, "size of return value of %q+D is larger than %wd bytes",
449                      fndecl, larger_than_size);
450         }
451     }
452
453   gimple_set_body (fndecl, NULL);
454   if (DECL_STRUCT_FUNCTION (fndecl) == 0
455       && !cgraph_get_node (fndecl)->origin)
456     {
457       /* Stop pointing to the local nodes about to be freed.
458          But DECL_INITIAL must remain nonzero so we know this
459          was an actual function definition.
460          For a nested function, this is done in c_pop_function_context.
461          If rest_of_compilation set this to 0, leave it 0.  */
462       if (DECL_INITIAL (fndecl) != 0)
463         DECL_INITIAL (fndecl) = error_mark_node;
464     }
465
466   input_location = saved_loc;
467
468   ggc_collect ();
469   timevar_pop (TV_REST_OF_COMPILATION);
470 }