OSDN Git Service

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