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"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "langhooks.h"
36 #include "diagnostic-core.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
48 #include "regset.h" /* FIXME: For reg_obstack. */
50 /* Gate: execute, or not, all of the non-trivial optimizations. */
53 gate_all_optimizations (void)
56 /* Don't bother doing anything if the program has errors.
57 We have to pass down the queue if we already went into SSA */
58 && (!seen_error () || gimple_in_ssa_p (cfun
)));
61 struct gimple_opt_pass pass_all_optimizations
=
65 "*all_optimizations", /* name */
66 gate_all_optimizations
, /* gate */
70 0, /* static_pass_number */
71 TV_OPTIMIZE
, /* tv_id */
72 0, /* properties_required */
73 0, /* properties_provided */
74 0, /* properties_destroyed */
75 0, /* todo_flags_start */
76 0 /* todo_flags_finish */
80 /* Gate: execute, or not, all of the non-trivial optimizations. */
83 gate_all_early_local_passes (void)
85 /* Don't bother doing anything if the program has errors. */
86 return (!seen_error () && !in_lto_p
);
90 execute_all_early_local_passes (void)
92 /* Once this pass (and its sub-passes) are complete, all functions
93 will be in SSA form. Technically this state change is happening
94 a tad early, since the sub-passes have not yet run, but since
95 none of the sub-passes are IPA passes and do not create new
96 functions, this is ok. We're setting this value for the benefit
97 of IPA passes that follow. */
98 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
99 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
103 struct simple_ipa_opt_pass pass_early_local_passes
=
107 "early_local_cleanups", /* name */
108 gate_all_early_local_passes
, /* gate */
109 execute_all_early_local_passes
, /* execute */
112 0, /* static_pass_number */
113 TV_EARLY_LOCAL
, /* tv_id */
114 0, /* properties_required */
115 0, /* properties_provided */
116 0, /* properties_destroyed */
117 0, /* todo_flags_start */
118 TODO_remove_functions
/* todo_flags_finish */
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 */
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 */
152 /* Pass: cleanup the CFG just before expanding trees to RTL.
153 This is just a round of label cleanups and case node grouping
154 because after the tree optimizers have run such cleanups may
158 execute_cleanup_cfg_post_optimizing (void)
160 unsigned int todo
= 0;
161 if (cleanup_tree_cfg ())
162 todo
|= TODO_update_ssa
;
163 maybe_remove_unreachable_handlers ();
164 cleanup_dead_labels ();
165 group_case_labels ();
166 if ((flag_compare_debug_opt
|| flag_compare_debug
)
167 && flag_dump_final_insns
)
169 FILE *final_output
= fopen (flag_dump_final_insns
, "a");
173 error ("could not open final insn dump file %qs: %m",
174 flag_dump_final_insns
);
175 flag_dump_final_insns
= NULL
;
179 int save_unnumbered
= flag_dump_unnumbered
;
180 int save_noaddr
= flag_dump_noaddr
;
182 flag_dump_noaddr
= flag_dump_unnumbered
= 1;
183 fprintf (final_output
, "\n");
184 dump_enumerated_decls (final_output
, dump_flags
| TDF_NOUID
);
185 flag_dump_noaddr
= save_noaddr
;
186 flag_dump_unnumbered
= save_unnumbered
;
187 if (fclose (final_output
))
189 error ("could not close final insn dump file %qs: %m",
190 flag_dump_final_insns
);
191 flag_dump_final_insns
= NULL
;
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 */
208 TV_TREE_CLEANUP_CFG
, /* tv_id */
209 PROP_cfg
, /* properties_required */
210 0, /* properties_provided */
211 0, /* properties_destroyed */
212 0, /* todo_flags_start */
213 TODO_remove_unused_locals
/* todo_flags_finish */
217 /* Pass: do the actions required to finish with tree-ssa optimization
221 execute_free_datastructures (void)
223 free_dominance_info (CDI_DOMINATORS
);
224 free_dominance_info (CDI_POST_DOMINATORS
);
226 /* And get rid of annotations we no longer need. */
227 delete_tree_cfg_annotations ();
232 /* IPA passes, compilation of earlier functions or inlining
233 might have changed some properties, such as marked functions nothrow,
234 pure, const or noreturn.
235 Remove redundant edges and basic blocks, and create new ones if necessary.
237 This pass can't be executed as stand alone pass from pass manager, because
238 in between inlining and this fixup the verify_flow_info would fail. */
241 execute_fixup_cfg (void)
244 gimple_stmt_iterator gsi
;
245 int todo
= gimple_in_ssa_p (cfun
) ? TODO_verify_ssa
: 0;
246 gcov_type count_scale
;
250 if (ENTRY_BLOCK_PTR
->count
)
251 count_scale
= ((cgraph_get_node (current_function_decl
)->count
252 * REG_BR_PROB_BASE
+ ENTRY_BLOCK_PTR
->count
/ 2)
253 / ENTRY_BLOCK_PTR
->count
);
255 count_scale
= REG_BR_PROB_BASE
;
257 ENTRY_BLOCK_PTR
->count
= cgraph_get_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
;
261 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
262 e
->count
= (e
->count
* count_scale
263 + REG_BR_PROB_BASE
/ 2) / REG_BR_PROB_BASE
;
267 bb
->count
= (bb
->count
* count_scale
268 + REG_BR_PROB_BASE
/ 2) / REG_BR_PROB_BASE
;
269 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
271 gimple stmt
= gsi_stmt (gsi
);
272 tree decl
= is_gimple_call (stmt
)
273 ? gimple_call_fndecl (stmt
)
277 int flags
= gimple_call_flags (stmt
);
278 if (flags
& (ECF_CONST
| ECF_PURE
| ECF_LOOPING_CONST_OR_PURE
))
280 if (gimple_purge_dead_abnormal_call_edges (bb
))
281 todo
|= TODO_cleanup_cfg
;
283 if (gimple_in_ssa_p (cfun
))
285 todo
|= TODO_update_ssa
| TODO_cleanup_cfg
;
290 if (flags
& ECF_NORETURN
291 && fixup_noreturn_call (stmt
))
292 todo
|= TODO_cleanup_cfg
;
295 if (maybe_clean_eh_stmt (stmt
)
296 && gimple_purge_dead_eh_edges (bb
))
297 todo
|= TODO_cleanup_cfg
;
300 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
301 e
->count
= (e
->count
* count_scale
302 + REG_BR_PROB_BASE
/ 2) / REG_BR_PROB_BASE
;
304 if (count_scale
!= REG_BR_PROB_BASE
)
305 compute_function_frequency ();
307 /* We just processed all calls. */
310 VEC_free (gimple
, gc
, MODIFIED_NORETURN_CALLS (cfun
));
311 MODIFIED_NORETURN_CALLS (cfun
) = NULL
;
314 /* Dump a textual representation of the flowgraph. */
316 gimple_dump_cfg (dump_file
, dump_flags
);
321 struct gimple_opt_pass pass_fixup_cfg
=
325 "*free_cfg_annotations", /* name */
327 execute_fixup_cfg
, /* execute */
330 0, /* static_pass_number */
332 PROP_cfg
, /* properties_required */
333 0, /* properties_provided */
334 0, /* properties_destroyed */
335 0, /* todo_flags_start */
336 0 /* todo_flags_finish */
340 /* Do the actions required to initialize internal data structures used
341 in tree-ssa optimization passes. */
344 execute_init_datastructures (void)
346 /* Allocate hash tables, arrays and other structures. */
347 init_tree_ssa (cfun
);
351 struct gimple_opt_pass pass_init_datastructures
=
355 "*init_datastructures", /* name */
357 execute_init_datastructures
, /* execute */
360 0, /* static_pass_number */
362 PROP_cfg
, /* properties_required */
363 0, /* properties_provided */
364 0, /* properties_destroyed */
365 0, /* todo_flags_start */
366 0 /* todo_flags_finish */
371 tree_lowering_passes (tree fn
)
373 tree saved_current_function_decl
= current_function_decl
;
375 current_function_decl
= fn
;
376 push_cfun (DECL_STRUCT_FUNCTION (fn
));
377 gimple_register_cfg_hooks ();
378 bitmap_obstack_initialize (NULL
);
379 execute_pass_list (all_lowering_passes
);
380 if (optimize
&& cgraph_global_info_ready
)
381 execute_pass_list (pass_early_local_passes
.pass
.sub
);
382 free_dominance_info (CDI_POST_DOMINATORS
);
383 free_dominance_info (CDI_DOMINATORS
);
385 current_function_decl
= saved_current_function_decl
;
386 bitmap_obstack_release (NULL
);
390 /* For functions-as-trees languages, this performs all optimization and
391 compilation for FNDECL. */
394 tree_rest_of_compilation (tree fndecl
)
396 location_t saved_loc
;
398 timevar_push (TV_REST_OF_COMPILATION
);
400 gcc_assert (cgraph_global_info_ready
);
402 /* Initialize the default bitmap obstack. */
403 bitmap_obstack_initialize (NULL
);
405 /* Initialize the RTL code for the function. */
406 current_function_decl
= fndecl
;
407 saved_loc
= input_location
;
408 input_location
= DECL_SOURCE_LOCATION (fndecl
);
409 init_function_start (fndecl
);
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_
, "size of return value of %q+D is %u bytes",
451 fndecl
, size_as_int
);
453 warning (OPT_Wlarger_than_
, "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_get_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_REST_OF_COMPILATION
);