1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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"
47 #include "tree-alias-common.h"
52 /* Global variables used to communicate with passes. */
54 bitmap vars_to_rename
;
57 /* The root of the compilation pass tree, once constructed. */
58 static struct tree_opt_pass
*all_passes
;
60 /* Pass: dump the gimplified, inlined, functions. */
62 static struct tree_opt_pass pass_gimple
=
69 0, /* static_pass_number */
71 0, /* properties_required */
72 PROP_gimple_any
, /* properties_provided */
73 0, /* properties_destroyed */
74 0, /* todo_flags_start */
75 TODO_dump_func
/* todo_flags_finish */
78 /* Gate: execute, or not, all of the non-trivial optimizations. */
81 gate_all_optimizations (void)
84 /* Don't bother doing anything if the program has errors. */
85 && !(errorcount
|| sorrycount
));
88 static struct tree_opt_pass pass_all_optimizations
=
91 gate_all_optimizations
, /* gate */
95 0, /* static_pass_number */
97 0, /* properties_required */
98 0, /* properties_provided */
99 0, /* properties_destroyed */
100 0, /* todo_flags_start */
101 0 /* todo_flags_finish */
104 /* Pass: do the actions required to finish with tree-ssa optimization
108 execute_free_datastructures (void)
112 /* ??? This isn't the right place for this. Worse, it got computed
113 more or less at random in various passes. */
114 free_dominance_info (CDI_DOMINATORS
);
116 /* Emit gotos for implicit jumps. */
117 disband_implicit_edges ();
119 /* Remove the ssa structures. Do it here since this includes statement
120 annotations that need to be intact during disband_implicit_edges. */
123 /* Re-chain the statements from the blocks. */
124 chain
= &DECL_SAVED_TREE (current_function_decl
);
125 *chain
= alloc_stmt_list ();
127 /* And get rid of annotations we no longer need. */
128 delete_tree_cfg_annotations ();
131 static struct tree_opt_pass pass_free_datastructures
=
135 execute_free_datastructures
, /* execute */
138 0, /* static_pass_number */
140 PROP_cfg
, /* properties_required */
141 0, /* properties_provided */
142 0, /* properties_destroyed */
143 0, /* todo_flags_start */
144 0 /* todo_flags_finish */
148 /* Do the actions required to initialize internal data structures used
149 in tree-ssa optimization passes. */
152 execute_init_datastructures (void)
154 /* Allocate hash tables, arrays and other structures. */
158 static struct tree_opt_pass pass_init_datastructures
=
162 execute_init_datastructures
, /* execute */
165 0, /* static_pass_number */
167 PROP_cfg
, /* properties_required */
168 0, /* properties_provided */
169 0, /* properties_destroyed */
170 0, /* todo_flags_start */
171 0 /* todo_flags_finish */
174 /* Iterate over the pass tree allocating dump file numbers. We want
175 to do this depth first, and independent of whether the pass is
179 register_one_dump_file (struct tree_opt_pass
*pass
)
181 char *dot_name
, *flag_name
;
187 /* See below in dup_pass_1. */
189 if (pass
->static_pass_number
)
190 sprintf (num
, "%d", ((int) pass
->static_pass_number
< 0
191 ? 1 : pass
->static_pass_number
));
193 dot_name
= concat (".", pass
->name
, num
, NULL
);
194 flag_name
= concat ("tree-", pass
->name
, num
, NULL
);
196 pass
->static_pass_number
= dump_register (dot_name
, flag_name
);
200 register_dump_files (struct tree_opt_pass
*pass
, int properties
)
204 /* Verify that all required properties are present. */
205 if (pass
->properties_required
& ~properties
)
208 if (pass
->properties_destroyed
& pass
->properties_provided
)
211 pass
->properties_required
= properties
;
212 pass
->properties_provided
= properties
=
213 (properties
| pass
->properties_provided
) & ~pass
->properties_destroyed
;
215 if (properties
& PROP_trees
)
216 register_one_dump_file (pass
);
218 properties
= register_dump_files (pass
->sub
, properties
);
226 /* Duplicate a pass that's to be run more than once. */
228 static struct tree_opt_pass
*
229 dup_pass_1 (struct tree_opt_pass
*pass
)
231 struct tree_opt_pass
*new;
233 new = xmalloc (sizeof (*new));
234 memcpy (new, pass
, sizeof (*new));
236 /* Indicate to register_dump_files that this pass has duplicates,
237 and so it should rename the dump file. The first instance will
238 be < 0, and be number of duplicates = -static_pass_number + 1.
239 Subsequent instances will be > 0 and just the duplicate number. */
242 int n
, p
= pass
->static_pass_number
;
249 pass
->static_pass_number
= p
;
250 new->static_pass_number
= n
;
256 /* Construct the pass tree. */
259 init_tree_optimization_passes (void)
261 struct tree_opt_pass
**p
;
263 #define NEXT_PASS(PASS) (*p = &PASS, p = &(*p)->next)
264 #define DUP_PASS(PASS) (*dup_pass_1 (&PASS))
267 NEXT_PASS (pass_gimple
);
268 NEXT_PASS (pass_remove_useless_stmts
);
269 NEXT_PASS (pass_mudflap_1
);
270 NEXT_PASS (pass_lower_cf
);
271 NEXT_PASS (pass_lower_eh
);
272 NEXT_PASS (pass_build_cfg
);
273 NEXT_PASS (pass_tree_profile
);
274 NEXT_PASS (pass_init_datastructures
);
275 NEXT_PASS (pass_all_optimizations
);
276 NEXT_PASS (pass_mudflap_2
);
277 NEXT_PASS (pass_free_datastructures
);
278 NEXT_PASS (pass_expand
);
279 NEXT_PASS (pass_rest_of_compilation
);
282 p
= &pass_all_optimizations
.sub
;
283 NEXT_PASS (pass_referenced_vars
);
284 NEXT_PASS (pass_build_pta
);
285 NEXT_PASS (pass_build_ssa
);
286 NEXT_PASS (pass_rename_ssa_copies
);
287 NEXT_PASS (pass_early_warn_uninitialized
);
288 NEXT_PASS (pass_dce
);
289 NEXT_PASS (pass_dominator
);
290 NEXT_PASS (pass_redundant_phi
);
291 NEXT_PASS (DUP_PASS (pass_dce
));
292 NEXT_PASS (pass_forwprop
);
293 NEXT_PASS (pass_phiopt
);
294 NEXT_PASS (pass_may_alias
);
295 NEXT_PASS (pass_tail_recursion
);
297 NEXT_PASS (pass_profile
);
298 NEXT_PASS (pass_lower_complex
);
299 NEXT_PASS (pass_sra
);
300 NEXT_PASS (DUP_PASS (pass_rename_ssa_copies
));
301 NEXT_PASS (DUP_PASS (pass_dominator
));
302 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
303 NEXT_PASS (DUP_PASS (pass_dce
));
304 NEXT_PASS (pass_dse
);
305 NEXT_PASS (DUP_PASS (pass_may_alias
));
306 NEXT_PASS (DUP_PASS (pass_forwprop
));
307 NEXT_PASS (DUP_PASS (pass_phiopt
));
308 NEXT_PASS (pass_ccp
);
309 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
310 NEXT_PASS (pass_fold_builtins
);
311 NEXT_PASS (pass_split_crit_edges
);
312 NEXT_PASS (pass_pre
);
313 NEXT_PASS (pass_loop
);
314 NEXT_PASS (DUP_PASS (pass_dominator
));
315 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
316 NEXT_PASS (pass_cd_dce
);
317 NEXT_PASS (DUP_PASS (pass_dse
));
318 NEXT_PASS (DUP_PASS (pass_forwprop
));
319 NEXT_PASS (DUP_PASS (pass_phiopt
));
320 NEXT_PASS (pass_tail_calls
);
321 NEXT_PASS (pass_late_warn_uninitialized
);
322 NEXT_PASS (pass_warn_function_return
);
323 NEXT_PASS (pass_del_pta
);
324 NEXT_PASS (pass_del_ssa
);
325 NEXT_PASS (pass_nrv
);
326 NEXT_PASS (pass_remove_useless_vars
);
330 NEXT_PASS (pass_loop_init
);
331 NEXT_PASS (pass_lim
);
332 NEXT_PASS (pass_loop_done
);
338 /* Register the passes with the tree dump code. */
339 register_dump_files (all_passes
, 0);
342 static void execute_pass_list (struct tree_opt_pass
*);
344 static unsigned int last_verified
;
347 execute_todo (unsigned int flags
)
349 if (flags
& TODO_rename_vars
)
351 rewrite_into_ssa (false);
352 bitmap_clear (vars_to_rename
);
355 if ((flags
& TODO_dump_func
) && dump_file
)
356 dump_function_to_file (current_function_decl
,
357 dump_file
, dump_flags
);
359 if (flags
& TODO_ggc_collect
)
362 #ifdef ENABLE_CHECKING
363 if (flags
& TODO_verify_ssa
)
365 if (flags
& TODO_verify_flow
)
367 if (flags
& TODO_verify_stmts
)
373 execute_one_pass (struct tree_opt_pass
*pass
)
377 /* See if we're supposed to run this pass. */
378 if (pass
->gate
&& !pass
->gate ())
381 /* Note that the folders should only create gimple expressions.
382 This is a hack until the new folder is ready. */
383 in_gimple_form
= (pass
->properties_provided
& PROP_trees
) != 0;
385 /* Run pre-pass verification. */
386 todo
= pass
->todo_flags_start
& ~last_verified
;
390 /* If a dump file name is present, open it if enabled. */
391 if (pass
->static_pass_number
)
393 dump_file
= dump_begin (pass
->static_pass_number
, &dump_flags
);
396 const char *dname
, *aname
;
397 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
398 aname
= (IDENTIFIER_POINTER
399 (DECL_ASSEMBLER_NAME (current_function_decl
)));
400 fprintf (dump_file
, "\n;; Function %s (%s)\n\n", dname
, aname
);
404 /* If a timevar is present, start it. */
406 timevar_push (pass
->tv_id
);
412 /* Run post-pass cleanup and verification. */
413 todo
= pass
->todo_flags_finish
;
414 last_verified
= todo
& TODO_verify_all
;
418 /* Close down timevar and dump file. */
420 timevar_pop (pass
->tv_id
);
423 dump_end (pass
->static_pass_number
, dump_file
);
431 execute_pass_list (struct tree_opt_pass
*pass
)
435 if (execute_one_pass (pass
) && pass
->sub
)
436 execute_pass_list (pass
->sub
);
443 /* For functions-as-trees languages, this performs all optimization and
444 compilation for FNDECL. */
447 tree_rest_of_compilation (tree fndecl
, bool nested_p
)
449 location_t saved_loc
;
450 struct cgraph_node
*saved_node
= NULL
, *node
;
452 timevar_push (TV_EXPAND
);
454 if (flag_unit_at_a_time
&& !cgraph_global_info_ready
)
457 /* Initialize the RTL code for the function. */
458 current_function_decl
= fndecl
;
459 saved_loc
= input_location
;
460 input_location
= DECL_SOURCE_LOCATION (fndecl
);
461 init_function_start (fndecl
);
463 /* Even though we're inside a function body, we still don't want to
464 call expand_expr to calculate the size of a variable-sized array.
465 We haven't necessarily assigned RTL to all variables yet, so it's
466 not safe to try to expand expressions involving them. */
467 cfun
->x_dont_save_pending_sizes_p
= 1;
469 node
= cgraph_node (fndecl
);
471 /* We might need the body of this function so that we can expand
472 it inline somewhere else. This means not lowering some constructs
473 such as exception handling. */
474 if (cgraph_preserve_function_body_p (fndecl
))
476 if (!flag_unit_at_a_time
)
478 struct cgraph_edge
*e
;
480 saved_node
= cgraph_clone_node (node
);
481 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
482 if (!e
->inline_failed
)
483 cgraph_clone_inlined_nodes (e
, true);
485 cfun
->saved_tree
= save_body (fndecl
, &cfun
->saved_args
);
488 if (flag_inline_trees
)
490 struct cgraph_edge
*e
;
491 for (e
= node
->callees
; e
; e
= e
->next_callee
)
492 if (!e
->inline_failed
|| warn_inline
)
496 timevar_push (TV_INTEGRATION
);
497 optimize_inline_calls (fndecl
);
498 timevar_pop (TV_INTEGRATION
);
503 vars_to_rename
= BITMAP_XMALLOC ();
505 /* If this is a nested function, protect the local variables in the stack
506 above us from being collected while we're compiling this function. */
510 /* Perform all tree transforms and optimizations. */
511 execute_pass_list (all_passes
);
513 /* Restore original body if still needed. */
514 if (cfun
->saved_tree
)
516 DECL_SAVED_TREE (fndecl
) = cfun
->saved_tree
;
517 DECL_ARGUMENTS (fndecl
) = cfun
->saved_args
;
519 /* When not in unit-at-a-time mode, we must preserve out of line copy
520 representing node before inlining. Restore original outgoing edges
521 using clone we created earlier. */
522 if (!flag_unit_at_a_time
)
524 struct cgraph_edge
*e
;
525 while (node
->callees
)
526 cgraph_remove_edge (node
->callees
);
527 node
->callees
= saved_node
->callees
;
528 saved_node
->callees
= NULL
;
529 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
531 cgraph_remove_node (saved_node
);
535 DECL_SAVED_TREE (fndecl
) = NULL
;
538 /* If requested, warn about function definitions where the function will
539 return a value (usually of some struct or union type) which itself will
540 take up a lot of stack space. */
541 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
543 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
545 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
546 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
547 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
550 unsigned int size_as_int
551 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
553 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
554 warning ("%Jsize of return value of '%D' is %u bytes",
555 fndecl
, fndecl
, size_as_int
);
557 warning ("%Jsize of return value of '%D' is larger than %wd bytes",
558 fndecl
, fndecl
, larger_than_size
);
562 if (!nested_p
&& !flag_inline_trees
)
564 DECL_SAVED_TREE (fndecl
) = NULL
;
565 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
566 && !cgraph_node (fndecl
)->origin
)
568 /* Stop pointing to the local nodes about to be freed.
569 But DECL_INITIAL must remain nonzero so we know this
570 was an actual function definition.
571 For a nested function, this is done in c_pop_function_context.
572 If rest_of_compilation set this to 0, leave it 0. */
573 if (DECL_INITIAL (fndecl
) != 0)
574 DECL_INITIAL (fndecl
) = error_mark_node
;
578 input_location
= saved_loc
;
582 /* Undo the GC context switch. */
585 timevar_pop (TV_EXPAND
);