OSDN Git Service

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