1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
32 #include "diagnostic.h"
33 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-dump.h"
39 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "tree-mudflap.h"
45 #include "tree-pass.h"
53 /* Gate: execute, or not, all of the non-trivial optimizations. */
56 gate_all_optimizations (void)
59 /* Don't bother doing anything if the program has errors.
60 We have to pass down the queue if we already went into SSA */
61 && (!(errorcount
|| sorrycount
) || gimple_in_ssa_p (cfun
)));
64 struct gimple_opt_pass pass_all_optimizations
=
69 gate_all_optimizations
, /* gate */
73 0, /* static_pass_number */
75 0, /* properties_required */
76 0, /* properties_provided */
77 0, /* properties_destroyed */
78 0, /* todo_flags_start */
79 0 /* todo_flags_finish */
83 /* Gate: execute, or not, all of the non-trivial optimizations. */
86 gate_all_early_local_passes (void)
88 /* Don't bother doing anything if the program has errors. */
89 return (!errorcount
&& !sorrycount
);
92 struct simple_ipa_opt_pass pass_early_local_passes
=
96 "early_local_cleanups", /* name */
97 gate_all_early_local_passes
, /* gate */
101 0, /* static_pass_number */
103 0, /* properties_required */
104 0, /* properties_provided */
105 0, /* properties_destroyed */
106 0, /* todo_flags_start */
107 TODO_remove_functions
/* todo_flags_finish */
112 execute_early_local_optimizations (void)
114 if (flag_unit_at_a_time
)
115 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
119 /* Gate: execute, or not, all of the non-trivial optimizations. */
122 gate_all_early_optimizations (void)
124 return (optimize
>= 1
125 /* Don't bother doing anything if the program has errors. */
126 && !(errorcount
|| sorrycount
));
129 struct gimple_opt_pass pass_all_early_optimizations
=
133 "early_optimizations", /* name */
134 gate_all_early_optimizations
, /* gate */
135 execute_early_local_optimizations
, /* execute */
138 0, /* static_pass_number */
140 0, /* properties_required */
141 0, /* properties_provided */
142 0, /* properties_destroyed */
143 0, /* todo_flags_start */
144 0 /* todo_flags_finish */
148 /* Pass: cleanup the CFG just before expanding trees to RTL.
149 This is just a round of label cleanups and case node grouping
150 because after the tree optimizers have run such cleanups may
154 execute_cleanup_cfg_pre_ipa (void)
160 struct gimple_opt_pass pass_cleanup_cfg
=
164 "cleanup_cfg", /* name */
166 execute_cleanup_cfg_pre_ipa
, /* execute */
169 0, /* static_pass_number */
171 PROP_cfg
, /* properties_required */
172 0, /* properties_provided */
173 0, /* properties_destroyed */
174 0, /* todo_flags_start */
175 TODO_dump_func
/* todo_flags_finish */
180 /* Pass: cleanup the CFG just before expanding trees to RTL.
181 This is just a round of label cleanups and case node grouping
182 because after the tree optimizers have run such cleanups may
186 execute_cleanup_cfg_post_optimizing (void)
188 fold_cond_expr_cond ();
190 cleanup_dead_labels ();
191 group_case_labels ();
195 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing
=
199 "final_cleanup", /* name */
201 execute_cleanup_cfg_post_optimizing
, /* execute */
204 0, /* static_pass_number */
206 PROP_cfg
, /* properties_required */
207 0, /* properties_provided */
208 0, /* properties_destroyed */
209 0, /* todo_flags_start */
210 TODO_dump_func
/* todo_flags_finish */
214 /* Pass: do the actions required to finish with tree-ssa optimization
218 execute_free_datastructures (void)
220 free_dominance_info (CDI_DOMINATORS
);
221 free_dominance_info (CDI_POST_DOMINATORS
);
223 /* Remove the ssa structures. */
229 struct gimple_opt_pass pass_free_datastructures
=
235 execute_free_datastructures
, /* execute */
238 0, /* static_pass_number */
240 PROP_cfg
, /* properties_required */
241 0, /* properties_provided */
242 0, /* properties_destroyed */
243 0, /* todo_flags_start */
244 0 /* todo_flags_finish */
247 /* Pass: free cfg annotations. */
250 execute_free_cfg_annotations (void)
252 /* And get rid of annotations we no longer need. */
253 delete_tree_cfg_annotations ();
258 struct gimple_opt_pass pass_free_cfg_annotations
=
264 execute_free_cfg_annotations
, /* 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 0 /* todo_flags_finish */
277 /* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining
278 might have changed some properties, such as marked functions nothrow.
279 Remove redundant edges and basic blocks, and create new ones if necessary.
281 This pass can't be executed as stand alone pass from pass manager, because
282 in between inlining and this fixup the verify_flow_info would fail. */
285 execute_fixup_cfg (void)
288 block_stmt_iterator bsi
;
289 int todo
= gimple_in_ssa_p (cfun
) ? TODO_verify_ssa
: 0;
291 cfun
->after_inlining
= true;
296 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
298 tree stmt
= bsi_stmt (bsi
);
299 tree call
= get_call_expr_in (stmt
);
300 tree decl
= call
? get_callee_fndecl (call
) : NULL
;
302 if (decl
&& call_expr_flags (call
) & (ECF_CONST
| ECF_PURE
)
303 && TREE_SIDE_EFFECTS (call
))
305 if (gimple_in_ssa_p (cfun
))
307 todo
|= TODO_update_ssa
| TODO_cleanup_cfg
;
310 TREE_SIDE_EFFECTS (call
) = 0;
312 if (decl
&& TREE_NOTHROW (decl
))
313 TREE_NOTHROW (call
) = 1;
314 if (!tree_could_throw_p (stmt
) && lookup_stmt_eh_region (stmt
))
315 remove_stmt_from_eh_region (stmt
);
317 if (tree_purge_dead_eh_edges (bb
))
318 todo
|= TODO_cleanup_cfg
;
321 /* Dump a textual representation of the flowgraph. */
323 dump_tree_cfg (dump_file
, dump_flags
);
328 /* Do the actions required to initialize internal data structures used
329 in tree-ssa optimization passes. */
332 execute_init_datastructures (void)
334 /* Allocate hash tables, arrays and other structures. */
339 /* Gate: initialize or not the SSA datastructures. */
342 gate_init_datastructures (void)
344 return (optimize
>= 1);
347 struct gimple_opt_pass pass_init_datastructures
=
352 gate_init_datastructures
, /* gate */
353 execute_init_datastructures
, /* execute */
356 0, /* static_pass_number */
358 PROP_cfg
, /* properties_required */
359 0, /* properties_provided */
360 0, /* properties_destroyed */
361 0, /* todo_flags_start */
362 0 /* todo_flags_finish */
367 tree_lowering_passes (tree fn
)
369 tree saved_current_function_decl
= current_function_decl
;
371 current_function_decl
= fn
;
372 push_cfun (DECL_STRUCT_FUNCTION (fn
));
373 tree_register_cfg_hooks ();
374 bitmap_obstack_initialize (NULL
);
375 execute_pass_list (all_lowering_passes
);
376 if (optimize
&& cgraph_global_info_ready
)
377 execute_pass_list (pass_early_local_passes
.pass
.sub
);
378 free_dominance_info (CDI_POST_DOMINATORS
);
379 free_dominance_info (CDI_DOMINATORS
);
381 current_function_decl
= saved_current_function_decl
;
382 bitmap_obstack_release (NULL
);
386 /* For functions-as-trees languages, this performs all optimization and
387 compilation for FNDECL. */
390 tree_rest_of_compilation (tree fndecl
)
392 location_t saved_loc
;
393 struct cgraph_node
*node
;
395 timevar_push (TV_EXPAND
);
397 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
399 node
= cgraph_node (fndecl
);
401 /* Initialize the default bitmap obstack. */
402 bitmap_obstack_initialize (NULL
);
404 /* Initialize the RTL code for the function. */
405 current_function_decl
= fndecl
;
406 saved_loc
= input_location
;
407 input_location
= DECL_SOURCE_LOCATION (fndecl
);
408 init_function_start (fndecl
);
410 /* Even though we're inside a function body, we still don't want to
411 call expand_expr to calculate the size of a variable-sized array.
412 We haven't necessarily assigned RTL to all variables yet, so it's
413 not safe to try to expand expressions involving them. */
414 cfun
->x_dont_save_pending_sizes_p
= 1;
416 tree_register_cfg_hooks ();
418 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
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 DECL_SAVED_TREE (fndecl
) = NULL
;
430 /* If requested, warn about function definitions where the function will
431 return a value (usually of some struct or union type) which itself will
432 take up a lot of stack space. */
433 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
435 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
437 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
438 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
439 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
442 unsigned int size_as_int
443 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
445 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
446 warning (OPT_Wlarger_than_eq
, "size of return value of %q+D is %u bytes",
447 fndecl
, size_as_int
);
449 warning (OPT_Wlarger_than_eq
, "size of return value of %q+D is larger than %wd bytes",
450 fndecl
, larger_than_size
);
454 if (!flag_inline_trees
)
456 DECL_SAVED_TREE (fndecl
) = NULL
;
457 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
458 && !cgraph_node (fndecl
)->origin
)
460 /* Stop pointing to the local nodes about to be freed.
461 But DECL_INITIAL must remain nonzero so we know this
462 was an actual function definition.
463 For a nested function, this is done in c_pop_function_context.
464 If rest_of_compilation set this to 0, leave it 0. */
465 if (DECL_INITIAL (fndecl
) != 0)
466 DECL_INITIAL (fndecl
) = error_mark_node
;
470 input_location
= saved_loc
;
473 timevar_pop (TV_EXPAND
);