OSDN Git Service

bd4bb0d1885ed72593386f405b61bf10edafaa09
[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: dump the gimplified, inlined, functions.  */
61
62 static struct tree_opt_pass pass_gimple = 
63 {
64   "gimple",                             /* name */
65   NULL,                                 /* gate */
66   NULL,                                 /* execute */
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
78 /* Gate: execute, or not, all of the non-trivial optimizations.  */
79
80 static bool
81 gate_all_optimizations (void)
82 {
83   return (optimize >= 1
84           /* Don't bother doing anything if the program has errors.  */
85           && !(errorcount || sorrycount));
86 }
87
88 static 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: 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
109 static void 
110 execute_cleanup_cfg_post_optimizing (void)
111 {
112   cleanup_tree_cfg ();
113   cleanup_dead_labels ();
114   group_case_labels ();
115 }
116
117 static 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
133 /* Pass: do the actions required to finish with tree-ssa optimization
134    passes.  */
135
136 static void
137 execute_free_datastructures (void)
138 {
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 ();
155
156   /* And get rid of annotations we no longer need.  */
157   delete_tree_cfg_annotations ();
158 }
159
160 static struct tree_opt_pass pass_free_datastructures =
161 {
162   NULL,                                 /* name */
163   NULL,                                 /* gate */
164   execute_free_datastructures,                  /* execute */
165   NULL,                                 /* sub */
166   NULL,                                 /* next */
167   0,                                    /* static_pass_number */
168   0,                                    /* tv_id */
169   PROP_cfg,                             /* properties_required */
170   0,                                    /* properties_provided */
171   0,                                    /* properties_destroyed */
172   0,                                    /* todo_flags_start */
173   0                                     /* todo_flags_finish */
174 };
175
176
177 /* Do the actions required to initialize internal data structures used
178    in tree-ssa optimization passes.  */
179
180 static void
181 execute_init_datastructures (void)
182 {
183   /* Allocate hash tables, arrays and other structures.  */
184   init_tree_ssa ();
185 }
186
187 static 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
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
207 static void
208 register_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
216   /* See below in dup_pass_1.  */
217   num[0] = '\0';
218   if (pass->static_pass_number)
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
228 static int 
229 register_dump_files (struct tree_opt_pass *pass, int properties)
230 {
231   do
232     {
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);
246       if (pass->sub)
247         properties = register_dump_files (pass->sub, properties);
248       pass = pass->next;
249     }
250   while (pass);
251
252   return properties;
253 }
254
255 /* Duplicate a pass that's to be run more than once.  */
256
257 static struct tree_opt_pass *
258 dup_pass_1 (struct tree_opt_pass *pass)
259 {
260   struct tree_opt_pass *new;
261
262   new = xmalloc (sizeof (*new));
263   memcpy (new, pass, sizeof (*new));
264
265   /* Indicate to register_dump_files that this pass has duplicates,
266      and so it should rename the dump file.  The first instance will
267      be < 0, and be number of duplicates = -static_pass_number + 1.
268      Subsequent instances will be > 0 and just the duplicate number.  */
269   if (pass->name)
270     {
271       int n, p = pass->static_pass_number;
272         
273       if (p)
274         n = -(--p) + 1;
275       else
276         n = 2, p = -1;
277
278       pass->static_pass_number = p;
279       new->static_pass_number = n;
280     }
281
282   return new;
283 }
284
285 /* Construct the pass tree.  */
286
287 void
288 init_tree_optimization_passes (void)
289 {
290   struct tree_opt_pass **p;
291
292 #define NEXT_PASS(PASS) (*p = &PASS, p = &(*p)->next)
293 #define DUP_PASS(PASS)  (*dup_pass_1 (&PASS))
294
295   p = &all_passes;
296   NEXT_PASS (pass_gimple);
297   NEXT_PASS (pass_remove_useless_stmts);
298   NEXT_PASS (pass_mudflap_1);
299   NEXT_PASS (pass_lower_cf);
300   NEXT_PASS (pass_lower_eh);
301   NEXT_PASS (pass_build_cfg);
302   NEXT_PASS (pass_tree_profile);
303   NEXT_PASS (pass_init_datastructures);
304   NEXT_PASS (pass_all_optimizations);
305   NEXT_PASS (pass_warn_function_return);
306   NEXT_PASS (pass_mudflap_2);
307   NEXT_PASS (pass_free_datastructures);
308   NEXT_PASS (pass_expand);
309   NEXT_PASS (pass_rest_of_compilation);
310   *p = NULL;
311
312   p = &pass_all_optimizations.sub;
313   NEXT_PASS (pass_referenced_vars);
314   NEXT_PASS (pass_build_pta);
315   NEXT_PASS (pass_build_ssa);
316   NEXT_PASS (pass_rename_ssa_copies);
317   NEXT_PASS (pass_early_warn_uninitialized);
318   NEXT_PASS (pass_dce);
319   NEXT_PASS (pass_dominator);
320   NEXT_PASS (pass_redundant_phi);
321   NEXT_PASS (DUP_PASS (pass_dce));
322   NEXT_PASS (pass_forwprop);
323   NEXT_PASS (pass_phiopt);
324   NEXT_PASS (pass_may_alias);
325   NEXT_PASS (pass_tail_recursion);
326   NEXT_PASS (pass_ch);
327   NEXT_PASS (pass_profile);
328   NEXT_PASS (pass_lower_complex);
329   NEXT_PASS (pass_sra);
330   NEXT_PASS (DUP_PASS (pass_rename_ssa_copies));
331   NEXT_PASS (DUP_PASS (pass_dominator));
332   NEXT_PASS (DUP_PASS (pass_redundant_phi));
333   NEXT_PASS (DUP_PASS (pass_dce));
334   NEXT_PASS (pass_dse);
335   NEXT_PASS (DUP_PASS (pass_may_alias));
336   NEXT_PASS (DUP_PASS (pass_forwprop));
337   NEXT_PASS (DUP_PASS (pass_phiopt));
338   NEXT_PASS (pass_ccp);
339   NEXT_PASS (DUP_PASS (pass_redundant_phi));
340   NEXT_PASS (pass_fold_builtins);
341   NEXT_PASS (pass_split_crit_edges);
342   NEXT_PASS (pass_pre);
343   NEXT_PASS (pass_loop);
344   NEXT_PASS (DUP_PASS (pass_dominator));
345   NEXT_PASS (DUP_PASS (pass_redundant_phi));
346   NEXT_PASS (pass_cd_dce);
347   NEXT_PASS (DUP_PASS (pass_dse));
348   NEXT_PASS (DUP_PASS (pass_forwprop));
349   NEXT_PASS (DUP_PASS (pass_phiopt));
350   NEXT_PASS (pass_tail_calls);
351   NEXT_PASS (pass_late_warn_uninitialized);
352   NEXT_PASS (pass_del_pta);
353   NEXT_PASS (pass_del_ssa);
354   NEXT_PASS (pass_nrv);
355   NEXT_PASS (pass_remove_useless_vars);
356   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
357   *p = NULL;
358
359   p = &pass_loop.sub;
360   NEXT_PASS (pass_loop_init);
361   NEXT_PASS (pass_lim);
362   NEXT_PASS (pass_loop_done);
363   *p = NULL;
364
365 #undef NEXT_PASS
366 #undef DUP_PASS
367
368   /* Register the passes with the tree dump code.  */
369   register_dump_files (all_passes, 0);
370 }
371
372 static void execute_pass_list (struct tree_opt_pass *);
373
374 static unsigned int last_verified;
375
376 static void
377 execute_todo (unsigned int flags)
378 {
379   if (flags & TODO_rename_vars)
380     {
381       rewrite_into_ssa (false);
382       bitmap_clear (vars_to_rename);
383     }
384
385   if ((flags & TODO_dump_func) && dump_file)
386     {
387       dump_function_to_file (current_function_decl,
388                              dump_file, dump_flags);
389
390       /* Flush the file.  If verification fails, we won't be able to
391          close the file before aborting.  */
392       fflush (dump_file);
393     }
394
395   if (flags & TODO_ggc_collect)
396     ggc_collect ();
397
398 #ifdef ENABLE_CHECKING
399   if (flags & TODO_verify_ssa)
400     verify_ssa ();
401   if (flags & TODO_verify_flow)
402     verify_flow_info ();
403   if (flags & TODO_verify_stmts)
404     verify_stmts ();
405 #endif
406 }
407
408 static bool
409 execute_one_pass (struct tree_opt_pass *pass)
410 {
411   unsigned int todo; 
412
413   /* See if we're supposed to run this pass.  */
414   if (pass->gate && !pass->gate ())
415     return false;
416
417   /* Note that the folders should only create gimple expressions.
418      This is a hack until the new folder is ready.  */
419   in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
420
421   /* Run pre-pass verification.  */
422   todo = pass->todo_flags_start & ~last_verified;
423   if (todo)
424     execute_todo (todo);
425
426   /* If a dump file name is present, open it if enabled.  */
427   if (pass->static_pass_number)
428     {
429       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
430       if (dump_file)
431         {
432           const char *dname, *aname;
433           dname = lang_hooks.decl_printable_name (current_function_decl, 2);
434           aname = (IDENTIFIER_POINTER
435                    (DECL_ASSEMBLER_NAME (current_function_decl)));
436           fprintf (dump_file, "\n;; Function %s (%s)\n\n", dname, aname);
437         }
438     }
439
440   /* If a timevar is present, start it.  */
441   if (pass->tv_id)
442     timevar_push (pass->tv_id);
443
444   /* Do it!  */
445   if (pass->execute)
446     pass->execute ();
447
448   /* Run post-pass cleanup and verification.  */
449   todo = pass->todo_flags_finish;
450   last_verified = todo & TODO_verify_all;
451   if (todo)
452     execute_todo (todo);
453
454   /* Close down timevar and dump file.  */
455   if (pass->tv_id)
456     timevar_pop (pass->tv_id);
457   if (dump_file)
458     {
459       dump_end (pass->static_pass_number, dump_file);
460       dump_file = NULL;
461     }
462
463   return true;
464 }
465
466 static void
467 execute_pass_list (struct tree_opt_pass *pass)
468 {
469   do
470     {
471       if (execute_one_pass (pass) && pass->sub)
472         execute_pass_list (pass->sub);
473       pass = pass->next;
474     }
475   while (pass);
476 }
477
478 \f
479 /* For functions-as-trees languages, this performs all optimization and
480    compilation for FNDECL.  */
481
482 void
483 tree_rest_of_compilation (tree fndecl, bool nested_p)
484 {
485   location_t saved_loc;
486   struct cgraph_node *saved_node = NULL, *node;
487
488   timevar_push (TV_EXPAND);
489
490   if (flag_unit_at_a_time && !cgraph_global_info_ready)
491     abort ();
492
493   /* Initialize the RTL code for the function.  */
494   current_function_decl = fndecl;
495   saved_loc = input_location;
496   input_location = DECL_SOURCE_LOCATION (fndecl);
497   init_function_start (fndecl);
498
499   /* Even though we're inside a function body, we still don't want to
500      call expand_expr to calculate the size of a variable-sized array.
501      We haven't necessarily assigned RTL to all variables yet, so it's
502      not safe to try to expand expressions involving them.  */
503   cfun->x_dont_save_pending_sizes_p = 1;
504
505   node = cgraph_node (fndecl);
506
507   /* We might need the body of this function so that we can expand
508      it inline somewhere else.  This means not lowering some constructs
509      such as exception handling.  */
510   if (cgraph_preserve_function_body_p (fndecl))
511     {
512       if (!flag_unit_at_a_time)
513         {
514           struct cgraph_edge *e;
515
516           saved_node = cgraph_clone_node (node);
517           for (e = saved_node->callees; e; e = e->next_callee)
518             if (!e->inline_failed)
519               cgraph_clone_inlined_nodes (e, true);
520         }
521       cfun->saved_tree = save_body (fndecl, &cfun->saved_args);
522     }
523
524   if (flag_inline_trees)
525     {
526       struct cgraph_edge *e;
527       for (e = node->callees; e; e = e->next_callee)
528         if (!e->inline_failed || warn_inline)
529           break;
530       if (e)
531         {
532           timevar_push (TV_INTEGRATION);
533           optimize_inline_calls (fndecl);
534           timevar_pop (TV_INTEGRATION);
535         }
536     }
537
538   if (!vars_to_rename)
539     vars_to_rename = BITMAP_XMALLOC ();
540
541   /* If this is a nested function, protect the local variables in the stack
542      above us from being collected while we're compiling this function.  */
543   if (nested_p)
544     ggc_push_context ();
545
546   /* Perform all tree transforms and optimizations.  */
547   execute_pass_list (all_passes);
548
549   /* Restore original body if still needed.  */
550   if (cfun->saved_tree)
551     {
552       DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
553       DECL_ARGUMENTS (fndecl) = cfun->saved_args;
554
555       /* When not in unit-at-a-time mode, we must preserve out of line copy
556          representing node before inlining.  Restore original outgoing edges
557          using clone we created earlier.  */
558       if (!flag_unit_at_a_time)
559         {
560           struct cgraph_edge *e;
561           while (node->callees)
562             cgraph_remove_edge (node->callees);
563           node->callees = saved_node->callees;
564           saved_node->callees = NULL;
565           for (e = saved_node->callees; e; e = e->next_callee)
566             e->caller = node;
567           cgraph_remove_node (saved_node);
568         }
569     }
570   else
571     DECL_SAVED_TREE (fndecl) = NULL;
572   cfun = 0;
573
574   /* If requested, warn about function definitions where the function will
575      return a value (usually of some struct or union type) which itself will
576      take up a lot of stack space.  */
577   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
578     {
579       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
580
581       if (ret_type && TYPE_SIZE_UNIT (ret_type)
582           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
583           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
584                                    larger_than_size))
585         {
586           unsigned int size_as_int
587             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
588
589           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
590             warning ("%Jsize of return value of '%D' is %u bytes",
591                      fndecl, fndecl, size_as_int);
592           else
593             warning ("%Jsize of return value of '%D' is larger than %wd bytes",
594                      fndecl, fndecl, larger_than_size);
595         }
596     }
597
598   if (!nested_p && !flag_inline_trees)
599     {
600       DECL_SAVED_TREE (fndecl) = NULL;
601       if (DECL_STRUCT_FUNCTION (fndecl) == 0
602           && !cgraph_node (fndecl)->origin)
603         {
604           /* Stop pointing to the local nodes about to be freed.
605              But DECL_INITIAL must remain nonzero so we know this
606              was an actual function definition.
607              For a nested function, this is done in c_pop_function_context.
608              If rest_of_compilation set this to 0, leave it 0.  */
609           if (DECL_INITIAL (fndecl) != 0)
610             DECL_INITIAL (fndecl) = error_mark_node;
611         }
612     }
613
614   input_location = saved_loc;
615
616   ggc_collect ();
617
618   /* Undo the GC context switch.  */
619   if (nested_p)
620     ggc_pop_context ();
621   timevar_pop (TV_EXPAND);
622 }