OSDN Git Service

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