OSDN Git Service

Enable tree browser for all front ends.
[pf3gnuchains/gcc-fork.git] / gcc / passes.c
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 /* This is the top level of cc1/c++.
23    It parses command args, opens files, invokes the various passes
24    in the proper order, and counts the time used by each.
25    Error messages and low-level interface to malloc also handled here.  */
26
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux.  */
29 #undef FFS  /* Some systems define this in param.h.  */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
34
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
38
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
41 #endif
42
43 #include "line-map.h"
44 #include "input.h"
45 #include "tree.h"
46 #include "rtl.h"
47 #include "tm_p.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "insn-config.h"
51 #include "insn-flags.h"
52 #include "hard-reg-set.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "except.h"
56 #include "function.h"
57 #include "toplev.h"
58 #include "expr.h"
59 #include "basic-block.h"
60 #include "intl.h"
61 #include "ggc.h"
62 #include "graph.h"
63 #include "loop.h"
64 #include "regs.h"
65 #include "timevar.h"
66 #include "diagnostic.h"
67 #include "params.h"
68 #include "reload.h"
69 #include "dwarf2asm.h"
70 #include "integrate.h"
71 #include "real.h"
72 #include "debug.h"
73 #include "target.h"
74 #include "langhooks.h"
75 #include "cfglayout.h"
76 #include "cfgloop.h"
77 #include "hosthooks.h"
78 #include "cgraph.h"
79 #include "opts.h"
80 #include "coverage.h"
81 #include "value-prof.h"
82 #include "alloc-pool.h"
83
84 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
85 #include "dwarf2out.h"
86 #endif
87
88 #if defined(DBX_DEBUGGING_INFO) || defined(XCOFF_DEBUGGING_INFO)
89 #include "dbxout.h"
90 #endif
91
92 #ifdef SDB_DEBUGGING_INFO
93 #include "sdbout.h"
94 #endif
95
96 #ifdef XCOFF_DEBUGGING_INFO
97 #include "xcoffout.h"           /* Needed for external data
98                                    declarations for e.g. AIX 4.x.  */
99 #endif
100
101 #ifndef HAVE_conditional_execution
102 #define HAVE_conditional_execution 0
103 #endif
104
105 /* Format to use to print dumpfile index value */
106 #ifndef DUMPFILE_FORMAT
107 #define DUMPFILE_FORMAT ".%02d."
108 #endif
109
110 /* Describes a dump file.  */
111
112 struct dump_file_info
113 {
114   /* The unique extension to apply, e.g. ".jump".  */
115   const char *const extension;
116
117   /* The -d<c> character that enables this dump file.  */
118   char const debug_switch;
119
120   /* True if there is a corresponding graph dump file.  */
121   char const graph_dump_p;
122
123   /* True if the user selected this dump.  */
124   char enabled;
125
126   /* True if the files have been initialized (ie truncated).  */
127   char initialized;
128 };
129
130 /* Enumerate the extant dump files.  */
131
132 enum dump_file_index
133 {
134   DFI_cgraph,
135   DFI_rtl,
136   DFI_sibling,
137   DFI_eh,
138   DFI_jump,
139   DFI_null,
140   DFI_cse,
141   DFI_addressof,
142   DFI_gcse,
143   DFI_loop,
144   DFI_bypass,
145   DFI_cfg,
146   DFI_bp,
147   DFI_vpt,
148   DFI_ce1,
149   DFI_tracer,
150   DFI_loop2,
151   DFI_web,
152   DFI_cse2,
153   DFI_life,
154   DFI_combine,
155   DFI_ce2,
156   DFI_regmove,
157   DFI_sched,
158   DFI_lreg,
159   DFI_greg,
160   DFI_postreload,
161   DFI_gcse2,
162   DFI_flow2,
163   DFI_peephole2,
164   DFI_ce3,
165   DFI_rnreg,
166   DFI_bbro,
167   DFI_branch_target_load,
168   DFI_sched2,
169   DFI_stack,
170   DFI_vartrack,
171   DFI_mach,
172   DFI_dbr,
173   DFI_MAX
174 };
175
176 /* Describes all the dump files.  Should be kept in order of the
177    pass and in sync with dump_file_index above.
178
179    Remaining -d letters:
180
181         "   e        m   q         "
182         "          K   O Q     WXY "
183 */
184
185 static struct dump_file_info dump_file_tbl[DFI_MAX] =
186 {
187   { "cgraph",   'U', 0, 0, 0 },
188   { "rtl",      'r', 0, 0, 0 },
189   { "sibling",  'i', 0, 0, 0 },
190   { "eh",       'h', 0, 0, 0 },
191   { "jump",     'j', 0, 0, 0 },
192   { "null",     'u', 0, 0, 0 },
193   { "cse",      's', 0, 0, 0 },
194   { "addressof", 'F', 0, 0, 0 },
195   { "gcse",     'G', 1, 0, 0 },
196   { "loop",     'L', 1, 0, 0 },
197   { "bypass",   'G', 1, 0, 0 }, /* Yes, duplicate enable switch.  */
198   { "cfg",      'f', 1, 0, 0 },
199   { "bp",       'b', 1, 0, 0 },
200   { "vpt",      'V', 1, 0, 0 },
201   { "ce1",      'C', 1, 0, 0 },
202   { "tracer",   'T', 1, 0, 0 },
203   { "loop2",    'L', 1, 0, 0 },
204   { "web",      'Z', 0, 0, 0 },
205   { "cse2",     't', 1, 0, 0 },
206   { "life",     'f', 1, 0, 0 }, /* Yes, duplicate enable switch.  */
207   { "combine",  'c', 1, 0, 0 },
208   { "ce2",      'C', 1, 0, 0 },
209   { "regmove",  'N', 1, 0, 0 },
210   { "sched",    'S', 1, 0, 0 },
211   { "lreg",     'l', 1, 0, 0 },
212   { "greg",     'g', 1, 0, 0 },
213   { "postreload", 'o', 1, 0, 0 },
214   { "gcse2",    'J', 0, 0, 0 },
215   { "flow2",    'w', 1, 0, 0 },
216   { "peephole2", 'z', 1, 0, 0 },
217   { "ce3",      'E', 1, 0, 0 },
218   { "rnreg",    'n', 1, 0, 0 },
219   { "bbro",     'B', 1, 0, 0 },
220   { "btl",      'd', 1, 0, 0 }, /* Yes, duplicate enable switch.  */
221   { "sched2",   'R', 1, 0, 0 },
222   { "stack",    'k', 1, 0, 0 },
223   { "vartrack", 'V', 1, 0, 0 }, /* Yes, duplicate enable switch.  */
224   { "mach",     'M', 1, 0, 0 },
225   { "dbr",      'd', 0, 0, 0 },
226 };
227
228 /* Routine to open a dump file.  Return true if the dump file is enabled.  */
229
230 static int
231 open_dump_file (enum dump_file_index index, tree decl)
232 {
233   char *dump_name;
234   const char *open_arg;
235   char seq[16];
236
237   if (! dump_file_tbl[index].enabled)
238     return 0;
239
240   timevar_push (TV_DUMP);
241   if (dump_file != NULL)
242     fclose (dump_file);
243
244   sprintf (seq, DUMPFILE_FORMAT, index);
245
246   if (! dump_file_tbl[index].initialized)
247     {
248       /* If we've not initialized the files, do so now.  */
249       if (graph_dump_format != no_graph
250           && dump_file_tbl[index].graph_dump_p)
251         {
252           dump_name = concat (seq, dump_file_tbl[index].extension, NULL);
253           clean_graph_dump_file (dump_base_name, dump_name);
254           free (dump_name);
255         }
256       dump_file_tbl[index].initialized = 1;
257       open_arg = "w";
258     }
259   else
260     open_arg = "a";
261
262   dump_name = concat (dump_base_name, seq,
263                       dump_file_tbl[index].extension, NULL);
264
265   dump_file = fopen (dump_name, open_arg);
266   if (dump_file == NULL)
267     fatal_error ("can't open %s: %m", dump_name);
268
269   free (dump_name);
270
271   if (decl)
272     fprintf (dump_file, "\n;; Function %s%s\n\n",
273              lang_hooks.decl_printable_name (decl, 2),
274              cfun->function_frequency == FUNCTION_FREQUENCY_HOT
275              ? " (hot)"
276              : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
277              ? " (unlikely executed)"
278              : "");
279
280   timevar_pop (TV_DUMP);
281   return 1;
282 }
283
284 /* Routine to close a dump file.  */
285
286 static void
287 close_dump_file (enum dump_file_index index,
288                  void (*func) (FILE *, rtx),
289                  rtx insns)
290 {
291   if (! dump_file)
292     return;
293
294   timevar_push (TV_DUMP);
295   if (insns
296       && graph_dump_format != no_graph
297       && dump_file_tbl[index].graph_dump_p)
298     {
299       char seq[16];
300       char *suffix;
301
302       sprintf (seq, DUMPFILE_FORMAT, index);
303       suffix = concat (seq, dump_file_tbl[index].extension, NULL);
304       print_rtl_graph_with_bb (dump_base_name, suffix, insns);
305       free (suffix);
306     }
307
308   if (func && insns)
309     func (dump_file, insns);
310
311   fflush (dump_file);
312   fclose (dump_file);
313
314   dump_file = NULL;
315   timevar_pop (TV_DUMP);
316 }
317
318 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
319    and TYPE_DECL nodes.
320
321    This does nothing for local (non-static) variables, unless the
322    variable is a register variable with an ASMSPEC.  In that case, or
323    if the variable is not an automatic, it sets up the RTL and
324    outputs any assembler code (label definition, storage allocation
325    and initialization).
326
327    DECL is the declaration.  If ASMSPEC is nonzero, it specifies
328    the assembler symbol name to be used.  TOP_LEVEL is nonzero
329    if this declaration is not within a function.  */
330
331 void
332 rest_of_decl_compilation (tree decl,
333                           const char *asmspec,
334                           int top_level,
335                           int at_end)
336 {
337   /* We deferred calling assemble_alias so that we could collect
338      other attributes such as visibility.  Emit the alias now.  */
339   {
340     tree alias;
341     alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
342     if (alias)
343       {
344         alias = TREE_VALUE (TREE_VALUE (alias));
345         alias = get_identifier (TREE_STRING_POINTER (alias));
346         assemble_alias (decl, alias);
347       }
348   }
349
350   /* Forward declarations for nested functions are not "external",
351      but we need to treat them as if they were.  */
352   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
353       || TREE_CODE (decl) == FUNCTION_DECL)
354     {
355       timevar_push (TV_VARCONST);
356
357       if (asmspec)
358         make_decl_rtl (decl, asmspec);
359
360       /* Don't output anything when a tentative file-scope definition
361          is seen.  But at end of compilation, do output code for them.
362
363          We do output all variables when unit-at-a-time is active and rely on
364          callgraph code to defer them except for forward declarations
365          (see gcc.c-torture/compile/920624-1.c) */
366       if ((at_end
367            || !DECL_DEFER_OUTPUT (decl)
368            || (flag_unit_at_a_time && DECL_INITIAL (decl)))
369           && !DECL_EXTERNAL (decl))
370         {
371           if (flag_unit_at_a_time && !cgraph_global_info_ready
372               && TREE_CODE (decl) != FUNCTION_DECL && top_level)
373             cgraph_varpool_finalize_decl (decl);
374           else
375             assemble_variable (decl, top_level, at_end, 0);
376         }
377
378 #ifdef ASM_FINISH_DECLARE_OBJECT
379       if (decl == last_assemble_variable_decl)
380         {
381           ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
382                                      top_level, at_end);
383         }
384 #endif
385
386       timevar_pop (TV_VARCONST);
387     }
388   else if (DECL_REGISTER (decl) && asmspec != 0)
389     {
390       if (decode_reg_name (asmspec) >= 0)
391         {
392           SET_DECL_RTL (decl, NULL_RTX);
393           make_decl_rtl (decl, asmspec);
394         }
395       else
396         {
397           error ("%Hinvalid register name `%s' for register variable",
398                  &DECL_SOURCE_LOCATION (decl), asmspec);
399           DECL_REGISTER (decl) = 0;
400           if (!top_level)
401             expand_decl (decl);
402         }
403     }
404   else if (TREE_CODE (decl) == TYPE_DECL)
405     {
406       timevar_push (TV_SYMOUT);
407       debug_hooks->type_decl (decl, !top_level);
408       timevar_pop (TV_SYMOUT);
409     }
410 }
411
412 /* Called after finishing a record, union or enumeral type.  */
413
414 void
415 rest_of_type_compilation (tree type, int toplev)
416 {
417   /* Avoid confusing the debug information machinery when there are
418      errors.  */
419   if (errorcount != 0 || sorrycount != 0)
420     return;
421
422   timevar_push (TV_SYMOUT);
423   debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
424   timevar_pop (TV_SYMOUT);
425 }
426
427 /* Turn the RTL into assembly.  */
428 static void
429 rest_of_handle_final (tree decl, rtx insns)
430 {
431   timevar_push (TV_FINAL);
432   {
433     rtx x;
434     const char *fnname;
435
436     /* Get the function's name, as described by its RTL.  This may be
437        different from the DECL_NAME name used in the source file.  */
438
439     x = DECL_RTL (decl);
440     if (GET_CODE (x) != MEM)
441       abort ();
442     x = XEXP (x, 0);
443     if (GET_CODE (x) != SYMBOL_REF)
444       abort ();
445     fnname = XSTR (x, 0);
446
447     assemble_start_function (decl, fnname);
448     final_start_function (insns, asm_out_file, optimize);
449     final (insns, asm_out_file, optimize, 0);
450     final_end_function ();
451
452 #ifdef IA64_UNWIND_INFO
453     /* ??? The IA-64 ".handlerdata" directive must be issued before
454        the ".endp" directive that closes the procedure descriptor.  */
455     output_function_exception_table ();
456 #endif
457
458     assemble_end_function (decl, fnname);
459
460 #ifndef IA64_UNWIND_INFO
461     /* Otherwise, it feels unclean to switch sections in the middle.  */
462     output_function_exception_table ();
463 #endif
464
465     if (! quiet_flag)
466       fflush (asm_out_file);
467
468     /* Release all memory allocated by flow.  */
469     free_basic_block_vars ();
470
471     /* Release all memory held by regsets now.  */
472     regset_release_memory ();
473   }
474   timevar_pop (TV_FINAL);
475
476   ggc_collect ();
477 }
478
479 #ifdef DELAY_SLOTS
480 /* Run delay slot optimization.  */
481 static void
482 rest_of_handle_delay_slots (tree decl, rtx insns)
483 {
484   timevar_push (TV_DBR_SCHED);
485   open_dump_file (DFI_dbr, decl);
486
487   dbr_schedule (insns, dump_file);
488
489   close_dump_file (DFI_dbr, print_rtl, insns);
490   timevar_pop (TV_DBR_SCHED);
491
492   ggc_collect ();
493 }
494 #endif
495
496 #ifdef STACK_REGS
497 /* Convert register usage from flat register file usage to a stack
498    register file.  */
499 static void
500 rest_of_handle_stack_regs (tree decl, rtx insns)
501 {
502 #if defined (HAVE_ATTR_length)
503   /* If flow2 creates new instructions which need splitting
504      and scheduling after reload is not done, they might not be
505      split until final which doesn't allow splitting
506      if HAVE_ATTR_length.  */
507 #ifdef INSN_SCHEDULING
508   if (optimize && !flag_schedule_insns_after_reload)
509 #else
510   if (optimize)
511 #endif
512     {
513       timevar_push (TV_SHORTEN_BRANCH);
514       split_all_insns (1);
515       timevar_pop (TV_SHORTEN_BRANCH);
516     }
517 #endif
518
519   timevar_push (TV_REG_STACK);
520   open_dump_file (DFI_stack, decl);
521
522   if (reg_to_stack (dump_file) && optimize)
523     {
524       if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
525                        | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
526           && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
527         {
528           reorder_basic_blocks ();
529           cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
530         }
531     }
532
533   close_dump_file (DFI_stack, print_rtl_with_bb, insns);
534   timevar_pop (TV_REG_STACK);
535
536   ggc_collect ();
537 }
538 #endif
539
540 /* Track the variables, ie. compute where the variable is stored at each position in function.  */
541 static void
542 rest_of_handle_variable_tracking (tree decl, rtx insns)
543 {
544   timevar_push (TV_VAR_TRACKING);
545   open_dump_file (DFI_vartrack, decl);
546
547   variable_tracking_main ();
548
549   close_dump_file (DFI_vartrack, print_rtl_with_bb, insns);
550   timevar_pop (TV_VAR_TRACKING);
551 }
552
553 /* Machine independent reorg pass.  */
554 static void
555 rest_of_handle_machine_reorg (tree decl, rtx insns)
556 {
557   timevar_push (TV_MACH_DEP);
558   open_dump_file (DFI_mach, decl);
559
560   targetm.machine_dependent_reorg ();
561
562   close_dump_file (DFI_mach, print_rtl, insns);
563   timevar_pop (TV_MACH_DEP);
564
565   ggc_collect ();
566 }
567
568
569 /* Run new register allocator.  Return TRUE if we must exit
570    rest_of_compilation upon return.  */
571 static bool
572 rest_of_handle_new_regalloc (tree decl, rtx insns)
573 {
574   int failure;
575
576   delete_trivially_dead_insns (insns, max_reg_num ());
577   reg_alloc ();
578
579   timevar_pop (TV_LOCAL_ALLOC);
580   if (dump_file_tbl[DFI_lreg].enabled)
581     {
582       timevar_push (TV_DUMP);
583
584       close_dump_file (DFI_lreg, NULL, NULL);
585       timevar_pop (TV_DUMP);
586     }
587
588   /* XXX clean up the whole mess to bring live info in shape again.  */
589   timevar_push (TV_GLOBAL_ALLOC);
590   open_dump_file (DFI_greg, decl);
591
592   build_insn_chain (insns);
593   failure = reload (insns, 0);
594
595   timevar_pop (TV_GLOBAL_ALLOC);
596
597   if (dump_file_tbl[DFI_greg].enabled)
598     {
599       timevar_push (TV_DUMP);
600
601       dump_global_regs (dump_file);
602
603       close_dump_file (DFI_greg, print_rtl_with_bb, insns);
604       timevar_pop (TV_DUMP);
605     }
606
607   if (failure)
608     return true;
609
610   reload_completed = 1;
611
612   return false;
613 }
614
615 /* Run old register allocator.  Return TRUE if we must exit
616    rest_of_compilation upon return.  */
617 static bool
618 rest_of_handle_old_regalloc (tree decl, rtx insns)
619 {
620   int failure;
621   int rebuild_notes;
622
623   /* Allocate the reg_renumber array.  */
624   allocate_reg_info (max_regno, FALSE, TRUE);
625
626   /* And the reg_equiv_memory_loc array.  */
627   reg_equiv_memory_loc = xcalloc (max_regno, sizeof (rtx));
628
629   allocate_initial_values (reg_equiv_memory_loc);
630
631   regclass (insns, max_reg_num (), dump_file);
632   rebuild_notes = local_alloc ();
633
634   timevar_pop (TV_LOCAL_ALLOC);
635
636   /* Local allocation may have turned an indirect jump into a direct
637      jump.  If so, we must rebuild the JUMP_LABEL fields of jumping
638      instructions.  */
639   if (rebuild_notes)
640     {
641       timevar_push (TV_JUMP);
642
643       rebuild_jump_labels (insns);
644       purge_all_dead_edges (0);
645
646       timevar_pop (TV_JUMP);
647     }
648
649   if (dump_file_tbl[DFI_lreg].enabled)
650     {
651       timevar_push (TV_DUMP);
652
653       dump_flow_info (dump_file);
654       dump_local_alloc (dump_file);
655
656       close_dump_file (DFI_lreg, print_rtl_with_bb, insns);
657       timevar_pop (TV_DUMP);
658     }
659
660   ggc_collect ();
661
662   timevar_push (TV_GLOBAL_ALLOC);
663   open_dump_file (DFI_greg, decl);
664
665   /* If optimizing, allocate remaining pseudo-regs.  Do the reload
666      pass fixing up any insns that are invalid.  */
667
668   if (optimize)
669     failure = global_alloc (dump_file);
670   else
671     {
672       build_insn_chain (insns);
673       failure = reload (insns, 0);
674     }
675
676   timevar_pop (TV_GLOBAL_ALLOC);
677
678   if (dump_file_tbl[DFI_greg].enabled)
679     {
680       timevar_push (TV_DUMP);
681
682       dump_global_regs (dump_file);
683
684       close_dump_file (DFI_greg, print_rtl_with_bb, insns);
685       timevar_pop (TV_DUMP);
686     }
687
688   return failure;
689 }
690
691 /* Run the regrename and cprop passes.  */
692 static void
693 rest_of_handle_regrename (tree decl, rtx insns)
694 {
695   timevar_push (TV_RENAME_REGISTERS);
696   open_dump_file (DFI_rnreg, decl);
697
698   if (flag_rename_registers)
699     regrename_optimize ();
700   if (flag_cprop_registers)
701     copyprop_hardreg_forward ();
702
703   close_dump_file (DFI_rnreg, print_rtl_with_bb, insns);
704   timevar_pop (TV_RENAME_REGISTERS);
705 }
706
707 /* Reorder basic blocks.  */
708 static void
709 rest_of_handle_reorder_blocks (tree decl, rtx insns)
710 {
711   bool changed;
712   open_dump_file (DFI_bbro, decl);
713
714   /* Last attempt to optimize CFG, as scheduling, peepholing and insn
715      splitting possibly introduced more crossjumping opportunities.  */
716   changed = cleanup_cfg (CLEANUP_EXPENSIVE
717                          | (!HAVE_conditional_execution
718                             ? CLEANUP_UPDATE_LIFE : 0));
719
720   if (flag_sched2_use_traces && flag_schedule_insns_after_reload)
721     tracer ();
722   if (flag_reorder_blocks || flag_reorder_blocks_and_partition)
723     reorder_basic_blocks ();
724   if (flag_reorder_blocks || flag_reorder_blocks_and_partition
725       || (flag_sched2_use_traces && flag_schedule_insns_after_reload))
726     changed |= cleanup_cfg (CLEANUP_EXPENSIVE
727                             | (!HAVE_conditional_execution
728                                ? CLEANUP_UPDATE_LIFE : 0));
729
730   /* On conditional execution targets we can not update the life cheaply, so
731      we deffer the updating to after both cleanups.  This may lose some cases
732      but should not be terribly bad.  */
733   if (changed && HAVE_conditional_execution)
734     update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
735                       PROP_DEATH_NOTES);
736   close_dump_file (DFI_bbro, print_rtl_with_bb, insns);
737 }
738
739 #ifdef INSN_SCHEDULING
740 /* Run instruction scheduler.  */
741 static void
742 rest_of_handle_sched (tree decl, rtx insns)
743 {
744   timevar_push (TV_SCHED);
745
746   /* Print function header into sched dump now
747      because doing the sched analysis makes some of the dump.  */
748   if (optimize > 0 && flag_schedule_insns)
749     {
750       open_dump_file (DFI_sched, decl);
751
752       /* Do control and data sched analysis,
753          and write some of the results to dump file.  */
754
755       schedule_insns (dump_file);
756
757       close_dump_file (DFI_sched, print_rtl_with_bb, insns);
758     }
759   timevar_pop (TV_SCHED);
760
761   ggc_collect ();
762 }
763
764 /* Run second scheduling pass after reload.  */
765 static void
766 rest_of_handle_sched2 (tree decl, rtx insns)
767 {
768   timevar_push (TV_SCHED2);
769   open_dump_file (DFI_sched2, decl);
770
771   /* Do control and data sched analysis again,
772      and write some more of the results to dump file.  */
773
774   split_all_insns (1);
775
776   if (flag_sched2_use_superblocks || flag_sched2_use_traces)
777     {
778       schedule_ebbs (dump_file);
779       /* No liveness updating code yet, but it should be easy to do.
780          reg-stack recomputes the liveness when needed for now.  */
781       count_or_remove_death_notes (NULL, 1);
782       cleanup_cfg (CLEANUP_EXPENSIVE);
783     }
784   else
785     schedule_insns (dump_file);
786
787   close_dump_file (DFI_sched2, print_rtl_with_bb, insns);
788   timevar_pop (TV_SCHED2);
789
790   ggc_collect ();
791 }
792 #endif
793
794 static void
795 rest_of_handle_gcse2 (tree decl, rtx insns)
796 {
797   open_dump_file (DFI_gcse2, decl);
798
799   gcse_after_reload_main (insns, dump_file);
800   rebuild_jump_labels (insns);
801   delete_trivially_dead_insns (insns, max_reg_num ());
802   close_dump_file (DFI_gcse2, print_rtl_with_bb, insns);
803
804   ggc_collect ();
805
806 #ifdef ENABLE_CHECKING
807   verify_flow_info ();
808 #endif
809 }
810
811 /* Register allocation pre-pass, to reduce number of moves necessary
812    for two-address machines.  */
813 static void
814 rest_of_handle_regmove (tree decl, rtx insns)
815 {
816   timevar_push (TV_REGMOVE);
817   open_dump_file (DFI_regmove, decl);
818
819   regmove_optimize (insns, max_reg_num (), dump_file);
820
821   cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
822   close_dump_file (DFI_regmove, print_rtl_with_bb, insns);
823   timevar_pop (TV_REGMOVE);
824
825   ggc_collect ();
826 }
827
828 /* Run tracer.  */
829 static void
830 rest_of_handle_tracer (tree decl, rtx insns)
831 {
832   open_dump_file (DFI_tracer, decl);
833   if (dump_file)
834     dump_flow_info (dump_file);
835   tracer ();
836   cleanup_cfg (CLEANUP_EXPENSIVE);
837   reg_scan (insns, max_reg_num (), 0);
838   close_dump_file (DFI_tracer, print_rtl_with_bb, get_insns ());
839 }
840
841 /* If-conversion and CFG cleanup.  */
842 static void
843 rest_of_handle_if_conversion (tree decl, rtx insns)
844 {
845   open_dump_file (DFI_ce1, decl);
846   if (flag_if_conversion)
847     {
848       timevar_push (TV_IFCVT);
849       if (dump_file)
850         dump_flow_info (dump_file);
851       cleanup_cfg (CLEANUP_EXPENSIVE);
852       reg_scan (insns, max_reg_num (), 0);
853       if_convert (0);
854       timevar_pop (TV_IFCVT);
855     }
856   timevar_push (TV_JUMP);
857   cleanup_cfg (CLEANUP_EXPENSIVE);
858   reg_scan (insns, max_reg_num (), 0);
859   timevar_pop (TV_JUMP);
860   close_dump_file (DFI_ce1, print_rtl_with_bb, get_insns ());
861 }
862
863 /* Rerun if-conversion, as combine may have simplified things enough
864    to now meet sequence length restrictions.  */
865 static void
866 rest_of_handle_if_after_combine (tree decl, rtx insns)
867 {
868   timevar_push (TV_IFCVT);
869   open_dump_file (DFI_ce2, decl);
870
871   no_new_pseudos = 0;
872   if_convert (1);
873   no_new_pseudos = 1;
874
875   close_dump_file (DFI_ce2, print_rtl_with_bb, insns);
876   timevar_pop (TV_IFCVT);
877 }
878
879 static void
880 rest_of_handle_web (tree decl, rtx insns)
881 {
882   open_dump_file (DFI_web, decl);
883   timevar_push (TV_WEB);
884   web_main ();
885   delete_trivially_dead_insns (insns, max_reg_num ());
886   cleanup_cfg (CLEANUP_EXPENSIVE);
887
888   timevar_pop (TV_WEB);
889   close_dump_file (DFI_web, print_rtl_with_bb, insns);
890   reg_scan (get_insns (), max_reg_num (), 0);
891 }
892
893 /* Do branch profiling and static profile estimation passes.  */
894 static void
895 rest_of_handle_branch_prob (tree decl, rtx insns)
896 {
897   struct loops loops;
898
899   timevar_push (TV_BRANCH_PROB);
900   open_dump_file (DFI_bp, decl);
901
902   if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
903     branch_prob ();
904
905   /* Discover and record the loop depth at the head of each basic
906      block.  The loop infrastructure does the real job for us.  */
907   flow_loops_find (&loops, LOOP_TREE);
908
909   if (dump_file)
910     flow_loops_dump (&loops, dump_file, NULL, 0);
911
912   /* Estimate using heuristics if no profiling info is available.  */
913   if (flag_guess_branch_prob)
914     estimate_probability (&loops);
915
916   flow_loops_free (&loops);
917   free_dominance_info (CDI_DOMINATORS);
918   close_dump_file (DFI_bp, print_rtl_with_bb, insns);
919   timevar_pop (TV_BRANCH_PROB);
920 }
921
922 /* Do optimizations based on expression value profiles.  */
923 static void
924 rest_of_handle_value_profile_transformations (tree decl, rtx insns)
925 {
926   open_dump_file (DFI_vpt, decl);
927   timevar_push (TV_VPT);
928
929   if (value_profile_transformations ())
930     cleanup_cfg (CLEANUP_EXPENSIVE);
931
932   timevar_pop (TV_VPT);
933   close_dump_file (DFI_vpt, print_rtl_with_bb, insns);
934 }
935
936 /* Do control and data flow analysis; write some of the results to the
937    dump file.  */
938 static void
939 rest_of_handle_cfg (tree decl, rtx insns)
940 {
941   open_dump_file (DFI_cfg, decl);
942   if (dump_file)
943     dump_flow_info (dump_file);
944   if (optimize)
945     cleanup_cfg (CLEANUP_EXPENSIVE
946                  | (flag_thread_jumps ? CLEANUP_THREADING : 0));
947
948   /* It may make more sense to mark constant functions after dead code is
949      eliminated by life_analysis, but we need to do it early, as -fprofile-arcs
950      may insert code making function non-constant, but we still must consider
951      it as constant, otherwise -fbranch-probabilities will not read data back.
952
953      life_analysis rarely eliminates modification of external memory.
954    */
955   if (optimize)
956     {
957       /* Alias analysis depends on this information and mark_constant_function
958        depends on alias analysis.  */
959       reg_scan (insns, max_reg_num (), 1);
960       mark_constant_function ();
961     }
962
963   close_dump_file (DFI_cfg, print_rtl_with_bb, insns);
964 }
965
966 /* Purge addressofs.  */
967 static void
968 rest_of_handle_addressof (tree decl, rtx insns)
969 {
970   open_dump_file (DFI_addressof, decl);
971
972   purge_addressof (insns);
973   if (optimize && purge_all_dead_edges (0))
974     delete_unreachable_blocks ();
975   reg_scan (insns, max_reg_num (), 1);
976
977   close_dump_file (DFI_addressof, print_rtl, insns);
978 }
979
980 /* Perform jump bypassing and control flow optimizations.  */
981 static void
982 rest_of_handle_jump_bypass (tree decl, rtx insns)
983 {
984   timevar_push (TV_BYPASS);
985   open_dump_file (DFI_bypass, decl);
986
987   cleanup_cfg (CLEANUP_EXPENSIVE);
988   reg_scan (insns, max_reg_num (), 1);
989
990   if (bypass_jumps (dump_file))
991     {
992       rebuild_jump_labels (insns);
993       cleanup_cfg (CLEANUP_EXPENSIVE);
994       delete_trivially_dead_insns (insns, max_reg_num ());
995     }
996
997   close_dump_file (DFI_bypass, print_rtl_with_bb, insns);
998   timevar_pop (TV_BYPASS);
999
1000   ggc_collect ();
1001
1002 #ifdef ENABLE_CHECKING
1003   verify_flow_info ();
1004 #endif
1005 }
1006
1007 /* Try combining insns through substitution.  */
1008 static void
1009 rest_of_handle_combine (tree decl, rtx insns)
1010 {
1011   int rebuild_jump_labels_after_combine = 0;
1012
1013   timevar_push (TV_COMBINE);
1014   open_dump_file (DFI_combine, decl);
1015
1016   rebuild_jump_labels_after_combine
1017     = combine_instructions (insns, max_reg_num ());
1018
1019   /* Combining insns may have turned an indirect jump into a
1020      direct jump.  Rebuild the JUMP_LABEL fields of jumping
1021      instructions.  */
1022   if (rebuild_jump_labels_after_combine)
1023     {
1024       timevar_push (TV_JUMP);
1025       rebuild_jump_labels (insns);
1026       timevar_pop (TV_JUMP);
1027
1028       cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
1029     }
1030
1031   close_dump_file (DFI_combine, print_rtl_with_bb, insns);
1032   timevar_pop (TV_COMBINE);
1033
1034   ggc_collect ();
1035 }
1036
1037 /* Perform life analysis.  */
1038 static void
1039 rest_of_handle_life (tree decl, rtx insns)
1040 {
1041   open_dump_file (DFI_life, decl);
1042   regclass_init ();
1043
1044 #ifdef ENABLE_CHECKING
1045   verify_flow_info ();
1046 #endif
1047   life_analysis (dump_file, PROP_FINAL);
1048   if (optimize)
1049     cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_UPDATE_LIFE
1050                  | CLEANUP_LOG_LINKS
1051                  | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1052   timevar_pop (TV_FLOW);
1053
1054   if (extra_warnings)
1055     {
1056       setjmp_vars_warning (DECL_INITIAL (decl));
1057       setjmp_args_warning ();
1058     }
1059
1060   if (optimize)
1061     {
1062       if (!flag_new_regalloc && initialize_uninitialized_subregs ())
1063         {
1064           /* Insns were inserted, and possibly pseudos created, so
1065              things might look a bit different.  */
1066           insns = get_insns ();
1067           allocate_reg_life_data ();
1068           update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1069                             PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
1070         }
1071     }
1072
1073   no_new_pseudos = 1;
1074
1075   close_dump_file (DFI_life, print_rtl_with_bb, insns);
1076
1077   ggc_collect ();
1078 }
1079
1080 /* Perform common subexpression elimination.  Nonzero value from
1081    `cse_main' means that jumps were simplified and some code may now
1082    be unreachable, so do jump optimization again.  */
1083 static void
1084 rest_of_handle_cse (tree decl, rtx insns)
1085 {
1086   int tem;
1087
1088   open_dump_file (DFI_cse, decl);
1089   if (dump_file)
1090     dump_flow_info (dump_file);
1091   timevar_push (TV_CSE);
1092
1093   reg_scan (insns, max_reg_num (), 1);
1094
1095   tem = cse_main (insns, max_reg_num (), 0, dump_file);
1096   if (tem)
1097     rebuild_jump_labels (insns);
1098   if (purge_all_dead_edges (0))
1099     delete_unreachable_blocks ();
1100
1101   delete_trivially_dead_insns (insns, max_reg_num ());
1102
1103   /* If we are not running more CSE passes, then we are no longer
1104      expecting CSE to be run.  But always rerun it in a cheap mode.  */
1105   cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
1106
1107   if (tem || optimize > 1)
1108     cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1109
1110   timevar_pop (TV_CSE);
1111   close_dump_file (DFI_cse, print_rtl_with_bb, insns);
1112 }
1113
1114 /* Run second CSE pass after loop optimizations.  */
1115 static void
1116 rest_of_handle_cse2 (tree decl, rtx insns)
1117 {
1118   int tem;
1119
1120   timevar_push (TV_CSE2);
1121   open_dump_file (DFI_cse2, decl);
1122   if (dump_file)
1123     dump_flow_info (dump_file);
1124   /* CFG is no longer maintained up-to-date.  */
1125   tem = cse_main (insns, max_reg_num (), 1, dump_file);
1126
1127   /* Run a pass to eliminate duplicated assignments to condition code
1128      registers.  We have to run this after bypass_jumps, because it
1129      makes it harder for that pass to determine whether a jump can be
1130      bypassed safely.  */
1131   cse_condition_code_reg ();
1132
1133   purge_all_dead_edges (0);
1134   delete_trivially_dead_insns (insns, max_reg_num ());
1135
1136   if (tem)
1137     {
1138       timevar_push (TV_JUMP);
1139       rebuild_jump_labels (insns);
1140       cleanup_cfg (CLEANUP_EXPENSIVE);
1141       timevar_pop (TV_JUMP);
1142     }
1143   reg_scan (insns, max_reg_num (), 0);
1144   close_dump_file (DFI_cse2, print_rtl_with_bb, insns);
1145   ggc_collect ();
1146   timevar_pop (TV_CSE2);
1147 }
1148
1149 /* Perform global cse.  */
1150 static void
1151 rest_of_handle_gcse (tree decl, rtx insns)
1152 {
1153   int save_csb, save_cfj;
1154   int tem2 = 0, tem;
1155
1156   timevar_push (TV_GCSE);
1157   open_dump_file (DFI_gcse, decl);
1158
1159   tem = gcse_main (insns, dump_file);
1160   rebuild_jump_labels (insns);
1161   delete_trivially_dead_insns (insns, max_reg_num ());
1162
1163   save_csb = flag_cse_skip_blocks;
1164   save_cfj = flag_cse_follow_jumps;
1165   flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
1166
1167   /* If -fexpensive-optimizations, re-run CSE to clean up things done
1168      by gcse.  */
1169   if (flag_expensive_optimizations)
1170     {
1171       timevar_push (TV_CSE);
1172       reg_scan (insns, max_reg_num (), 1);
1173       tem2 = cse_main (insns, max_reg_num (), 0, dump_file);
1174       purge_all_dead_edges (0);
1175       delete_trivially_dead_insns (insns, max_reg_num ());
1176       timevar_pop (TV_CSE);
1177       cse_not_expected = !flag_rerun_cse_after_loop;
1178     }
1179
1180   /* If gcse or cse altered any jumps, rerun jump optimizations to clean
1181      things up.  Then possibly re-run CSE again.  */
1182   while (tem || tem2)
1183     {
1184       tem = tem2 = 0;
1185       timevar_push (TV_JUMP);
1186       rebuild_jump_labels (insns);
1187       cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1188       timevar_pop (TV_JUMP);
1189
1190       if (flag_expensive_optimizations)
1191         {
1192           timevar_push (TV_CSE);
1193           reg_scan (insns, max_reg_num (), 1);
1194           tem2 = cse_main (insns, max_reg_num (), 0, dump_file);
1195           purge_all_dead_edges (0);
1196           delete_trivially_dead_insns (insns, max_reg_num ());
1197           timevar_pop (TV_CSE);
1198         }
1199     }
1200
1201   close_dump_file (DFI_gcse, print_rtl_with_bb, insns);
1202   timevar_pop (TV_GCSE);
1203
1204   ggc_collect ();
1205   flag_cse_skip_blocks = save_csb;
1206   flag_cse_follow_jumps = save_cfj;
1207 #ifdef ENABLE_CHECKING
1208   verify_flow_info ();
1209 #endif
1210 }
1211
1212 /* Move constant computations out of loops.  */
1213 static void
1214 rest_of_handle_loop_optimize (tree decl, rtx insns)
1215 {
1216   int do_unroll, do_prefetch;
1217
1218   timevar_push (TV_LOOP);
1219   delete_dead_jumptables ();
1220   cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1221   open_dump_file (DFI_loop, decl);
1222
1223   /* CFG is no longer maintained up-to-date.  */
1224   free_bb_for_insn ();
1225
1226   if (flag_unroll_loops)
1227     do_unroll = LOOP_AUTO_UNROLL;       /* Having two unrollers is useless.  */
1228   else
1229     do_unroll = flag_old_unroll_loops ? LOOP_UNROLL : LOOP_AUTO_UNROLL;
1230   do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
1231
1232   if (flag_rerun_loop_opt)
1233     {
1234       cleanup_barriers ();
1235
1236       /* We only want to perform unrolling once.  */
1237       loop_optimize (insns, dump_file, do_unroll);
1238       do_unroll = 0;
1239
1240       /* The first call to loop_optimize makes some instructions
1241          trivially dead.  We delete those instructions now in the
1242          hope that doing so will make the heuristics in loop work
1243          better and possibly speed up compilation.  */
1244       delete_trivially_dead_insns (insns, max_reg_num ());
1245
1246       /* The regscan pass is currently necessary as the alias
1247          analysis code depends on this information.  */
1248       reg_scan (insns, max_reg_num (), 1);
1249     }
1250   cleanup_barriers ();
1251   loop_optimize (insns, dump_file, do_unroll | do_prefetch);
1252
1253   /* Loop can create trivially dead instructions.  */
1254   delete_trivially_dead_insns (insns, max_reg_num ());
1255   close_dump_file (DFI_loop, print_rtl, insns);
1256   timevar_pop (TV_LOOP);
1257   find_basic_blocks (insns, max_reg_num (), dump_file);
1258
1259   ggc_collect ();
1260 }
1261
1262 /* Perform loop optimizations.  It might be better to do them a bit
1263    sooner, but we want the profile feedback to work more
1264    efficiently.  */
1265 static void
1266 rest_of_handle_loop2 (tree decl, rtx insns)
1267 {
1268   struct loops *loops;
1269   basic_block bb;
1270
1271   if (!flag_unswitch_loops
1272       && !flag_peel_loops
1273       && !flag_unroll_loops
1274       && !flag_branch_on_count_reg)
1275     return;
1276
1277   timevar_push (TV_LOOP);
1278   open_dump_file (DFI_loop2, decl);
1279   if (dump_file)
1280     dump_flow_info (dump_file);
1281
1282   /* Initialize structures for layout changes.  */
1283   cfg_layout_initialize ();
1284
1285   loops = loop_optimizer_init (dump_file);
1286
1287   if (loops)
1288     {
1289       /* The optimizations:  */
1290       if (flag_unswitch_loops)
1291         unswitch_loops (loops);
1292
1293       if (flag_peel_loops || flag_unroll_loops)
1294         unroll_and_peel_loops (loops,
1295                                (flag_peel_loops ? UAP_PEEL : 0) |
1296                                (flag_unroll_loops ? UAP_UNROLL : 0) |
1297                                (flag_unroll_all_loops ? UAP_UNROLL_ALL : 0));
1298
1299 #ifdef HAVE_doloop_end
1300       if (flag_branch_on_count_reg && HAVE_doloop_end)
1301         doloop_optimize_loops (loops);
1302 #endif /* HAVE_doloop_end */
1303
1304       loop_optimizer_finalize (loops, dump_file);
1305     }
1306
1307   /* Finalize layout changes.  */
1308   FOR_EACH_BB (bb)
1309     if (bb->next_bb != EXIT_BLOCK_PTR)
1310       bb->rbi->next = bb->next_bb;
1311   cfg_layout_finalize ();
1312
1313   cleanup_cfg (CLEANUP_EXPENSIVE);
1314   delete_trivially_dead_insns (insns, max_reg_num ());
1315   reg_scan (insns, max_reg_num (), 0);
1316   if (dump_file)
1317     dump_flow_info (dump_file);
1318   close_dump_file (DFI_loop2, print_rtl_with_bb, get_insns ());
1319   timevar_pop (TV_LOOP);
1320   ggc_collect ();
1321 }
1322
1323 /* This is called from finish_function (within langhooks.parse_file)
1324    after each top-level definition is parsed.
1325    It is supposed to compile that function or variable
1326    and output the assembler code for it.
1327    After we return, the tree storage is freed.  */
1328
1329 void
1330 rest_of_compilation (tree decl)
1331 {
1332   rtx insns;
1333
1334   timevar_push (TV_REST_OF_COMPILATION);
1335
1336   /* Register rtl specific functions for cfg.  */
1337   rtl_register_cfg_hooks ();
1338
1339   /* Now that we're out of the frontend, we shouldn't have any more
1340      CONCATs anywhere.  */
1341   generating_concat_p = 0;
1342
1343   /* When processing delayed functions, prepare_function_start() won't
1344      have been run to re-initialize it.  */
1345   cse_not_expected = ! optimize;
1346
1347   if (!cfun->dont_emit_block_notes)
1348     {
1349       /* First, make sure that NOTE_BLOCK is set correctly for each
1350          NOTE_INSN_BLOCK_BEG/NOTE_INSN_BLOCK_END note.  */
1351       if (!cfun->x_whole_function_mode_p)
1352         identify_blocks ();
1353
1354       /* In function-at-a-time mode, we do not attempt to keep the BLOCK
1355          tree in sensible shape.  So, we just recalculate it here.  */
1356       if (cfun->x_whole_function_mode_p)
1357         reorder_blocks ();
1358     }
1359   else
1360     finalize_block_changes ();
1361
1362   init_flow ();
1363
1364   /* Dump the rtl code if we are dumping rtl.  */
1365   if (open_dump_file (DFI_rtl, decl))
1366     close_dump_file (DFI_rtl, print_rtl, get_insns ());
1367
1368   /* Convert from NOTE_INSN_EH_REGION style notes, and do other
1369      sorts of eh initialization.  Delay this until after the
1370      initial rtl dump so that we can see the original nesting.  */
1371   convert_from_eh_region_ranges ();
1372
1373   /* If we're emitting a nested function, make sure its parent gets
1374      emitted as well.  Doing otherwise confuses debug info.  */
1375   {
1376     tree parent;
1377     for (parent = DECL_CONTEXT (current_function_decl);
1378          parent != NULL_TREE;
1379          parent = get_containing_scope (parent))
1380       if (TREE_CODE (parent) == FUNCTION_DECL)
1381         TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1382   }
1383
1384   /* We are now committed to emitting code for this function.  Do any
1385      preparation, such as emitting abstract debug info for the inline
1386      before it gets mangled by optimization.  */
1387   if (cgraph_function_possibly_inlined_p (decl))
1388     (*debug_hooks->outlining_inline_function) (decl);
1389
1390   /* Remove any notes we don't need.  That will make iterating
1391      over the instruction sequence faster, and allow the garbage
1392      collector to reclaim the memory used by the notes.  */
1393   remove_unnecessary_notes ();
1394   if (!cfun->dont_emit_block_notes)
1395     reorder_blocks ();
1396
1397   ggc_collect ();
1398
1399   /* Initialize some variables used by the optimizers.  */
1400   init_function_for_compilation ();
1401
1402   if (! DECL_DEFER_OUTPUT (decl))
1403     TREE_ASM_WRITTEN (decl) = 1;
1404
1405   /* Now that integrate will no longer see our rtl, we need not
1406      distinguish between the return value of this function and the
1407      return value of called functions.  Also, we can remove all SETs
1408      of subregs of hard registers; they are only here because of
1409      integrate.  Also, we can now initialize pseudos intended to
1410      carry magic hard reg data throughout the function.  */
1411   rtx_equal_function_value_matters = 0;
1412   purge_hard_subreg_sets (get_insns ());
1413
1414   /* Early return if there were errors.  We can run afoul of our
1415      consistency checks, and there's not really much point in fixing them.
1416      Don't return yet if -Wreturn-type; we need to do cleanup_cfg.  */
1417   if (((rtl_dump_and_exit || flag_syntax_only) && !warn_return_type)
1418       || errorcount || sorrycount)
1419     goto exit_rest_of_compilation;
1420
1421   timevar_push (TV_JUMP);
1422   open_dump_file (DFI_sibling, decl);
1423   insns = get_insns ();
1424   rebuild_jump_labels (insns);
1425   find_exception_handler_labels ();
1426   find_basic_blocks (insns, max_reg_num (), dump_file);
1427
1428   delete_unreachable_blocks ();
1429
1430   /* Turn NOTE_INSN_PREDICTIONs into branch predictions.  */
1431   if (flag_guess_branch_prob)
1432     {
1433       timevar_push (TV_BRANCH_PROB);
1434       note_prediction_to_br_prob ();
1435       timevar_pop (TV_BRANCH_PROB);
1436     }
1437
1438   timevar_pop (TV_JUMP);
1439
1440   if (cfun->tail_call_emit)
1441     fixup_tail_calls ();
1442
1443   insn_locators_initialize ();
1444   /* Complete generation of exception handling code.  */
1445   if (doing_eh (0))
1446     {
1447       timevar_push (TV_JUMP);
1448       open_dump_file (DFI_eh, decl);
1449
1450       finish_eh_generation ();
1451
1452       close_dump_file (DFI_eh, print_rtl, get_insns ());
1453       timevar_pop (TV_JUMP);
1454     }
1455
1456   /* Delay emitting hard_reg_initial_value sets until after EH landing pad
1457      generation, which might create new sets.  */
1458   emit_initial_value_sets ();
1459
1460 #ifdef FINALIZE_PIC
1461   /* If we are doing position-independent code generation, now
1462      is the time to output special prologues and epilogues.
1463      We do not want to do this earlier, because it just clutters
1464      up inline functions with meaningless insns.  */
1465   if (flag_pic)
1466     FINALIZE_PIC;
1467 #endif
1468
1469   insns = get_insns ();
1470
1471   /* Copy any shared structure that should not be shared.  */
1472   unshare_all_rtl (current_function_decl, insns);
1473
1474 #ifdef SETJMP_VIA_SAVE_AREA
1475   /* This must be performed before virtual register instantiation.
1476      Please be aware that everything in the compiler that can look
1477      at the RTL up to this point must understand that REG_SAVE_AREA
1478      is just like a use of the REG contained inside.  */
1479   if (current_function_calls_alloca)
1480     optimize_save_area_alloca (insns);
1481 #endif
1482
1483   /* Instantiate all virtual registers.  */
1484   instantiate_virtual_regs (current_function_decl, insns);
1485
1486   open_dump_file (DFI_jump, decl);
1487
1488   /* Always do one jump optimization pass to ensure that JUMP_LABEL fields
1489      are initialized and to compute whether control can drop off the end
1490      of the function.  */
1491
1492   timevar_push (TV_JUMP);
1493   /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB.  Do this
1494      before jump optimization switches branch directions.  */
1495   if (flag_guess_branch_prob)
1496     expected_value_to_br_prob ();
1497
1498   reg_scan (insns, max_reg_num (), 0);
1499   rebuild_jump_labels (insns);
1500   find_basic_blocks (insns, max_reg_num (), dump_file);
1501   delete_trivially_dead_insns (insns, max_reg_num ());
1502   if (dump_file)
1503     dump_flow_info (dump_file);
1504   cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_PRE_LOOP
1505                | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1506
1507   create_loop_notes ();
1508
1509   purge_line_number_notes (insns);
1510
1511   timevar_pop (TV_JUMP);
1512   close_dump_file (DFI_jump, print_rtl, insns);
1513
1514   /* Now is when we stop if -fsyntax-only and -Wreturn-type.  */
1515   if (rtl_dump_and_exit || flag_syntax_only || DECL_DEFER_OUTPUT (decl))
1516     goto exit_rest_of_compilation;
1517
1518   timevar_push (TV_JUMP);
1519
1520   if (optimize)
1521     cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1522
1523   /* Jump optimization, and the removal of NULL pointer checks, may
1524      have reduced the number of instructions substantially.  CSE, and
1525      future passes, allocate arrays whose dimensions involve the
1526      maximum instruction UID, so if we can reduce the maximum UID
1527      we'll save big on memory.  */
1528   renumber_insns (dump_file);
1529   timevar_pop (TV_JUMP);
1530
1531   close_dump_file (DFI_jump, print_rtl_with_bb, insns);
1532
1533   ggc_collect ();
1534
1535   if (optimize > 0)
1536     rest_of_handle_cse (decl, insns);
1537
1538   rest_of_handle_addressof (decl, insns);
1539
1540   ggc_collect ();
1541
1542   if (optimize > 0)
1543     {
1544       if (flag_gcse)
1545         rest_of_handle_gcse (decl, insns);
1546
1547       if (flag_loop_optimize)
1548         rest_of_handle_loop_optimize (decl, insns);
1549
1550       if (flag_gcse)
1551         rest_of_handle_jump_bypass (decl, insns);
1552     }
1553
1554   timevar_push (TV_FLOW);
1555
1556   rest_of_handle_cfg (decl, insns);
1557
1558   if (!flag_tree_based_profiling
1559       && (optimize > 0 || profile_arc_flag
1560           || flag_test_coverage || flag_branch_probabilities))
1561     {
1562       rtl_register_profile_hooks ();
1563       rtl_register_value_prof_hooks ();
1564       rest_of_handle_branch_prob (decl, insns);
1565
1566       if (flag_branch_probabilities
1567           && flag_profile_values
1568           && flag_value_profile_transformations)
1569         rest_of_handle_value_profile_transformations (decl, insns);
1570
1571       /* Remove the death notes created for vpt.  */
1572       if (flag_profile_values)
1573         count_or_remove_death_notes (NULL, 1);
1574     }
1575
1576   if (optimize > 0)
1577     rest_of_handle_if_conversion (decl, insns);
1578
1579   if (flag_tracer)
1580     rest_of_handle_tracer (decl, insns);
1581
1582   if (optimize > 0)
1583     rest_of_handle_loop2 (decl, insns);
1584
1585   if (flag_web)
1586     rest_of_handle_web (decl, insns);
1587
1588   if (flag_rerun_cse_after_loop)
1589     rest_of_handle_cse2 (decl, insns);
1590
1591   cse_not_expected = 1;
1592
1593   rest_of_handle_life (decl, insns);
1594
1595   if (optimize > 0)
1596     rest_of_handle_combine (decl, insns);
1597
1598   if (flag_if_conversion)
1599     rest_of_handle_if_after_combine (decl, insns);
1600
1601   /* The optimization to partition hot/cold basic blocks into separate
1602      sections of the .o file does not work well with exception handling.
1603      Don't call it if there are exceptions. */
1604
1605   if (flag_reorder_blocks_and_partition && !flag_exceptions)
1606     {
1607       no_new_pseudos = 0;
1608       partition_hot_cold_basic_blocks ();
1609       allocate_reg_life_data ();
1610       update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES, 
1611                         PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
1612       no_new_pseudos = 1;
1613     }
1614
1615   if (optimize > 0 && (flag_regmove || flag_expensive_optimizations))
1616     rest_of_handle_regmove (decl, insns);
1617
1618   /* Do unconditional splitting before register allocation to allow machine
1619      description to add extra information not needed previously.  */
1620   split_all_insns (1);
1621
1622 #ifdef OPTIMIZE_MODE_SWITCHING
1623   timevar_push (TV_MODE_SWITCH);
1624
1625   no_new_pseudos = 0;
1626   optimize_mode_switching (NULL);
1627   no_new_pseudos = 1;
1628
1629   timevar_pop (TV_MODE_SWITCH);
1630 #endif
1631
1632   /* Any of the several passes since flow1 will have munged register
1633      lifetime data a bit.  We need it to be up to date for scheduling
1634      (see handling of reg_known_equiv in init_alias_analysis).  */
1635   recompute_reg_usage (insns, !optimize_size);
1636
1637 #ifdef INSN_SCHEDULING
1638   rest_of_handle_sched (decl, insns);
1639 #endif
1640
1641   /* Determine if the current function is a leaf before running reload
1642      since this can impact optimizations done by the prologue and
1643      epilogue thus changing register elimination offsets.  */
1644   current_function_is_leaf = leaf_function_p ();
1645
1646   timevar_push (TV_LOCAL_ALLOC);
1647   open_dump_file (DFI_lreg, decl);
1648
1649   if (flag_new_regalloc)
1650     {
1651       if (rest_of_handle_new_regalloc (decl, insns))
1652         goto exit_rest_of_compilation;
1653     }
1654   else
1655     {
1656       if (rest_of_handle_old_regalloc (decl, insns))
1657         goto exit_rest_of_compilation;
1658     }
1659
1660   ggc_collect ();
1661
1662   open_dump_file (DFI_postreload, decl);
1663
1664   /* Do a very simple CSE pass over just the hard registers.  */
1665   if (optimize > 0)
1666     {
1667       timevar_push (TV_RELOAD_CSE_REGS);
1668       reload_cse_regs (insns);
1669       /* reload_cse_regs can eliminate potentially-trapping MEMs.
1670          Remove any EH edges associated with them.  */
1671       if (flag_non_call_exceptions)
1672         purge_all_dead_edges (0);
1673       timevar_pop (TV_RELOAD_CSE_REGS);
1674     }
1675
1676   close_dump_file (DFI_postreload, print_rtl_with_bb, insns);
1677
1678   if (optimize > 0 && flag_gcse_after_reload)
1679     rest_of_handle_gcse2 (decl, insns);
1680
1681   /* Re-create the death notes which were deleted during reload.  */
1682   timevar_push (TV_FLOW2);
1683   open_dump_file (DFI_flow2, decl);
1684
1685 #ifdef ENABLE_CHECKING
1686   verify_flow_info ();
1687 #endif
1688
1689   /* If optimizing, then go ahead and split insns now.  */
1690 #ifndef STACK_REGS
1691   if (optimize > 0)
1692 #endif
1693     split_all_insns (0);
1694
1695     if (flag_branch_target_load_optimize)
1696       {
1697         open_dump_file (DFI_branch_target_load, decl);
1698
1699         branch_target_load_optimize (/*after_prologue_epilogue_gen=*/false);
1700
1701         close_dump_file (DFI_branch_target_load, print_rtl_with_bb, insns);
1702
1703         ggc_collect ();
1704       }
1705
1706   if (optimize)
1707     cleanup_cfg (CLEANUP_EXPENSIVE);
1708
1709   /* On some machines, the prologue and epilogue code, or parts thereof,
1710      can be represented as RTL.  Doing so lets us schedule insns between
1711      it and the rest of the code and also allows delayed branch
1712      scheduling to operate in the epilogue.  */
1713   thread_prologue_and_epilogue_insns (insns);
1714   epilogue_completed = 1;
1715
1716   if (optimize)
1717     {
1718       life_analysis (dump_file, PROP_POSTRELOAD);
1719       cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE
1720                    | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1721
1722       /* This is kind of a heuristic.  We need to run combine_stack_adjustments
1723          even for machines with possibly nonzero RETURN_POPS_ARGS
1724          and ACCUMULATE_OUTGOING_ARGS.  We expect that only ports having
1725          push instructions will have popping returns.  */
1726 #ifndef PUSH_ROUNDING
1727       if (!ACCUMULATE_OUTGOING_ARGS)
1728 #endif
1729         combine_stack_adjustments ();
1730
1731       ggc_collect ();
1732     }
1733
1734   flow2_completed = 1;
1735
1736   close_dump_file (DFI_flow2, print_rtl_with_bb, insns);
1737   timevar_pop (TV_FLOW2);
1738
1739 #ifdef HAVE_peephole2
1740   if (optimize > 0 && flag_peephole2)
1741     {
1742       timevar_push (TV_PEEPHOLE2);
1743       open_dump_file (DFI_peephole2, decl);
1744
1745       peephole2_optimize (dump_file);
1746
1747       close_dump_file (DFI_peephole2, print_rtl_with_bb, insns);
1748       timevar_pop (TV_PEEPHOLE2);
1749     }
1750 #endif
1751
1752   open_dump_file (DFI_ce3, decl);
1753   if (optimize)
1754     /* Last attempt to optimize CFG, as scheduling, peepholing and insn
1755        splitting possibly introduced more crossjumping opportunities.  */
1756     cleanup_cfg (CLEANUP_EXPENSIVE
1757                  | CLEANUP_UPDATE_LIFE 
1758                  | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1759   if (flag_if_conversion2)
1760     {
1761       timevar_push (TV_IFCVT2);
1762
1763       if_convert (1);
1764
1765       timevar_pop (TV_IFCVT2);
1766     }
1767   close_dump_file (DFI_ce3, print_rtl_with_bb, insns);
1768
1769   if (optimize > 0)
1770     {
1771       if (flag_rename_registers || flag_cprop_registers)
1772         rest_of_handle_regrename (decl, insns);
1773
1774       rest_of_handle_reorder_blocks (decl, insns);
1775     }
1776
1777   if (flag_branch_target_load_optimize2)
1778     {
1779       /* Leave this a warning for now so that it is possible to experiment
1780          with running this pass twice.  In 3.6, we should either make this
1781          an error, or use separate dump files.  */
1782       if (flag_branch_target_load_optimize)
1783         warning ("branch target register load optimization is not intended "
1784                  "to be run twice");
1785
1786       open_dump_file (DFI_branch_target_load, decl);
1787
1788       branch_target_load_optimize (/*after_prologue_epilogue_gen=*/true);
1789
1790       close_dump_file (DFI_branch_target_load, print_rtl_with_bb, insns);
1791
1792       ggc_collect ();
1793     }
1794
1795 #ifdef INSN_SCHEDULING
1796   if (optimize > 0 && flag_schedule_insns_after_reload)
1797     rest_of_handle_sched2 (decl, insns);
1798 #endif
1799
1800 #ifdef LEAF_REGISTERS
1801   current_function_uses_only_leaf_regs
1802     = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
1803 #endif
1804
1805 #ifdef STACK_REGS
1806   rest_of_handle_stack_regs (decl, insns);
1807 #endif
1808
1809   compute_alignments ();
1810
1811   if (flag_var_tracking)
1812     rest_of_handle_variable_tracking (decl, insns);
1813
1814   /* CFG is no longer maintained up-to-date.  */
1815   free_bb_for_insn ();
1816
1817   if (targetm.machine_dependent_reorg != 0)
1818     rest_of_handle_machine_reorg (decl, insns);
1819
1820   purge_line_number_notes (insns);
1821   cleanup_barriers ();
1822
1823 #ifdef DELAY_SLOTS
1824   if (optimize > 0 && flag_delayed_branch)
1825     rest_of_handle_delay_slots (decl, insns);
1826 #endif
1827
1828 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
1829   timevar_push (TV_SHORTEN_BRANCH);
1830   split_all_insns_noflow ();
1831   timevar_pop (TV_SHORTEN_BRANCH);
1832 #endif
1833
1834   convert_to_eh_region_ranges ();
1835
1836   /* Shorten branches.  */
1837   timevar_push (TV_SHORTEN_BRANCH);
1838   shorten_branches (get_insns ());
1839   timevar_pop (TV_SHORTEN_BRANCH);
1840
1841   set_nothrow_function_flags ();
1842   if (current_function_nothrow)
1843     /* Now we know that this can't throw; set the flag for the benefit
1844        of other functions later in this translation unit.  */
1845     TREE_NOTHROW (current_function_decl) = 1;
1846
1847   rest_of_handle_final (decl, insns);
1848
1849   /* Write DBX symbols if requested.  */
1850
1851   /* Note that for those inline functions where we don't initially
1852      know for certain that we will be generating an out-of-line copy,
1853      the first invocation of this routine (rest_of_compilation) will
1854      skip over this code by doing a `goto exit_rest_of_compilation;'.
1855      Later on, wrapup_global_declarations will (indirectly) call
1856      rest_of_compilation again for those inline functions that need
1857      to have out-of-line copies generated.  During that call, we
1858      *will* be routed past here.  */
1859
1860   timevar_push (TV_SYMOUT);
1861   (*debug_hooks->function_decl) (decl);
1862   timevar_pop (TV_SYMOUT);
1863
1864  exit_rest_of_compilation:
1865
1866   coverage_end_function ();
1867
1868   /* In case the function was not output,
1869      don't leave any temporary anonymous types
1870      queued up for sdb output.  */
1871 #ifdef SDB_DEBUGGING_INFO
1872   if (write_symbols == SDB_DEBUG)
1873     sdbout_types (NULL_TREE);
1874 #endif
1875
1876   reload_completed = 0;
1877   epilogue_completed = 0;
1878   flow2_completed = 0;
1879   no_new_pseudos = 0;
1880
1881   timevar_push (TV_FINAL);
1882
1883   /* Clear out the insn_length contents now that they are no
1884      longer valid.  */
1885   init_insn_lengths ();
1886
1887   /* Show no temporary slots allocated.  */
1888   init_temp_slots ();
1889
1890   free_basic_block_vars ();
1891   free_bb_for_insn ();
1892
1893   timevar_pop (TV_FINAL);
1894
1895   if (targetm.binds_local_p (current_function_decl))
1896     {
1897       int pref = cfun->preferred_stack_boundary;
1898       if (cfun->recursive_call_emit
1899           && cfun->stack_alignment_needed > cfun->preferred_stack_boundary)
1900         pref = cfun->stack_alignment_needed;
1901       cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
1902         = pref;
1903     }
1904
1905   /* Make sure volatile mem refs aren't considered valid operands for
1906      arithmetic insns.  We must call this here if this is a nested inline
1907      function, since the above code leaves us in the init_recog state
1908      (from final.c), and the function context push/pop code does not
1909      save/restore volatile_ok.
1910
1911      ??? Maybe it isn't necessary for expand_start_function to call this
1912      anymore if we do it here?  */
1913
1914   init_recog_no_volatile ();
1915
1916   /* We're done with this function.  Free up memory if we can.  */
1917   free_after_parsing (cfun);
1918
1919   ggc_collect ();
1920
1921   timevar_pop (TV_REST_OF_COMPILATION);
1922 }
1923
1924 void
1925 init_optimization_passes (void)
1926 {
1927   open_dump_file (DFI_cgraph, NULL);
1928   cgraph_dump_file = dump_file;
1929   dump_file = NULL;
1930 }
1931
1932 void
1933 finish_optimization_passes (void)
1934 {
1935   if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
1936     {
1937       timevar_push (TV_DUMP);
1938       open_dump_file (DFI_bp, NULL);
1939
1940       end_branch_prob ();
1941
1942       close_dump_file (DFI_bp, NULL, NULL_RTX);
1943       timevar_pop (TV_DUMP);
1944     }
1945
1946   if (optimize > 0 && open_dump_file (DFI_combine, NULL))
1947     {
1948       timevar_push (TV_DUMP);
1949       dump_combine_total_stats (dump_file);
1950       close_dump_file (DFI_combine, NULL, NULL_RTX);
1951       timevar_pop (TV_DUMP);
1952     }
1953
1954   dump_file = cgraph_dump_file;
1955   cgraph_dump_file = NULL;
1956   close_dump_file (DFI_cgraph, NULL, NULL_RTX);
1957
1958   /* Do whatever is necessary to finish printing the graphs.  */
1959   if (graph_dump_format != no_graph)
1960     {
1961       int i;
1962
1963       for (i = 0; i < (int) DFI_MAX; ++i)
1964         if (dump_file_tbl[i].initialized && dump_file_tbl[i].graph_dump_p)
1965           {
1966             char seq[16];
1967             char *suffix;
1968
1969             sprintf (seq, DUMPFILE_FORMAT, i);
1970             suffix = concat (seq, dump_file_tbl[i].extension, NULL);
1971             finish_graph_dump_file (dump_base_name, suffix);
1972             free (suffix);
1973           }
1974     }
1975
1976 }
1977
1978 bool
1979 enable_rtl_dump_file (int letter)
1980 {
1981   bool matched = false;
1982   int i;
1983
1984   if (letter == 'a')
1985     {
1986       for (i = 0; i < (int) DFI_MAX; ++i)
1987         dump_file_tbl[i].enabled = 1;
1988       matched = true;
1989     }
1990   else
1991     {
1992       for (i = 0; i < (int) DFI_MAX; ++i)
1993         if (letter == dump_file_tbl[i].debug_switch)
1994           {
1995             dump_file_tbl[i].enabled = 1;
1996             matched = true;
1997           }
1998     }
1999
2000   return matched;
2001 }