OSDN Git Service

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