OSDN Git Service

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