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 We have to pass down the queue if we already went into SSA */
62 && (!(errorcount
|| sorrycount
) || in_ssa_p
));
65 struct tree_opt_pass pass_all_optimizations
=
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)
88 /* Don't bother doing anything if the program has errors. */
89 && !(errorcount
|| sorrycount
));
92 struct tree_opt_pass pass_early_local_passes
=
95 gate_all_early_local_passes
, /* gate */
99 0, /* static_pass_number */
101 0, /* properties_required */
102 0, /* properties_provided */
103 0, /* properties_destroyed */
104 0, /* todo_flags_start */
105 0, /* todo_flags_finish */
108 /* Gate: execute, or not, all of the non-trivial optimizations. */
111 gate_all_early_optimizations (void)
113 return (optimize
>= 1
114 /* Sort of Hack: In non-unit-at-a-time we need to run the early
115 optimizations anyway. The early optimization pass is run once
116 in IPA queue and once in late local passes. In unit-at-a-time
117 the second invocation will get cgraph_global_info_ready. */
118 && !cgraph_global_info_ready
119 /* Don't bother doing anything if the program has errors. */
120 && !(errorcount
|| sorrycount
));
123 struct tree_opt_pass pass_all_early_optimizations
=
126 gate_all_early_optimizations
, /* gate */
130 0, /* static_pass_number */
132 0, /* properties_required */
133 0, /* properties_provided */
134 0, /* properties_destroyed */
135 0, /* todo_flags_start */
136 0, /* todo_flags_finish */
140 /* Pass: cleanup the CFG just before expanding trees to RTL.
141 This is just a round of label cleanups and case node grouping
142 because after the tree optimizers have run such cleanups may
146 execute_cleanup_cfg_pre_ipa (void)
151 struct tree_opt_pass pass_cleanup_cfg
=
153 "cleanup_cfg", /* name */
155 execute_cleanup_cfg_pre_ipa
, /* execute */
158 0, /* static_pass_number */
160 PROP_cfg
, /* properties_required */
161 0, /* properties_provided */
162 0, /* properties_destroyed */
163 0, /* todo_flags_start */
164 TODO_dump_func
, /* todo_flags_finish */
169 /* Pass: cleanup the CFG just before expanding trees to RTL.
170 This is just a round of label cleanups and case node grouping
171 because after the tree optimizers have run such cleanups may
175 execute_cleanup_cfg_post_optimizing (void)
177 fold_cond_expr_cond ();
179 cleanup_dead_labels ();
180 group_case_labels ();
183 struct tree_opt_pass pass_cleanup_cfg_post_optimizing
=
185 "final_cleanup", /* name */
187 execute_cleanup_cfg_post_optimizing
, /* execute */
190 0, /* static_pass_number */
192 PROP_cfg
, /* properties_required */
193 0, /* properties_provided */
194 0, /* properties_destroyed */
195 0, /* todo_flags_start */
196 TODO_dump_func
, /* todo_flags_finish */
200 /* Pass: do the actions required to finish with tree-ssa optimization
204 execute_free_datastructures (void)
206 /* ??? This isn't the right place for this. Worse, it got computed
207 more or less at random in various passes. */
208 free_dominance_info (CDI_DOMINATORS
);
209 free_dominance_info (CDI_POST_DOMINATORS
);
211 /* Remove the ssa structures. Do it here since this includes statement
212 annotations that need to be intact during disband_implicit_edges. */
217 struct tree_opt_pass pass_free_datastructures
=
221 execute_free_datastructures
, /* execute */
224 0, /* static_pass_number */
226 PROP_cfg
, /* properties_required */
227 0, /* properties_provided */
228 0, /* properties_destroyed */
229 0, /* todo_flags_start */
230 0, /* todo_flags_finish */
233 /* Pass: free cfg annotations. */
236 execute_free_cfg_annotations (void)
239 block_stmt_iterator bsi
;
241 /* Emit gotos for implicit jumps. */
242 disband_implicit_edges ();
244 /* Remove annotations from every tree in the function. */
246 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
248 tree stmt
= bsi_stmt (bsi
);
249 ggc_free (stmt
->common
.ann
);
250 stmt
->common
.ann
= NULL
;
253 /* And get rid of annotations we no longer need. */
254 delete_tree_cfg_annotations ();
257 struct tree_opt_pass pass_free_cfg_annotations
=
261 execute_free_cfg_annotations
, /* execute */
264 0, /* static_pass_number */
266 PROP_cfg
, /* properties_required */
267 0, /* properties_provided */
268 0, /* properties_destroyed */
269 0, /* todo_flags_start */
270 0, /* todo_flags_finish */
273 /* Pass: fixup_cfg - IPA passes or compilation of earlier functions might've
274 changed some properties - such as marked functions nothrow. Remove now
275 redundant edges and basic blocks. */
278 execute_fixup_cfg (void)
281 block_stmt_iterator bsi
;
286 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
288 tree stmt
= bsi_stmt (bsi
);
289 tree call
= get_call_expr_in (stmt
);
291 if (call
&& call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
))
292 TREE_SIDE_EFFECTS (call
) = 0;
293 if (!tree_could_throw_p (stmt
) && lookup_stmt_eh_region (stmt
))
294 remove_stmt_from_eh_region (stmt
);
296 tree_purge_dead_eh_edges (bb
);
302 struct tree_opt_pass pass_fixup_cfg
=
306 execute_fixup_cfg
, /* execute */
309 0, /* static_pass_number */
311 PROP_cfg
, /* properties_required */
312 0, /* properties_provided */
313 0, /* properties_destroyed */
314 0, /* todo_flags_start */
315 0, /* todo_flags_finish */
319 /* Do the actions required to initialize internal data structures used
320 in tree-ssa optimization passes. */
323 execute_init_datastructures (void)
325 /* Allocate hash tables, arrays and other structures. */
329 struct tree_opt_pass pass_init_datastructures
=
333 execute_init_datastructures
, /* execute */
336 0, /* static_pass_number */
338 PROP_cfg
, /* properties_required */
339 0, /* properties_provided */
340 0, /* properties_destroyed */
341 0, /* todo_flags_start */
342 0, /* todo_flags_finish */
347 tree_lowering_passes (tree fn
)
349 tree saved_current_function_decl
= current_function_decl
;
351 current_function_decl
= fn
;
352 push_cfun (DECL_STRUCT_FUNCTION (fn
));
353 tree_register_cfg_hooks ();
354 bitmap_obstack_initialize (NULL
);
355 execute_pass_list (all_lowering_passes
);
356 if (cgraph_global_info_ready
&& optimize
)
357 execute_pass_list (pass_all_early_optimizations
.sub
);
358 free_dominance_info (CDI_POST_DOMINATORS
);
359 free_dominance_info (CDI_DOMINATORS
);
360 current_function_decl
= saved_current_function_decl
;
361 bitmap_obstack_release (NULL
);
365 /* For functions-as-trees languages, this performs all optimization and
366 compilation for FNDECL. */
369 tree_rest_of_compilation (tree fndecl
)
371 location_t saved_loc
;
372 struct cgraph_node
*node
;
374 timevar_push (TV_EXPAND
);
376 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
378 node
= cgraph_node (fndecl
);
380 /* Initialize the RTL code for the function. */
381 current_function_decl
= fndecl
;
382 saved_loc
= input_location
;
383 input_location
= DECL_SOURCE_LOCATION (fndecl
);
384 init_function_start (fndecl
);
386 /* Even though we're inside a function body, we still don't want to
387 call expand_expr to calculate the size of a variable-sized array.
388 We haven't necessarily assigned RTL to all variables yet, so it's
389 not safe to try to expand expressions involving them. */
390 cfun
->x_dont_save_pending_sizes_p
= 1;
391 cfun
->after_inlining
= true;
393 /* We are not going to maintain the cgraph edges up to date.
394 Kill it so it won't confuse us. */
395 while (node
->callees
)
397 /* In non-unit-at-a-time we must mark all referenced functions as needed.
399 if (node
->callees
->callee
->analyzed
&& !flag_unit_at_a_time
)
400 cgraph_mark_needed_node (node
->callees
->callee
);
401 cgraph_remove_edge (node
->callees
);
404 /* We are not going to maintain the cgraph edges up to date.
405 Kill it so it won't confuse us. */
406 cgraph_node_remove_callees (node
);
408 bitmap_obstack_initialize (NULL
);
409 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
411 tree_register_cfg_hooks ();
412 /* Perform all tree transforms and optimizations. */
413 execute_pass_list (all_passes
);
415 bitmap_obstack_release (®_obstack
);
417 /* Release the default bitmap obstack. */
418 bitmap_obstack_release (NULL
);
420 DECL_SAVED_TREE (fndecl
) = NULL
;
423 /* If requested, warn about function definitions where the function will
424 return a value (usually of some struct or union type) which itself will
425 take up a lot of stack space. */
426 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
428 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
430 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
431 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
432 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
435 unsigned int size_as_int
436 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
438 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
439 warning (0, "size of return value of %q+D is %u bytes",
440 fndecl
, size_as_int
);
442 warning (0, "size of return value of %q+D is larger than %wd bytes",
443 fndecl
, larger_than_size
);
447 DECL_SAVED_TREE (fndecl
) = NULL
;
448 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
449 && !cgraph_node (fndecl
)->origin
)
451 /* Stop pointing to the local nodes about to be freed.
452 But DECL_INITIAL must remain nonzero so we know this
453 was an actual function definition.
454 For a nested function, this is done in c_pop_function_context.
455 If rest_of_compilation set this to 0, leave it 0. */
456 if (DECL_INITIAL (fndecl
) != 0)
457 DECL_INITIAL (fndecl
) = error_mark_node
;
460 input_location
= saved_loc
;
463 timevar_pop (TV_EXPAND
);