OSDN Git Service

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