OSDN Git Service

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