Move get_current_pass_name to ici.c .
[official-gcc.git] / gcc / tree-optimize.c
blob8742f3b0cc08e8421f7f88ccc486b4f6b4bca101
1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
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.
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.
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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
35 #include "flags.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "timevar.h"
39 #include "function.h"
40 #include "langhooks.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
47 #include "ggc.h"
48 #include "cgraph.h"
49 #include "graph.h"
50 #include "cfgloop.h"
51 #include "except.h"
52 #include "plugin.h"
54 /* Gate: execute, or not, all of the non-trivial optimizations. */
56 static bool
57 gate_all_optimizations (void)
59 return (optimize >= 1
60 /* Don't bother doing anything if the program has errors.
61 We have to pass down the queue if we already went into SSA */
62 && (!(errorcount || sorrycount) || gimple_in_ssa_p (cfun)));
65 struct gimple_opt_pass pass_all_optimizations =
68 GIMPLE_PASS,
69 "*all_optimizations", /* name */
70 gate_all_optimizations, /* gate */
71 NULL, /* execute */
72 NULL, /* sub */
73 NULL, /* next */
74 0, /* static_pass_number */
75 TV_NONE, /* tv_id */
76 0, /* properties_required */
77 0, /* properties_provided */
78 0, /* properties_destroyed */
79 0, /* todo_flags_start */
80 0 /* todo_flags_finish */
84 /* Gate: execute, or not, all of the non-trivial optimizations. */
86 static bool
87 gate_all_early_local_passes (void)
89 /* Don't bother doing anything if the program has errors. */
90 return (!errorcount && !sorrycount && !in_lto_p);
93 struct simple_ipa_opt_pass pass_early_local_passes =
96 SIMPLE_IPA_PASS,
97 "early_local_cleanups", /* name */
98 gate_all_early_local_passes, /* gate */
99 NULL, /* execute */
100 NULL, /* sub */
101 NULL, /* next */
102 0, /* static_pass_number */
103 TV_NONE, /* tv_id */
104 0, /* properties_required */
105 0, /* properties_provided */
106 0, /* properties_destroyed */
107 0, /* todo_flags_start */
108 TODO_remove_functions /* todo_flags_finish */
112 static unsigned int
113 execute_early_local_optimizations (void)
115 /* First time we start with early optimization we need to advance
116 cgraph state so newly inserted functions are also early optimized.
117 However we execute early local optimizations for lately inserted
118 functions, in that case don't reset cgraph state back to IPA_SSA. */
119 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
120 cgraph_state = CGRAPH_STATE_IPA_SSA;
121 return 0;
124 /* Gate: execute, or not, all of the non-trivial optimizations. */
126 static bool
127 gate_all_early_optimizations (void)
129 return (optimize >= 1
130 /* Don't bother doing anything if the program has errors. */
131 && !(errorcount || sorrycount));
134 struct gimple_opt_pass pass_all_early_optimizations =
137 GIMPLE_PASS,
138 "early_optimizations", /* name */
139 gate_all_early_optimizations, /* gate */
140 execute_early_local_optimizations, /* execute */
141 NULL, /* sub */
142 NULL, /* next */
143 0, /* static_pass_number */
144 TV_NONE, /* tv_id */
145 0, /* properties_required */
146 0, /* properties_provided */
147 0, /* properties_destroyed */
148 0, /* todo_flags_start */
149 0 /* todo_flags_finish */
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. */
158 static unsigned int
159 execute_cleanup_cfg_pre_ipa (void)
161 cleanup_tree_cfg ();
162 return 0;
165 struct gimple_opt_pass pass_cleanup_cfg =
168 GIMPLE_PASS,
169 "cleanup_cfg", /* name */
170 NULL, /* gate */
171 execute_cleanup_cfg_pre_ipa, /* execute */
172 NULL, /* sub */
173 NULL, /* next */
174 0, /* static_pass_number */
175 TV_NONE, /* tv_id */
176 PROP_cfg, /* properties_required */
177 0, /* properties_provided */
178 0, /* properties_destroyed */
179 0, /* todo_flags_start */
180 TODO_dump_func /* todo_flags_finish */
185 /* Pass: cleanup the CFG just before expanding trees to RTL.
186 This is just a round of label cleanups and case node grouping
187 because after the tree optimizers have run such cleanups may
188 be necessary. */
190 static unsigned int
191 execute_cleanup_cfg_post_optimizing (void)
193 fold_cond_expr_cond ();
194 cleanup_tree_cfg ();
195 cleanup_dead_labels ();
196 group_case_labels ();
197 return 0;
200 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing =
203 GIMPLE_PASS,
204 "optimized", /* name */
205 NULL, /* gate */
206 execute_cleanup_cfg_post_optimizing, /* execute */
207 NULL, /* sub */
208 NULL, /* next */
209 0, /* static_pass_number */
210 TV_NONE, /* tv_id */
211 PROP_cfg, /* properties_required */
212 0, /* properties_provided */
213 0, /* properties_destroyed */
214 0, /* todo_flags_start */
215 TODO_dump_func /* todo_flags_finish */
216 | TODO_remove_unused_locals
220 /* Pass: do the actions required to finish with tree-ssa optimization
221 passes. */
223 unsigned int
224 execute_free_datastructures (void)
226 free_dominance_info (CDI_DOMINATORS);
227 free_dominance_info (CDI_POST_DOMINATORS);
229 /* And get rid of annotations we no longer need. */
230 delete_tree_cfg_annotations ();
232 return 0;
235 /* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining
236 might have changed some properties, such as marked functions nothrow.
237 Remove redundant edges and basic blocks, and create new ones if necessary.
239 This pass can't be executed as stand alone pass from pass manager, because
240 in between inlining and this fixup the verify_flow_info would fail. */
242 unsigned int
243 execute_fixup_cfg (void)
245 basic_block bb;
246 gimple_stmt_iterator gsi;
247 int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0;
249 if (cfun->eh)
250 FOR_EACH_BB (bb)
252 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
254 gimple stmt = gsi_stmt (gsi);
255 tree decl = is_gimple_call (stmt)
256 ? gimple_call_fndecl (stmt)
257 : NULL;
259 if (decl
260 && gimple_call_flags (stmt) & (ECF_CONST
261 | ECF_PURE
262 | ECF_LOOPING_CONST_OR_PURE))
264 if (gimple_in_ssa_p (cfun))
266 todo |= TODO_update_ssa | TODO_cleanup_cfg;
267 mark_symbols_for_renaming (stmt);
268 update_stmt (stmt);
272 maybe_clean_eh_stmt (stmt);
275 if (gimple_purge_dead_eh_edges (bb))
276 todo |= TODO_cleanup_cfg;
279 /* Dump a textual representation of the flowgraph. */
280 if (dump_file)
281 gimple_dump_cfg (dump_file, dump_flags);
283 return todo;
286 struct gimple_opt_pass pass_fixup_cfg =
289 GIMPLE_PASS,
290 "*free_cfg_annotations", /* name */
291 NULL, /* gate */
292 execute_fixup_cfg, /* execute */
293 NULL, /* sub */
294 NULL, /* next */
295 0, /* static_pass_number */
296 TV_NONE, /* tv_id */
297 PROP_cfg, /* properties_required */
298 0, /* properties_provided */
299 0, /* properties_destroyed */
300 0, /* todo_flags_start */
301 0 /* todo_flags_finish */
305 /* Do the actions required to initialize internal data structures used
306 in tree-ssa optimization passes. */
308 static unsigned int
309 execute_init_datastructures (void)
311 /* Allocate hash tables, arrays and other structures. */
312 init_tree_ssa (cfun);
313 return 0;
316 struct gimple_opt_pass pass_init_datastructures =
319 GIMPLE_PASS,
320 "*init_datastructures", /* name */
321 NULL, /* gate */
322 execute_init_datastructures, /* 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 */
335 void
336 tree_lowering_passes (tree fn)
338 tree saved_current_function_decl = current_function_decl;
340 current_function_decl = fn;
341 push_cfun (DECL_STRUCT_FUNCTION (fn));
342 gimple_register_cfg_hooks ();
343 bitmap_obstack_initialize (NULL);
344 execute_pass_list (all_lowering_passes);
345 if (optimize && cgraph_global_info_ready)
346 execute_pass_list (pass_early_local_passes.pass.sub);
347 free_dominance_info (CDI_POST_DOMINATORS);
348 free_dominance_info (CDI_DOMINATORS);
349 compact_blocks ();
350 current_function_decl = saved_current_function_decl;
351 bitmap_obstack_release (NULL);
352 pop_cfun ();
355 /* For functions-as-trees languages, this performs all optimization and
356 compilation for FNDECL. */
358 void
359 tree_rest_of_compilation (tree fndecl)
361 location_t saved_loc;
362 struct cgraph_node *node;
363 int substitute_status;
365 timevar_push (TV_EXPAND);
367 gcc_assert (cgraph_global_info_ready);
369 node = cgraph_node (fndecl);
371 /* Initialize the default bitmap obstack. */
372 bitmap_obstack_initialize (NULL);
374 /* Initialize the RTL code for the function. */
375 current_function_decl = fndecl;
376 saved_loc = input_location;
377 input_location = DECL_SOURCE_LOCATION (fndecl);
378 init_function_start (fndecl);
380 /* Even though we're inside a function body, we still don't want to
381 call expand_expr to calculate the size of a variable-sized array.
382 We haven't necessarily assigned RTL to all variables yet, so it's
383 not safe to try to expand expressions involving them. */
384 cfun->dont_save_pending_sizes_p = 1;
386 gimple_register_cfg_hooks ();
388 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
390 execute_all_ipa_transforms ();
392 /* Perform all tree transforms and optimizations. */
394 /* Signal the start of passes. */
395 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
397 /* ICI Event: Substitution of pass manager. */
398 /* Try calling the event - if not successful, or if the plugin did not
399 manipulate passes, fall back on the default pass ordering. */
400 substitute_status = 0;
401 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_EXECUTION, &substitute_status);
402 if (substitute_status == 0)
403 execute_pass_list (all_passes);
405 /* Signal the end of passes. */
406 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
408 bitmap_obstack_release (&reg_obstack);
410 /* Release the default bitmap obstack. */
411 bitmap_obstack_release (NULL);
413 set_cfun (NULL);
415 /* If requested, warn about function definitions where the function will
416 return a value (usually of some struct or union type) which itself will
417 take up a lot of stack space. */
418 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
420 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
422 if (ret_type && TYPE_SIZE_UNIT (ret_type)
423 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
424 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
425 larger_than_size))
427 unsigned int size_as_int
428 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
430 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
431 warning (OPT_Wlarger_than_eq, "size of return value of %q+D is %u bytes",
432 fndecl, size_as_int);
433 else
434 warning (OPT_Wlarger_than_eq, "size of return value of %q+D is larger than %wd bytes",
435 fndecl, larger_than_size);
439 gimple_set_body (fndecl, NULL);
440 if (DECL_STRUCT_FUNCTION (fndecl) == 0
441 && !cgraph_node (fndecl)->origin)
443 /* Stop pointing to the local nodes about to be freed.
444 But DECL_INITIAL must remain nonzero so we know this
445 was an actual function definition.
446 For a nested function, this is done in c_pop_function_context.
447 If rest_of_compilation set this to 0, leave it 0. */
448 if (DECL_INITIAL (fndecl) != 0)
449 DECL_INITIAL (fndecl) = error_mark_node;
452 input_location = saved_loc;
454 ggc_collect ();
455 timevar_pop (TV_EXPAND);