OSDN Git Service

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