OSDN Git Service

2005-06-28 Thomas Koenig <Thomas.Koenig@online.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-optimize.c
1 /* Top-level control of tree optimizations.
2    Copyright 2001, 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 "ggc.h"
48 #include "cgraph.h"
49 #include "graph.h"
50 #include "cfgloop.h"
51 #include "except.h"
52
53 /* Global variables used to communicate with passes.  */
54 int dump_flags;
55 bool in_gimple_form;
56
57 /* The root of the compilation pass tree, once constructed.  */
58 static struct tree_opt_pass *all_passes, *all_ipa_passes, *all_lowering_passes;
59
60 /* Gate: execute, or not, all of the non-trivial optimizations.  */
61
62 static bool
63 gate_all_optimizations (void)
64 {
65   return (optimize >= 1
66           /* Don't bother doing anything if the program has errors.  */
67           && !(errorcount || sorrycount));
68 }
69
70 static struct tree_opt_pass pass_all_optimizations =
71 {
72   NULL,                                 /* name */
73   gate_all_optimizations,               /* gate */
74   NULL,                                 /* execute */
75   NULL,                                 /* sub */
76   NULL,                                 /* next */
77   0,                                    /* static_pass_number */
78   0,                                    /* tv_id */
79   0,                                    /* properties_required */
80   0,                                    /* properties_provided */
81   0,                                    /* properties_destroyed */
82   0,                                    /* todo_flags_start */
83   0,                                    /* todo_flags_finish */
84   0                                     /* letter */
85 };
86
87 static struct tree_opt_pass pass_early_local_passes =
88 {
89   NULL,                                 /* name */
90   gate_all_optimizations,               /* gate */
91   NULL,                                 /* execute */
92   NULL,                                 /* sub */
93   NULL,                                 /* next */
94   0,                                    /* static_pass_number */
95   0,                                    /* tv_id */
96   0,                                    /* properties_required */
97   0,                                    /* properties_provided */
98   0,                                    /* properties_destroyed */
99   0,                                    /* todo_flags_start */
100   0,                                    /* todo_flags_finish */
101   0                                     /* letter */
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_pre_ipa (void)
111 {
112   cleanup_tree_cfg ();
113 }
114
115 static struct tree_opt_pass pass_cleanup_cfg =
116 {
117   "cleanup_cfg",                        /* name */
118   NULL,                                 /* gate */
119   execute_cleanup_cfg_pre_ipa,          /* execute */
120   NULL,                                 /* sub */
121   NULL,                                 /* next */
122   0,                                    /* static_pass_number */
123   0,                                    /* tv_id */
124   PROP_cfg,                             /* properties_required */
125   0,                                    /* properties_provided */
126   0,                                    /* properties_destroyed */
127   0,                                    /* todo_flags_start */
128   TODO_dump_func,                                       /* todo_flags_finish */
129   0                                     /* letter */
130 };
131
132
133 /* Pass: cleanup the CFG just before expanding trees to RTL.
134    This is just a round of label cleanups and case node grouping
135    because after the tree optimizers have run such cleanups may
136    be necessary.  */
137
138 static void 
139 execute_cleanup_cfg_post_optimizing (void)
140 {
141   cleanup_tree_cfg ();
142   cleanup_dead_labels ();
143   group_case_labels ();
144 }
145
146 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing =
147 {
148   "final_cleanup",                      /* name */
149   NULL,                                 /* gate */
150   execute_cleanup_cfg_post_optimizing,  /* execute */
151   NULL,                                 /* sub */
152   NULL,                                 /* next */
153   0,                                    /* static_pass_number */
154   0,                                    /* tv_id */
155   PROP_cfg,                             /* properties_required */
156   0,                                    /* properties_provided */
157   0,                                    /* properties_destroyed */
158   0,                                    /* todo_flags_start */
159   TODO_dump_func,                                       /* todo_flags_finish */
160   0                                     /* letter */
161 };
162
163 /* Pass: do the actions required to finish with tree-ssa optimization
164    passes.  */
165
166 static void
167 execute_free_datastructures (void)
168 {
169   /* ??? This isn't the right place for this.  Worse, it got computed
170      more or less at random in various passes.  */
171   free_dominance_info (CDI_DOMINATORS);
172   free_dominance_info (CDI_POST_DOMINATORS);
173
174   /* Remove the ssa structures.  Do it here since this includes statement
175      annotations that need to be intact during disband_implicit_edges.  */
176   delete_tree_ssa ();
177 }
178
179 static struct tree_opt_pass pass_free_datastructures =
180 {
181   NULL,                                 /* name */
182   NULL,                                 /* gate */
183   execute_free_datastructures,                  /* execute */
184   NULL,                                 /* sub */
185   NULL,                                 /* next */
186   0,                                    /* static_pass_number */
187   0,                                    /* tv_id */
188   PROP_cfg,                             /* properties_required */
189   0,                                    /* properties_provided */
190   0,                                    /* properties_destroyed */
191   0,                                    /* todo_flags_start */
192   0,                                    /* todo_flags_finish */
193   0                                     /* letter */
194 };
195 /* Pass: free cfg annotations.  */
196
197 static void
198 execute_free_cfg_annotations (void)
199 {
200   basic_block bb;
201   block_stmt_iterator bsi;
202
203   /* Emit gotos for implicit jumps.  */
204   disband_implicit_edges ();
205
206   /* Remove annotations from every tree in the function.  */
207   FOR_EACH_BB (bb)
208     for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
209       {
210         tree stmt = bsi_stmt (bsi);
211         ggc_free (stmt->common.ann);
212         stmt->common.ann = NULL;
213       }
214
215   /* And get rid of annotations we no longer need.  */
216   delete_tree_cfg_annotations ();
217 }
218
219 static struct tree_opt_pass pass_free_cfg_annotations =
220 {
221   NULL,                                 /* name */
222   NULL,                                 /* gate */
223   execute_free_cfg_annotations,         /* execute */
224   NULL,                                 /* sub */
225   NULL,                                 /* next */
226   0,                                    /* static_pass_number */
227   0,                                    /* tv_id */
228   PROP_cfg,                             /* properties_required */
229   0,                                    /* properties_provided */
230   0,                                    /* properties_destroyed */
231   0,                                    /* todo_flags_start */
232   0,                                    /* todo_flags_finish */
233   0                                     /* letter */
234 };
235 /* Pass: fixup_cfg - IPA passes or compilation of earlier functions might've
236    changed some properties - such as marked functions nothrow.  Remove now
237    redundant edges and basic blocks.  */
238
239 static void
240 execute_fixup_cfg (void)
241 {
242   basic_block bb;
243   block_stmt_iterator bsi;
244
245   if (cfun->eh)
246     FOR_EACH_BB (bb)
247       {
248         for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
249           {
250             tree stmt = bsi_stmt (bsi);
251             tree call = get_call_expr_in (stmt);
252
253             if (call && call_expr_flags (call) & (ECF_CONST | ECF_PURE))
254               TREE_SIDE_EFFECTS (call) = 0;
255             if (!tree_could_throw_p (stmt) && lookup_stmt_eh_region (stmt))
256               remove_stmt_from_eh_region (stmt);
257           }
258         tree_purge_dead_eh_edges (bb);
259       }
260     
261   cleanup_tree_cfg ();
262 }
263
264 static struct tree_opt_pass pass_fixup_cfg =
265 {
266   NULL,                                 /* name */
267   NULL,                                 /* gate */
268   execute_fixup_cfg,                    /* execute */
269   NULL,                                 /* sub */
270   NULL,                                 /* next */
271   0,                                    /* static_pass_number */
272   0,                                    /* tv_id */
273   PROP_cfg,                             /* properties_required */
274   0,                                    /* properties_provided */
275   0,                                    /* properties_destroyed */
276   0,                                    /* todo_flags_start */
277   0,                                    /* todo_flags_finish */
278   0                                     /* letter */
279 };
280
281 /* Do the actions required to initialize internal data structures used
282    in tree-ssa optimization passes.  */
283
284 static void
285 execute_init_datastructures (void)
286 {
287   /* Allocate hash tables, arrays and other structures.  */
288   init_tree_ssa ();
289 }
290
291 static struct tree_opt_pass pass_init_datastructures =
292 {
293   NULL,                                 /* name */
294   NULL,                                 /* gate */
295   execute_init_datastructures,          /* execute */
296   NULL,                                 /* sub */
297   NULL,                                 /* next */
298   0,                                    /* static_pass_number */
299   0,                                    /* tv_id */
300   PROP_cfg,                             /* properties_required */
301   0,                                    /* properties_provided */
302   0,                                    /* properties_destroyed */
303   0,                                    /* todo_flags_start */
304   0,                                    /* todo_flags_finish */
305   0                                     /* letter */
306 };
307
308 /* Iterate over the pass tree allocating dump file numbers.  We want
309    to do this depth first, and independent of whether the pass is
310    enabled or not.  */
311
312 static void
313 register_one_dump_file (struct tree_opt_pass *pass, bool ipa, int n)
314 {
315   char *dot_name, *flag_name, *glob_name;
316   char num[10];
317
318   /* See below in next_pass_1.  */
319   num[0] = '\0';
320   if (pass->static_pass_number != -1)
321     sprintf (num, "%d", ((int) pass->static_pass_number < 0
322                          ? 1 : pass->static_pass_number));
323
324   dot_name = concat (".", pass->name, num, NULL);
325   if (ipa)
326     {
327       flag_name = concat ("ipa-", pass->name, num, NULL);
328       glob_name = concat ("ipa-", pass->name, NULL);
329       /* First IPA dump is cgraph that is dumped via separate channels.  */
330       pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
331                                                 TDF_IPA, n + 1, 0);
332     }
333   else if (pass->properties_provided & PROP_trees)
334     {
335       flag_name = concat ("tree-", pass->name, num, NULL);
336       glob_name = concat ("tree-", pass->name, NULL);
337       pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
338                                                 TDF_TREE, n + TDI_tree_all, 0);
339     }
340   else
341     {
342       flag_name = concat ("rtl-", pass->name, num, NULL);
343       glob_name = concat ("rtl-", pass->name, NULL);
344       pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
345                                                 TDF_RTL, n, pass->letter);
346     }
347 }
348
349 static int 
350 register_dump_files (struct tree_opt_pass *pass, bool ipa, int properties)
351 {
352   static int n = 0;
353   do
354     {
355       int new_properties;
356       int pass_number;
357
358       pass->properties_required = properties;
359       new_properties =
360         (properties | pass->properties_provided) & ~pass->properties_destroyed;
361
362       /* Reset the counter when we reach RTL-based passes.  */
363       if ((pass->properties_provided ^ pass->properties_required) & PROP_rtl)
364         n = 0;
365
366       pass_number = n;
367       if (pass->name)
368         n++;
369
370       if (pass->sub)
371         new_properties = register_dump_files (pass->sub, false, new_properties);
372
373       /* If we have a gate, combine the properties that we could have with
374          and without the pass being examined.  */
375       if (pass->gate)
376         properties &= new_properties;
377       else
378         properties = new_properties;
379
380       pass->properties_provided = properties;
381       if (pass->name)
382         register_one_dump_file (pass, ipa, pass_number);
383
384       pass = pass->next;
385     }
386   while (pass);
387
388   return properties;
389 }
390
391 /* Add a pass to the pass list. Duplicate the pass if it's already
392    in the list.  */
393
394 static struct tree_opt_pass **
395 next_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
396 {
397
398   /* A nonzero static_pass_number indicates that the
399      pass is already in the list.  */
400   if (pass->static_pass_number)
401     {
402       struct tree_opt_pass *new;
403
404       new = xmalloc (sizeof (*new));
405       memcpy (new, pass, sizeof (*new));
406
407       /* Indicate to register_dump_files that this pass has duplicates,
408          and so it should rename the dump file.  The first instance will
409          be -1, and be number of duplicates = -static_pass_number - 1.
410          Subsequent instances will be > 0 and just the duplicate number.  */
411       if (pass->name)
412         {
413           pass->static_pass_number -= 1;
414           new->static_pass_number = -pass->static_pass_number;
415         }
416       
417       *list = new;
418     }
419   else
420     {
421       pass->static_pass_number = -1;
422       *list = pass;
423     }  
424   
425   return &(*list)->next;
426           
427 }
428
429 /* Construct the pass tree.  */
430
431 void
432 init_tree_optimization_passes (void)
433 {
434   struct tree_opt_pass **p;
435
436 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &PASS))
437   /* Intraprocedural optimization passes.  */
438   p = &all_ipa_passes;
439   NEXT_PASS (pass_early_ipa_inline);
440   NEXT_PASS (pass_early_local_passes);
441   NEXT_PASS (pass_ipa_inline);
442   *p = NULL;
443
444   /* All passes needed to lower the function into shape optimizers can operate
445      on.  These passes are performed before interprocedural passes, unlike rest
446      of local passes (all_passes).  */
447   p = &all_lowering_passes;
448   NEXT_PASS (pass_remove_useless_stmts);
449   NEXT_PASS (pass_mudflap_1);
450   NEXT_PASS (pass_lower_cf); 
451   NEXT_PASS (pass_lower_eh); 
452   NEXT_PASS (pass_build_cfg); 
453   NEXT_PASS (pass_lower_complex_O0);
454   NEXT_PASS (pass_lower_vector);
455   NEXT_PASS (pass_warn_function_return);
456   NEXT_PASS (pass_early_tree_profile);
457   *p = NULL;
458
459   p = &pass_early_local_passes.sub;
460   NEXT_PASS (pass_tree_profile);
461   NEXT_PASS (pass_cleanup_cfg);
462   NEXT_PASS (pass_rebuild_cgraph_edges);
463   *p = NULL;
464
465   p = &all_passes;
466   NEXT_PASS (pass_fixup_cfg);
467   NEXT_PASS (pass_init_datastructures);
468   NEXT_PASS (pass_all_optimizations);
469   NEXT_PASS (pass_warn_function_noreturn);
470   NEXT_PASS (pass_mudflap_2);
471   NEXT_PASS (pass_free_datastructures);
472   NEXT_PASS (pass_free_cfg_annotations);
473   NEXT_PASS (pass_expand);
474   NEXT_PASS (pass_rest_of_compilation);
475   *p = NULL;
476
477   p = &pass_all_optimizations.sub;
478   NEXT_PASS (pass_referenced_vars);
479   NEXT_PASS (pass_create_structure_vars);
480   NEXT_PASS (pass_build_ssa);
481   NEXT_PASS (pass_build_pta);  
482   NEXT_PASS (pass_may_alias);
483   NEXT_PASS (pass_return_slot);
484   NEXT_PASS (pass_del_pta);  
485   NEXT_PASS (pass_rename_ssa_copies);
486   NEXT_PASS (pass_early_warn_uninitialized);
487
488   /* Initial scalar cleanups.  */
489   NEXT_PASS (pass_ccp);
490   NEXT_PASS (pass_fre);
491   NEXT_PASS (pass_dce);
492   NEXT_PASS (pass_forwprop);
493   NEXT_PASS (pass_copy_prop);
494   NEXT_PASS (pass_vrp);
495   NEXT_PASS (pass_dce);
496   NEXT_PASS (pass_merge_phi);
497   NEXT_PASS (pass_dominator);
498
499   NEXT_PASS (pass_phiopt);
500   NEXT_PASS (pass_build_pta);  
501   NEXT_PASS (pass_may_alias);
502   NEXT_PASS (pass_del_pta);  
503   NEXT_PASS (pass_tail_recursion);
504   NEXT_PASS (pass_profile);
505   NEXT_PASS (pass_ch);
506   NEXT_PASS (pass_stdarg);
507   NEXT_PASS (pass_lower_complex);
508   NEXT_PASS (pass_sra);
509   /* FIXME: SRA may generate arbitrary gimple code, exposing new
510      aliased and call-clobbered variables.  As mentioned below,
511      pass_may_alias should be a TODO item.  */
512   NEXT_PASS (pass_may_alias);
513   NEXT_PASS (pass_rename_ssa_copies);
514   NEXT_PASS (pass_dominator);
515   NEXT_PASS (pass_copy_prop);
516   NEXT_PASS (pass_dce);
517   NEXT_PASS (pass_dse);
518   NEXT_PASS (pass_may_alias);
519   NEXT_PASS (pass_forwprop);
520   NEXT_PASS (pass_phiopt);
521   NEXT_PASS (pass_object_sizes);
522   NEXT_PASS (pass_store_ccp);
523   NEXT_PASS (pass_store_copy_prop);
524   NEXT_PASS (pass_fold_builtins);
525   /* FIXME: May alias should a TODO but for 4.0.0,
526      we add may_alias right after fold builtins
527      which can create arbitrary GIMPLE.  */
528   NEXT_PASS (pass_may_alias);
529   NEXT_PASS (pass_cse_reciprocals);
530   NEXT_PASS (pass_split_crit_edges);
531   NEXT_PASS (pass_reassoc);
532   NEXT_PASS (pass_pre);
533   NEXT_PASS (pass_sink_code);
534   NEXT_PASS (pass_loop);
535   NEXT_PASS (pass_dominator);
536   NEXT_PASS (pass_copy_prop);
537   NEXT_PASS (pass_cd_dce);
538   /* FIXME: If DCE is not run before checking for uninitialized uses,
539      we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
540      However, this also causes us to misdiagnose cases that should be
541      real warnings (e.g., testsuite/gcc.dg/pr18501.c).
542      
543      To fix the false positives in uninit-5.c, we would have to
544      account for the predicates protecting the set and the use of each
545      variable.  Using a representation like Gated Single Assignment
546      may help.  */
547   NEXT_PASS (pass_late_warn_uninitialized);
548   NEXT_PASS (pass_dse);
549   NEXT_PASS (pass_forwprop);
550   NEXT_PASS (pass_phiopt);
551   NEXT_PASS (pass_tail_calls);
552   NEXT_PASS (pass_rename_ssa_copies);
553   NEXT_PASS (pass_uncprop);
554   NEXT_PASS (pass_del_ssa);
555   NEXT_PASS (pass_nrv);
556   NEXT_PASS (pass_remove_useless_vars);
557   NEXT_PASS (pass_mark_used_blocks);
558   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
559   *p = NULL;
560
561   p = &pass_loop.sub;
562   NEXT_PASS (pass_loop_init);
563   NEXT_PASS (pass_copy_prop);
564   NEXT_PASS (pass_lim);
565   NEXT_PASS (pass_unswitch);
566   NEXT_PASS (pass_scev_cprop);
567   NEXT_PASS (pass_record_bounds);
568   NEXT_PASS (pass_linear_transform);
569   NEXT_PASS (pass_iv_canon);
570   NEXT_PASS (pass_if_conversion);
571   NEXT_PASS (pass_vectorize);
572   /* NEXT_PASS (pass_may_alias) cannot be done again because the
573      vectorizer creates alias relations that are not supported by
574      pass_may_alias.  */
575   NEXT_PASS (pass_lower_vector_ssa);
576   NEXT_PASS (pass_complete_unroll);
577   NEXT_PASS (pass_iv_optimize);
578   NEXT_PASS (pass_loop_done);
579   *p = NULL;
580
581 #undef NEXT_PASS
582
583   register_dump_files (all_lowering_passes, false, PROP_gimple_any);
584   register_dump_files (all_passes, false, PROP_gimple_any
585                                           | PROP_gimple_lcf
586                                           | PROP_gimple_leh
587                                           | PROP_cfg);
588   register_dump_files (all_ipa_passes, true, PROP_gimple_any
589                                              | PROP_gimple_lcf
590                                              | PROP_gimple_leh
591                                              | PROP_cfg);
592 }
593
594 static unsigned int last_verified;
595
596 static void
597 execute_todo (struct tree_opt_pass *pass, unsigned int flags, bool use_required)
598 {
599   int properties 
600     = use_required ? pass->properties_required : pass->properties_provided;
601
602 #if defined ENABLE_CHECKING
603   if (need_ssa_update_p ())
604     gcc_assert (flags & TODO_update_ssa_any);
605 #endif
606
607   if (flags & TODO_update_ssa_any)
608     {
609       unsigned update_flags = flags & TODO_update_ssa_any;
610       update_ssa (update_flags);
611     }
612
613   if (flags & TODO_cleanup_cfg)
614     {
615       if (current_loops)
616         cleanup_tree_cfg_loop ();
617       else
618         cleanup_tree_cfg ();
619     }
620
621   if ((flags & TODO_dump_func)
622       && dump_file && current_function_decl)
623     {
624       if (properties & PROP_trees)
625         dump_function_to_file (current_function_decl,
626                                dump_file, dump_flags);
627       else if (properties & PROP_cfg)
628         print_rtl_with_bb (dump_file, get_insns ());
629       else
630         print_rtl (dump_file, get_insns ());
631
632       /* Flush the file.  If verification fails, we won't be able to
633          close the file before dieing.  */
634       fflush (dump_file);
635     }
636   if ((flags & TODO_dump_cgraph)
637       && dump_file && !current_function_decl)
638     {
639       dump_cgraph (dump_file);
640       /* Flush the file.  If verification fails, we won't be able to
641          close the file before aborting.  */
642       fflush (dump_file);
643     }
644
645   if (flags & TODO_ggc_collect)
646     {
647       ggc_collect ();
648     }
649
650 #if defined ENABLE_CHECKING
651   if ((pass->properties_required & PROP_ssa)
652       && !(pass->properties_destroyed & PROP_ssa))
653     verify_ssa (true);
654   if (flags & TODO_verify_flow)
655     verify_flow_info ();
656   if (flags & TODO_verify_stmts)
657     verify_stmts ();
658   if (flags & TODO_verify_loops)
659     verify_loop_closed_ssa ();
660 #endif
661 }
662
663 static bool
664 execute_one_pass (struct tree_opt_pass *pass)
665 {
666   unsigned int todo; 
667
668   /* See if we're supposed to run this pass.  */
669   if (pass->gate && !pass->gate ())
670     return false;
671
672   /* Note that the folders should only create gimple expressions.
673      This is a hack until the new folder is ready.  */
674   in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
675
676   /* Run pre-pass verification.  */
677   todo = pass->todo_flags_start & ~last_verified;
678   if (todo)
679     execute_todo (pass, todo, true);
680
681   /* If a dump file name is present, open it if enabled.  */
682   if (pass->static_pass_number != -1)
683     {
684       bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
685       dump_file_name = get_dump_file_name (pass->static_pass_number);
686       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
687       if (dump_file && current_function_decl)
688         {
689           const char *dname, *aname;
690           dname = lang_hooks.decl_printable_name (current_function_decl, 2);
691           aname = (IDENTIFIER_POINTER
692                    (DECL_ASSEMBLER_NAME (current_function_decl)));
693           fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
694              cfun->function_frequency == FUNCTION_FREQUENCY_HOT
695              ? " (hot)"
696              : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
697              ? " (unlikely executed)"
698              : "");
699         }
700
701       if (initializing_dump
702           && graph_dump_format != no_graph
703           && (pass->properties_provided & (PROP_cfg | PROP_rtl))
704               == (PROP_cfg | PROP_rtl))
705         clean_graph_dump_file (dump_file_name);
706     }
707
708   /* If a timevar is present, start it.  */
709   if (pass->tv_id)
710     timevar_push (pass->tv_id);
711
712   /* Do it!  */
713   if (pass->execute)
714     pass->execute ();
715
716   /* Stop timevar.  */
717   if (pass->tv_id)
718     timevar_pop (pass->tv_id);
719
720   if (dump_file
721       && (pass->properties_provided & (PROP_cfg | PROP_rtl))
722           == (PROP_cfg | PROP_rtl))
723     print_rtl_with_bb (dump_file, get_insns ());
724
725   /* Run post-pass cleanup and verification.  */
726   todo = pass->todo_flags_finish;
727   last_verified = todo & TODO_verify_all;
728   if (todo)
729     execute_todo (pass, todo, false);
730
731   /* Flush and close dump file.  */
732   if (dump_file_name)
733     {
734       free ((char *) dump_file_name);
735       dump_file_name = NULL;
736     }
737   if (dump_file)
738     {
739       dump_end (pass->static_pass_number, dump_file);
740       dump_file = NULL;
741     }
742
743   return true;
744 }
745
746 static void
747 execute_pass_list (struct tree_opt_pass *pass)
748 {
749   do
750     {
751       if (execute_one_pass (pass) && pass->sub)
752         execute_pass_list (pass->sub);
753       pass = pass->next;
754     }
755   while (pass);
756 }
757
758 /* Same as execute_pass_list but assume that subpasses of IPA passes
759    are local passes.  */
760 static void
761 execute_ipa_pass_list (struct tree_opt_pass *pass)
762 {
763   do
764     {
765       if (execute_one_pass (pass) && pass->sub)
766         {
767           struct cgraph_node *node;
768           for (node = cgraph_nodes; node; node = node->next)
769             if (node->analyzed)
770               {
771                 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
772                 current_function_decl = node->decl;
773                 execute_pass_list (pass->sub);
774                 free_dominance_info (CDI_DOMINATORS);
775                 free_dominance_info (CDI_POST_DOMINATORS);
776                 current_function_decl = NULL;
777                 pop_cfun ();
778                 ggc_collect ();
779               }
780         }
781       pass = pass->next;
782     }
783   while (pass);
784 }
785
786 void
787 tree_lowering_passes (tree fn)
788 {
789   tree saved_current_function_decl = current_function_decl;
790
791   current_function_decl = fn;
792   push_cfun (DECL_STRUCT_FUNCTION (fn));
793   tree_register_cfg_hooks ();
794   bitmap_obstack_initialize (NULL);
795   execute_pass_list (all_lowering_passes);
796   free_dominance_info (CDI_POST_DOMINATORS);
797   compact_blocks ();
798   current_function_decl = saved_current_function_decl;
799   bitmap_obstack_release (NULL);
800   pop_cfun ();
801 }
802
803 /* Execute all IPA passes.  */
804 void
805 ipa_passes (void)
806 {
807   cfun = NULL;
808   tree_register_cfg_hooks ();
809   bitmap_obstack_initialize (NULL);
810   execute_ipa_pass_list (all_ipa_passes);
811   bitmap_obstack_release (NULL);
812 }
813 \f
814
815 /* Update recursively all inlined_to pointers of functions
816    inlined into NODE to INLINED_TO.  */
817 static void
818 update_inlined_to_pointers (struct cgraph_node *node,
819                             struct cgraph_node *inlined_to)
820 {
821   struct cgraph_edge *e;
822   for (e = node->callees; e; e = e->next_callee)
823     {
824       if (e->callee->global.inlined_to)
825         {
826           e->callee->global.inlined_to = inlined_to;
827           update_inlined_to_pointers (e->callee, inlined_to);
828         }
829     }
830 }
831
832 \f
833 /* For functions-as-trees languages, this performs all optimization and
834    compilation for FNDECL.  */
835
836 void
837 tree_rest_of_compilation (tree fndecl)
838 {
839   location_t saved_loc;
840   struct cgraph_node *saved_node = NULL, *node;
841
842   timevar_push (TV_EXPAND);
843
844   gcc_assert (!flag_unit_at_a_time || cgraph_global_info_ready);
845
846   /* Initialize the RTL code for the function.  */
847   current_function_decl = fndecl;
848   saved_loc = input_location;
849   input_location = DECL_SOURCE_LOCATION (fndecl);
850   init_function_start (fndecl);
851
852   /* Even though we're inside a function body, we still don't want to
853      call expand_expr to calculate the size of a variable-sized array.
854      We haven't necessarily assigned RTL to all variables yet, so it's
855      not safe to try to expand expressions involving them.  */
856   cfun->x_dont_save_pending_sizes_p = 1;
857   cfun->after_inlining = true;
858
859   node = cgraph_node (fndecl);
860
861   /* We might need the body of this function so that we can expand
862      it inline somewhere else.  This means not lowering some constructs
863      such as exception handling.  */
864   if (cgraph_preserve_function_body_p (fndecl))
865     {
866       if (!flag_unit_at_a_time)
867         {
868           struct cgraph_edge *e;
869
870           saved_node = cgraph_clone_node (node, node->count, 1);
871           for (e = saved_node->callees; e; e = e->next_callee)
872             if (!e->inline_failed)
873               cgraph_clone_inlined_nodes (e, true);
874         }
875       cfun->saved_static_chain_decl = cfun->static_chain_decl;
876       save_body (fndecl, &cfun->saved_args, &cfun->saved_static_chain_decl);
877     }
878
879   if (flag_inline_trees)
880     {
881       struct cgraph_edge *e;
882       for (e = node->callees; e; e = e->next_callee)
883         if (!e->inline_failed || warn_inline)
884           break;
885       if (e)
886         {
887           timevar_push (TV_INTEGRATION);
888           optimize_inline_calls (fndecl);
889           timevar_pop (TV_INTEGRATION);
890         }
891     }
892   /* We are not going to maintain the cgraph edges up to date.
893      Kill it so it won't confuse us.  */
894   while (node->callees)
895     {
896       /* In non-unit-at-a-time we must mark all referenced functions as needed.
897          */
898       if (node->callees->callee->analyzed && !flag_unit_at_a_time)
899         cgraph_mark_needed_node (node->callees->callee);
900       cgraph_remove_edge (node->callees);
901     }
902
903   /* We are not going to maintain the cgraph edges up to date.
904      Kill it so it won't confuse us.  */
905   cgraph_node_remove_callees (node);
906
907
908   /* Initialize the default bitmap obstack.  */
909   bitmap_obstack_initialize (NULL);
910   bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
911   
912   tree_register_cfg_hooks ();
913   /* Perform all tree transforms and optimizations.  */
914   execute_pass_list (all_passes);
915   
916   bitmap_obstack_release (&reg_obstack);
917
918   /* Release the default bitmap obstack.  */
919   bitmap_obstack_release (NULL);
920   
921   /* Restore original body if still needed.  */
922   if (cfun->saved_cfg)
923     {
924       DECL_ARGUMENTS (fndecl) = cfun->saved_args;
925       cfun->cfg = cfun->saved_cfg;
926       cfun->eh = cfun->saved_eh;
927       cfun->saved_cfg = NULL;
928       cfun->saved_eh = NULL;
929       cfun->saved_args = NULL_TREE;
930       cfun->static_chain_decl = cfun->saved_static_chain_decl;
931       cfun->saved_static_chain_decl = NULL;
932       /* When not in unit-at-a-time mode, we must preserve out of line copy
933          representing node before inlining.  Restore original outgoing edges
934          using clone we created earlier.  */
935       if (!flag_unit_at_a_time)
936         {
937           struct cgraph_edge *e;
938
939           node = cgraph_node (current_function_decl);
940           cgraph_node_remove_callees (node);
941           node->callees = saved_node->callees;
942           saved_node->callees = NULL;
943           update_inlined_to_pointers (node, node);
944           for (e = node->callees; e; e = e->next_callee)
945             e->caller = node;
946           cgraph_remove_node (saved_node);
947         }
948     }
949   else
950     DECL_SAVED_TREE (fndecl) = NULL;
951   cfun = 0;
952
953   /* If requested, warn about function definitions where the function will
954      return a value (usually of some struct or union type) which itself will
955      take up a lot of stack space.  */
956   if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
957     {
958       tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
959
960       if (ret_type && TYPE_SIZE_UNIT (ret_type)
961           && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
962           && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
963                                    larger_than_size))
964         {
965           unsigned int size_as_int
966             = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
967
968           if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
969             warning (0, "%Jsize of return value of %qD is %u bytes",
970                      fndecl, fndecl, size_as_int);
971           else
972             warning (0, "%Jsize of return value of %qD is larger than %wd bytes",
973                      fndecl, fndecl, larger_than_size);
974         }
975     }
976
977   if (!flag_inline_trees)
978     {
979       DECL_SAVED_TREE (fndecl) = NULL;
980       if (DECL_STRUCT_FUNCTION (fndecl) == 0
981           && !cgraph_node (fndecl)->origin)
982         {
983           /* Stop pointing to the local nodes about to be freed.
984              But DECL_INITIAL must remain nonzero so we know this
985              was an actual function definition.
986              For a nested function, this is done in c_pop_function_context.
987              If rest_of_compilation set this to 0, leave it 0.  */
988           if (DECL_INITIAL (fndecl) != 0)
989             DECL_INITIAL (fndecl) = error_mark_node;
990         }
991     }
992
993   input_location = saved_loc;
994
995   ggc_collect ();
996   timevar_pop (TV_EXPAND);
997 }