OSDN Git Service

* Makefile.in (distclean): Don't try to remove empty directories.
[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 /* Iterate over the pass tree allocating dump file numbers.  We want
157    to do this depth first, and independent of whether the pass is
158    enabled or not.  */
159
160 static void
161 register_one_dump_file (struct tree_opt_pass *pass)
162 {
163   char *dot_name, *flag_name;
164   char num[10];
165
166   if (!pass->name)
167     return;
168
169   /* See below in dup_pass_1.  */
170   num[0] = '\0';
171   if (pass->static_pass_number)
172     sprintf (num, "%d", ((int) pass->static_pass_number < 0
173                          ? 1 : pass->static_pass_number));
174
175   dot_name = concat (".", pass->name, num, NULL);
176   flag_name = concat ("tree-", pass->name, num, NULL);
177
178   pass->static_pass_number = dump_register (dot_name, flag_name);
179 }
180
181 static int 
182 register_dump_files (struct tree_opt_pass *pass, int properties)
183 {
184   do
185     {
186       /* Verify that all required properties are present.  */
187       if (pass->properties_required & ~properties)
188         abort ();
189
190       if (pass->properties_destroyed & pass->properties_provided)
191         abort ();
192
193       pass->properties_required = properties;
194       pass->properties_provided = properties =
195         (properties | pass->properties_provided) & ~pass->properties_destroyed;
196
197       if (properties & PROP_trees)
198         register_one_dump_file (pass);
199       if (pass->sub)
200         properties = register_dump_files (pass->sub, properties);
201       pass = pass->next;
202     }
203   while (pass);
204
205   return properties;
206 }
207
208 /* Duplicate a pass that's to be run more than once.  */
209
210 static struct tree_opt_pass *
211 dup_pass_1 (struct tree_opt_pass *pass)
212 {
213   struct tree_opt_pass *new;
214
215   new = xmalloc (sizeof (*new));
216   memcpy (new, pass, sizeof (*new));
217
218   /* Indicate to register_dump_files that this pass has duplicates,
219      and so it should rename the dump file.  The first instance will
220      be < 0, and be number of duplicates = -static_pass_number + 1.
221      Subsequent instances will be > 0 and just the duplicate number.  */
222   if (pass->name)
223     {
224       int n, p = pass->static_pass_number;
225         
226       if (p)
227         n = -(--p) + 1;
228       else
229         n = 2, p = -1;
230
231       pass->static_pass_number = p;
232       new->static_pass_number = n;
233     }
234
235   return new;
236 }
237
238 /* Construct the pass tree.  */
239
240 void
241 init_tree_optimization_passes (void)
242 {
243   struct tree_opt_pass **p;
244
245 #define NEXT_PASS(PASS) (*p = &PASS, p = &(*p)->next)
246 #define DUP_PASS(PASS)  (*dup_pass_1 (&PASS))
247
248   p = &all_passes;
249   NEXT_PASS (pass_gimple);
250   NEXT_PASS (pass_remove_useless_stmts);
251   NEXT_PASS (pass_mudflap_1);
252   NEXT_PASS (pass_lower_cf);
253   NEXT_PASS (pass_lower_eh);
254   NEXT_PASS (pass_build_cfg);
255   NEXT_PASS (pass_tree_profile);
256   NEXT_PASS (pass_all_optimizations);
257   NEXT_PASS (pass_mudflap_2);
258   NEXT_PASS (pass_free_datastructures);
259   NEXT_PASS (pass_expand);
260   NEXT_PASS (pass_rest_of_compilation);
261   *p = NULL;
262
263   p = &pass_all_optimizations.sub;
264   NEXT_PASS (pass_referenced_vars);
265   NEXT_PASS (pass_build_pta);
266   NEXT_PASS (pass_build_ssa);
267   NEXT_PASS (pass_rename_ssa_copies);
268   NEXT_PASS (pass_early_warn_uninitialized);
269   NEXT_PASS (pass_dce);
270   NEXT_PASS (pass_dominator);
271   NEXT_PASS (pass_redundant_phi);
272   NEXT_PASS (DUP_PASS (pass_dce));
273   NEXT_PASS (pass_forwprop);
274   NEXT_PASS (pass_phiopt);
275   NEXT_PASS (pass_may_alias);
276   NEXT_PASS (pass_tail_recursion);
277   NEXT_PASS (pass_ch);
278   NEXT_PASS (pass_del_pta);
279   NEXT_PASS (pass_profile);
280   NEXT_PASS (pass_lower_complex);
281   NEXT_PASS (pass_sra);
282   NEXT_PASS (DUP_PASS (pass_rename_ssa_copies));
283   NEXT_PASS (DUP_PASS (pass_dominator));
284   NEXT_PASS (DUP_PASS (pass_redundant_phi));
285   NEXT_PASS (DUP_PASS (pass_dce));
286   NEXT_PASS (pass_dse);
287   NEXT_PASS (DUP_PASS (pass_forwprop));
288   NEXT_PASS (DUP_PASS (pass_phiopt));
289   NEXT_PASS (pass_ccp);
290   NEXT_PASS (DUP_PASS (pass_redundant_phi));
291   NEXT_PASS (pass_fold_builtins);
292   NEXT_PASS (pass_split_crit_edges);
293   NEXT_PASS (pass_pre);
294   NEXT_PASS (DUP_PASS (pass_dominator));
295   NEXT_PASS (DUP_PASS (pass_redundant_phi));
296   NEXT_PASS (pass_cd_dce);
297   NEXT_PASS (DUP_PASS (pass_dse));
298   NEXT_PASS (DUP_PASS (pass_forwprop));
299   NEXT_PASS (DUP_PASS (pass_phiopt));
300   NEXT_PASS (pass_tail_calls);
301   NEXT_PASS (pass_late_warn_uninitialized);
302   NEXT_PASS (pass_warn_function_return);
303   NEXT_PASS (pass_del_ssa);
304   NEXT_PASS (pass_nrv);
305   NEXT_PASS (pass_remove_useless_vars);
306   *p = NULL;
307
308 #undef NEXT_PASS
309 #undef DUP_PASS
310
311   /* Register the passes with the tree dump code.  */
312   register_dump_files (all_passes, 0);
313 }
314
315 static void execute_pass_list (struct tree_opt_pass *);
316
317 static unsigned int last_verified;
318
319 static void
320 execute_todo (unsigned int flags)
321 {
322   if (flags & TODO_rename_vars)
323     {
324       if (bitmap_first_set_bit (vars_to_rename) >= 0)
325         rewrite_into_ssa ();
326       BITMAP_XFREE (vars_to_rename);
327     }
328
329   if ((flags & TODO_dump_func) && dump_file)
330     dump_function_to_file (current_function_decl,
331                            dump_file, dump_flags);
332
333   if (flags & TODO_ggc_collect)
334     ggc_collect ();
335
336 #ifdef ENABLE_CHECKING
337   if (flags & TODO_verify_ssa)
338     verify_ssa ();
339   if (flags & TODO_verify_flow)
340     verify_flow_info ();
341   if (flags & TODO_verify_stmts)
342     verify_stmts ();
343 #endif
344 }
345
346 static bool
347 execute_one_pass (struct tree_opt_pass *pass)
348 {
349   unsigned int todo; 
350
351   /* See if we're supposed to run this pass.  */
352   if (pass->gate && !pass->gate ())
353     return false;
354
355   /* Note that the folders should only create gimple expressions.
356      This is a hack until the new folder is ready.  */
357   in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
358
359   /* Run pre-pass verification.  */
360   todo = pass->todo_flags_start & ~last_verified;
361   if (todo)
362     execute_todo (todo);
363
364   /* If a dump file name is present, open it if enabled.  */
365   if (pass->static_pass_number)
366     {
367       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
368       if (dump_file)
369         {
370           const char *dname, *aname;
371           dname = lang_hooks.decl_printable_name (current_function_decl, 2);
372           aname = (IDENTIFIER_POINTER
373                    (DECL_ASSEMBLER_NAME (current_function_decl)));
374           fprintf (dump_file, "\n;; Function %s (%s)\n\n", dname, aname);
375         }
376     }
377
378   /* If a timevar is present, start it.  */
379   if (pass->tv_id)
380     timevar_push (pass->tv_id);
381
382   /* If the pass is requesting ssa variable renaming, allocate the bitmap.  */
383   if (pass->todo_flags_finish & TODO_rename_vars)
384     vars_to_rename = BITMAP_XMALLOC ();
385
386   /* Do it!  */
387   if (pass->execute)
388     pass->execute ();
389
390   /* Run post-pass cleanup and verification.  */
391   todo = pass->todo_flags_finish;
392   last_verified = todo & TODO_verify_all;
393   if (todo)
394     execute_todo (todo);
395
396   /* Close down timevar and dump file.  */
397   if (pass->tv_id)
398     timevar_pop (pass->tv_id);
399   if (dump_file)
400     {
401       dump_end (pass->static_pass_number, dump_file);
402       dump_file = NULL;
403     }
404
405   return true;
406 }
407
408 static void
409 execute_pass_list (struct tree_opt_pass *pass)
410 {
411   do
412     {
413       if (execute_one_pass (pass) && pass->sub)
414         execute_pass_list (pass->sub);
415       pass = pass->next;
416     }
417   while (pass);
418 }
419
420 \f
421 /* For functions-as-trees languages, this performs all optimization and
422    compilation for FNDECL.  */
423
424 void
425 tree_rest_of_compilation (tree fndecl, bool nested_p)
426 {
427   location_t saved_loc;
428   struct cgraph_node *saved_node = NULL, *node;
429
430   timevar_push (TV_EXPAND);
431
432   if (flag_unit_at_a_time && !cgraph_global_info_ready)
433     abort ();
434
435   /* Initialize the RTL code for the function.  */
436   current_function_decl = fndecl;
437   saved_loc = input_location;
438   input_location = DECL_SOURCE_LOCATION (fndecl);
439   init_function_start (fndecl);
440
441   /* This function is being processed in whole-function mode.  */
442   cfun->x_whole_function_mode_p = 1;
443
444   /* Even though we're inside a function body, we still don't want to
445      call expand_expr to calculate the size of a variable-sized array.
446      We haven't necessarily assigned RTL to all variables yet, so it's
447      not safe to try to expand expressions involving them.  */
448   immediate_size_expand = 0;
449   cfun->x_dont_save_pending_sizes_p = 1;
450
451   node = cgraph_node (fndecl);
452
453   /* We might need the body of this function so that we can expand
454      it inline somewhere else.  This means not lowering some constructs
455      such as exception handling.  */
456   if (cgraph_preserve_function_body_p (fndecl))
457     {
458       if (!flag_unit_at_a_time)
459         {
460           struct cgraph_edge *e;
461
462           saved_node = cgraph_clone_node (node);
463           for (e = saved_node->callees; e; e = e->next_callee)
464             if (!e->inline_failed)
465               cgraph_clone_inlined_nodes (e, true);
466         }
467       cfun->saved_tree = save_body (fndecl, &cfun->saved_args);
468     }
469
470   if (flag_inline_trees)
471     {
472       struct cgraph_edge *e;
473       for (e = node->callees; e; e = e->next_callee)
474         if (!e->inline_failed || warn_inline)
475           break;
476       if (e)
477         {
478           timevar_push (TV_INTEGRATION);
479           optimize_inline_calls (fndecl);
480           timevar_pop (TV_INTEGRATION);
481         }
482     }
483
484   /* If this is a nested function, protect the local variables in the stack
485      above us from being collected while we're compiling this function.  */
486   if (nested_p)
487     ggc_push_context ();
488
489   /* Perform all tree transforms and optimizations.  */
490   execute_pass_list (all_passes);
491
492   /* Restore original body if still needed.  */
493   if (cfun->saved_tree)
494     {
495       DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
496       DECL_ARGUMENTS (fndecl) = cfun->saved_args;
497
498       /* When not in unit-at-a-time mode, we must preserve out of line copy
499          representing node before inlining.  Restore original outgoing edges
500          using clone we created earlier.  */
501       if (!flag_unit_at_a_time)
502         {
503           struct cgraph_edge *e;
504           while (node->callees)
505             cgraph_remove_edge (node->callees);
506           node->callees = saved_node->callees;
507           saved_node->callees = NULL;
508           for (e = saved_node->callees; e; e = e->next_callee)
509             e->caller = node;
510           cgraph_remove_node (saved_node);
511         }
512     }
513   else
514     DECL_SAVED_TREE (fndecl) = NULL;
515   cfun = 0;
516
517   /* If requested, warn about function definitions where the function will
518      return a value (usually of some struct or union type) which itself will
519      take up a lot of stack space.  */
520   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
521     {
522       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
523
524       if (ret_type && TYPE_SIZE_UNIT (ret_type)
525           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
526           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
527                                    larger_than_size))
528         {
529           unsigned int size_as_int
530             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
531
532           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
533             warning ("%Jsize of return value of '%D' is %u bytes",
534                      fndecl, fndecl, size_as_int);
535           else
536             warning ("%Jsize of return value of '%D' is larger than %wd bytes",
537                      fndecl, fndecl, larger_than_size);
538         }
539     }
540
541   if (!nested_p && !flag_inline_trees)
542     {
543       DECL_SAVED_TREE (fndecl) = NULL;
544       if (DECL_STRUCT_FUNCTION (fndecl) == 0
545           && !cgraph_node (fndecl)->origin)
546         {
547           /* Stop pointing to the local nodes about to be freed.
548              But DECL_INITIAL must remain nonzero so we know this
549              was an actual function definition.
550              For a nested function, this is done in c_pop_function_context.
551              If rest_of_compilation set this to 0, leave it 0.  */
552           if (DECL_INITIAL (fndecl) != 0)
553             DECL_INITIAL (fndecl) = error_mark_node;
554         }
555     }
556
557   input_location = saved_loc;
558
559   ggc_collect ();
560
561   /* Undo the GC context switch.  */
562   if (nested_p)
563     ggc_pop_context ();
564   timevar_pop (TV_EXPAND);
565 }