Fix problems with labels with hot/cold partitioning.
[official-gcc.git] / gcc / passes.c
blobfa9bc22fbd30abf960d4a53a9009cae35fe0b8a3
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.
5 This file is part of GCC.
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.
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.
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. */
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. */
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>
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
41 #endif
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"
85 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
86 #include "dwarf2out.h"
87 #endif
89 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
90 #include "dbxout.h"
91 #endif
93 #ifdef SDB_DEBUGGING_INFO
94 #include "sdbout.h"
95 #endif
97 #ifdef XCOFF_DEBUGGING_INFO
98 #include "xcoffout.h" /* Needed for external data
99 declarations for e.g. AIX 4.x. */
100 #endif
102 #ifndef HAVE_conditional_execution
103 #define HAVE_conditional_execution 0
104 #endif
106 /* Format to use to print dumpfile index value */
107 #ifndef DUMPFILE_FORMAT
108 #define DUMPFILE_FORMAT ".%02d."
109 #endif
111 static int initializing_dump = 0;
113 /* Routine to open a dump file. Return true if the dump file is enabled. */
115 static int
116 open_dump_file (enum tree_dump_index index, tree decl)
118 if (! dump_enabled_p (index))
119 return 0;
121 timevar_push (TV_DUMP);
123 gcc_assert (!dump_file && !dump_file_name);
125 dump_file_name = get_dump_file_name (index);
126 initializing_dump = !dump_initialized_p (index);
127 dump_file = dump_begin (index, NULL);
129 if (dump_file == NULL)
130 fatal_error ("can't open %s: %m", dump_file_name);
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 : "");
141 timevar_pop (TV_DUMP);
142 return 1;
145 /* Routine to close a dump file. */
147 static void
148 close_dump_file (enum tree_dump_index index,
149 void (*func) (FILE *, rtx),
150 rtx insns)
152 if (! dump_file)
153 return;
155 timevar_push (TV_DUMP);
156 if (insns
157 && graph_dump_format != no_graph)
159 /* If we've not initialized the files, do so now. */
160 if (initializing_dump)
161 clean_graph_dump_file (dump_file_name);
163 print_rtl_graph_with_bb (dump_file_name, insns);
166 if (func && insns)
167 func (dump_file, insns);
169 dump_end (index, dump_file);
170 free ((char *) dump_file_name);
172 dump_file = NULL;
173 dump_file_name = NULL;
174 timevar_pop (TV_DUMP);
177 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
178 and TYPE_DECL nodes.
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).
186 DECL is the declaration. TOP_LEVEL is nonzero
187 if this declaration is not within a function. */
189 void
190 rest_of_decl_compilation (tree decl,
191 int top_level,
192 int at_end)
194 /* We deferred calling assemble_alias so that we could collect
195 other attributes such as visibility. Emit the alias now. */
197 tree alias;
198 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
199 if (alias)
201 alias = TREE_VALUE (TREE_VALUE (alias));
202 alias = get_identifier (TREE_STRING_POINTER (alias));
203 assemble_alias (decl, alias);
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);
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)
217 timevar_push (TV_VARCONST);
219 /* Don't output anything when a tentative file-scope definition
220 is seen. But at end of compilation, do output code for them.
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))
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);
237 #ifdef ASM_FINISH_DECLARE_OBJECT
238 if (decl == last_assemble_variable_decl)
240 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
241 top_level, at_end);
243 #endif
245 timevar_pop (TV_VARCONST);
247 else if (TREE_CODE (decl) == TYPE_DECL)
249 timevar_push (TV_SYMOUT);
250 debug_hooks->type_decl (decl, !top_level);
251 timevar_pop (TV_SYMOUT);
254 /* Let cgraph know about the existence of variables. */
255 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
256 cgraph_varpool_node (decl);
259 /* Called after finishing a record, union or enumeral type. */
261 void
262 rest_of_type_compilation (tree type, int toplev)
264 /* Avoid confusing the debug information machinery when there are
265 errors. */
266 if (errorcount != 0 || sorrycount != 0)
267 return;
269 timevar_push (TV_SYMOUT);
270 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
271 timevar_pop (TV_SYMOUT);
274 /* Turn the RTL into assembly. */
275 static void
276 rest_of_handle_final (void)
278 timevar_push (TV_FINAL);
280 rtx x;
281 const char *fnname;
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. */
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);
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 ();
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
303 assemble_end_function (current_function_decl, fnname);
305 #ifndef TARGET_UNWIND_INFO
306 /* Otherwise, it feels unclean to switch sections in the middle. */
307 output_function_exception_table ();
308 #endif
310 user_defined_section_attribute = false;
312 if (! quiet_flag)
313 fflush (asm_out_file);
315 /* Release all memory allocated by flow. */
316 free_basic_block_vars ();
319 /* Write DBX symbols if requested. */
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. */
330 timevar_push (TV_SYMOUT);
331 (*debug_hooks->function_decl) (current_function_decl);
332 timevar_pop (TV_SYMOUT);
334 ggc_collect ();
335 timevar_pop (TV_FINAL);
338 #ifdef DELAY_SLOTS
339 /* Run delay slot optimization. */
340 static void
341 rest_of_handle_delay_slots (void)
343 timevar_push (TV_DBR_SCHED);
344 open_dump_file (DFI_dbr, current_function_decl);
346 dbr_schedule (get_insns (), dump_file);
348 close_dump_file (DFI_dbr, print_rtl, get_insns ());
350 ggc_collect ();
352 timevar_pop (TV_DBR_SCHED);
354 #endif
356 #ifdef STACK_REGS
357 /* Convert register usage from flat register file usage to a stack
358 register file. */
359 static void
360 rest_of_handle_stack_regs (void)
362 #if defined (HAVE_ATTR_length)
363 /* If flow2 creates new instructions which need splitting
364 and scheduling after reload is not done, they might not be
365 split until final which doesn't allow splitting
366 if HAVE_ATTR_length. */
367 #ifdef INSN_SCHEDULING
368 if (optimize && !flag_schedule_insns_after_reload)
369 #else
370 if (optimize)
371 #endif
373 timevar_push (TV_SHORTEN_BRANCH);
374 split_all_insns (1);
375 timevar_pop (TV_SHORTEN_BRANCH);
377 #endif
379 timevar_push (TV_REG_STACK);
380 open_dump_file (DFI_stack, current_function_decl);
382 if (reg_to_stack (dump_file) && optimize)
384 if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
385 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
386 && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
388 reorder_basic_blocks (0);
389 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
393 close_dump_file (DFI_stack, print_rtl_with_bb, get_insns ());
395 ggc_collect ();
396 timevar_pop (TV_REG_STACK);
398 #endif
400 /* Track the variables, i.e. compute where the variable is stored at each position in function. */
401 static void
402 rest_of_handle_variable_tracking (void)
404 timevar_push (TV_VAR_TRACKING);
405 open_dump_file (DFI_vartrack, current_function_decl);
407 variable_tracking_main ();
409 close_dump_file (DFI_vartrack, print_rtl_with_bb, get_insns ());
410 timevar_pop (TV_VAR_TRACKING);
413 /* Machine dependent reorg pass. */
414 static void
415 rest_of_handle_machine_reorg (void)
417 timevar_push (TV_MACH_DEP);
418 open_dump_file (DFI_mach, current_function_decl);
420 targetm.machine_dependent_reorg ();
422 close_dump_file (DFI_mach, print_rtl, get_insns ());
424 ggc_collect ();
425 timevar_pop (TV_MACH_DEP);
429 /* Run old register allocator. Return TRUE if we must exit
430 rest_of_compilation upon return. */
431 static bool
432 rest_of_handle_old_regalloc (void)
434 int failure;
435 int rebuild_notes;
437 timevar_push (TV_LOCAL_ALLOC);
438 open_dump_file (DFI_lreg, current_function_decl);
440 /* Allocate the reg_renumber array. */
441 allocate_reg_info (max_regno, FALSE, TRUE);
443 /* And the reg_equiv_memory_loc array. */
444 VARRAY_GROW (reg_equiv_memory_loc_varray, max_regno);
445 reg_equiv_memory_loc = &VARRAY_RTX (reg_equiv_memory_loc_varray, 0);
447 allocate_initial_values (reg_equiv_memory_loc);
449 regclass (get_insns (), max_reg_num (), dump_file);
450 rebuild_notes = local_alloc ();
452 timevar_pop (TV_LOCAL_ALLOC);
454 /* Local allocation may have turned an indirect jump into a direct
455 jump. If so, we must rebuild the JUMP_LABEL fields of jumping
456 instructions. */
457 if (rebuild_notes)
459 timevar_push (TV_JUMP);
461 rebuild_jump_labels (get_insns ());
462 purge_all_dead_edges (0);
463 delete_unreachable_blocks ();
465 timevar_pop (TV_JUMP);
468 if (dump_enabled_p (DFI_lreg))
470 timevar_push (TV_DUMP);
471 dump_flow_info (dump_file);
472 dump_local_alloc (dump_file);
473 timevar_pop (TV_DUMP);
476 close_dump_file (DFI_lreg, print_rtl_with_bb, get_insns ());
478 ggc_collect ();
480 timevar_push (TV_GLOBAL_ALLOC);
481 open_dump_file (DFI_greg, current_function_decl);
483 /* If optimizing, allocate remaining pseudo-regs. Do the reload
484 pass fixing up any insns that are invalid. */
486 if (optimize)
487 failure = global_alloc (dump_file);
488 else
490 build_insn_chain (get_insns ());
491 failure = reload (get_insns (), 0);
494 if (dump_enabled_p (DFI_greg))
496 timevar_push (TV_DUMP);
497 dump_global_regs (dump_file);
498 timevar_pop (TV_DUMP);
500 close_dump_file (DFI_greg, print_rtl_with_bb, get_insns ());
503 ggc_collect ();
505 timevar_pop (TV_GLOBAL_ALLOC);
507 return failure;
510 /* Run the regrename and cprop passes. */
511 static void
512 rest_of_handle_regrename (void)
514 timevar_push (TV_RENAME_REGISTERS);
515 open_dump_file (DFI_rnreg, current_function_decl);
517 if (flag_rename_registers)
518 regrename_optimize ();
519 if (flag_cprop_registers)
520 copyprop_hardreg_forward ();
522 close_dump_file (DFI_rnreg, print_rtl_with_bb, get_insns ());
523 timevar_pop (TV_RENAME_REGISTERS);
526 /* Reorder basic blocks. */
527 static void
528 rest_of_handle_reorder_blocks (void)
530 bool changed;
531 unsigned int liveness_flags;
533 open_dump_file (DFI_bbro, current_function_decl);
535 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
536 splitting possibly introduced more crossjumping opportunities. */
537 liveness_flags = (!HAVE_conditional_execution ? CLEANUP_UPDATE_LIFE : 0);
538 changed = cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
540 if (flag_sched2_use_traces && flag_schedule_insns_after_reload)
541 tracer (liveness_flags);
542 if (flag_reorder_blocks || flag_reorder_blocks_and_partition)
543 reorder_basic_blocks (liveness_flags);
544 if (flag_reorder_blocks || flag_reorder_blocks_and_partition
545 || (flag_sched2_use_traces && flag_schedule_insns_after_reload))
546 changed |= cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
548 /* On conditional execution targets we can not update the life cheaply, so
549 we deffer the updating to after both cleanups. This may lose some cases
550 but should not be terribly bad. */
551 if (changed && HAVE_conditional_execution)
552 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
553 PROP_DEATH_NOTES);
554 close_dump_file (DFI_bbro, print_rtl_with_bb, get_insns ());
557 /* Partition hot and cold basic blocks. */
558 static void
559 rest_of_handle_partition_blocks (void)
561 no_new_pseudos = 0;
562 partition_hot_cold_basic_blocks ();
563 allocate_reg_life_data ();
564 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
565 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
566 no_new_pseudos = 1;
569 #ifdef INSN_SCHEDULING
570 /* Run instruction scheduler. */
571 /* Perform SMS module scheduling. */
572 static void
573 rest_of_handle_sms (void)
575 basic_block bb;
576 sbitmap blocks;
578 timevar_push (TV_SMS);
579 open_dump_file (DFI_sms, current_function_decl);
581 /* We want to be able to create new pseudos. */
582 no_new_pseudos = 0;
583 /* Collect loop information to be used in SMS. */
584 cfg_layout_initialize (CLEANUP_UPDATE_LIFE);
585 sms_schedule (dump_file);
586 close_dump_file (DFI_sms, print_rtl, get_insns ());
588 /* Update the life information, because we add pseudos. */
589 max_regno = max_reg_num ();
590 allocate_reg_info (max_regno, FALSE, FALSE);
591 blocks = sbitmap_alloc (last_basic_block);
592 sbitmap_ones (blocks);
593 update_life_info (blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
594 (PROP_DEATH_NOTES
595 | PROP_REG_INFO
596 | PROP_KILL_DEAD_CODE
597 | PROP_SCAN_DEAD_CODE));
599 no_new_pseudos = 1;
601 /* Finalize layout changes. */
602 FOR_EACH_BB (bb)
603 if (bb->next_bb != EXIT_BLOCK_PTR)
604 bb->rbi->next = bb->next_bb;
605 cfg_layout_finalize ();
606 free_dominance_info (CDI_DOMINATORS);
607 ggc_collect ();
608 timevar_pop (TV_SMS);
611 /* Run instruction scheduler. */
612 static void
613 rest_of_handle_sched (void)
615 timevar_push (TV_SCHED);
617 /* Print function header into sched dump now
618 because doing the sched analysis makes some of the dump. */
619 open_dump_file (DFI_sched, current_function_decl);
621 /* Do control and data sched analysis,
622 and write some of the results to dump file. */
624 schedule_insns (dump_file);
626 close_dump_file (DFI_sched, print_rtl_with_bb, get_insns ());
628 ggc_collect ();
629 timevar_pop (TV_SCHED);
632 /* Run second scheduling pass after reload. */
633 static void
634 rest_of_handle_sched2 (void)
636 timevar_push (TV_SCHED2);
637 open_dump_file (DFI_sched2, current_function_decl);
639 /* Do control and data sched analysis again,
640 and write some more of the results to dump file. */
642 split_all_insns (1);
644 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
646 schedule_ebbs (dump_file);
647 /* No liveness updating code yet, but it should be easy to do.
648 reg-stack recomputes the liveness when needed for now. */
649 count_or_remove_death_notes (NULL, 1);
650 cleanup_cfg (CLEANUP_EXPENSIVE);
652 else
653 schedule_insns (dump_file);
655 close_dump_file (DFI_sched2, print_rtl_with_bb, get_insns ());
657 ggc_collect ();
659 timevar_pop (TV_SCHED2);
661 #endif
663 static void
664 rest_of_handle_gcse2 (void)
666 timevar_push (TV_GCSE_AFTER_RELOAD);
667 open_dump_file (DFI_gcse2, current_function_decl);
669 gcse_after_reload_main (get_insns ());
670 rebuild_jump_labels (get_insns ());
671 delete_trivially_dead_insns (get_insns (), max_reg_num ());
672 close_dump_file (DFI_gcse2, print_rtl_with_bb, get_insns ());
674 ggc_collect ();
676 #ifdef ENABLE_CHECKING
677 verify_flow_info ();
678 #endif
680 timevar_pop (TV_GCSE_AFTER_RELOAD);
683 /* Register allocation pre-pass, to reduce number of moves necessary
684 for two-address machines. */
685 static void
686 rest_of_handle_regmove (void)
688 timevar_push (TV_REGMOVE);
689 open_dump_file (DFI_regmove, current_function_decl);
691 regmove_optimize (get_insns (), max_reg_num (), dump_file);
693 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
694 close_dump_file (DFI_regmove, print_rtl_with_bb, get_insns ());
696 ggc_collect ();
697 timevar_pop (TV_REGMOVE);
700 /* Run tracer. */
701 static void
702 rest_of_handle_tracer (void)
704 open_dump_file (DFI_tracer, current_function_decl);
705 if (dump_file)
706 dump_flow_info (dump_file);
707 tracer (0);
708 cleanup_cfg (CLEANUP_EXPENSIVE);
709 reg_scan (get_insns (), max_reg_num ());
710 close_dump_file (DFI_tracer, print_rtl_with_bb, get_insns ());
713 /* If-conversion and CFG cleanup. */
714 static void
715 rest_of_handle_if_conversion (void)
717 timevar_push (TV_IFCVT);
718 open_dump_file (DFI_ce1, current_function_decl);
720 if (flag_if_conversion)
722 if (dump_file)
723 dump_flow_info (dump_file);
724 cleanup_cfg (CLEANUP_EXPENSIVE);
725 reg_scan (get_insns (), max_reg_num ());
726 if_convert (0);
729 timevar_push (TV_JUMP);
730 cleanup_cfg (CLEANUP_EXPENSIVE);
731 reg_scan (get_insns (), max_reg_num ());
732 timevar_pop (TV_JUMP);
734 close_dump_file (DFI_ce1, print_rtl_with_bb, get_insns ());
735 timevar_pop (TV_IFCVT);
738 /* Rerun if-conversion, as combine may have simplified things enough
739 to now meet sequence length restrictions. */
740 static void
741 rest_of_handle_if_after_combine (void)
743 timevar_push (TV_IFCVT);
744 open_dump_file (DFI_ce2, current_function_decl);
746 no_new_pseudos = 0;
747 if_convert (1);
748 no_new_pseudos = 1;
750 close_dump_file (DFI_ce2, print_rtl_with_bb, get_insns ());
751 timevar_pop (TV_IFCVT);
754 static void
755 rest_of_handle_if_after_reload (void)
757 timevar_push (TV_IFCVT2);
758 open_dump_file (DFI_ce3, current_function_decl);
760 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
761 splitting possibly introduced more crossjumping opportunities. */
762 cleanup_cfg (CLEANUP_EXPENSIVE
763 | CLEANUP_UPDATE_LIFE
764 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
765 if (flag_if_conversion2)
766 if_convert (1);
767 close_dump_file (DFI_ce3, print_rtl_with_bb, get_insns ());
768 timevar_pop (TV_IFCVT2);
771 static void
772 rest_of_handle_web (void)
774 open_dump_file (DFI_web, current_function_decl);
775 timevar_push (TV_WEB);
776 web_main ();
777 delete_trivially_dead_insns (get_insns (), max_reg_num ());
778 cleanup_cfg (CLEANUP_EXPENSIVE);
780 timevar_pop (TV_WEB);
781 close_dump_file (DFI_web, print_rtl_with_bb, get_insns ());
782 reg_scan (get_insns (), max_reg_num ());
785 /* Do branch profiling and static profile estimation passes. */
786 static void
787 rest_of_handle_branch_prob (void)
789 struct loops loops;
791 timevar_push (TV_BRANCH_PROB);
792 open_dump_file (DFI_bp, current_function_decl);
794 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
795 branch_prob ();
797 /* Discover and record the loop depth at the head of each basic
798 block. The loop infrastructure does the real job for us. */
799 flow_loops_find (&loops);
801 if (dump_file)
802 flow_loops_dump (&loops, dump_file, NULL, 0);
804 /* Estimate using heuristics if no profiling info is available. */
805 if (flag_guess_branch_prob)
806 estimate_probability (&loops);
808 flow_loops_free (&loops);
809 free_dominance_info (CDI_DOMINATORS);
810 close_dump_file (DFI_bp, print_rtl_with_bb, get_insns ());
811 timevar_pop (TV_BRANCH_PROB);
814 /* Do optimizations based on expression value profiles. */
815 static void
816 rest_of_handle_value_profile_transformations (void)
818 open_dump_file (DFI_vpt, current_function_decl);
819 timevar_push (TV_VPT);
821 if (value_profile_transformations ())
822 cleanup_cfg (CLEANUP_EXPENSIVE);
824 timevar_pop (TV_VPT);
825 close_dump_file (DFI_vpt, print_rtl_with_bb, get_insns ());
828 /* Do control and data flow analysis; write some of the results to the
829 dump file. */
830 static void
831 rest_of_handle_cfg (void)
833 open_dump_file (DFI_cfg, current_function_decl);
834 if (dump_file)
835 dump_flow_info (dump_file);
836 if (optimize)
837 cleanup_cfg (CLEANUP_EXPENSIVE
838 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
840 /* It may make more sense to mark constant functions after dead code is
841 eliminated by life_analysis, but we need to do it early, as -fprofile-arcs
842 may insert code making function non-constant, but we still must consider
843 it as constant, otherwise -fbranch-probabilities will not read data back.
845 life_analysis rarely eliminates modification of external memory.
847 FIXME: now with tree based profiling we are in the trap described above
848 again. It seems to be easiest to disable the optimization for time
849 being before the problem is either solved by moving the transformation
850 to the IPA level (we need the CFG for this) or the very early optimization
851 passes are made to ignore the const/pure flags so code does not change. */
852 if (optimize
853 && (!flag_tree_based_profiling
854 || (!profile_arc_flag && !flag_branch_probabilities)))
856 /* Alias analysis depends on this information and mark_constant_function
857 depends on alias analysis. */
858 reg_scan (get_insns (), max_reg_num ());
859 mark_constant_function ();
862 close_dump_file (DFI_cfg, print_rtl_with_bb, get_insns ());
865 /* Perform jump bypassing and control flow optimizations. */
866 static void
867 rest_of_handle_jump_bypass (void)
869 timevar_push (TV_BYPASS);
870 open_dump_file (DFI_bypass, current_function_decl);
872 cleanup_cfg (CLEANUP_EXPENSIVE);
873 reg_scan (get_insns (), max_reg_num ());
875 if (bypass_jumps (dump_file))
877 rebuild_jump_labels (get_insns ());
878 cleanup_cfg (CLEANUP_EXPENSIVE);
879 delete_trivially_dead_insns (get_insns (), max_reg_num ());
882 close_dump_file (DFI_bypass, print_rtl_with_bb, get_insns ());
883 timevar_pop (TV_BYPASS);
885 ggc_collect ();
887 #ifdef ENABLE_CHECKING
888 verify_flow_info ();
889 #endif
892 /* Try combining insns through substitution. */
893 static void
894 rest_of_handle_combine (void)
896 int rebuild_jump_labels_after_combine = 0;
898 timevar_push (TV_COMBINE);
899 open_dump_file (DFI_combine, current_function_decl);
901 rebuild_jump_labels_after_combine
902 = combine_instructions (get_insns (), max_reg_num ());
904 /* Combining insns may have turned an indirect jump into a
905 direct jump. Rebuild the JUMP_LABEL fields of jumping
906 instructions. */
907 if (rebuild_jump_labels_after_combine)
909 timevar_push (TV_JUMP);
910 rebuild_jump_labels (get_insns ());
911 timevar_pop (TV_JUMP);
913 delete_dead_jumptables ();
914 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
917 close_dump_file (DFI_combine, print_rtl_with_bb, get_insns ());
918 timevar_pop (TV_COMBINE);
920 ggc_collect ();
923 /* Perform life analysis. */
924 static void
925 rest_of_handle_life (void)
927 open_dump_file (DFI_life, current_function_decl);
928 regclass_init ();
930 #ifdef ENABLE_CHECKING
931 verify_flow_info ();
932 #endif
933 life_analysis (dump_file, PROP_FINAL);
934 if (optimize)
935 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE | CLEANUP_LOG_LINKS
936 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
938 if (extra_warnings)
940 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
941 setjmp_args_warning ();
944 if (optimize)
946 if (initialize_uninitialized_subregs ())
948 /* Insns were inserted, and possibly pseudos created, so
949 things might look a bit different. */
950 allocate_reg_life_data ();
951 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
952 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
956 no_new_pseudos = 1;
958 close_dump_file (DFI_life, print_rtl_with_bb, get_insns ());
960 ggc_collect ();
963 /* Perform common subexpression elimination. Nonzero value from
964 `cse_main' means that jumps were simplified and some code may now
965 be unreachable, so do jump optimization again. */
966 static void
967 rest_of_handle_cse (void)
969 int tem;
971 open_dump_file (DFI_cse, current_function_decl);
972 if (dump_file)
973 dump_flow_info (dump_file);
974 timevar_push (TV_CSE);
976 reg_scan (get_insns (), max_reg_num ());
978 tem = cse_main (get_insns (), max_reg_num (), dump_file);
979 if (tem)
980 rebuild_jump_labels (get_insns ());
981 if (purge_all_dead_edges (0))
982 delete_unreachable_blocks ();
984 delete_trivially_dead_insns (get_insns (), max_reg_num ());
986 /* If we are not running more CSE passes, then we are no longer
987 expecting CSE to be run. But always rerun it in a cheap mode. */
988 cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
990 if (tem)
991 delete_dead_jumptables ();
993 if (tem || optimize > 1)
994 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
996 timevar_pop (TV_CSE);
997 close_dump_file (DFI_cse, print_rtl_with_bb, get_insns ());
999 ggc_collect ();
1002 /* Run second CSE pass after loop optimizations. */
1003 static void
1004 rest_of_handle_cse2 (void)
1006 int tem;
1008 timevar_push (TV_CSE2);
1009 open_dump_file (DFI_cse2, current_function_decl);
1010 if (dump_file)
1011 dump_flow_info (dump_file);
1012 /* CFG is no longer maintained up-to-date. */
1013 tem = cse_main (get_insns (), max_reg_num (), dump_file);
1015 /* Run a pass to eliminate duplicated assignments to condition code
1016 registers. We have to run this after bypass_jumps, because it
1017 makes it harder for that pass to determine whether a jump can be
1018 bypassed safely. */
1019 cse_condition_code_reg ();
1021 purge_all_dead_edges (0);
1022 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1024 if (tem)
1026 timevar_push (TV_JUMP);
1027 rebuild_jump_labels (get_insns ());
1028 delete_dead_jumptables ();
1029 cleanup_cfg (CLEANUP_EXPENSIVE);
1030 timevar_pop (TV_JUMP);
1032 reg_scan (get_insns (), max_reg_num ());
1033 close_dump_file (DFI_cse2, print_rtl_with_bb, get_insns ());
1034 timevar_pop (TV_CSE2);
1036 ggc_collect ();
1039 /* Perform global cse. */
1040 static void
1041 rest_of_handle_gcse (void)
1043 int save_csb, save_cfj;
1044 int tem2 = 0, tem;
1046 timevar_push (TV_GCSE);
1047 open_dump_file (DFI_gcse, current_function_decl);
1049 tem = gcse_main (get_insns (), dump_file);
1050 rebuild_jump_labels (get_insns ());
1051 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1053 save_csb = flag_cse_skip_blocks;
1054 save_cfj = flag_cse_follow_jumps;
1055 flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
1057 /* If -fexpensive-optimizations, re-run CSE to clean up things done
1058 by gcse. */
1059 if (flag_expensive_optimizations)
1061 timevar_push (TV_CSE);
1062 reg_scan (get_insns (), max_reg_num ());
1063 tem2 = cse_main (get_insns (), max_reg_num (), dump_file);
1064 purge_all_dead_edges (0);
1065 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1066 timevar_pop (TV_CSE);
1067 cse_not_expected = !flag_rerun_cse_after_loop;
1070 /* If gcse or cse altered any jumps, rerun jump optimizations to clean
1071 things up. */
1072 if (tem || tem2)
1074 timevar_push (TV_JUMP);
1075 rebuild_jump_labels (get_insns ());
1076 delete_dead_jumptables ();
1077 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1078 timevar_pop (TV_JUMP);
1081 close_dump_file (DFI_gcse, print_rtl_with_bb, get_insns ());
1082 timevar_pop (TV_GCSE);
1084 ggc_collect ();
1085 flag_cse_skip_blocks = save_csb;
1086 flag_cse_follow_jumps = save_cfj;
1087 #ifdef ENABLE_CHECKING
1088 verify_flow_info ();
1089 #endif
1092 /* Move constant computations out of loops. */
1093 static void
1094 rest_of_handle_loop_optimize (void)
1096 int do_prefetch;
1098 timevar_push (TV_LOOP);
1099 open_dump_file (DFI_loop, current_function_decl);
1101 /* CFG is no longer maintained up-to-date. */
1102 free_bb_for_insn ();
1103 profile_status = PROFILE_ABSENT;
1105 do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
1107 if (flag_rerun_loop_opt)
1109 cleanup_barriers ();
1111 /* We only want to perform unrolling once. */
1112 loop_optimize (get_insns (), dump_file, 0);
1114 /* The first call to loop_optimize makes some instructions
1115 trivially dead. We delete those instructions now in the
1116 hope that doing so will make the heuristics in loop work
1117 better and possibly speed up compilation. */
1118 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1120 /* The regscan pass is currently necessary as the alias
1121 analysis code depends on this information. */
1122 reg_scan (get_insns (), max_reg_num ());
1124 cleanup_barriers ();
1125 loop_optimize (get_insns (), dump_file, do_prefetch);
1127 /* Loop can create trivially dead instructions. */
1128 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1129 find_basic_blocks (get_insns ());
1130 close_dump_file (DFI_loop, print_rtl, get_insns ());
1131 timevar_pop (TV_LOOP);
1133 ggc_collect ();
1136 /* Perform loop optimizations. It might be better to do them a bit
1137 sooner, but we want the profile feedback to work more
1138 efficiently. */
1139 static void
1140 rest_of_handle_loop2 (void)
1142 struct loops *loops;
1143 basic_block bb;
1145 if (!flag_move_loop_invariants
1146 && !flag_unswitch_loops
1147 && !flag_peel_loops
1148 && !flag_unroll_loops
1149 && !flag_branch_on_count_reg)
1150 return;
1152 timevar_push (TV_LOOP);
1153 open_dump_file (DFI_loop2, current_function_decl);
1154 if (dump_file)
1155 dump_flow_info (dump_file);
1157 /* Initialize structures for layout changes. */
1158 cfg_layout_initialize (0);
1160 loops = loop_optimizer_init (dump_file);
1162 if (loops)
1164 /* The optimizations: */
1165 if (flag_move_loop_invariants)
1166 move_loop_invariants (loops);
1168 if (flag_unswitch_loops)
1169 unswitch_loops (loops);
1171 if (flag_peel_loops || flag_unroll_loops)
1172 unroll_and_peel_loops (loops,
1173 (flag_peel_loops ? UAP_PEEL : 0) |
1174 (flag_unroll_loops ? UAP_UNROLL : 0) |
1175 (flag_unroll_all_loops ? UAP_UNROLL_ALL : 0));
1177 #ifdef HAVE_doloop_end
1178 if (flag_branch_on_count_reg && HAVE_doloop_end)
1179 doloop_optimize_loops (loops);
1180 #endif /* HAVE_doloop_end */
1182 loop_optimizer_finalize (loops, dump_file);
1185 free_dominance_info (CDI_DOMINATORS);
1187 /* Finalize layout changes. */
1188 FOR_EACH_BB (bb)
1189 if (bb->next_bb != EXIT_BLOCK_PTR)
1190 bb->rbi->next = bb->next_bb;
1191 cfg_layout_finalize ();
1193 cleanup_cfg (CLEANUP_EXPENSIVE);
1194 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1195 reg_scan (get_insns (), max_reg_num ());
1196 if (dump_file)
1197 dump_flow_info (dump_file);
1198 close_dump_file (DFI_loop2, print_rtl_with_bb, get_insns ());
1199 timevar_pop (TV_LOOP);
1200 ggc_collect ();
1203 static void
1204 rest_of_handle_branch_target_load_optimize (void)
1206 static int warned = 0;
1208 /* Leave this a warning for now so that it is possible to experiment
1209 with running this pass twice. In 3.6, we should either make this
1210 an error, or use separate dump files. */
1211 if (flag_branch_target_load_optimize
1212 && flag_branch_target_load_optimize2
1213 && !warned)
1215 warning ("branch target register load optimization is not intended "
1216 "to be run twice");
1218 warned = 1;
1221 open_dump_file (DFI_branch_target_load, current_function_decl);
1222 branch_target_load_optimize (epilogue_completed);
1223 close_dump_file (DFI_branch_target_load, print_rtl_with_bb, get_insns ());
1224 ggc_collect ();
1227 #ifdef OPTIMIZE_MODE_SWITCHING
1228 static void
1229 rest_of_handle_mode_switching (void)
1231 timevar_push (TV_MODE_SWITCH);
1233 no_new_pseudos = 0;
1234 optimize_mode_switching (NULL);
1235 no_new_pseudos = 1;
1237 timevar_pop (TV_MODE_SWITCH);
1239 #endif
1241 static void
1242 rest_of_handle_jump (void)
1244 ggc_collect ();
1246 timevar_push (TV_JUMP);
1247 open_dump_file (DFI_sibling, current_function_decl);
1249 delete_unreachable_blocks ();
1250 #ifdef ENABLE_CHECKING
1251 verify_flow_info ();
1252 #endif
1254 if (cfun->tail_call_emit)
1255 fixup_tail_calls ();
1257 close_dump_file (DFI_sibling, print_rtl, get_insns ());
1258 timevar_pop (TV_JUMP);
1261 static void
1262 rest_of_handle_eh (void)
1264 insn_locators_initialize ();
1265 /* Complete generation of exception handling code. */
1266 if (doing_eh (0))
1268 timevar_push (TV_JUMP);
1269 open_dump_file (DFI_eh, current_function_decl);
1271 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1273 finish_eh_generation ();
1275 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1277 close_dump_file (DFI_eh, print_rtl, get_insns ());
1278 timevar_pop (TV_JUMP);
1282 static void
1283 rest_of_handle_stack_adjustments (void)
1285 life_analysis (dump_file, PROP_POSTRELOAD);
1286 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE
1287 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1289 /* This is kind of a heuristic. We need to run combine_stack_adjustments
1290 even for machines with possibly nonzero RETURN_POPS_ARGS
1291 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
1292 push instructions will have popping returns. */
1293 #ifndef PUSH_ROUNDING
1294 if (!ACCUMULATE_OUTGOING_ARGS)
1295 #endif
1296 combine_stack_adjustments ();
1299 static void
1300 rest_of_handle_flow2 (void)
1302 timevar_push (TV_FLOW2);
1303 open_dump_file (DFI_flow2, current_function_decl);
1305 /* Re-create the death notes which were deleted during reload. */
1306 #ifdef ENABLE_CHECKING
1307 verify_flow_info ();
1308 #endif
1310 /* If optimizing, then go ahead and split insns now. */
1311 #ifndef STACK_REGS
1312 if (optimize > 0)
1313 #endif
1314 split_all_insns (0);
1316 if (flag_branch_target_load_optimize)
1318 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1319 rest_of_handle_branch_target_load_optimize ();
1320 open_dump_file (DFI_flow2, current_function_decl);
1323 if (optimize)
1324 cleanup_cfg (CLEANUP_EXPENSIVE);
1326 /* On some machines, the prologue and epilogue code, or parts thereof,
1327 can be represented as RTL. Doing so lets us schedule insns between
1328 it and the rest of the code and also allows delayed branch
1329 scheduling to operate in the epilogue. */
1330 thread_prologue_and_epilogue_insns (get_insns ());
1331 epilogue_completed = 1;
1333 if (optimize)
1334 rest_of_handle_stack_adjustments ();
1336 flow2_completed = 1;
1338 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1339 timevar_pop (TV_FLOW2);
1341 ggc_collect ();
1345 static void
1346 rest_of_handle_jump2 (void)
1348 open_dump_file (DFI_jump, current_function_decl);
1350 /* Always do one jump optimization pass to ensure that JUMP_LABEL fields
1351 are initialized and to compute whether control can drop off the end
1352 of the function. */
1354 timevar_push (TV_JUMP);
1355 /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB. Do this
1356 before jump optimization switches branch directions. */
1357 if (flag_guess_branch_prob)
1358 expected_value_to_br_prob ();
1360 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1361 reg_scan (get_insns (), max_reg_num ());
1362 if (dump_file)
1363 dump_flow_info (dump_file);
1364 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_PRE_LOOP
1365 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1367 create_loop_notes ();
1369 purge_line_number_notes (get_insns ());
1371 if (optimize)
1372 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1374 /* Jump optimization, and the removal of NULL pointer checks, may
1375 have reduced the number of instructions substantially. CSE, and
1376 future passes, allocate arrays whose dimensions involve the
1377 maximum instruction UID, so if we can reduce the maximum UID
1378 we'll save big on memory. */
1379 renumber_insns (dump_file);
1381 close_dump_file (DFI_jump, print_rtl_with_bb, get_insns ());
1382 timevar_pop (TV_JUMP);
1384 ggc_collect ();
1387 #ifdef HAVE_peephole2
1388 static void
1389 rest_of_handle_peephole2 (void)
1391 timevar_push (TV_PEEPHOLE2);
1392 open_dump_file (DFI_peephole2, current_function_decl);
1394 peephole2_optimize (dump_file);
1396 close_dump_file (DFI_peephole2, print_rtl_with_bb, get_insns ());
1397 timevar_pop (TV_PEEPHOLE2);
1399 #endif
1401 static void
1402 rest_of_handle_postreload (void)
1404 timevar_push (TV_RELOAD_CSE_REGS);
1405 open_dump_file (DFI_postreload, current_function_decl);
1407 /* Do a very simple CSE pass over just the hard registers. */
1408 reload_cse_regs (get_insns ());
1409 /* reload_cse_regs can eliminate potentially-trapping MEMs.
1410 Remove any EH edges associated with them. */
1411 if (flag_non_call_exceptions)
1412 purge_all_dead_edges (0);
1414 close_dump_file (DFI_postreload, print_rtl_with_bb, get_insns ());
1415 timevar_pop (TV_RELOAD_CSE_REGS);
1418 static void
1419 rest_of_handle_shorten_branches (void)
1421 /* Shorten branches. */
1422 timevar_push (TV_SHORTEN_BRANCH);
1423 shorten_branches (get_insns ());
1424 timevar_pop (TV_SHORTEN_BRANCH);
1427 static void
1428 rest_of_clean_state (void)
1430 rtx insn, next;
1431 coverage_end_function ();
1433 /* It is very important to decompose the RTL instruction chain here:
1434 debug information keeps pointing into CODE_LABEL insns inside the function
1435 body. If these remain pointing to the other insns, we end up preserving
1436 whole RTL chain and attached detailed debug info in memory. */
1437 for (insn = get_insns (); insn; insn = next)
1439 next = NEXT_INSN (insn);
1440 NEXT_INSN (insn) = NULL;
1441 PREV_INSN (insn) = NULL;
1444 /* In case the function was not output,
1445 don't leave any temporary anonymous types
1446 queued up for sdb output. */
1447 #ifdef SDB_DEBUGGING_INFO
1448 if (write_symbols == SDB_DEBUG)
1449 sdbout_types (NULL_TREE);
1450 #endif
1452 reload_completed = 0;
1453 epilogue_completed = 0;
1454 flow2_completed = 0;
1455 no_new_pseudos = 0;
1457 timevar_push (TV_FINAL);
1459 /* Clear out the insn_length contents now that they are no
1460 longer valid. */
1461 init_insn_lengths ();
1463 /* Show no temporary slots allocated. */
1464 init_temp_slots ();
1466 free_basic_block_vars ();
1467 free_bb_for_insn ();
1469 timevar_pop (TV_FINAL);
1471 if (targetm.binds_local_p (current_function_decl))
1473 int pref = cfun->preferred_stack_boundary;
1474 if (cfun->stack_alignment_needed > cfun->preferred_stack_boundary)
1475 pref = cfun->stack_alignment_needed;
1476 cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
1477 = pref;
1480 /* Make sure volatile mem refs aren't considered valid operands for
1481 arithmetic insns. We must call this here if this is a nested inline
1482 function, since the above code leaves us in the init_recog state
1483 (from final.c), and the function context push/pop code does not
1484 save/restore volatile_ok.
1486 ??? Maybe it isn't necessary for expand_start_function to call this
1487 anymore if we do it here? */
1489 init_recog_no_volatile ();
1491 /* We're done with this function. Free up memory if we can. */
1492 free_after_parsing (cfun);
1493 free_after_compilation (cfun);
1497 /* This function is called from the pass manager in tree-optimize.c
1498 after all tree passes have finished for a single function, and we
1499 have expanded the function body from trees to RTL.
1500 Once we are here, we have decided that we're supposed to output
1501 that function, i.e. that we should write assembler code for it.
1503 We run a series of low-level passes here on the function's RTL
1504 representation. Each pass is called via a rest_of_* function. */
1506 static void
1507 rest_of_compilation (void)
1509 /* If we're emitting a nested function, make sure its parent gets
1510 emitted as well. Doing otherwise confuses debug info. */
1512 tree parent;
1513 for (parent = DECL_CONTEXT (current_function_decl);
1514 parent != NULL_TREE;
1515 parent = get_containing_scope (parent))
1516 if (TREE_CODE (parent) == FUNCTION_DECL)
1517 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1520 /* We are now committed to emitting code for this function. Do any
1521 preparation, such as emitting abstract debug info for the inline
1522 before it gets mangled by optimization. */
1523 if (cgraph_function_possibly_inlined_p (current_function_decl))
1524 (*debug_hooks->outlining_inline_function) (current_function_decl);
1526 /* Remove any notes we don't need. That will make iterating
1527 over the instruction sequence faster, and allow the garbage
1528 collector to reclaim the memory used by the notes. */
1529 remove_unnecessary_notes ();
1531 /* Initialize some variables used by the optimizers. */
1532 init_function_for_compilation ();
1534 TREE_ASM_WRITTEN (current_function_decl) = 1;
1536 /* Early return if there were errors. We can run afoul of our
1537 consistency checks, and there's not really much point in fixing them. */
1538 if (rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount)
1539 goto exit_rest_of_compilation;
1541 rest_of_handle_jump ();
1543 rest_of_handle_eh ();
1545 /* Delay emitting hard_reg_initial_value sets until after EH landing pad
1546 generation, which might create new sets. */
1547 emit_initial_value_sets ();
1549 #ifdef FINALIZE_PIC
1550 /* If we are doing position-independent code generation, now
1551 is the time to output special prologues and epilogues.
1552 We do not want to do this earlier, because it just clutters
1553 up inline functions with meaningless insns. */
1554 if (flag_pic)
1555 FINALIZE_PIC;
1556 #endif
1558 /* Copy any shared structure that should not be shared. */
1559 unshare_all_rtl ();
1561 #ifdef SETJMP_VIA_SAVE_AREA
1562 /* This must be performed before virtual register instantiation.
1563 Please be aware that everything in the compiler that can look
1564 at the RTL up to this point must understand that REG_SAVE_AREA
1565 is just like a use of the REG contained inside. */
1566 if (current_function_calls_alloca)
1567 optimize_save_area_alloca ();
1568 #endif
1570 /* Instantiate all virtual registers. */
1571 instantiate_virtual_regs ();
1573 rest_of_handle_jump2 ();
1575 if (optimize > 0)
1576 rest_of_handle_cse ();
1578 if (optimize > 0)
1580 if (flag_gcse)
1581 rest_of_handle_gcse ();
1583 if (flag_loop_optimize)
1584 rest_of_handle_loop_optimize ();
1586 if (flag_gcse)
1587 rest_of_handle_jump_bypass ();
1590 timevar_push (TV_FLOW);
1591 rest_of_handle_cfg ();
1593 if (!flag_tree_based_profiling
1594 && (optimize > 0 || profile_arc_flag
1595 || flag_test_coverage || flag_branch_probabilities))
1597 rtl_register_profile_hooks ();
1598 rtl_register_value_prof_hooks ();
1599 rest_of_handle_branch_prob ();
1601 if (flag_branch_probabilities
1602 && flag_profile_values
1603 && (flag_value_profile_transformations
1604 || flag_speculative_prefetching))
1605 rest_of_handle_value_profile_transformations ();
1607 /* Remove the death notes created for vpt. */
1608 if (flag_profile_values)
1609 count_or_remove_death_notes (NULL, 1);
1612 if (optimize > 0)
1613 rest_of_handle_if_conversion ();
1615 if (optimize > 0 && flag_tracer)
1616 rest_of_handle_tracer ();
1618 if (optimize > 0
1619 && flag_loop_optimize2)
1620 rest_of_handle_loop2 ();
1622 if (optimize > 0 && flag_web)
1623 rest_of_handle_web ();
1625 if (optimize > 0 && flag_rerun_cse_after_loop)
1626 rest_of_handle_cse2 ();
1628 cse_not_expected = 1;
1630 rest_of_handle_life ();
1631 timevar_pop (TV_FLOW);
1633 if (optimize > 0)
1634 rest_of_handle_combine ();
1636 if (optimize > 0 && flag_if_conversion)
1637 rest_of_handle_if_after_combine ();
1639 /* The optimization to partition hot/cold basic blocks into separate
1640 sections of the .o file does not work well with linkonce or with
1641 user defined section attributes. Don't call it if either case
1642 arises. */
1644 if (flag_reorder_blocks_and_partition
1645 && !DECL_ONE_ONLY (current_function_decl)
1646 && !user_defined_section_attribute)
1647 rest_of_handle_partition_blocks ();
1649 if (optimize > 0 && flag_regmove)
1650 rest_of_handle_regmove ();
1652 /* Do unconditional splitting before register allocation to allow machine
1653 description to add extra information not needed previously. */
1654 split_all_insns (1);
1656 #ifdef OPTIMIZE_MODE_SWITCHING
1657 rest_of_handle_mode_switching ();
1658 #endif
1660 /* Any of the several passes since flow1 will have munged register
1661 lifetime data a bit. We need it to be up to date for scheduling
1662 (see handling of reg_known_equiv in init_alias_analysis). */
1663 recompute_reg_usage ();
1665 #ifdef INSN_SCHEDULING
1666 if (optimize > 0 && flag_modulo_sched)
1667 rest_of_handle_sms ();
1669 if (flag_schedule_insns)
1670 rest_of_handle_sched ();
1671 #endif
1673 /* Determine if the current function is a leaf before running reload
1674 since this can impact optimizations done by the prologue and
1675 epilogue thus changing register elimination offsets. */
1676 current_function_is_leaf = leaf_function_p ();
1678 if (rest_of_handle_old_regalloc ())
1679 goto exit_rest_of_compilation;
1681 if (optimize > 0)
1682 rest_of_handle_postreload ();
1684 if (optimize > 0 && flag_gcse_after_reload)
1685 rest_of_handle_gcse2 ();
1687 rest_of_handle_flow2 ();
1689 #ifdef HAVE_peephole2
1690 if (optimize > 0 && flag_peephole2)
1691 rest_of_handle_peephole2 ();
1692 #endif
1694 if (optimize > 0)
1695 rest_of_handle_if_after_reload ();
1697 if (optimize > 0)
1699 if (flag_rename_registers || flag_cprop_registers)
1700 rest_of_handle_regrename ();
1702 rest_of_handle_reorder_blocks ();
1705 if (flag_branch_target_load_optimize2)
1706 rest_of_handle_branch_target_load_optimize ();
1708 #ifdef LEAF_REGISTERS
1709 current_function_uses_only_leaf_regs
1710 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
1711 #endif
1713 #ifdef INSN_SCHEDULING
1714 if (optimize > 0 && flag_schedule_insns_after_reload)
1715 rest_of_handle_sched2 ();
1716 #endif
1718 #ifdef STACK_REGS
1719 rest_of_handle_stack_regs ();
1720 #endif
1722 compute_alignments ();
1724 /* Aggressively duplicate basic blocks ending in computed gotos to the
1725 tails of their predecessors, unless we are optimizing for size. */
1726 if (flag_expensive_optimizations && !optimize_size)
1727 duplicate_computed_gotos ();
1729 if (flag_var_tracking)
1730 rest_of_handle_variable_tracking ();
1732 /* CFG is no longer maintained up-to-date. */
1733 free_bb_for_insn ();
1735 if (targetm.machine_dependent_reorg != 0)
1736 rest_of_handle_machine_reorg ();
1738 purge_line_number_notes (get_insns ());
1739 cleanup_barriers ();
1741 #ifdef DELAY_SLOTS
1742 if (flag_delayed_branch)
1743 rest_of_handle_delay_slots ();
1744 #endif
1746 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
1747 timevar_push (TV_SHORTEN_BRANCH);
1748 split_all_insns_noflow ();
1749 timevar_pop (TV_SHORTEN_BRANCH);
1750 #endif
1752 convert_to_eh_region_ranges ();
1754 rest_of_handle_shorten_branches ();
1756 set_nothrow_function_flags ();
1758 rest_of_handle_final ();
1760 exit_rest_of_compilation:
1762 rest_of_clean_state ();
1765 void
1766 finish_optimization_passes (void)
1768 enum tree_dump_index i;
1769 struct dump_file_info *dfi;
1770 char *name;
1772 timevar_push (TV_DUMP);
1773 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
1775 open_dump_file (DFI_bp, NULL);
1776 end_branch_prob ();
1777 close_dump_file (DFI_bp, NULL, NULL_RTX);
1780 if (optimize > 0 && open_dump_file (DFI_combine, NULL))
1782 dump_combine_total_stats (dump_file);
1783 close_dump_file (DFI_combine, NULL, NULL_RTX);
1786 /* Do whatever is necessary to finish printing the graphs. */
1787 if (graph_dump_format != no_graph)
1788 for (i = DFI_MIN; (dfi = get_dump_file_info (i)) != NULL; ++i)
1789 if (dump_initialized_p (i)
1790 && (dfi->flags & TDF_RTL) != 0
1791 && (name = get_dump_file_name (i)) != NULL)
1793 finish_graph_dump_file (name);
1794 free (name);
1797 timevar_pop (TV_DUMP);
1800 struct tree_opt_pass pass_rest_of_compilation =
1802 NULL, /* name */
1803 NULL, /* gate */
1804 rest_of_compilation, /* execute */
1805 NULL, /* sub */
1806 NULL, /* next */
1807 0, /* static_pass_number */
1808 TV_REST_OF_COMPILATION, /* tv_id */
1809 PROP_rtl, /* properties_required */
1810 0, /* properties_provided */
1811 PROP_rtl, /* properties_destroyed */
1812 0, /* todo_flags_start */
1813 TODO_ggc_collect, /* todo_flags_finish */
1814 0 /* letter */