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: gimplify the function if it's not been done. */
65 /* We have this test here rather than as the gate because we always
66 want to dump the original gimplified function. */
67 if (!lang_hooks
.gimple_before_inlining
)
68 gimplify_function_tree (current_function_decl
);
71 static struct tree_opt_pass pass_gimple
=
75 execute_gimple
, /* execute */
78 0, /* static_pass_number */
80 0, /* properties_required */
81 PROP_gimple_any
, /* properties_provided */
82 0, /* properties_destroyed */
83 0, /* todo_flags_start */
84 TODO_dump_func
/* todo_flags_finish */
87 /* Pass: replace the outermost BIND_EXPR. We removed all of them while
88 optimizing, but the tree->rtl expander requires it. */
91 execute_rebuild_bind (void)
93 DECL_SAVED_TREE (current_function_decl
)
94 = build (BIND_EXPR
, void_type_node
, NULL_TREE
,
95 DECL_SAVED_TREE (current_function_decl
), NULL_TREE
);
98 static struct tree_opt_pass pass_rebuild_bind
=
102 execute_rebuild_bind
, /* execute */
105 0, /* static_pass_number */
107 0, /* properties_required */
108 0, /* properties_provided */
109 0, /* properties_destroyed */
110 0, /* todo_flags_start */
111 0 /* todo_flags_finish */
114 /* Gate: execute, or not, all of the non-trivial optimizations. */
117 gate_all_optimizations (void)
119 return (optimize
>= 1
120 /* Don't bother doing anything if the program has errors. */
121 && !(errorcount
|| sorrycount
));
124 static struct tree_opt_pass pass_all_optimizations
=
127 gate_all_optimizations
, /* gate */
131 0, /* static_pass_number */
133 0, /* properties_required */
134 0, /* properties_provided */
135 0, /* properties_destroyed */
136 0, /* todo_flags_start */
137 0 /* todo_flags_finish */
140 /* Pass: do the actions required to finish with tree-ssa optimization
144 execute_del_cfg (void)
149 /* ??? This isn't the right place for this. Worse, it got computed
150 more or less at random in various passes. */
151 free_dominance_info (CDI_DOMINATORS
);
153 /* Emit gotos for implicit jumps. */
154 disband_implicit_edges ();
156 /* Remove the ssa structures. Do it here since this includes statement
157 annotations that need to be intact during disband_implicit_edges. */
160 /* Re-chain the statements from the blocks. */
161 chain
= &DECL_SAVED_TREE (current_function_decl
);
162 *chain
= alloc_stmt_list ();
165 append_to_statement_list_force (bb
->stmt_list
, chain
);
168 /* And get rid of the cfg. */
172 static struct tree_opt_pass pass_del_cfg
=
176 execute_del_cfg
, /* execute */
179 0, /* static_pass_number */
181 PROP_cfg
, /* properties_required */
182 0, /* properties_provided */
183 PROP_cfg
, /* properties_destroyed */
184 0, /* todo_flags_start */
185 0 /* todo_flags_finish */
188 /* Iterate over the pass tree allocating dump file numbers. We want
189 to do this depth first, and independent of whether the pass is
193 register_one_dump_file (struct tree_opt_pass
*pass
)
195 char *dot_name
, *flag_name
;
201 /* See below in dup_pass_1. */
203 if (pass
->static_pass_number
)
204 sprintf (num
, "%d", ((int) pass
->static_pass_number
< 0
205 ? 1 : pass
->static_pass_number
));
207 dot_name
= concat (".", pass
->name
, num
, NULL
);
208 flag_name
= concat ("tree-", pass
->name
, num
, NULL
);
210 pass
->static_pass_number
= dump_register (dot_name
, flag_name
);
214 register_dump_files (struct tree_opt_pass
*pass
, int properties
)
218 /* Verify that all required properties are present. */
219 if (pass
->properties_required
& ~properties
)
222 if (pass
->properties_destroyed
& pass
->properties_provided
)
225 pass
->properties_required
= properties
;
226 pass
->properties_provided
= properties
=
227 (properties
| pass
->properties_provided
) & ~pass
->properties_destroyed
;
229 if (properties
& PROP_trees
)
230 register_one_dump_file (pass
);
232 properties
= register_dump_files (pass
->sub
, properties
);
240 /* Duplicate a pass that's to be run more than once. */
242 static struct tree_opt_pass
*
243 dup_pass_1 (struct tree_opt_pass
*pass
)
245 struct tree_opt_pass
*new;
247 new = xmalloc (sizeof (*new));
248 memcpy (new, pass
, sizeof (*new));
250 /* Indicate to register_dump_files that this pass has duplicates,
251 and so it should rename the dump file. The first instance will
252 be < 0, and be number of duplicates = -static_pass_number + 1.
253 Subsequent instances will be > 0 and just the duplicate number. */
256 int n
, p
= pass
->static_pass_number
;
263 pass
->static_pass_number
= p
;
264 new->static_pass_number
= n
;
270 /* Construct the pass tree. */
273 init_tree_optimization_passes (void)
275 struct tree_opt_pass
**p
;
277 #define NEXT_PASS(PASS) (*p = &PASS, p = &(*p)->next)
278 #define DUP_PASS(PASS) (*dup_pass_1 (&PASS))
281 NEXT_PASS (pass_gimple
);
282 NEXT_PASS (pass_remove_useless_stmts
);
283 NEXT_PASS (pass_mudflap_1
);
284 NEXT_PASS (pass_lower_cf
);
285 NEXT_PASS (pass_lower_eh
);
286 NEXT_PASS (pass_all_optimizations
);
287 NEXT_PASS (pass_mudflap_2
);
288 NEXT_PASS (pass_rebuild_bind
);
289 NEXT_PASS (pass_expand
);
290 NEXT_PASS (pass_rest_of_compilation
);
293 p
= &pass_all_optimizations
.sub
;
294 NEXT_PASS (pass_build_cfg
);
295 NEXT_PASS (pass_tree_profile
);
296 NEXT_PASS (pass_referenced_vars
);
297 NEXT_PASS (pass_build_pta
);
298 NEXT_PASS (pass_build_ssa
);
299 NEXT_PASS (pass_rename_ssa_copies
);
300 NEXT_PASS (pass_early_warn_uninitialized
);
301 NEXT_PASS (pass_dce
);
302 NEXT_PASS (pass_dominator
);
303 NEXT_PASS (pass_redundant_phi
);
304 NEXT_PASS (DUP_PASS (pass_dce
));
305 NEXT_PASS (pass_forwprop
);
306 NEXT_PASS (pass_phiopt
);
307 NEXT_PASS (pass_may_alias
);
308 NEXT_PASS (pass_tail_recursion
);
310 NEXT_PASS (pass_del_pta
);
311 NEXT_PASS (pass_profile
);
312 NEXT_PASS (pass_lower_complex
);
313 NEXT_PASS (pass_sra
);
314 NEXT_PASS (DUP_PASS (pass_rename_ssa_copies
));
315 NEXT_PASS (DUP_PASS (pass_dominator
));
316 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
317 NEXT_PASS (DUP_PASS (pass_dce
));
318 NEXT_PASS (pass_dse
);
319 NEXT_PASS (DUP_PASS (pass_forwprop
));
320 NEXT_PASS (DUP_PASS (pass_phiopt
));
321 NEXT_PASS (pass_ccp
);
322 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
323 NEXT_PASS (pass_fold_builtins
);
324 NEXT_PASS (pass_split_crit_edges
);
325 NEXT_PASS (pass_pre
);
326 NEXT_PASS (DUP_PASS (pass_dominator
));
327 NEXT_PASS (DUP_PASS (pass_redundant_phi
));
328 NEXT_PASS (pass_cd_dce
);
329 NEXT_PASS (DUP_PASS (pass_dse
));
330 NEXT_PASS (DUP_PASS (pass_forwprop
));
331 NEXT_PASS (DUP_PASS (pass_phiopt
));
332 NEXT_PASS (pass_tail_calls
);
333 NEXT_PASS (pass_late_warn_uninitialized
);
334 NEXT_PASS (pass_warn_function_return
);
335 NEXT_PASS (pass_del_ssa
);
336 NEXT_PASS (pass_nrv
);
337 NEXT_PASS (pass_remove_useless_vars
);
338 NEXT_PASS (pass_del_cfg
);
344 /* Register the passes with the tree dump code. */
345 register_dump_files (all_passes
, 0);
348 static void execute_pass_list (struct tree_opt_pass
*);
350 static unsigned int last_verified
;
353 execute_todo (unsigned int flags
)
355 if (flags
& TODO_rename_vars
)
357 if (bitmap_first_set_bit (vars_to_rename
) >= 0)
359 BITMAP_XFREE (vars_to_rename
);
362 if ((flags
& TODO_dump_func
) && dump_file
)
363 dump_function_to_file (current_function_decl
,
364 dump_file
, dump_flags
);
366 if (flags
& TODO_ggc_collect
)
369 #ifdef ENABLE_CHECKING
370 if (flags
& TODO_verify_ssa
)
372 if (flags
& TODO_verify_flow
)
374 if (flags
& TODO_verify_stmts
)
380 execute_one_pass (struct tree_opt_pass
*pass
)
384 /* See if we're supposed to run this pass. */
385 if (pass
->gate
&& !pass
->gate ())
388 /* Note that the folders should only create gimple expressions.
389 This is a hack until the new folder is ready. */
390 in_gimple_form
= (pass
->properties_provided
& PROP_trees
) != 0;
392 /* Run pre-pass verification. */
393 todo
= pass
->todo_flags_start
& ~last_verified
;
397 /* If a dump file name is present, open it if enabled. */
398 if (pass
->static_pass_number
)
400 dump_file
= dump_begin (pass
->static_pass_number
, &dump_flags
);
403 const char *dname
, *aname
;
404 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
405 aname
= (IDENTIFIER_POINTER
406 (DECL_ASSEMBLER_NAME (current_function_decl
)));
407 fprintf (dump_file
, "\n;; Function %s (%s)\n\n", dname
, aname
);
411 /* If a timevar is present, start it. */
413 timevar_push (pass
->tv_id
);
415 /* If the pass is requesting ssa variable renaming, allocate the bitmap. */
416 if (pass
->todo_flags_finish
& TODO_rename_vars
)
417 vars_to_rename
= BITMAP_XMALLOC ();
423 /* Run post-pass cleanup and verification. */
424 todo
= pass
->todo_flags_finish
;
425 last_verified
= todo
& TODO_verify_all
;
429 /* Close down timevar and dump file. */
431 timevar_pop (pass
->tv_id
);
434 dump_end (pass
->static_pass_number
, dump_file
);
442 execute_pass_list (struct tree_opt_pass
*pass
)
446 if (execute_one_pass (pass
) && pass
->sub
)
447 execute_pass_list (pass
->sub
);
454 /* For functions-as-trees languages, this performs all optimization and
455 compilation for FNDECL. */
458 tree_rest_of_compilation (tree fndecl
, bool nested_p
)
460 location_t saved_loc
;
461 struct cgraph_node
*saved_node
= NULL
, *node
;
463 timevar_push (TV_EXPAND
);
465 if (flag_unit_at_a_time
&& !cgraph_global_info_ready
)
468 /* Initialize the RTL code for the function. */
469 current_function_decl
= fndecl
;
470 saved_loc
= input_location
;
471 input_location
= DECL_SOURCE_LOCATION (fndecl
);
472 init_function_start (fndecl
);
474 /* This function is being processed in whole-function mode. */
475 cfun
->x_whole_function_mode_p
= 1;
477 /* Even though we're inside a function body, we still don't want to
478 call expand_expr to calculate the size of a variable-sized array.
479 We haven't necessarily assigned RTL to all variables yet, so it's
480 not safe to try to expand expressions involving them. */
481 immediate_size_expand
= 0;
482 cfun
->x_dont_save_pending_sizes_p
= 1;
484 node
= cgraph_node (fndecl
);
486 /* We might need the body of this function so that we can expand
487 it inline somewhere else. This means not lowering some constructs
488 such as exception handling. */
489 if (cgraph_preserve_function_body_p (fndecl
))
491 if (!flag_unit_at_a_time
)
493 struct cgraph_edge
*e
;
495 saved_node
= cgraph_clone_node (node
);
496 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
497 if (!e
->inline_failed
)
498 cgraph_clone_inlined_nodes (e
, true);
500 cfun
->saved_tree
= save_body (fndecl
, &cfun
->saved_args
);
503 if (flag_inline_trees
)
505 struct cgraph_edge
*e
;
506 for (e
= node
->callees
; e
; e
= e
->next_callee
)
507 if (!e
->inline_failed
|| warn_inline
)
511 timevar_push (TV_INTEGRATION
);
512 optimize_inline_calls (fndecl
);
513 timevar_pop (TV_INTEGRATION
);
517 /* If this is a nested function, protect the local variables in the stack
518 above us from being collected while we're compiling this function. */
522 /* Perform all tree transforms and optimizations. */
523 execute_pass_list (all_passes
);
525 /* Restore original body if still needed. */
526 if (cfun
->saved_tree
)
528 DECL_SAVED_TREE (fndecl
) = cfun
->saved_tree
;
529 DECL_ARGUMENTS (fndecl
) = cfun
->saved_args
;
531 /* When not in unit-at-a-time mode, we must preserve out of line copy
532 representing node before inlining. Restore original outgoing edges
533 using clone we created earlier. */
534 if (!flag_unit_at_a_time
)
536 struct cgraph_edge
*e
;
537 while (node
->callees
)
538 cgraph_remove_edge (node
->callees
);
539 node
->callees
= saved_node
->callees
;
540 saved_node
->callees
= NULL
;
541 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
543 cgraph_remove_node (saved_node
);
547 DECL_SAVED_TREE (fndecl
) = NULL
;
550 /* If requested, warn about function definitions where the function will
551 return a value (usually of some struct or union type) which itself will
552 take up a lot of stack space. */
553 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
555 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
557 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
558 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
559 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
562 unsigned int size_as_int
563 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
565 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
566 warning ("%Jsize of return value of '%D' is %u bytes",
567 fndecl
, fndecl
, size_as_int
);
569 warning ("%Jsize of return value of '%D' is larger than %wd bytes",
570 fndecl
, fndecl
, larger_than_size
);
574 if (!nested_p
&& !flag_inline_trees
)
576 DECL_SAVED_TREE (fndecl
) = NULL
;
577 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
578 && !cgraph_node (fndecl
)->origin
)
580 /* Stop pointing to the local nodes about to be freed.
581 But DECL_INITIAL must remain nonzero so we know this
582 was an actual function definition.
583 For a nested function, this is done in c_pop_function_context.
584 If rest_of_compilation set this to 0, leave it 0. */
585 if (DECL_INITIAL (fndecl
) != 0)
586 DECL_INITIAL (fndecl
) = error_mark_node
;
590 input_location
= saved_loc
;
594 /* Undo the GC context switch. */
597 timevar_pop (TV_EXPAND
);