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>
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)
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/>. */
24 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "basic-block.h"
33 #include "tree-flow.h"
34 #include "tree-dump.h"
37 #include "langhooks.h"
41 #include "tree-inline.h"
42 #include "tree-mudflap.h"
43 #include "tree-pass.h"
50 #include "regset.h" /* FIXME: For reg_obstack. */
52 /* Gate: execute, or not, all of the non-trivial optimizations. */
55 gate_all_optimizations (void)
58 /* Don't bother doing anything if the program has errors.
59 We have to pass down the queue if we already went into SSA */
60 && (!seen_error () || gimple_in_ssa_p (cfun)));
63 struct gimple_opt_pass pass_all_optimizations =
67 "*all_optimizations", /* name */
68 gate_all_optimizations, /* gate */
72 0, /* static_pass_number */
74 0, /* properties_required */
75 0, /* properties_provided */
76 0, /* properties_destroyed */
77 0, /* todo_flags_start */
78 0 /* todo_flags_finish */
82 /* Gate: execute, or not, all of the non-trivial optimizations. */
85 gate_all_early_local_passes (void)
87 /* Don't bother doing anything if the program has errors. */
88 return (!seen_error () && !in_lto_p);
91 struct simple_ipa_opt_pass pass_early_local_passes =
95 "early_local_cleanups", /* name */
96 gate_all_early_local_passes, /* gate */
100 0, /* static_pass_number */
102 0, /* properties_required */
103 0, /* properties_provided */
104 0, /* properties_destroyed */
105 0, /* todo_flags_start */
106 TODO_remove_functions /* todo_flags_finish */
111 execute_early_local_optimizations (void)
113 /* First time we start with early optimization we need to advance
114 cgraph state so newly inserted functions are also early optimized.
115 However we execute early local optimizations for lately inserted
116 functions, in that case don't reset cgraph state back to IPA_SSA. */
117 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
118 cgraph_state = CGRAPH_STATE_IPA_SSA;
122 /* Gate: execute, or not, all of the non-trivial optimizations. */
125 gate_all_early_optimizations (void)
127 return (optimize >= 1
128 /* Don't bother doing anything if the program has errors. */
132 struct gimple_opt_pass pass_all_early_optimizations =
136 "early_optimizations", /* name */
137 gate_all_early_optimizations, /* gate */
138 execute_early_local_optimizations, /* execute */
141 0, /* static_pass_number */
143 0, /* properties_required */
144 0, /* properties_provided */
145 0, /* properties_destroyed */
146 0, /* todo_flags_start */
147 0 /* todo_flags_finish */
151 /* Pass: cleanup the CFG just before expanding trees to RTL.
152 This is just a round of label cleanups and case node grouping
153 because after the tree optimizers have run such cleanups may
157 execute_cleanup_cfg_pre_ipa (void)
163 struct gimple_opt_pass pass_cleanup_cfg =
167 "cleanup_cfg", /* name */
169 execute_cleanup_cfg_pre_ipa, /* execute */
172 0, /* static_pass_number */
174 PROP_cfg, /* properties_required */
175 0, /* properties_provided */
176 0, /* properties_destroyed */
177 0, /* todo_flags_start */
178 TODO_dump_func /* todo_flags_finish */
183 /* Pass: cleanup the CFG just before expanding trees to RTL.
184 This is just a round of label cleanups and case node grouping
185 because after the tree optimizers have run such cleanups may
189 execute_cleanup_cfg_post_optimizing (void)
191 fold_cond_expr_cond ();
193 cleanup_dead_labels ();
194 group_case_labels ();
198 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing =
202 "optimized", /* name */
204 execute_cleanup_cfg_post_optimizing, /* execute */
207 0, /* static_pass_number */
209 PROP_cfg, /* properties_required */
210 0, /* properties_provided */
211 0, /* properties_destroyed */
212 0, /* todo_flags_start */
213 TODO_dump_func /* todo_flags_finish */
214 | TODO_remove_unused_locals
218 /* Pass: do the actions required to finish with tree-ssa optimization
222 execute_free_datastructures (void)
224 free_dominance_info (CDI_DOMINATORS);
225 free_dominance_info (CDI_POST_DOMINATORS);
227 /* And get rid of annotations we no longer need. */
228 delete_tree_cfg_annotations ();
233 /* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining
234 might have changed some properties, such as marked functions nothrow,
235 pure, const or noreturn.
236 Remove redundant edges and basic blocks, and create new ones if necessary.
238 This pass can't be executed as stand alone pass from pass manager, because
239 in between inlining and this fixup the verify_flow_info would fail. */
242 execute_fixup_cfg (void)
245 gimple_stmt_iterator gsi;
246 int todo = gimple_in_ssa_p (cfun) ? TODO_verify_ssa : 0;
247 gcov_type count_scale;
251 if (ENTRY_BLOCK_PTR->count)
252 count_scale = (cgraph_node (current_function_decl)->count * REG_BR_PROB_BASE
253 + ENTRY_BLOCK_PTR->count / 2) / ENTRY_BLOCK_PTR->count;
255 count_scale = REG_BR_PROB_BASE;
257 ENTRY_BLOCK_PTR->count = cgraph_node (current_function_decl)->count;
258 EXIT_BLOCK_PTR->count = (EXIT_BLOCK_PTR->count * count_scale
259 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
263 bb->count = (bb->count * count_scale
264 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
265 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
267 gimple stmt = gsi_stmt (gsi);
268 tree decl = is_gimple_call (stmt)
269 ? gimple_call_fndecl (stmt)
273 int flags = gimple_call_flags (stmt);
274 if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
276 if (gimple_in_ssa_p (cfun))
278 todo |= TODO_update_ssa | TODO_cleanup_cfg;
279 mark_symbols_for_renaming (stmt);
284 if (flags & ECF_NORETURN
285 && fixup_noreturn_call (stmt))
286 todo |= TODO_cleanup_cfg;
289 maybe_clean_eh_stmt (stmt);
292 if (gimple_purge_dead_eh_edges (bb))
293 todo |= TODO_cleanup_cfg;
294 FOR_EACH_EDGE (e, ei, bb->succs)
295 e->count = (e->count * count_scale
296 + REG_BR_PROB_BASE / 2) / REG_BR_PROB_BASE;
298 if (count_scale != REG_BR_PROB_BASE)
299 compute_function_frequency ();
301 /* We just processed all calls. */
304 VEC_free (gimple, gc, MODIFIED_NORETURN_CALLS (cfun));
305 MODIFIED_NORETURN_CALLS (cfun) = NULL;
308 /* Dump a textual representation of the flowgraph. */
310 gimple_dump_cfg (dump_file, dump_flags);
315 struct gimple_opt_pass pass_fixup_cfg =
319 "*free_cfg_annotations", /* name */
321 execute_fixup_cfg, /* execute */
324 0, /* static_pass_number */
326 PROP_cfg, /* properties_required */
327 0, /* properties_provided */
328 0, /* properties_destroyed */
329 0, /* todo_flags_start */
330 0 /* todo_flags_finish */
334 /* Do the actions required to initialize internal data structures used
335 in tree-ssa optimization passes. */
338 execute_init_datastructures (void)
340 /* Allocate hash tables, arrays and other structures. */
341 init_tree_ssa (cfun);
345 struct gimple_opt_pass pass_init_datastructures =
349 "*init_datastructures", /* name */
351 execute_init_datastructures, /* execute */
354 0, /* static_pass_number */
356 PROP_cfg, /* properties_required */
357 0, /* properties_provided */
358 0, /* properties_destroyed */
359 0, /* todo_flags_start */
360 0 /* todo_flags_finish */
365 tree_lowering_passes (tree fn)
367 tree saved_current_function_decl = current_function_decl;
369 current_function_decl = fn;
370 push_cfun (DECL_STRUCT_FUNCTION (fn));
371 gimple_register_cfg_hooks ();
372 bitmap_obstack_initialize (NULL);
373 execute_pass_list (all_lowering_passes);
374 if (optimize && cgraph_global_info_ready)
375 execute_pass_list (pass_early_local_passes.pass.sub);
376 free_dominance_info (CDI_POST_DOMINATORS);
377 free_dominance_info (CDI_DOMINATORS);
379 current_function_decl = saved_current_function_decl;
380 bitmap_obstack_release (NULL);
384 /* For functions-as-trees languages, this performs all optimization and
385 compilation for FNDECL. */
388 tree_rest_of_compilation (tree fndecl)
390 location_t saved_loc;
392 timevar_push (TV_EXPAND);
394 gcc_assert (cgraph_global_info_ready);
396 /* Initialize the default bitmap obstack. */
397 bitmap_obstack_initialize (NULL);
399 /* Initialize the RTL code for the function. */
400 current_function_decl = fndecl;
401 saved_loc = input_location;
402 input_location = DECL_SOURCE_LOCATION (fndecl);
403 init_function_start (fndecl);
405 /* Even though we're inside a function body, we still don't want to
406 call expand_expr to calculate the size of a variable-sized array.
407 We haven't necessarily assigned RTL to all variables yet, so it's
408 not safe to try to expand expressions involving them. */
409 cfun->dont_save_pending_sizes_p = 1;
411 gimple_register_cfg_hooks ();
413 bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/
415 execute_all_ipa_transforms ();
417 /* Perform all tree transforms and optimizations. */
419 /* Signal the start of passes. */
420 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_START, NULL);
422 execute_pass_list (all_passes);
424 /* Signal the end of passes. */
425 invoke_plugin_callbacks (PLUGIN_ALL_PASSES_END, NULL);
427 bitmap_obstack_release (®_obstack);
429 /* Release the default bitmap obstack. */
430 bitmap_obstack_release (NULL);
434 /* If requested, warn about function definitions where the function will
435 return a value (usually of some struct or union type) which itself will
436 take up a lot of stack space. */
437 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
439 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
441 if (ret_type && TYPE_SIZE_UNIT (ret_type)
442 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
443 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
446 unsigned int size_as_int
447 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
449 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
450 warning (OPT_Wlarger_than_eq, "size of return value of %q+D is %u bytes",
451 fndecl, size_as_int);
453 warning (OPT_Wlarger_than_eq, "size of return value of %q+D is larger than %wd bytes",
454 fndecl, larger_than_size);
458 gimple_set_body (fndecl, NULL);
459 if (DECL_STRUCT_FUNCTION (fndecl) == 0
460 && !cgraph_node (fndecl)->origin)
462 /* Stop pointing to the local nodes about to be freed.
463 But DECL_INITIAL must remain nonzero so we know this
464 was an actual function definition.
465 For a nested function, this is done in c_pop_function_context.
466 If rest_of_compilation set this to 0, leave it 0. */
467 if (DECL_INITIAL (fndecl) != 0)
468 DECL_INITIAL (fndecl) = error_mark_node;
471 input_location = saved_loc;
474 timevar_pop (TV_EXPAND);