1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
40 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
54 /* Gate: execute, or not, all of the non-trivial optimizations. */
57 gate_all_optimizations (void)
60 /* Don't bother doing anything if the program has errors. */
61 && !(errorcount
|| sorrycount
));
64 struct tree_opt_pass pass_all_optimizations
=
67 gate_all_optimizations
, /* gate */
71 0, /* static_pass_number */
73 0, /* properties_required */
74 0, /* properties_provided */
75 0, /* properties_destroyed */
76 0, /* todo_flags_start */
77 0, /* todo_flags_finish */
81 struct tree_opt_pass pass_early_local_passes
=
84 gate_all_optimizations
, /* gate */
88 0, /* static_pass_number */
90 0, /* properties_required */
91 0, /* properties_provided */
92 0, /* properties_destroyed */
93 0, /* todo_flags_start */
94 0, /* todo_flags_finish */
98 /* Pass: cleanup the CFG just before expanding trees to RTL.
99 This is just a round of label cleanups and case node grouping
100 because after the tree optimizers have run such cleanups may
104 execute_cleanup_cfg_pre_ipa (void)
109 struct tree_opt_pass pass_cleanup_cfg
=
111 "cleanup_cfg", /* name */
113 execute_cleanup_cfg_pre_ipa
, /* execute */
116 0, /* static_pass_number */
118 PROP_cfg
, /* properties_required */
119 0, /* properties_provided */
120 0, /* properties_destroyed */
121 0, /* todo_flags_start */
122 TODO_dump_func
, /* todo_flags_finish */
127 /* Pass: cleanup the CFG just before expanding trees to RTL.
128 This is just a round of label cleanups and case node grouping
129 because after the tree optimizers have run such cleanups may
133 execute_cleanup_cfg_post_optimizing (void)
135 fold_cond_expr_cond ();
136 mark_array_ref_addressable ();
138 cleanup_dead_labels ();
139 group_case_labels ();
142 struct tree_opt_pass pass_cleanup_cfg_post_optimizing
=
144 "final_cleanup", /* name */
146 execute_cleanup_cfg_post_optimizing
, /* execute */
149 0, /* static_pass_number */
151 PROP_cfg
, /* properties_required */
152 0, /* properties_provided */
153 0, /* properties_destroyed */
154 0, /* todo_flags_start */
155 TODO_dump_func
, /* todo_flags_finish */
159 /* Pass: do the actions required to finish with tree-ssa optimization
163 execute_free_datastructures (void)
165 /* ??? This isn't the right place for this. Worse, it got computed
166 more or less at random in various passes. */
167 free_dominance_info (CDI_DOMINATORS
);
168 free_dominance_info (CDI_POST_DOMINATORS
);
170 /* Remove the ssa structures. Do it here since this includes statement
171 annotations that need to be intact during disband_implicit_edges. */
175 struct tree_opt_pass pass_free_datastructures
=
179 execute_free_datastructures
, /* execute */
182 0, /* static_pass_number */
184 PROP_cfg
, /* properties_required */
185 0, /* properties_provided */
186 0, /* properties_destroyed */
187 0, /* todo_flags_start */
188 0, /* todo_flags_finish */
191 /* Pass: free cfg annotations. */
194 execute_free_cfg_annotations (void)
197 block_stmt_iterator bsi
;
199 /* Emit gotos for implicit jumps. */
200 disband_implicit_edges ();
202 /* Remove annotations from every tree in the function. */
204 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
206 tree stmt
= bsi_stmt (bsi
);
207 ggc_free (stmt
->common
.ann
);
208 stmt
->common
.ann
= NULL
;
211 /* And get rid of annotations we no longer need. */
212 delete_tree_cfg_annotations ();
215 struct tree_opt_pass pass_free_cfg_annotations
=
219 execute_free_cfg_annotations
, /* execute */
222 0, /* static_pass_number */
224 PROP_cfg
, /* properties_required */
225 0, /* properties_provided */
226 0, /* properties_destroyed */
227 0, /* todo_flags_start */
228 0, /* todo_flags_finish */
231 /* Pass: fixup_cfg - IPA passes or compilation of earlier functions might've
232 changed some properties - such as marked functions nothrow. Remove now
233 redundant edges and basic blocks. */
236 execute_fixup_cfg (void)
239 block_stmt_iterator bsi
;
244 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
246 tree stmt
= bsi_stmt (bsi
);
247 tree call
= get_call_expr_in (stmt
);
249 if (call
&& call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
))
250 TREE_SIDE_EFFECTS (call
) = 0;
251 if (!tree_could_throw_p (stmt
) && lookup_stmt_eh_region (stmt
))
252 remove_stmt_from_eh_region (stmt
);
254 tree_purge_dead_eh_edges (bb
);
260 struct tree_opt_pass pass_fixup_cfg
=
262 "fixupcfg", /* name */
264 execute_fixup_cfg
, /* execute */
267 0, /* static_pass_number */
269 PROP_cfg
, /* properties_required */
270 0, /* properties_provided */
271 0, /* properties_destroyed */
272 0, /* todo_flags_start */
273 TODO_dump_func
, /* todo_flags_finish */
277 /* Do the actions required to initialize internal data structures used
278 in tree-ssa optimization passes. */
281 execute_init_datastructures (void)
283 /* Allocate hash tables, arrays and other structures. */
287 struct tree_opt_pass pass_init_datastructures
=
291 execute_init_datastructures
, /* execute */
294 0, /* static_pass_number */
296 PROP_cfg
, /* properties_required */
297 0, /* properties_provided */
298 0, /* properties_destroyed */
299 0, /* todo_flags_start */
300 0, /* todo_flags_finish */
305 tree_lowering_passes (tree fn
)
307 tree saved_current_function_decl
= current_function_decl
;
309 current_function_decl
= fn
;
310 push_cfun (DECL_STRUCT_FUNCTION (fn
));
311 tree_register_cfg_hooks ();
312 bitmap_obstack_initialize (NULL
);
313 execute_pass_list (all_lowering_passes
);
314 free_dominance_info (CDI_POST_DOMINATORS
);
316 current_function_decl
= saved_current_function_decl
;
317 bitmap_obstack_release (NULL
);
321 /* Update recursively all inlined_to pointers of functions
322 inlined into NODE to INLINED_TO. */
324 update_inlined_to_pointers (struct cgraph_node
*node
,
325 struct cgraph_node
*inlined_to
)
327 struct cgraph_edge
*e
;
328 for (e
= node
->callees
; e
; e
= e
->next_callee
)
330 if (e
->callee
->global
.inlined_to
)
332 e
->callee
->global
.inlined_to
= inlined_to
;
333 update_inlined_to_pointers (e
->callee
, inlined_to
);
339 /* For functions-as-trees languages, this performs all optimization and
340 compilation for FNDECL. */
343 tree_rest_of_compilation (tree fndecl
)
345 location_t saved_loc
;
346 struct cgraph_node
*saved_node
= NULL
, *node
;
348 timevar_push (TV_EXPAND
);
350 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
352 /* Initialize the RTL code for the function. */
353 current_function_decl
= fndecl
;
354 saved_loc
= input_location
;
355 input_location
= DECL_SOURCE_LOCATION (fndecl
);
356 init_function_start (fndecl
);
358 /* Even though we're inside a function body, we still don't want to
359 call expand_expr to calculate the size of a variable-sized array.
360 We haven't necessarily assigned RTL to all variables yet, so it's
361 not safe to try to expand expressions involving them. */
362 cfun
->x_dont_save_pending_sizes_p
= 1;
363 cfun
->after_inlining
= true;
365 node
= cgraph_node (fndecl
);
367 /* We might need the body of this function so that we can expand
368 it inline somewhere else. This means not lowering some constructs
369 such as exception handling. */
370 if (cgraph_preserve_function_body_p (fndecl
))
372 if (!flag_unit_at_a_time
)
374 struct cgraph_edge
*e
;
376 saved_node
= cgraph_clone_node (node
, node
->count
, 1, false);
377 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
378 if (!e
->inline_failed
)
379 cgraph_clone_inlined_nodes (e
, true, false);
381 cfun
->saved_static_chain_decl
= cfun
->static_chain_decl
;
382 save_body (fndecl
, &cfun
->saved_args
, &cfun
->saved_static_chain_decl
);
385 if (flag_inline_trees
)
387 struct cgraph_edge
*e
;
388 for (e
= node
->callees
; e
; e
= e
->next_callee
)
389 if (!e
->inline_failed
|| warn_inline
)
393 timevar_push (TV_INTEGRATION
);
394 optimize_inline_calls (fndecl
);
395 timevar_pop (TV_INTEGRATION
);
398 /* We are not going to maintain the cgraph edges up to date.
399 Kill it so it won't confuse us. */
400 while (node
->callees
)
402 /* In non-unit-at-a-time we must mark all referenced functions as needed.
404 if (node
->callees
->callee
->analyzed
&& !flag_unit_at_a_time
)
405 cgraph_mark_needed_node (node
->callees
->callee
);
406 cgraph_remove_edge (node
->callees
);
409 /* We are not going to maintain the cgraph edges up to date.
410 Kill it so it won't confuse us. */
411 cgraph_node_remove_callees (node
);
414 /* Initialize the default bitmap obstack. */
415 bitmap_obstack_initialize (NULL
);
416 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
418 tree_register_cfg_hooks ();
419 /* Perform all tree transforms and optimizations. */
420 execute_pass_list (all_passes
);
422 bitmap_obstack_release (®_obstack
);
424 /* Release the default bitmap obstack. */
425 bitmap_obstack_release (NULL
);
427 /* Restore original body if still needed. */
430 DECL_ARGUMENTS (fndecl
) = cfun
->saved_args
;
431 cfun
->cfg
= cfun
->saved_cfg
;
432 cfun
->eh
= cfun
->saved_eh
;
433 DECL_INITIAL (fndecl
) = cfun
->saved_blocks
;
434 cfun
->unexpanded_var_list
= cfun
->saved_unexpanded_var_list
;
435 cfun
->saved_cfg
= NULL
;
436 cfun
->saved_eh
= NULL
;
437 cfun
->saved_args
= NULL_TREE
;
438 cfun
->saved_blocks
= NULL_TREE
;
439 cfun
->saved_unexpanded_var_list
= NULL_TREE
;
440 cfun
->static_chain_decl
= cfun
->saved_static_chain_decl
;
441 cfun
->saved_static_chain_decl
= NULL
;
442 /* When not in unit-at-a-time mode, we must preserve out of line copy
443 representing node before inlining. Restore original outgoing edges
444 using clone we created earlier. */
445 if (!flag_unit_at_a_time
)
447 struct cgraph_edge
*e
;
449 node
= cgraph_node (current_function_decl
);
450 cgraph_node_remove_callees (node
);
451 node
->callees
= saved_node
->callees
;
452 saved_node
->callees
= NULL
;
453 update_inlined_to_pointers (node
, node
);
454 for (e
= node
->callees
; e
; e
= e
->next_callee
)
456 cgraph_remove_node (saved_node
);
460 DECL_SAVED_TREE (fndecl
) = NULL
;
463 /* If requested, warn about function definitions where the function will
464 return a value (usually of some struct or union type) which itself will
465 take up a lot of stack space. */
466 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
468 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
470 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
471 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
472 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
475 unsigned int size_as_int
476 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
478 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
479 warning (0, "size of return value of %q+D is %u bytes",
480 fndecl
, size_as_int
);
482 warning (0, "size of return value of %q+D is larger than %wd bytes",
483 fndecl
, larger_than_size
);
487 if (!flag_inline_trees
)
489 DECL_SAVED_TREE (fndecl
) = NULL
;
490 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
491 && !cgraph_node (fndecl
)->origin
)
493 /* Stop pointing to the local nodes about to be freed.
494 But DECL_INITIAL must remain nonzero so we know this
495 was an actual function definition.
496 For a nested function, this is done in c_pop_function_context.
497 If rest_of_compilation set this to 0, leave it 0. */
498 if (DECL_INITIAL (fndecl
) != 0)
499 DECL_INITIAL (fndecl
) = error_mark_node
;
503 input_location
= saved_loc
;
506 timevar_pop (TV_EXPAND
);