OSDN Git Service

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