]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-optimize.c
mips.h (ISA_HAS_INT_CONDMOVE): Delete.
[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
165b54c3
SB
104/* Pass: cleanup the CFG just before expanding trees to RTL.
105 This is just a round of label cleanups and case node grouping
106 because after the tree optimizers have run such cleanups may
107 be necessary. */
108
109static void
110execute_cleanup_cfg_post_optimizing (void)
111{
112 cleanup_tree_cfg ();
113 cleanup_dead_labels ();
114 group_case_labels ();
115}
116
117static struct tree_opt_pass pass_cleanup_cfg_post_optimizing =
118{
119 NULL, /* name */
120 NULL, /* gate */
121 execute_cleanup_cfg_post_optimizing, /* execute */
122 NULL, /* sub */
123 NULL, /* next */
124 0, /* static_pass_number */
125 0, /* tv_id */
126 PROP_cfg, /* properties_required */
127 0, /* properties_provided */
128 0, /* properties_destroyed */
129 0, /* todo_flags_start */
130 0 /* todo_flags_finish */
131};
132
6de9cd9a
DN
133/* Pass: do the actions required to finish with tree-ssa optimization
134 passes. */
135
136static void
242229bb 137execute_free_datastructures (void)
6de9cd9a 138{
6de9cd9a
DN
139 tree *chain;
140
141 /* ??? This isn't the right place for this. Worse, it got computed
142 more or less at random in various passes. */
143 free_dominance_info (CDI_DOMINATORS);
144
145 /* Emit gotos for implicit jumps. */
146 disband_implicit_edges ();
147
148 /* Remove the ssa structures. Do it here since this includes statement
149 annotations that need to be intact during disband_implicit_edges. */
150 delete_tree_ssa ();
151
152 /* Re-chain the statements from the blocks. */
153 chain = &DECL_SAVED_TREE (current_function_decl);
154 *chain = alloc_stmt_list ();
6de9cd9a 155
242229bb
JH
156 /* And get rid of annotations we no longer need. */
157 delete_tree_cfg_annotations ();
6de9cd9a
DN
158}
159
242229bb 160static struct tree_opt_pass pass_free_datastructures =
6de9cd9a
DN
161{
162 NULL, /* name */
163 NULL, /* gate */
242229bb 164 execute_free_datastructures, /* execute */
6de9cd9a
DN
165 NULL, /* sub */
166 NULL, /* next */
167 0, /* static_pass_number */
168 0, /* tv_id */
169 PROP_cfg, /* properties_required */
170 0, /* properties_provided */
242229bb 171 0, /* properties_destroyed */
6de9cd9a
DN
172 0, /* todo_flags_start */
173 0 /* todo_flags_finish */
174};
175
33c94679
DN
176
177/* Do the actions required to initialize internal data structures used
178 in tree-ssa optimization passes. */
179
180static void
181execute_init_datastructures (void)
182{
183 /* Allocate hash tables, arrays and other structures. */
184 init_tree_ssa ();
185}
186
187static struct tree_opt_pass pass_init_datastructures =
188{
189 NULL, /* name */
190 NULL, /* gate */
191 execute_init_datastructures, /* execute */
192 NULL, /* sub */
193 NULL, /* next */
194 0, /* static_pass_number */
195 0, /* tv_id */
196 PROP_cfg, /* properties_required */
197 0, /* properties_provided */
198 0, /* properties_destroyed */
199 0, /* todo_flags_start */
200 0 /* todo_flags_finish */
201};
202
6de9cd9a
DN
203/* Iterate over the pass tree allocating dump file numbers. We want
204 to do this depth first, and independent of whether the pass is
205 enabled or not. */
206
207static void
208register_one_dump_file (struct tree_opt_pass *pass)
209{
210 char *dot_name, *flag_name;
211 char num[10];
212
213 if (!pass->name)
214 return;
215
b3fade83 216 /* See below in next_pass_1. */
6de9cd9a 217 num[0] = '\0';
b3fade83 218 if (pass->static_pass_number != -1)
6de9cd9a
DN
219 sprintf (num, "%d", ((int) pass->static_pass_number < 0
220 ? 1 : pass->static_pass_number));
221
222 dot_name = concat (".", pass->name, num, NULL);
223 flag_name = concat ("tree-", pass->name, num, NULL);
224
225 pass->static_pass_number = dump_register (dot_name, flag_name);
226}
227
2f8e398b
PB
228static int
229register_dump_files (struct tree_opt_pass *pass, int properties)
6de9cd9a
DN
230{
231 do
232 {
2f8e398b
PB
233 /* Verify that all required properties are present. */
234 if (pass->properties_required & ~properties)
235 abort ();
236
237 if (pass->properties_destroyed & pass->properties_provided)
238 abort ();
239
240 pass->properties_required = properties;
241 pass->properties_provided = properties =
242 (properties | pass->properties_provided) & ~pass->properties_destroyed;
243
244 if (properties & PROP_trees)
245 register_one_dump_file (pass);
6de9cd9a 246 if (pass->sub)
2f8e398b 247 properties = register_dump_files (pass->sub, properties);
6de9cd9a
DN
248 pass = pass->next;
249 }
250 while (pass);
2f8e398b
PB
251
252 return properties;
6de9cd9a
DN
253}
254
b3fade83
BB
255/* Add a pass to the pass list. Duplicate the pass if it's already
256 in the list. */
6de9cd9a 257
b3fade83
BB
258static struct tree_opt_pass **
259next_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
6de9cd9a 260{
6de9cd9a 261
b3fade83
BB
262 /* A non-zero static_pass_number indicates that the
263 pass is already in the list. */
264 if (pass->static_pass_number)
6de9cd9a 265 {
b3fade83
BB
266 struct tree_opt_pass *new;
267
268 new = xmalloc (sizeof (*new));
269 memcpy (new, pass, sizeof (*new));
270
271 /* Indicate to register_dump_files that this pass has duplicates,
272 and so it should rename the dump file. The first instance will
273 be -1, and be number of duplicates = -static_pass_number - 1.
274 Subsequent instances will be > 0 and just the duplicate number. */
275 if (pass->name)
276 {
277 pass->static_pass_number -= 1;
278 new->static_pass_number = -pass->static_pass_number;
279 }
280
281 *list = new;
6de9cd9a 282 }
b3fade83
BB
283 else
284 {
285 pass->static_pass_number = -1;
286 *list = pass;
287 }
288
289 return &(*list)->next;
290
6de9cd9a
DN
291}
292
293/* Construct the pass tree. */
294
295void
296init_tree_optimization_passes (void)
297{
298 struct tree_opt_pass **p;
299
b3fade83 300#define NEXT_PASS(PASS) (p = next_pass_1 (p, &PASS))
6de9cd9a
DN
301
302 p = &all_passes;
303 NEXT_PASS (pass_gimple);
304 NEXT_PASS (pass_remove_useless_stmts);
305 NEXT_PASS (pass_mudflap_1);
306 NEXT_PASS (pass_lower_cf);
307 NEXT_PASS (pass_lower_eh);
242229bb 308 NEXT_PASS (pass_build_cfg);
26277d41 309 NEXT_PASS (pass_pre_expand);
242229bb 310 NEXT_PASS (pass_tree_profile);
33c94679 311 NEXT_PASS (pass_init_datastructures);
6de9cd9a 312 NEXT_PASS (pass_all_optimizations);
55e99d52 313 NEXT_PASS (pass_warn_function_return);
6de9cd9a 314 NEXT_PASS (pass_mudflap_2);
242229bb 315 NEXT_PASS (pass_free_datastructures);
2f8e398b
PB
316 NEXT_PASS (pass_expand);
317 NEXT_PASS (pass_rest_of_compilation);
6de9cd9a
DN
318 *p = NULL;
319
320 p = &pass_all_optimizations.sub;
6de9cd9a
DN
321 NEXT_PASS (pass_referenced_vars);
322 NEXT_PASS (pass_build_pta);
323 NEXT_PASS (pass_build_ssa);
c1b763fa 324 NEXT_PASS (pass_may_alias);
6de9cd9a
DN
325 NEXT_PASS (pass_rename_ssa_copies);
326 NEXT_PASS (pass_early_warn_uninitialized);
327 NEXT_PASS (pass_dce);
328 NEXT_PASS (pass_dominator);
329 NEXT_PASS (pass_redundant_phi);
b3fade83 330 NEXT_PASS (pass_dce);
6de9cd9a
DN
331 NEXT_PASS (pass_forwprop);
332 NEXT_PASS (pass_phiopt);
333 NEXT_PASS (pass_may_alias);
334 NEXT_PASS (pass_tail_recursion);
335 NEXT_PASS (pass_ch);
6de9cd9a 336 NEXT_PASS (pass_profile);
6de9cd9a 337 NEXT_PASS (pass_sra);
b3fade83
BB
338 NEXT_PASS (pass_rename_ssa_copies);
339 NEXT_PASS (pass_dominator);
340 NEXT_PASS (pass_redundant_phi);
341 NEXT_PASS (pass_dce);
6de9cd9a 342 NEXT_PASS (pass_dse);
b3fade83
BB
343 NEXT_PASS (pass_may_alias);
344 NEXT_PASS (pass_forwprop);
345 NEXT_PASS (pass_phiopt);
6de9cd9a 346 NEXT_PASS (pass_ccp);
b3fade83 347 NEXT_PASS (pass_redundant_phi);
6de9cd9a
DN
348 NEXT_PASS (pass_fold_builtins);
349 NEXT_PASS (pass_split_crit_edges);
350 NEXT_PASS (pass_pre);
c66b6c66 351 NEXT_PASS (pass_loop);
b3fade83
BB
352 NEXT_PASS (pass_dominator);
353 NEXT_PASS (pass_redundant_phi);
6de9cd9a 354 NEXT_PASS (pass_cd_dce);
b3fade83
BB
355 NEXT_PASS (pass_dse);
356 NEXT_PASS (pass_forwprop);
357 NEXT_PASS (pass_phiopt);
6de9cd9a
DN
358 NEXT_PASS (pass_tail_calls);
359 NEXT_PASS (pass_late_warn_uninitialized);
d8903b30 360 NEXT_PASS (pass_del_pta);
6de9cd9a
DN
361 NEXT_PASS (pass_del_ssa);
362 NEXT_PASS (pass_nrv);
363 NEXT_PASS (pass_remove_useless_vars);
165b54c3 364 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
6de9cd9a
DN
365 *p = NULL;
366
c66b6c66
ZD
367 p = &pass_loop.sub;
368 NEXT_PASS (pass_loop_init);
a7e5372d 369 NEXT_PASS (pass_lim);
79fe1b3b 370 NEXT_PASS (pass_vectorize);
c66b6c66
ZD
371 NEXT_PASS (pass_loop_done);
372 *p = NULL;
373
6de9cd9a 374#undef NEXT_PASS
6de9cd9a
DN
375
376 /* Register the passes with the tree dump code. */
2f8e398b 377 register_dump_files (all_passes, 0);
6de9cd9a
DN
378}
379
380static void execute_pass_list (struct tree_opt_pass *);
381
6de9cd9a
DN
382static unsigned int last_verified;
383
384static void
385execute_todo (unsigned int flags)
386{
387 if (flags & TODO_rename_vars)
388 {
5f240ec4
ZD
389 rewrite_into_ssa (false);
390 bitmap_clear (vars_to_rename);
6de9cd9a
DN
391 }
392
393 if ((flags & TODO_dump_func) && dump_file)
2aaf3dd5
DN
394 {
395 dump_function_to_file (current_function_decl,
396 dump_file, dump_flags);
397
398 /* Flush the file. If verification fails, we won't be able to
399 close the file before aborting. */
400 fflush (dump_file);
401 }
6de9cd9a
DN
402
403 if (flags & TODO_ggc_collect)
404 ggc_collect ();
405
406#ifdef ENABLE_CHECKING
407 if (flags & TODO_verify_ssa)
408 verify_ssa ();
409 if (flags & TODO_verify_flow)
410 verify_flow_info ();
411 if (flags & TODO_verify_stmts)
412 verify_stmts ();
413#endif
414}
415
416static bool
417execute_one_pass (struct tree_opt_pass *pass)
418{
419 unsigned int todo;
420
421 /* See if we're supposed to run this pass. */
422 if (pass->gate && !pass->gate ())
423 return false;
424
2f8e398b
PB
425 /* Note that the folders should only create gimple expressions.
426 This is a hack until the new folder is ready. */
427 in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
6de9cd9a
DN
428
429 /* Run pre-pass verification. */
430 todo = pass->todo_flags_start & ~last_verified;
431 if (todo)
432 execute_todo (todo);
433
434 /* If a dump file name is present, open it if enabled. */
b3fade83 435 if (pass->static_pass_number != -1)
6de9cd9a
DN
436 {
437 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
438 if (dump_file)
439 {
440 const char *dname, *aname;
673fda6b 441 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
6de9cd9a
DN
442 aname = (IDENTIFIER_POINTER
443 (DECL_ASSEMBLER_NAME (current_function_decl)));
444 fprintf (dump_file, "\n;; Function %s (%s)\n\n", dname, aname);
445 }
446 }
447
448 /* If a timevar is present, start it. */
449 if (pass->tv_id)
450 timevar_push (pass->tv_id);
451
6de9cd9a
DN
452 /* Do it! */
453 if (pass->execute)
454 pass->execute ();
4985cde3 455
6de9cd9a
DN
456 /* Run post-pass cleanup and verification. */
457 todo = pass->todo_flags_finish;
458 last_verified = todo & TODO_verify_all;
459 if (todo)
460 execute_todo (todo);
461
6de9cd9a
DN
462 /* Close down timevar and dump file. */
463 if (pass->tv_id)
464 timevar_pop (pass->tv_id);
465 if (dump_file)
466 {
467 dump_end (pass->static_pass_number, dump_file);
468 dump_file = NULL;
469 }
470
471 return true;
472}
473
474static void
475execute_pass_list (struct tree_opt_pass *pass)
476{
477 do
478 {
479 if (execute_one_pass (pass) && pass->sub)
480 execute_pass_list (pass->sub);
481 pass = pass->next;
482 }
483 while (pass);
484}
485
486\f
4985cde3
RH
487/* For functions-as-trees languages, this performs all optimization and
488 compilation for FNDECL. */
489
490void
c1f927e8 491tree_rest_of_compilation (tree fndecl, bool nested_p)
4985cde3 492{
5d4854c8 493 location_t saved_loc;
6de9cd9a 494 struct cgraph_node *saved_node = NULL, *node;
5d4854c8 495
4985cde3
RH
496 timevar_push (TV_EXPAND);
497
4985cde3
RH
498 if (flag_unit_at_a_time && !cgraph_global_info_ready)
499 abort ();
500
4985cde3
RH
501 /* Initialize the RTL code for the function. */
502 current_function_decl = fndecl;
5d4854c8 503 saved_loc = input_location;
f31686a3 504 input_location = DECL_SOURCE_LOCATION (fndecl);
4985cde3
RH
505 init_function_start (fndecl);
506
4985cde3
RH
507 /* Even though we're inside a function body, we still don't want to
508 call expand_expr to calculate the size of a variable-sized array.
509 We haven't necessarily assigned RTL to all variables yet, so it's
510 not safe to try to expand expressions involving them. */
4985cde3
RH
511 cfun->x_dont_save_pending_sizes_p = 1;
512
18c6ada9
JH
513 node = cgraph_node (fndecl);
514
515 /* We might need the body of this function so that we can expand
516 it inline somewhere else. This means not lowering some constructs
517 such as exception handling. */
518 if (cgraph_preserve_function_body_p (fndecl))
519 {
520 if (!flag_unit_at_a_time)
521 {
522 struct cgraph_edge *e;
523
524 saved_node = cgraph_clone_node (node);
525 for (e = saved_node->callees; e; e = e->next_callee)
526 if (!e->inline_failed)
527 cgraph_clone_inlined_nodes (e, true);
528 }
1a837f77
RK
529 cfun->saved_static_chain_decl = cfun->static_chain_decl;
530 cfun->saved_tree = save_body (fndecl, &cfun->saved_args,
531 &cfun->saved_static_chain_decl);
18c6ada9
JH
532 }
533
534 if (flag_inline_trees)
535 {
536 struct cgraph_edge *e;
537 for (e = node->callees; e; e = e->next_callee)
538 if (!e->inline_failed || warn_inline)
539 break;
540 if (e)
541 {
542 timevar_push (TV_INTEGRATION);
543 optimize_inline_calls (fndecl);
544 timevar_pop (TV_INTEGRATION);
545 }
546 }
547
5f240ec4
ZD
548 if (!vars_to_rename)
549 vars_to_rename = BITMAP_XMALLOC ();
550
4985cde3
RH
551 /* If this is a nested function, protect the local variables in the stack
552 above us from being collected while we're compiling this function. */
c1f927e8 553 if (nested_p)
4985cde3
RH
554 ggc_push_context ();
555
2f8e398b
PB
556 /* Perform all tree transforms and optimizations. */
557 execute_pass_list (all_passes);
4985cde3 558
6de9cd9a
DN
559 /* Restore original body if still needed. */
560 if (cfun->saved_tree)
561 {
562 DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
563 DECL_ARGUMENTS (fndecl) = cfun->saved_args;
1a837f77 564 cfun->static_chain_decl = cfun->saved_static_chain_decl;
18c6ada9 565
6de9cd9a
DN
566 /* When not in unit-at-a-time mode, we must preserve out of line copy
567 representing node before inlining. Restore original outgoing edges
568 using clone we created earlier. */
569 if (!flag_unit_at_a_time)
570 {
571 struct cgraph_edge *e;
572 while (node->callees)
573 cgraph_remove_edge (node->callees);
574 node->callees = saved_node->callees;
575 saved_node->callees = NULL;
576 for (e = saved_node->callees; e; e = e->next_callee)
577 e->caller = node;
578 cgraph_remove_node (saved_node);
579 }
580 }
581 else
582 DECL_SAVED_TREE (fndecl) = NULL;
583 cfun = 0;
4985cde3
RH
584
585 /* If requested, warn about function definitions where the function will
586 return a value (usually of some struct or union type) which itself will
587 take up a lot of stack space. */
588 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
589 {
590 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
591
592 if (ret_type && TYPE_SIZE_UNIT (ret_type)
593 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
594 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
595 larger_than_size))
596 {
4985cde3
RH
597 unsigned int size_as_int
598 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
599
600 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
ddd2d57e
RH
601 warning ("%Jsize of return value of '%D' is %u bytes",
602 fndecl, fndecl, size_as_int);
4985cde3 603 else
ddd2d57e
RH
604 warning ("%Jsize of return value of '%D' is larger than %wd bytes",
605 fndecl, fndecl, larger_than_size);
4985cde3
RH
606 }
607 }
608
6de9cd9a 609 if (!nested_p && !flag_inline_trees)
4985cde3 610 {
d173e685 611 DECL_SAVED_TREE (fndecl) = NULL;
6de9cd9a
DN
612 if (DECL_STRUCT_FUNCTION (fndecl) == 0
613 && !cgraph_node (fndecl)->origin)
d173e685
JH
614 {
615 /* Stop pointing to the local nodes about to be freed.
616 But DECL_INITIAL must remain nonzero so we know this
617 was an actual function definition.
618 For a nested function, this is done in c_pop_function_context.
619 If rest_of_compilation set this to 0, leave it 0. */
620 if (DECL_INITIAL (fndecl) != 0)
621 DECL_INITIAL (fndecl) = error_mark_node;
d173e685 622 }
4985cde3
RH
623 }
624
5d4854c8
RH
625 input_location = saved_loc;
626
6de9cd9a
DN
627 ggc_collect ();
628
629 /* Undo the GC context switch. */
630 if (nested_p)
631 ggc_pop_context ();
4985cde3
RH
632 timevar_pop (TV_EXPAND);
633}