* random.c (random_seed): Use correct variable.
[official-gcc.git] / gcc / tree-optimize.c
blobee03a98f1d69fd5eecde6e0eb53bdd3c3170bc56
1 /* Control and data flow functions for trees.
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)
10 any later version.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
35 #include "flags.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "timevar.h"
39 #include "function.h"
40 #include "langhooks.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
47 #include "tree-alias-common.h"
48 #include "ggc.h"
49 #include "cgraph.h"
52 /* Global variables used to communicate with passes. */
53 int dump_flags;
54 bitmap vars_to_rename;
55 bool in_gimple_form;
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. */
62 static void
63 execute_gimple (void)
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 =
73 "gimple", /* name */
74 NULL, /* gate */
75 execute_gimple, /* execute */
76 NULL, /* sub */
77 NULL, /* next */
78 0, /* static_pass_number */
79 0, /* tv_id */
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. */
90 static void
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 =
100 NULL, /* name */
101 NULL, /* gate */
102 execute_rebuild_bind, /* execute */
103 NULL, /* sub */
104 NULL, /* next */
105 0, /* static_pass_number */
106 0, /* tv_id */
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. */
116 static bool
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 =
126 NULL, /* name */
127 gate_all_optimizations, /* gate */
128 NULL, /* execute */
129 NULL, /* sub */
130 NULL, /* next */
131 0, /* static_pass_number */
132 0, /* tv_id */
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
141 passes. */
143 static void
144 execute_del_cfg (void)
146 basic_block bb;
147 tree *chain;
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. */
158 delete_tree_ssa ();
160 /* Re-chain the statements from the blocks. */
161 chain = &DECL_SAVED_TREE (current_function_decl);
162 *chain = alloc_stmt_list ();
163 FOR_EACH_BB (bb)
165 append_to_statement_list_force (bb->stmt_list, chain);
168 /* And get rid of the cfg. */
169 delete_tree_cfg ();
172 static struct tree_opt_pass pass_del_cfg =
174 NULL, /* name */
175 NULL, /* gate */
176 execute_del_cfg, /* execute */
177 NULL, /* sub */
178 NULL, /* next */
179 0, /* static_pass_number */
180 0, /* tv_id */
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
190 enabled or not. */
192 static void
193 register_one_dump_file (struct tree_opt_pass *pass)
195 char *dot_name, *flag_name;
196 char num[10];
198 if (!pass->name)
199 return;
201 /* See below in dup_pass_1. */
202 num[0] = '\0';
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);
213 static void
214 register_dump_files (struct tree_opt_pass *pass)
218 register_one_dump_file (pass);
219 if (pass->sub)
220 register_dump_files (pass->sub);
221 pass = pass->next;
223 while (pass);
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. */
240 if (pass->name)
242 int n, p = pass->static_pass_number;
244 if (p)
245 n = -(--p) + 1;
246 else
247 n = 2, p = -1;
249 pass->static_pass_number = p;
250 new->static_pass_number = n;
253 return new;
256 /* Construct the pass tree. */
258 void
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))
266 p = &all_passes;
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_all_optimizations);
273 NEXT_PASS (pass_mudflap_2);
274 NEXT_PASS (pass_rebuild_bind);
275 *p = NULL;
277 p = &pass_all_optimizations.sub;
278 NEXT_PASS (pass_build_cfg);
279 NEXT_PASS (pass_tree_profile);
280 NEXT_PASS (pass_referenced_vars);
281 NEXT_PASS (pass_build_pta);
282 NEXT_PASS (pass_build_ssa);
283 NEXT_PASS (pass_rename_ssa_copies);
284 NEXT_PASS (pass_early_warn_uninitialized);
285 NEXT_PASS (pass_dce);
286 NEXT_PASS (pass_dominator);
287 NEXT_PASS (pass_redundant_phi);
288 NEXT_PASS (DUP_PASS (pass_dce));
289 NEXT_PASS (pass_forwprop);
290 NEXT_PASS (pass_phiopt);
291 NEXT_PASS (pass_may_alias);
292 NEXT_PASS (pass_tail_recursion);
293 NEXT_PASS (pass_ch);
294 NEXT_PASS (pass_del_pta);
295 NEXT_PASS (pass_profile);
296 NEXT_PASS (pass_lower_complex);
297 NEXT_PASS (pass_sra);
298 NEXT_PASS (DUP_PASS (pass_rename_ssa_copies));
299 NEXT_PASS (DUP_PASS (pass_dominator));
300 NEXT_PASS (DUP_PASS (pass_redundant_phi));
301 NEXT_PASS (DUP_PASS (pass_dce));
302 NEXT_PASS (pass_dse);
303 NEXT_PASS (DUP_PASS (pass_forwprop));
304 NEXT_PASS (DUP_PASS (pass_phiopt));
305 NEXT_PASS (pass_ccp);
306 NEXT_PASS (DUP_PASS (pass_redundant_phi));
307 NEXT_PASS (pass_fold_builtins);
308 NEXT_PASS (pass_split_crit_edges);
309 NEXT_PASS (pass_pre);
310 NEXT_PASS (DUP_PASS (pass_dominator));
311 NEXT_PASS (DUP_PASS (pass_redundant_phi));
312 NEXT_PASS (pass_cd_dce);
313 NEXT_PASS (DUP_PASS (pass_dse));
314 NEXT_PASS (DUP_PASS (pass_forwprop));
315 NEXT_PASS (DUP_PASS (pass_phiopt));
316 NEXT_PASS (pass_tail_calls);
317 NEXT_PASS (pass_late_warn_uninitialized);
318 NEXT_PASS (pass_warn_function_return);
319 NEXT_PASS (pass_del_ssa);
320 NEXT_PASS (pass_nrv);
321 NEXT_PASS (pass_remove_useless_vars);
322 NEXT_PASS (pass_del_cfg);
323 *p = NULL;
325 #undef NEXT_PASS
326 #undef DUP_PASS
328 /* Register the passes with the tree dump code. */
329 register_dump_files (all_passes);
332 static void execute_pass_list (struct tree_opt_pass *);
334 static unsigned int current_properties;
335 static unsigned int last_verified;
337 static void
338 execute_todo (unsigned int flags)
340 if (flags & TODO_rename_vars)
342 if (bitmap_first_set_bit (vars_to_rename) >= 0)
343 rewrite_into_ssa ();
344 BITMAP_XFREE (vars_to_rename);
347 if ((flags & TODO_dump_func) && dump_file)
348 dump_function_to_file (current_function_decl,
349 dump_file, dump_flags);
351 if (flags & TODO_ggc_collect)
352 ggc_collect ();
354 #ifdef ENABLE_CHECKING
355 if (flags & TODO_verify_ssa)
356 verify_ssa ();
357 if (flags & TODO_verify_flow)
358 verify_flow_info ();
359 if (flags & TODO_verify_stmts)
360 verify_stmts ();
361 #endif
364 static bool
365 execute_one_pass (struct tree_opt_pass *pass)
367 unsigned int todo;
369 /* See if we're supposed to run this pass. */
370 if (pass->gate && !pass->gate ())
371 return false;
373 /* Verify that all required properties are present. */
374 if (pass->properties_required & ~current_properties)
375 abort ();
377 /* Run pre-pass verification. */
378 todo = pass->todo_flags_start & ~last_verified;
379 if (todo)
380 execute_todo (todo);
382 /* If a dump file name is present, open it if enabled. */
383 if (pass->static_pass_number)
385 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
386 if (dump_file)
388 const char *dname, *aname;
389 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
390 aname = (IDENTIFIER_POINTER
391 (DECL_ASSEMBLER_NAME (current_function_decl)));
392 fprintf (dump_file, "\n;; Function %s (%s)\n\n", dname, aname);
396 /* If a timevar is present, start it. */
397 if (pass->tv_id)
398 timevar_push (pass->tv_id);
400 /* If the pass is requesting ssa variable renaming, allocate the bitmap. */
401 if (pass->todo_flags_finish & TODO_rename_vars)
402 vars_to_rename = BITMAP_XMALLOC ();
404 /* Do it! */
405 if (pass->execute)
406 pass->execute ();
408 /* Run post-pass cleanup and verification. */
409 todo = pass->todo_flags_finish;
410 last_verified = todo & TODO_verify_all;
411 if (todo)
412 execute_todo (todo);
414 /* Update properties. */
415 current_properties &= ~pass->properties_destroyed;
416 current_properties |= pass->properties_provided;
418 /* Close down timevar and dump file. */
419 if (pass->tv_id)
420 timevar_pop (pass->tv_id);
421 if (dump_file)
423 dump_end (pass->static_pass_number, dump_file);
424 dump_file = NULL;
427 return true;
430 static void
431 execute_pass_list (struct tree_opt_pass *pass)
435 if (execute_one_pass (pass) && pass->sub)
436 execute_pass_list (pass->sub);
437 pass = pass->next;
439 while (pass);
443 /* Called to move the SAVE_EXPRs for parameter declarations in a
444 nested function into the nested function. DATA is really the
445 nested FUNCTION_DECL. */
447 static tree
448 set_save_expr_context (tree *tp,
449 int *walk_subtrees,
450 void *data)
452 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
453 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
454 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
455 circularity. */
456 else if (DECL_P (*tp))
457 *walk_subtrees = 0;
459 return NULL;
462 /* For functions-as-trees languages, this performs all optimization and
463 compilation for FNDECL. */
465 void
466 tree_rest_of_compilation (tree fndecl, bool nested_p)
468 location_t saved_loc;
469 struct cgraph_node *saved_node = NULL, *node;
471 timevar_push (TV_EXPAND);
473 if (flag_unit_at_a_time && !cgraph_global_info_ready)
474 abort ();
476 /* Initialize the RTL code for the function. */
477 current_function_decl = fndecl;
478 saved_loc = input_location;
479 input_location = DECL_SOURCE_LOCATION (fndecl);
480 init_function_start (fndecl);
482 /* This function is being processed in whole-function mode. */
483 cfun->x_whole_function_mode_p = 1;
485 /* Even though we're inside a function body, we still don't want to
486 call expand_expr to calculate the size of a variable-sized array.
487 We haven't necessarily assigned RTL to all variables yet, so it's
488 not safe to try to expand expressions involving them. */
489 immediate_size_expand = 0;
490 cfun->x_dont_save_pending_sizes_p = 1;
492 node = cgraph_node (fndecl);
494 /* We might need the body of this function so that we can expand
495 it inline somewhere else. This means not lowering some constructs
496 such as exception handling. */
497 if (cgraph_preserve_function_body_p (fndecl))
499 if (!flag_unit_at_a_time)
501 struct cgraph_edge *e;
503 saved_node = cgraph_clone_node (node);
504 for (e = saved_node->callees; e; e = e->next_callee)
505 if (!e->inline_failed)
506 cgraph_clone_inlined_nodes (e, true);
508 cfun->saved_tree = save_body (fndecl, &cfun->saved_args);
511 if (flag_inline_trees)
513 struct cgraph_edge *e;
514 for (e = node->callees; e; e = e->next_callee)
515 if (!e->inline_failed || warn_inline)
516 break;
517 if (e)
519 timevar_push (TV_INTEGRATION);
520 optimize_inline_calls (fndecl);
521 timevar_pop (TV_INTEGRATION);
525 /* Note that the folders should only create gimple expressions.
526 This is a hack until the new folder is ready. */
527 in_gimple_form = true;
529 /* Perform all tree transforms and optimizations. */
530 execute_pass_list (all_passes);
532 /* Note that the folders can create non-gimple expressions again. */
533 in_gimple_form = false;
535 /* If the function has a variably modified type, there may be
536 SAVE_EXPRs in the parameter types. Their context must be set to
537 refer to this function; they cannot be expanded in the containing
538 function. */
539 if (decl_function_context (fndecl) == current_function_decl
540 && variably_modified_type_p (TREE_TYPE (fndecl)))
541 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
542 NULL);
544 /* Expand the variables recorded during gimple lowering. This must
545 occur before the call to expand_function_start to ensure that
546 all used variables are expanded before we expand anything on the
547 PENDING_SIZES list. */
548 expand_used_vars ();
550 /* Set up parameters and prepare for return, for the function. */
551 expand_function_start (fndecl, 0);
553 /* If this function is `main', emit a call to `__main'
554 to run global initializers, etc. */
555 if (DECL_NAME (fndecl)
556 && MAIN_NAME_P (DECL_NAME (fndecl))
557 && DECL_FILE_SCOPE_P (fndecl))
558 expand_main_function ();
560 /* Generate the RTL for this function. */
561 expand_expr_stmt_value (DECL_SAVED_TREE (fndecl), 0, 0);
563 /* We hard-wired immediate_size_expand to zero above.
564 expand_function_end will decrement this variable. So, we set the
565 variable to one here, so that after the decrement it will remain
566 zero. */
567 immediate_size_expand = 1;
569 /* Make sure the locus is set to the end of the function, so that
570 epilogue line numbers and warnings are set properly. */
571 if (cfun->function_end_locus.file)
572 input_location = cfun->function_end_locus;
574 /* The following insns belong to the top scope. */
575 record_block_change (DECL_INITIAL (current_function_decl));
577 /* Generate rtl for function exit. */
578 expand_function_end ();
580 /* If this is a nested function, protect the local variables in the stack
581 above us from being collected while we're compiling this function. */
582 if (nested_p)
583 ggc_push_context ();
585 /* There's no need to defer outputting this function any more; we
586 know we want to output it. */
587 DECL_DEFER_OUTPUT (fndecl) = 0;
589 /* Run the optimizers and output the assembler code for this function. */
590 rest_of_compilation (fndecl);
592 /* Restore original body if still needed. */
593 if (cfun->saved_tree)
595 DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
596 DECL_ARGUMENTS (fndecl) = cfun->saved_args;
598 /* When not in unit-at-a-time mode, we must preserve out of line copy
599 representing node before inlining. Restore original outgoing edges
600 using clone we created earlier. */
601 if (!flag_unit_at_a_time)
603 struct cgraph_edge *e;
604 while (node->callees)
605 cgraph_remove_edge (node->callees);
606 node->callees = saved_node->callees;
607 saved_node->callees = NULL;
608 for (e = saved_node->callees; e; e = e->next_callee)
609 e->caller = node;
610 cgraph_remove_node (saved_node);
613 else
614 DECL_SAVED_TREE (fndecl) = NULL;
615 cfun = 0;
617 /* If requested, warn about function definitions where the function will
618 return a value (usually of some struct or union type) which itself will
619 take up a lot of stack space. */
620 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
622 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
624 if (ret_type && TYPE_SIZE_UNIT (ret_type)
625 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
626 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
627 larger_than_size))
629 unsigned int size_as_int
630 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
632 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
633 warning ("%Jsize of return value of '%D' is %u bytes",
634 fndecl, fndecl, size_as_int);
635 else
636 warning ("%Jsize of return value of '%D' is larger than %wd bytes",
637 fndecl, fndecl, larger_than_size);
641 if (!nested_p && !flag_inline_trees)
643 DECL_SAVED_TREE (fndecl) = NULL;
644 if (DECL_STRUCT_FUNCTION (fndecl) == 0
645 && !cgraph_node (fndecl)->origin)
647 /* Stop pointing to the local nodes about to be freed.
648 But DECL_INITIAL must remain nonzero so we know this
649 was an actual function definition.
650 For a nested function, this is done in c_pop_function_context.
651 If rest_of_compilation set this to 0, leave it 0. */
652 if (DECL_INITIAL (fndecl) != 0)
653 DECL_INITIAL (fndecl) = error_mark_node;
657 input_location = saved_loc;
659 ggc_collect ();
661 /* Undo the GC context switch. */
662 if (nested_p)
663 ggc_pop_context ();
664 timevar_pop (TV_EXPAND);