]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-optimize.c
Daily bump.
[thirdparty/gcc.git] / gcc / tree-optimize.c
CommitLineData
ac534736 1/* Top-level control of tree optimizations.
283334f0 2 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
6de9cd9a 3 Contributed by Diego Novillo <dnovillo@redhat.com>
4985cde3
RH
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
6de9cd9a 25#include "tm.h"
4985cde3 26#include "tree.h"
6de9cd9a
DN
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"
4985cde3 35#include "flags.h"
6de9cd9a
DN
36#include "tree-flow.h"
37#include "tree-dump.h"
4985cde3 38#include "timevar.h"
4985cde3 39#include "function.h"
6de9cd9a
DN
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"
4985cde3 48#include "ggc.h"
6de9cd9a
DN
49#include "cgraph.h"
50
51
52/* Global variables used to communicate with passes. */
53int dump_flags;
54bitmap vars_to_rename;
55bool in_gimple_form;
56
57/* The root of the compilation pass tree, once constructed. */
58static struct tree_opt_pass *all_passes;
59
84936f6f 60/* Pass: dump the gimplified, inlined, functions. */
6de9cd9a
DN
61
62static struct tree_opt_pass pass_gimple =
63{
64 "gimple", /* name */
65 NULL, /* gate */
84936f6f 66 NULL, /* execute */
6de9cd9a
DN
67 NULL, /* sub */
68 NULL, /* next */
69 0, /* static_pass_number */
70 0, /* tv_id */
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 */
76};
77
6de9cd9a
DN
78/* Gate: execute, or not, all of the non-trivial optimizations. */
79
80static bool
81gate_all_optimizations (void)
82{
83 return (optimize >= 1
84 /* Don't bother doing anything if the program has errors. */
85 && !(errorcount || sorrycount));
86}
87
88static struct tree_opt_pass pass_all_optimizations =
89{
90 NULL, /* name */
91 gate_all_optimizations, /* gate */
92 NULL, /* execute */
93 NULL, /* sub */
94 NULL, /* next */
95 0, /* static_pass_number */
96 0, /* tv_id */
97 0, /* properties_required */
98 0, /* properties_provided */
99 0, /* properties_destroyed */
100 0, /* todo_flags_start */
101 0 /* todo_flags_finish */
102};
103
104/* Pass: do the actions required to finish with tree-ssa optimization
105 passes. */
106
107static void
242229bb 108execute_free_datastructures (void)
6de9cd9a 109{
6de9cd9a
DN
110 tree *chain;
111
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);
115
116 /* Emit gotos for implicit jumps. */
117 disband_implicit_edges ();
118
119 /* Remove the ssa structures. Do it here since this includes statement
120 annotations that need to be intact during disband_implicit_edges. */
121 delete_tree_ssa ();
122
123 /* Re-chain the statements from the blocks. */
124 chain = &DECL_SAVED_TREE (current_function_decl);
125 *chain = alloc_stmt_list ();
6de9cd9a 126
242229bb
JH
127 /* And get rid of annotations we no longer need. */
128 delete_tree_cfg_annotations ();
6de9cd9a
DN
129}
130
242229bb 131static struct tree_opt_pass pass_free_datastructures =
6de9cd9a
DN
132{
133 NULL, /* name */
134 NULL, /* gate */
242229bb 135 execute_free_datastructures, /* execute */
6de9cd9a
DN
136 NULL, /* sub */
137 NULL, /* next */
138 0, /* static_pass_number */
139 0, /* tv_id */
140 PROP_cfg, /* properties_required */
141 0, /* properties_provided */
242229bb 142 0, /* properties_destroyed */
6de9cd9a
DN
143 0, /* todo_flags_start */
144 0 /* todo_flags_finish */
145};
146
33c94679
DN
147
148/* Do the actions required to initialize internal data structures used
149 in tree-ssa optimization passes. */
150
151static void
152execute_init_datastructures (void)
153{
154 /* Allocate hash tables, arrays and other structures. */
155 init_tree_ssa ();
156}
157
158static struct tree_opt_pass pass_init_datastructures =
159{
160 NULL, /* name */
161 NULL, /* gate */
162 execute_init_datastructures, /* execute */
163 NULL, /* sub */
164 NULL, /* next */
165 0, /* static_pass_number */
166 0, /* tv_id */
167 PROP_cfg, /* properties_required */
168 0, /* properties_provided */
169 0, /* properties_destroyed */
170 0, /* todo_flags_start */
171 0 /* todo_flags_finish */
172};
173
6de9cd9a
DN
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
176 enabled or not. */
177
178static void
179register_one_dump_file (struct tree_opt_pass *pass)
180{
181 char *dot_name, *flag_name;
182 char num[10];
183
184 if (!pass->name)
185 return;
186
187 /* See below in dup_pass_1. */
188 num[0] = '\0';
189 if (pass->static_pass_number)
190 sprintf (num, "%d", ((int) pass->static_pass_number < 0
191 ? 1 : pass->static_pass_number));
192
193 dot_name = concat (".", pass->name, num, NULL);
194 flag_name = concat ("tree-", pass->name, num, NULL);
195
196 pass->static_pass_number = dump_register (dot_name, flag_name);
197}
198
2f8e398b
PB
199static int
200register_dump_files (struct tree_opt_pass *pass, int properties)
6de9cd9a
DN
201{
202 do
203 {
2f8e398b
PB
204 /* Verify that all required properties are present. */
205 if (pass->properties_required & ~properties)
206 abort ();
207
208 if (pass->properties_destroyed & pass->properties_provided)
209 abort ();
210
211 pass->properties_required = properties;
212 pass->properties_provided = properties =
213 (properties | pass->properties_provided) & ~pass->properties_destroyed;
214
215 if (properties & PROP_trees)
216 register_one_dump_file (pass);
6de9cd9a 217 if (pass->sub)
2f8e398b 218 properties = register_dump_files (pass->sub, properties);
6de9cd9a
DN
219 pass = pass->next;
220 }
221 while (pass);
2f8e398b
PB
222
223 return properties;
6de9cd9a
DN
224}
225
226/* Duplicate a pass that's to be run more than once. */
227
228static struct tree_opt_pass *
229dup_pass_1 (struct tree_opt_pass *pass)
230{
231 struct tree_opt_pass *new;
232
233 new = xmalloc (sizeof (*new));
234 memcpy (new, pass, sizeof (*new));
235
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)
241 {
242 int n, p = pass->static_pass_number;
243
244 if (p)
245 n = -(--p) + 1;
246 else
247 n = 2, p = -1;
248
249 pass->static_pass_number = p;
250 new->static_pass_number = n;
251 }
252
253 return new;
254}
255
256/* Construct the pass tree. */
257
258void
259init_tree_optimization_passes (void)
260{
261 struct tree_opt_pass **p;
262
263#define NEXT_PASS(PASS) (*p = &PASS, p = &(*p)->next)
264#define DUP_PASS(PASS) (*dup_pass_1 (&PASS))
265
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);
242229bb
JH
272 NEXT_PASS (pass_build_cfg);
273 NEXT_PASS (pass_tree_profile);
33c94679 274 NEXT_PASS (pass_init_datastructures);
6de9cd9a 275 NEXT_PASS (pass_all_optimizations);
55e99d52 276 NEXT_PASS (pass_warn_function_return);
6de9cd9a 277 NEXT_PASS (pass_mudflap_2);
242229bb 278 NEXT_PASS (pass_free_datastructures);
2f8e398b
PB
279 NEXT_PASS (pass_expand);
280 NEXT_PASS (pass_rest_of_compilation);
6de9cd9a
DN
281 *p = NULL;
282
283 p = &pass_all_optimizations.sub;
6de9cd9a
DN
284 NEXT_PASS (pass_referenced_vars);
285 NEXT_PASS (pass_build_pta);
286 NEXT_PASS (pass_build_ssa);
287 NEXT_PASS (pass_rename_ssa_copies);
288 NEXT_PASS (pass_early_warn_uninitialized);
289 NEXT_PASS (pass_dce);
290 NEXT_PASS (pass_dominator);
291 NEXT_PASS (pass_redundant_phi);
292 NEXT_PASS (DUP_PASS (pass_dce));
293 NEXT_PASS (pass_forwprop);
294 NEXT_PASS (pass_phiopt);
295 NEXT_PASS (pass_may_alias);
296 NEXT_PASS (pass_tail_recursion);
297 NEXT_PASS (pass_ch);
6de9cd9a
DN
298 NEXT_PASS (pass_profile);
299 NEXT_PASS (pass_lower_complex);
300 NEXT_PASS (pass_sra);
301 NEXT_PASS (DUP_PASS (pass_rename_ssa_copies));
302 NEXT_PASS (DUP_PASS (pass_dominator));
303 NEXT_PASS (DUP_PASS (pass_redundant_phi));
304 NEXT_PASS (DUP_PASS (pass_dce));
305 NEXT_PASS (pass_dse);
d8903b30 306 NEXT_PASS (DUP_PASS (pass_may_alias));
6de9cd9a
DN
307 NEXT_PASS (DUP_PASS (pass_forwprop));
308 NEXT_PASS (DUP_PASS (pass_phiopt));
309 NEXT_PASS (pass_ccp);
310 NEXT_PASS (DUP_PASS (pass_redundant_phi));
311 NEXT_PASS (pass_fold_builtins);
312 NEXT_PASS (pass_split_crit_edges);
313 NEXT_PASS (pass_pre);
c66b6c66 314 NEXT_PASS (pass_loop);
6de9cd9a
DN
315 NEXT_PASS (DUP_PASS (pass_dominator));
316 NEXT_PASS (DUP_PASS (pass_redundant_phi));
317 NEXT_PASS (pass_cd_dce);
318 NEXT_PASS (DUP_PASS (pass_dse));
319 NEXT_PASS (DUP_PASS (pass_forwprop));
320 NEXT_PASS (DUP_PASS (pass_phiopt));
321 NEXT_PASS (pass_tail_calls);
322 NEXT_PASS (pass_late_warn_uninitialized);
d8903b30 323 NEXT_PASS (pass_del_pta);
6de9cd9a
DN
324 NEXT_PASS (pass_del_ssa);
325 NEXT_PASS (pass_nrv);
326 NEXT_PASS (pass_remove_useless_vars);
6de9cd9a
DN
327 *p = NULL;
328
c66b6c66
ZD
329 p = &pass_loop.sub;
330 NEXT_PASS (pass_loop_init);
a7e5372d 331 NEXT_PASS (pass_lim);
c66b6c66
ZD
332 NEXT_PASS (pass_loop_done);
333 *p = NULL;
334
6de9cd9a
DN
335#undef NEXT_PASS
336#undef DUP_PASS
337
338 /* Register the passes with the tree dump code. */
2f8e398b 339 register_dump_files (all_passes, 0);
6de9cd9a
DN
340}
341
342static void execute_pass_list (struct tree_opt_pass *);
343
6de9cd9a
DN
344static unsigned int last_verified;
345
346static void
347execute_todo (unsigned int flags)
348{
349 if (flags & TODO_rename_vars)
350 {
5f240ec4
ZD
351 rewrite_into_ssa (false);
352 bitmap_clear (vars_to_rename);
6de9cd9a
DN
353 }
354
355 if ((flags & TODO_dump_func) && dump_file)
2aaf3dd5
DN
356 {
357 dump_function_to_file (current_function_decl,
358 dump_file, dump_flags);
359
360 /* Flush the file. If verification fails, we won't be able to
361 close the file before aborting. */
362 fflush (dump_file);
363 }
6de9cd9a
DN
364
365 if (flags & TODO_ggc_collect)
366 ggc_collect ();
367
368#ifdef ENABLE_CHECKING
369 if (flags & TODO_verify_ssa)
370 verify_ssa ();
371 if (flags & TODO_verify_flow)
372 verify_flow_info ();
373 if (flags & TODO_verify_stmts)
374 verify_stmts ();
375#endif
376}
377
378static bool
379execute_one_pass (struct tree_opt_pass *pass)
380{
381 unsigned int todo;
382
383 /* See if we're supposed to run this pass. */
384 if (pass->gate && !pass->gate ())
385 return false;
386
2f8e398b
PB
387 /* Note that the folders should only create gimple expressions.
388 This is a hack until the new folder is ready. */
389 in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
6de9cd9a
DN
390
391 /* Run pre-pass verification. */
392 todo = pass->todo_flags_start & ~last_verified;
393 if (todo)
394 execute_todo (todo);
395
396 /* If a dump file name is present, open it if enabled. */
397 if (pass->static_pass_number)
398 {
399 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
400 if (dump_file)
401 {
402 const char *dname, *aname;
673fda6b 403 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
6de9cd9a
DN
404 aname = (IDENTIFIER_POINTER
405 (DECL_ASSEMBLER_NAME (current_function_decl)));
406 fprintf (dump_file, "\n;; Function %s (%s)\n\n", dname, aname);
407 }
408 }
409
410 /* If a timevar is present, start it. */
411 if (pass->tv_id)
412 timevar_push (pass->tv_id);
413
6de9cd9a
DN
414 /* Do it! */
415 if (pass->execute)
416 pass->execute ();
4985cde3 417
6de9cd9a
DN
418 /* Run post-pass cleanup and verification. */
419 todo = pass->todo_flags_finish;
420 last_verified = todo & TODO_verify_all;
421 if (todo)
422 execute_todo (todo);
423
6de9cd9a
DN
424 /* Close down timevar and dump file. */
425 if (pass->tv_id)
426 timevar_pop (pass->tv_id);
427 if (dump_file)
428 {
429 dump_end (pass->static_pass_number, dump_file);
430 dump_file = NULL;
431 }
432
433 return true;
434}
435
436static void
437execute_pass_list (struct tree_opt_pass *pass)
438{
439 do
440 {
441 if (execute_one_pass (pass) && pass->sub)
442 execute_pass_list (pass->sub);
443 pass = pass->next;
444 }
445 while (pass);
446}
447
448\f
4985cde3
RH
449/* For functions-as-trees languages, this performs all optimization and
450 compilation for FNDECL. */
451
452void
c1f927e8 453tree_rest_of_compilation (tree fndecl, bool nested_p)
4985cde3 454{
5d4854c8 455 location_t saved_loc;
6de9cd9a 456 struct cgraph_node *saved_node = NULL, *node;
5d4854c8 457
4985cde3
RH
458 timevar_push (TV_EXPAND);
459
4985cde3
RH
460 if (flag_unit_at_a_time && !cgraph_global_info_ready)
461 abort ();
462
4985cde3
RH
463 /* Initialize the RTL code for the function. */
464 current_function_decl = fndecl;
5d4854c8 465 saved_loc = input_location;
f31686a3 466 input_location = DECL_SOURCE_LOCATION (fndecl);
4985cde3
RH
467 init_function_start (fndecl);
468
4985cde3
RH
469 /* Even though we're inside a function body, we still don't want to
470 call expand_expr to calculate the size of a variable-sized array.
471 We haven't necessarily assigned RTL to all variables yet, so it's
472 not safe to try to expand expressions involving them. */
4985cde3
RH
473 cfun->x_dont_save_pending_sizes_p = 1;
474
18c6ada9
JH
475 node = cgraph_node (fndecl);
476
477 /* We might need the body of this function so that we can expand
478 it inline somewhere else. This means not lowering some constructs
479 such as exception handling. */
480 if (cgraph_preserve_function_body_p (fndecl))
481 {
482 if (!flag_unit_at_a_time)
483 {
484 struct cgraph_edge *e;
485
486 saved_node = cgraph_clone_node (node);
487 for (e = saved_node->callees; e; e = e->next_callee)
488 if (!e->inline_failed)
489 cgraph_clone_inlined_nodes (e, true);
490 }
491 cfun->saved_tree = save_body (fndecl, &cfun->saved_args);
492 }
493
494 if (flag_inline_trees)
495 {
496 struct cgraph_edge *e;
497 for (e = node->callees; e; e = e->next_callee)
498 if (!e->inline_failed || warn_inline)
499 break;
500 if (e)
501 {
502 timevar_push (TV_INTEGRATION);
503 optimize_inline_calls (fndecl);
504 timevar_pop (TV_INTEGRATION);
505 }
506 }
507
5f240ec4
ZD
508 if (!vars_to_rename)
509 vars_to_rename = BITMAP_XMALLOC ();
510
4985cde3
RH
511 /* If this is a nested function, protect the local variables in the stack
512 above us from being collected while we're compiling this function. */
c1f927e8 513 if (nested_p)
4985cde3
RH
514 ggc_push_context ();
515
2f8e398b
PB
516 /* Perform all tree transforms and optimizations. */
517 execute_pass_list (all_passes);
4985cde3 518
6de9cd9a
DN
519 /* Restore original body if still needed. */
520 if (cfun->saved_tree)
521 {
522 DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
523 DECL_ARGUMENTS (fndecl) = cfun->saved_args;
18c6ada9 524
6de9cd9a
DN
525 /* When not in unit-at-a-time mode, we must preserve out of line copy
526 representing node before inlining. Restore original outgoing edges
527 using clone we created earlier. */
528 if (!flag_unit_at_a_time)
529 {
530 struct cgraph_edge *e;
531 while (node->callees)
532 cgraph_remove_edge (node->callees);
533 node->callees = saved_node->callees;
534 saved_node->callees = NULL;
535 for (e = saved_node->callees; e; e = e->next_callee)
536 e->caller = node;
537 cgraph_remove_node (saved_node);
538 }
539 }
540 else
541 DECL_SAVED_TREE (fndecl) = NULL;
542 cfun = 0;
4985cde3
RH
543
544 /* If requested, warn about function definitions where the function will
545 return a value (usually of some struct or union type) which itself will
546 take up a lot of stack space. */
547 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
548 {
549 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
550
551 if (ret_type && TYPE_SIZE_UNIT (ret_type)
552 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
553 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
554 larger_than_size))
555 {
4985cde3
RH
556 unsigned int size_as_int
557 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
558
559 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
ddd2d57e
RH
560 warning ("%Jsize of return value of '%D' is %u bytes",
561 fndecl, fndecl, size_as_int);
4985cde3 562 else
ddd2d57e
RH
563 warning ("%Jsize of return value of '%D' is larger than %wd bytes",
564 fndecl, fndecl, larger_than_size);
4985cde3
RH
565 }
566 }
567
6de9cd9a 568 if (!nested_p && !flag_inline_trees)
4985cde3 569 {
d173e685 570 DECL_SAVED_TREE (fndecl) = NULL;
6de9cd9a
DN
571 if (DECL_STRUCT_FUNCTION (fndecl) == 0
572 && !cgraph_node (fndecl)->origin)
d173e685
JH
573 {
574 /* Stop pointing to the local nodes about to be freed.
575 But DECL_INITIAL must remain nonzero so we know this
576 was an actual function definition.
577 For a nested function, this is done in c_pop_function_context.
578 If rest_of_compilation set this to 0, leave it 0. */
579 if (DECL_INITIAL (fndecl) != 0)
580 DECL_INITIAL (fndecl) = error_mark_node;
d173e685 581 }
4985cde3
RH
582 }
583
5d4854c8
RH
584 input_location = saved_loc;
585
6de9cd9a
DN
586 ggc_collect ();
587
588 /* Undo the GC context switch. */
589 if (nested_p)
590 ggc_pop_context ();
4985cde3
RH
591 timevar_pop (TV_EXPAND);
592}