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