1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
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"
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
) || gimple_in_ssa_p (cfun
)));
65 struct gimple_opt_pass pass_all_optimizations
=
70 gate_all_optimizations
, /* gate */
74 0, /* static_pass_number */
76 0, /* properties_required */
77 0, /* properties_provided */
78 0, /* properties_destroyed */
79 0, /* todo_flags_start */
80 0 /* todo_flags_finish */
84 /* Gate: execute, or not, all of the non-trivial optimizations. */
87 gate_all_early_local_passes (void)
89 /* Don't bother doing anything if the program has errors. */
90 return (!errorcount
&& !sorrycount
);
93 struct simple_ipa_opt_pass pass_early_local_passes
=
97 "early_local_cleanups", /* name */
98 gate_all_early_local_passes
, /* gate */
102 0, /* static_pass_number */
104 0, /* properties_required */
105 0, /* properties_provided */
106 0, /* properties_destroyed */
107 0, /* todo_flags_start */
108 TODO_remove_functions
/* todo_flags_finish */
113 execute_early_local_optimizations (void)
115 /* First time we start with early optimization we need to advance
116 cgraph state so newly inserted functions are also early optimized.
117 However we execute early local optimizations for lately inserted
118 functions, in that case don't reset cgraph state back to IPA_SSA. */
119 if (cgraph_state
< CGRAPH_STATE_IPA_SSA
)
120 cgraph_state
= CGRAPH_STATE_IPA_SSA
;
124 /* Gate: execute, or not, all of the non-trivial optimizations. */
127 gate_all_early_optimizations (void)
129 return (optimize
>= 1
130 /* Don't bother doing anything if the program has errors. */
131 && !(errorcount
|| sorrycount
));
134 struct gimple_opt_pass pass_all_early_optimizations
=
138 "early_optimizations", /* name */
139 gate_all_early_optimizations
, /* gate */
140 execute_early_local_optimizations
, /* execute */
143 0, /* static_pass_number */
145 0, /* properties_required */
146 0, /* properties_provided */
147 0, /* properties_destroyed */
148 0, /* todo_flags_start */
149 0 /* todo_flags_finish */
153 /* Pass: cleanup the CFG just before expanding trees to RTL.
154 This is just a round of label cleanups and case node grouping
155 because after the tree optimizers have run such cleanups may
159 execute_cleanup_cfg_pre_ipa (void)
165 struct gimple_opt_pass pass_cleanup_cfg
=
169 "cleanup_cfg", /* name */
171 execute_cleanup_cfg_pre_ipa
, /* execute */
174 0, /* static_pass_number */
176 PROP_cfg
, /* properties_required */
177 0, /* properties_provided */
178 0, /* properties_destroyed */
179 0, /* todo_flags_start */
180 TODO_dump_func
/* todo_flags_finish */
185 /* Pass: cleanup the CFG just before expanding trees to RTL.
186 This is just a round of label cleanups and case node grouping
187 because after the tree optimizers have run such cleanups may
191 execute_cleanup_cfg_post_optimizing (void)
193 fold_cond_expr_cond ();
195 cleanup_dead_labels ();
196 group_case_labels ();
200 struct gimple_opt_pass pass_cleanup_cfg_post_optimizing
=
204 "optimized", /* name */
206 execute_cleanup_cfg_post_optimizing
, /* execute */
209 0, /* static_pass_number */
211 PROP_cfg
, /* properties_required */
212 0, /* properties_provided */
213 0, /* properties_destroyed */
214 0, /* todo_flags_start */
215 TODO_dump_func
/* todo_flags_finish */
216 | TODO_remove_unused_locals
220 /* Pass: do the actions required to finish with tree-ssa optimization
224 execute_free_datastructures (void)
226 free_dominance_info (CDI_DOMINATORS
);
227 free_dominance_info (CDI_POST_DOMINATORS
);
229 /* Remove the ssa structures. */
233 /* And get rid of annotations we no longer need. */
234 delete_tree_cfg_annotations ();
239 /* Pass: fixup_cfg. IPA passes, compilation of earlier functions or inlining
240 might have changed some properties, such as marked functions nothrow.
241 Remove redundant edges and basic blocks, and create new ones if necessary.
243 This pass can't be executed as stand alone pass from pass manager, because
244 in between inlining and this fixup the verify_flow_info would fail. */
247 execute_fixup_cfg (void)
250 gimple_stmt_iterator gsi
;
251 int todo
= gimple_in_ssa_p (cfun
) ? TODO_verify_ssa
: 0;
256 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
258 gimple stmt
= gsi_stmt (gsi
);
259 tree decl
= is_gimple_call (stmt
)
260 ? gimple_call_fndecl (stmt
)
264 && gimple_call_flags (stmt
) & (ECF_CONST
266 | ECF_LOOPING_CONST_OR_PURE
))
268 if (gimple_in_ssa_p (cfun
))
270 todo
|= TODO_update_ssa
| TODO_cleanup_cfg
;
271 mark_symbols_for_renaming (stmt
);
276 if (!stmt_could_throw_p (stmt
) && lookup_stmt_eh_region (stmt
))
277 remove_stmt_from_eh_region (stmt
);
280 if (gimple_purge_dead_eh_edges (bb
))
281 todo
|= TODO_cleanup_cfg
;
284 /* Dump a textual representation of the flowgraph. */
286 gimple_dump_cfg (dump_file
, dump_flags
);
291 struct gimple_opt_pass pass_fixup_cfg
=
297 execute_fixup_cfg
, /* execute */
300 0, /* static_pass_number */
302 PROP_cfg
, /* properties_required */
303 0, /* properties_provided */
304 0, /* properties_destroyed */
305 0, /* todo_flags_start */
306 0 /* todo_flags_finish */
310 /* Do the actions required to initialize internal data structures used
311 in tree-ssa optimization passes. */
314 execute_init_datastructures (void)
316 /* Allocate hash tables, arrays and other structures. */
317 init_tree_ssa (cfun
);
321 struct gimple_opt_pass pass_init_datastructures
=
327 execute_init_datastructures
, /* 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 */
341 tree_lowering_passes (tree fn
)
343 tree saved_current_function_decl
= current_function_decl
;
345 current_function_decl
= fn
;
346 push_cfun (DECL_STRUCT_FUNCTION (fn
));
347 gimple_register_cfg_hooks ();
348 bitmap_obstack_initialize (NULL
);
349 execute_pass_list (all_lowering_passes
);
350 if (optimize
&& cgraph_global_info_ready
)
351 execute_pass_list (pass_early_local_passes
.pass
.sub
);
352 free_dominance_info (CDI_POST_DOMINATORS
);
353 free_dominance_info (CDI_DOMINATORS
);
355 current_function_decl
= saved_current_function_decl
;
356 bitmap_obstack_release (NULL
);
360 /* For functions-as-trees languages, this performs all optimization and
361 compilation for FNDECL. */
364 tree_rest_of_compilation (tree fndecl
)
366 location_t saved_loc
;
367 struct cgraph_node
*node
;
369 timevar_push (TV_EXPAND
);
371 gcc_assert (cgraph_global_info_ready
);
373 node
= cgraph_node (fndecl
);
375 /* Initialize the default bitmap obstack. */
376 bitmap_obstack_initialize (NULL
);
378 /* Initialize the RTL code for the function. */
379 current_function_decl
= fndecl
;
380 saved_loc
= input_location
;
381 input_location
= DECL_SOURCE_LOCATION (fndecl
);
382 init_function_start (fndecl
);
384 /* Even though we're inside a function body, we still don't want to
385 call expand_expr to calculate the size of a variable-sized array.
386 We haven't necessarily assigned RTL to all variables yet, so it's
387 not safe to try to expand expressions involving them. */
388 cfun
->dont_save_pending_sizes_p
= 1;
390 gimple_register_cfg_hooks ();
392 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
393 /* Perform all tree transforms and optimizations. */
394 execute_pass_list (all_passes
);
396 bitmap_obstack_release (®_obstack
);
398 /* Release the default bitmap obstack. */
399 bitmap_obstack_release (NULL
);
403 /* If requested, warn about function definitions where the function will
404 return a value (usually of some struct or union type) which itself will
405 take up a lot of stack space. */
406 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
408 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
410 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
411 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
412 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
415 unsigned int size_as_int
416 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
418 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
419 warning (OPT_Wlarger_than_eq
, "size of return value of %q+D is %u bytes",
420 fndecl
, size_as_int
);
422 warning (OPT_Wlarger_than_eq
, "size of return value of %q+D is larger than %wd bytes",
423 fndecl
, larger_than_size
);
427 gimple_set_body (fndecl
, NULL
);
428 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
429 && !cgraph_node (fndecl
)->origin
)
431 /* Stop pointing to the local nodes about to be freed.
432 But DECL_INITIAL must remain nonzero so we know this
433 was an actual function definition.
434 For a nested function, this is done in c_pop_function_context.
435 If rest_of_compilation set this to 0, leave it 0. */
436 if (DECL_INITIAL (fndecl
) != 0)
437 DECL_INITIAL (fndecl
) = error_mark_node
;
440 input_location
= saved_loc
;
443 timevar_pop (TV_EXPAND
);