OSDN Git Service

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