Mark ChangeLog
[official-gcc.git] / gcc / passes.c
blob4d7fb5bf8ec3a0137ac6b6d947a92fa8886918e2
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 if (dump_file != NULL || dump_file_name != NULL)
124 abort ();
126 dump_file_name = get_dump_file_name (index);
127 initializing_dump = !dump_initialized_p (index);
128 dump_file = dump_begin (index, NULL);
130 if (dump_file == NULL)
131 fatal_error ("can't open %s: %m", dump_file_name);
133 if (decl)
134 fprintf (dump_file, "\n;; Function %s%s\n\n",
135 lang_hooks.decl_printable_name (decl, 2),
136 cfun->function_frequency == FUNCTION_FREQUENCY_HOT
137 ? " (hot)"
138 : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
139 ? " (unlikely executed)"
140 : "");
142 timevar_pop (TV_DUMP);
143 return 1;
146 /* Routine to close a dump file. */
148 static void
149 close_dump_file (enum tree_dump_index index,
150 void (*func) (FILE *, rtx),
151 rtx insns)
153 if (! dump_file)
154 return;
156 timevar_push (TV_DUMP);
157 if (insns
158 && graph_dump_format != no_graph)
160 /* If we've not initialized the files, do so now. */
161 if (initializing_dump)
163 clean_graph_dump_file (dump_file_name);
164 get_dump_file_info (index)->flags |= TDF_GRAPH;
167 print_rtl_graph_with_bb (dump_file_name, insns);
170 if (func && insns)
171 func (dump_file, insns);
173 dump_end (index, dump_file);
174 free ((char *) dump_file_name);
176 dump_file = NULL;
177 dump_file_name = NULL;
178 timevar_pop (TV_DUMP);
181 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
182 and TYPE_DECL nodes.
184 This does nothing for local (non-static) variables, unless the
185 variable is a register variable with DECL_ASSEMBLER_NAME set. In
186 that case, or if the variable is not an automatic, it sets up the
187 RTL and outputs any assembler code (label definition, storage
188 allocation and initialization).
190 DECL is the declaration. TOP_LEVEL is nonzero
191 if this declaration is not within a function. */
193 void
194 rest_of_decl_compilation (tree decl,
195 int top_level,
196 int at_end)
198 /* We deferred calling assemble_alias so that we could collect
199 other attributes such as visibility. Emit the alias now. */
201 tree alias;
202 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
203 if (alias)
205 alias = TREE_VALUE (TREE_VALUE (alias));
206 alias = get_identifier (TREE_STRING_POINTER (alias));
207 assemble_alias (decl, alias);
211 /* Can't defer this, because it needs to happen before any
212 later function definitions are processed. */
213 if (DECL_REGISTER (decl) && DECL_ASSEMBLER_NAME_SET_P (decl))
214 make_decl_rtl (decl);
216 /* Forward declarations for nested functions are not "external",
217 but we need to treat them as if they were. */
218 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
219 || TREE_CODE (decl) == FUNCTION_DECL)
221 timevar_push (TV_VARCONST);
223 /* Don't output anything when a tentative file-scope definition
224 is seen. But at end of compilation, do output code for them.
226 We do output all variables when unit-at-a-time is active and rely on
227 callgraph code to defer them except for forward declarations
228 (see gcc.c-torture/compile/920624-1.c) */
229 if ((at_end
230 || !DECL_DEFER_OUTPUT (decl)
231 || (flag_unit_at_a_time && DECL_INITIAL (decl)))
232 && !DECL_EXTERNAL (decl))
234 if (flag_unit_at_a_time && !cgraph_global_info_ready
235 && TREE_CODE (decl) != FUNCTION_DECL && top_level)
236 cgraph_varpool_finalize_decl (decl);
237 else
238 assemble_variable (decl, top_level, at_end, 0);
241 #ifdef ASM_FINISH_DECLARE_OBJECT
242 if (decl == last_assemble_variable_decl)
244 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
245 top_level, at_end);
247 #endif
249 timevar_pop (TV_VARCONST);
251 else if (TREE_CODE (decl) == TYPE_DECL
252 /* Like in rest_of_type_compilation, avoid confusing the debug
253 information machinery when there are errors. */
254 && !(sorrycount || errorcount))
256 timevar_push (TV_SYMOUT);
257 debug_hooks->type_decl (decl, !top_level);
258 timevar_pop (TV_SYMOUT);
261 /* Let cgraph know about the existance of variables. */
262 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
263 cgraph_varpool_node (decl);
266 /* Called after finishing a record, union or enumeral type. */
268 void
269 rest_of_type_compilation (tree type, int toplev)
271 /* Avoid confusing the debug information machinery when there are
272 errors. */
273 if (errorcount != 0 || sorrycount != 0)
274 return;
276 timevar_push (TV_SYMOUT);
277 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
278 timevar_pop (TV_SYMOUT);
281 /* Turn the RTL into assembly. */
282 static void
283 rest_of_handle_final (void)
285 timevar_push (TV_FINAL);
287 rtx x;
288 const char *fnname;
290 /* Get the function's name, as described by its RTL. This may be
291 different from the DECL_NAME name used in the source file. */
293 x = DECL_RTL (current_function_decl);
294 if (!MEM_P (x))
295 abort ();
296 x = XEXP (x, 0);
297 if (GET_CODE (x) != SYMBOL_REF)
298 abort ();
299 fnname = XSTR (x, 0);
301 assemble_start_function (current_function_decl, fnname);
302 final_start_function (get_insns (), asm_out_file, optimize);
303 final (get_insns (), asm_out_file, optimize, 0);
304 final_end_function ();
306 #ifdef TARGET_UNWIND_INFO
307 /* ??? The IA-64 ".handlerdata" directive must be issued before
308 the ".endp" directive that closes the procedure descriptor. */
309 output_function_exception_table ();
310 #endif
312 assemble_end_function (current_function_decl, fnname);
314 #ifndef TARGET_UNWIND_INFO
315 /* Otherwise, it feels unclean to switch sections in the middle. */
316 output_function_exception_table ();
317 #endif
319 user_defined_section_attribute = false;
321 if (! quiet_flag)
322 fflush (asm_out_file);
324 /* Release all memory allocated by flow. */
325 free_basic_block_vars ();
328 /* Write DBX symbols if requested. */
330 /* Note that for those inline functions where we don't initially
331 know for certain that we will be generating an out-of-line copy,
332 the first invocation of this routine (rest_of_compilation) will
333 skip over this code by doing a `goto exit_rest_of_compilation;'.
334 Later on, wrapup_global_declarations will (indirectly) call
335 rest_of_compilation again for those inline functions that need
336 to have out-of-line copies generated. During that call, we
337 *will* be routed past here. */
339 timevar_push (TV_SYMOUT);
340 (*debug_hooks->function_decl) (current_function_decl);
341 timevar_pop (TV_SYMOUT);
343 ggc_collect ();
344 timevar_pop (TV_FINAL);
347 #ifdef DELAY_SLOTS
348 /* Run delay slot optimization. */
349 static void
350 rest_of_handle_delay_slots (void)
352 timevar_push (TV_DBR_SCHED);
353 open_dump_file (DFI_dbr, current_function_decl);
355 dbr_schedule (get_insns (), dump_file);
357 close_dump_file (DFI_dbr, print_rtl, get_insns ());
359 ggc_collect ();
361 timevar_pop (TV_DBR_SCHED);
363 #endif
365 #ifdef STACK_REGS
366 /* Convert register usage from flat register file usage to a stack
367 register file. */
368 static void
369 rest_of_handle_stack_regs (void)
371 #if defined (HAVE_ATTR_length)
372 /* If flow2 creates new instructions which need splitting
373 and scheduling after reload is not done, they might not be
374 split until final which doesn't allow splitting
375 if HAVE_ATTR_length. */
376 #ifdef INSN_SCHEDULING
377 if (optimize && !flag_schedule_insns_after_reload)
378 #else
379 if (optimize)
380 #endif
382 timevar_push (TV_SHORTEN_BRANCH);
383 split_all_insns (1);
384 timevar_pop (TV_SHORTEN_BRANCH);
386 #endif
388 timevar_push (TV_REG_STACK);
389 open_dump_file (DFI_stack, current_function_decl);
391 if (reg_to_stack (dump_file) && optimize)
393 if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
394 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
395 && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
397 reorder_basic_blocks (0);
398 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
402 close_dump_file (DFI_stack, print_rtl_with_bb, get_insns ());
404 ggc_collect ();
405 timevar_pop (TV_REG_STACK);
407 #endif
409 /* Track the variables, i.e. compute where the variable is stored at each position in function. */
410 static void
411 rest_of_handle_variable_tracking (void)
413 timevar_push (TV_VAR_TRACKING);
414 open_dump_file (DFI_vartrack, current_function_decl);
416 variable_tracking_main ();
418 close_dump_file (DFI_vartrack, print_rtl_with_bb, get_insns ());
419 timevar_pop (TV_VAR_TRACKING);
422 /* Machine dependent reorg pass. */
423 static void
424 rest_of_handle_machine_reorg (void)
426 timevar_push (TV_MACH_DEP);
427 open_dump_file (DFI_mach, current_function_decl);
429 targetm.machine_dependent_reorg ();
431 close_dump_file (DFI_mach, print_rtl, get_insns ());
433 ggc_collect ();
434 timevar_pop (TV_MACH_DEP);
438 /* Run old register allocator. Return TRUE if we must exit
439 rest_of_compilation upon return. */
440 static bool
441 rest_of_handle_old_regalloc (void)
443 int failure;
444 int rebuild_notes;
446 timevar_push (TV_LOCAL_ALLOC);
447 open_dump_file (DFI_lreg, current_function_decl);
449 /* Allocate the reg_renumber array. */
450 allocate_reg_info (max_regno, FALSE, TRUE);
452 /* And the reg_equiv_memory_loc array. */
453 VARRAY_GROW (reg_equiv_memory_loc_varray, max_regno);
454 reg_equiv_memory_loc = &VARRAY_RTX (reg_equiv_memory_loc_varray, 0);
456 allocate_initial_values (reg_equiv_memory_loc);
458 regclass (get_insns (), max_reg_num (), dump_file);
459 rebuild_notes = local_alloc ();
461 timevar_pop (TV_LOCAL_ALLOC);
463 /* Local allocation may have turned an indirect jump into a direct
464 jump. If so, we must rebuild the JUMP_LABEL fields of jumping
465 instructions. */
466 if (rebuild_notes)
468 timevar_push (TV_JUMP);
470 rebuild_jump_labels (get_insns ());
471 purge_all_dead_edges (0);
472 delete_unreachable_blocks ();
474 timevar_pop (TV_JUMP);
477 if (dump_enabled_p (DFI_lreg))
479 timevar_push (TV_DUMP);
480 dump_flow_info (dump_file);
481 dump_local_alloc (dump_file);
482 timevar_pop (TV_DUMP);
485 close_dump_file (DFI_lreg, print_rtl_with_bb, get_insns ());
487 ggc_collect ();
489 timevar_push (TV_GLOBAL_ALLOC);
490 open_dump_file (DFI_greg, current_function_decl);
492 /* If optimizing, allocate remaining pseudo-regs. Do the reload
493 pass fixing up any insns that are invalid. */
495 if (optimize)
496 failure = global_alloc (dump_file);
497 else
499 build_insn_chain (get_insns ());
500 failure = reload (get_insns (), 0);
503 if (dump_enabled_p (DFI_greg))
505 timevar_push (TV_DUMP);
506 dump_global_regs (dump_file);
507 timevar_pop (TV_DUMP);
509 close_dump_file (DFI_greg, print_rtl_with_bb, get_insns ());
512 ggc_collect ();
514 timevar_pop (TV_GLOBAL_ALLOC);
516 return failure;
519 /* Run the regrename and cprop passes. */
520 static void
521 rest_of_handle_regrename (void)
523 timevar_push (TV_RENAME_REGISTERS);
524 open_dump_file (DFI_rnreg, current_function_decl);
526 if (flag_rename_registers)
527 regrename_optimize ();
528 if (flag_cprop_registers)
529 copyprop_hardreg_forward ();
531 close_dump_file (DFI_rnreg, print_rtl_with_bb, get_insns ());
532 timevar_pop (TV_RENAME_REGISTERS);
535 /* Reorder basic blocks. */
536 static void
537 rest_of_handle_reorder_blocks (void)
539 bool changed;
540 unsigned int liveness_flags;
542 open_dump_file (DFI_bbro, current_function_decl);
544 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
545 splitting possibly introduced more crossjumping opportunities. */
546 liveness_flags = (!HAVE_conditional_execution ? CLEANUP_UPDATE_LIFE : 0);
547 changed = cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
549 if (flag_sched2_use_traces && flag_schedule_insns_after_reload)
550 tracer (liveness_flags);
551 if (flag_reorder_blocks || flag_reorder_blocks_and_partition)
552 reorder_basic_blocks (liveness_flags);
553 if (flag_reorder_blocks || flag_reorder_blocks_and_partition
554 || (flag_sched2_use_traces && flag_schedule_insns_after_reload))
555 changed |= cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
557 /* On conditional execution targets we can not update the life cheaply, so
558 we deffer the updating to after both cleanups. This may lose some cases
559 but should not be terribly bad. */
560 if (changed && HAVE_conditional_execution)
561 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
562 PROP_DEATH_NOTES);
563 close_dump_file (DFI_bbro, print_rtl_with_bb, get_insns ());
566 /* Partition hot and cold basic blocks. */
567 static void
568 rest_of_handle_partition_blocks (void)
570 no_new_pseudos = 0;
571 partition_hot_cold_basic_blocks ();
572 allocate_reg_life_data ();
573 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
574 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
575 no_new_pseudos = 1;
578 #ifdef INSN_SCHEDULING
579 /* Run instruction scheduler. */
580 /* Perform SMS module scheduling. */
581 static void
582 rest_of_handle_sms (void)
584 sbitmap blocks;
586 timevar_push (TV_SMS);
587 open_dump_file (DFI_sms, current_function_decl);
589 /* We want to be able to create new pseudos. */
590 no_new_pseudos = 0;
591 sms_schedule (dump_file);
592 close_dump_file (DFI_sms, print_rtl, get_insns ());
595 /* Update the life information, because we add pseudos. */
596 max_regno = max_reg_num ();
597 allocate_reg_info (max_regno, FALSE, FALSE);
598 blocks = sbitmap_alloc (last_basic_block);
599 sbitmap_ones (blocks);
600 update_life_info (blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
601 (PROP_DEATH_NOTES
602 | PROP_REG_INFO
603 | PROP_KILL_DEAD_CODE
604 | PROP_SCAN_DEAD_CODE));
606 no_new_pseudos = 1;
608 ggc_collect ();
609 timevar_pop (TV_SMS);
612 /* Run instruction scheduler. */
613 static void
614 rest_of_handle_sched (void)
616 timevar_push (TV_SCHED);
618 /* Print function header into sched dump now
619 because doing the sched analysis makes some of the dump. */
620 open_dump_file (DFI_sched, current_function_decl);
622 /* Do control and data sched analysis,
623 and write some of the results to dump file. */
625 schedule_insns (dump_file);
627 close_dump_file (DFI_sched, print_rtl_with_bb, get_insns ());
629 ggc_collect ();
630 timevar_pop (TV_SCHED);
633 /* Run second scheduling pass after reload. */
634 static void
635 rest_of_handle_sched2 (void)
637 timevar_push (TV_SCHED2);
638 open_dump_file (DFI_sched2, current_function_decl);
640 /* Do control and data sched analysis again,
641 and write some more of the results to dump file. */
643 split_all_insns (1);
645 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
647 schedule_ebbs (dump_file);
648 /* No liveness updating code yet, but it should be easy to do.
649 reg-stack recomputes the liveness when needed for now. */
650 count_or_remove_death_notes (NULL, 1);
651 cleanup_cfg (CLEANUP_EXPENSIVE);
653 else
654 schedule_insns (dump_file);
656 close_dump_file (DFI_sched2, print_rtl_with_bb, get_insns ());
658 ggc_collect ();
660 timevar_pop (TV_SCHED2);
662 #endif
664 static void
665 rest_of_handle_gcse2 (void)
667 timevar_push (TV_GCSE_AFTER_RELOAD);
668 open_dump_file (DFI_gcse2, current_function_decl);
670 gcse_after_reload_main (get_insns ());
671 rebuild_jump_labels (get_insns ());
672 delete_trivially_dead_insns (get_insns (), max_reg_num ());
673 close_dump_file (DFI_gcse2, print_rtl_with_bb, get_insns ());
675 ggc_collect ();
677 #ifdef ENABLE_CHECKING
678 verify_flow_info ();
679 #endif
681 timevar_pop (TV_GCSE_AFTER_RELOAD);
684 /* Register allocation pre-pass, to reduce number of moves necessary
685 for two-address machines. */
686 static void
687 rest_of_handle_regmove (void)
689 timevar_push (TV_REGMOVE);
690 open_dump_file (DFI_regmove, current_function_decl);
692 regmove_optimize (get_insns (), max_reg_num (), dump_file);
694 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
695 close_dump_file (DFI_regmove, print_rtl_with_bb, get_insns ());
697 ggc_collect ();
698 timevar_pop (TV_REGMOVE);
701 /* Run tracer. */
702 static void
703 rest_of_handle_tracer (void)
705 open_dump_file (DFI_tracer, current_function_decl);
706 if (dump_file)
707 dump_flow_info (dump_file);
708 tracer (0);
709 cleanup_cfg (CLEANUP_EXPENSIVE);
710 reg_scan (get_insns (), max_reg_num ());
711 close_dump_file (DFI_tracer, print_rtl_with_bb, get_insns ());
714 /* If-conversion and CFG cleanup. */
715 static void
716 rest_of_handle_if_conversion (void)
718 timevar_push (TV_IFCVT);
719 open_dump_file (DFI_ce1, current_function_decl);
721 if (flag_if_conversion)
723 if (dump_file)
724 dump_flow_info (dump_file);
725 cleanup_cfg (CLEANUP_EXPENSIVE);
726 reg_scan (get_insns (), max_reg_num ());
727 if_convert (0);
730 timevar_push (TV_JUMP);
731 cleanup_cfg (CLEANUP_EXPENSIVE);
732 reg_scan (get_insns (), max_reg_num ());
733 timevar_pop (TV_JUMP);
735 close_dump_file (DFI_ce1, print_rtl_with_bb, get_insns ());
736 timevar_pop (TV_IFCVT);
739 /* Rerun if-conversion, as combine may have simplified things enough
740 to now meet sequence length restrictions. */
741 static void
742 rest_of_handle_if_after_combine (void)
744 timevar_push (TV_IFCVT);
745 open_dump_file (DFI_ce2, current_function_decl);
747 no_new_pseudos = 0;
748 if_convert (1);
749 no_new_pseudos = 1;
751 close_dump_file (DFI_ce2, print_rtl_with_bb, get_insns ());
752 timevar_pop (TV_IFCVT);
755 static void
756 rest_of_handle_if_after_reload (void)
758 timevar_push (TV_IFCVT2);
759 open_dump_file (DFI_ce3, current_function_decl);
761 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
762 splitting possibly introduced more crossjumping opportunities. */
763 cleanup_cfg (CLEANUP_EXPENSIVE
764 | CLEANUP_UPDATE_LIFE
765 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
766 if (flag_if_conversion2)
767 if_convert (1);
768 close_dump_file (DFI_ce3, print_rtl_with_bb, get_insns ());
769 timevar_pop (TV_IFCVT2);
772 static void
773 rest_of_handle_web (void)
775 open_dump_file (DFI_web, current_function_decl);
776 timevar_push (TV_WEB);
777 web_main ();
778 delete_trivially_dead_insns (get_insns (), max_reg_num ());
779 cleanup_cfg (CLEANUP_EXPENSIVE);
781 timevar_pop (TV_WEB);
782 close_dump_file (DFI_web, print_rtl_with_bb, get_insns ());
783 reg_scan (get_insns (), max_reg_num ());
786 /* Do branch profiling and static profile estimation passes. */
787 static void
788 rest_of_handle_branch_prob (void)
790 struct loops loops;
792 timevar_push (TV_BRANCH_PROB);
793 open_dump_file (DFI_bp, current_function_decl);
795 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
796 branch_prob ();
798 /* Discover and record the loop depth at the head of each basic
799 block. The loop infrastructure does the real job for us. */
800 flow_loops_find (&loops, LOOP_TREE);
802 if (dump_file)
803 flow_loops_dump (&loops, dump_file, NULL, 0);
805 /* Estimate using heuristics if no profiling info is available. */
806 if (flag_guess_branch_prob)
807 estimate_probability (&loops);
809 flow_loops_free (&loops);
810 free_dominance_info (CDI_DOMINATORS);
811 close_dump_file (DFI_bp, print_rtl_with_bb, get_insns ());
812 timevar_pop (TV_BRANCH_PROB);
815 /* Do optimizations based on expression value profiles. */
816 static void
817 rest_of_handle_value_profile_transformations (void)
819 open_dump_file (DFI_vpt, current_function_decl);
820 timevar_push (TV_VPT);
822 if (value_profile_transformations ())
823 cleanup_cfg (CLEANUP_EXPENSIVE);
825 timevar_pop (TV_VPT);
826 close_dump_file (DFI_vpt, print_rtl_with_bb, get_insns ());
829 /* Do control and data flow analysis; write some of the results to the
830 dump file. */
831 static void
832 rest_of_handle_cfg (void)
834 open_dump_file (DFI_cfg, current_function_decl);
835 if (dump_file)
836 dump_flow_info (dump_file);
837 if (optimize)
838 cleanup_cfg (CLEANUP_EXPENSIVE
839 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
841 /* It may make more sense to mark constant functions after dead code is
842 eliminated by life_analysis, but we need to do it early, as -fprofile-arcs
843 may insert code making function non-constant, but we still must consider
844 it as constant, otherwise -fbranch-probabilities will not read data back.
846 life_analysis rarely eliminates modification of external memory.
848 FIXME: now with tree based profiling we are in the trap described above
849 again. It seems to be easiest to disable the optimization for time
850 being before the problem is either solved by moving the transformation
851 to the IPA level (we need the CFG for this) or the very early optimization
852 passes are made to ignore the const/pure flags so code does not change. */
853 if (optimize
854 && (!flag_tree_based_profiling
855 || (!profile_arc_flag && !flag_branch_probabilities)))
857 /* Alias analysis depends on this information and mark_constant_function
858 depends on alias analysis. */
859 reg_scan (get_insns (), max_reg_num ());
860 mark_constant_function ();
863 close_dump_file (DFI_cfg, print_rtl_with_bb, get_insns ());
866 /* Perform jump bypassing and control flow optimizations. */
867 static void
868 rest_of_handle_jump_bypass (void)
870 timevar_push (TV_BYPASS);
871 open_dump_file (DFI_bypass, current_function_decl);
873 cleanup_cfg (CLEANUP_EXPENSIVE);
874 reg_scan (get_insns (), max_reg_num ());
876 if (bypass_jumps (dump_file))
878 rebuild_jump_labels (get_insns ());
879 cleanup_cfg (CLEANUP_EXPENSIVE);
880 delete_trivially_dead_insns (get_insns (), max_reg_num ());
883 close_dump_file (DFI_bypass, print_rtl_with_bb, get_insns ());
884 timevar_pop (TV_BYPASS);
886 ggc_collect ();
888 #ifdef ENABLE_CHECKING
889 verify_flow_info ();
890 #endif
893 /* Try combining insns through substitution. */
894 static void
895 rest_of_handle_combine (void)
897 int rebuild_jump_labels_after_combine = 0;
899 timevar_push (TV_COMBINE);
900 open_dump_file (DFI_combine, current_function_decl);
902 rebuild_jump_labels_after_combine
903 = combine_instructions (get_insns (), max_reg_num ());
905 /* Combining insns may have turned an indirect jump into a
906 direct jump. Rebuild the JUMP_LABEL fields of jumping
907 instructions. */
908 if (rebuild_jump_labels_after_combine)
910 timevar_push (TV_JUMP);
911 rebuild_jump_labels (get_insns ());
912 timevar_pop (TV_JUMP);
914 delete_dead_jumptables ();
915 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
918 close_dump_file (DFI_combine, print_rtl_with_bb, get_insns ());
919 timevar_pop (TV_COMBINE);
921 ggc_collect ();
924 /* Perform life analysis. */
925 static void
926 rest_of_handle_life (void)
928 open_dump_file (DFI_life, current_function_decl);
929 regclass_init ();
931 #ifdef ENABLE_CHECKING
932 verify_flow_info ();
933 #endif
934 life_analysis (dump_file, PROP_FINAL);
935 if (optimize)
936 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE | CLEANUP_LOG_LINKS
937 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
939 if (extra_warnings)
941 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
942 setjmp_args_warning ();
945 if (optimize)
947 if (initialize_uninitialized_subregs ())
949 /* Insns were inserted, and possibly pseudos created, so
950 things might look a bit different. */
951 allocate_reg_life_data ();
952 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
953 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
957 no_new_pseudos = 1;
959 close_dump_file (DFI_life, print_rtl_with_bb, get_insns ());
961 ggc_collect ();
964 /* Perform common subexpression elimination. Nonzero value from
965 `cse_main' means that jumps were simplified and some code may now
966 be unreachable, so do jump optimization again. */
967 static void
968 rest_of_handle_cse (void)
970 int tem;
972 open_dump_file (DFI_cse, current_function_decl);
973 if (dump_file)
974 dump_flow_info (dump_file);
975 timevar_push (TV_CSE);
977 reg_scan (get_insns (), max_reg_num ());
979 tem = cse_main (get_insns (), max_reg_num (), dump_file);
980 if (tem)
981 rebuild_jump_labels (get_insns ());
982 if (purge_all_dead_edges (0))
983 delete_unreachable_blocks ();
985 delete_trivially_dead_insns (get_insns (), max_reg_num ());
987 /* If we are not running more CSE passes, then we are no longer
988 expecting CSE to be run. But always rerun it in a cheap mode. */
989 cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
991 if (tem)
992 delete_dead_jumptables ();
994 if (tem || optimize > 1)
995 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
997 timevar_pop (TV_CSE);
998 close_dump_file (DFI_cse, print_rtl_with_bb, get_insns ());
1000 ggc_collect ();
1003 /* Run second CSE pass after loop optimizations. */
1004 static void
1005 rest_of_handle_cse2 (void)
1007 int tem;
1009 timevar_push (TV_CSE2);
1010 open_dump_file (DFI_cse2, current_function_decl);
1011 if (dump_file)
1012 dump_flow_info (dump_file);
1013 /* CFG is no longer maintained up-to-date. */
1014 tem = cse_main (get_insns (), max_reg_num (), dump_file);
1016 /* Run a pass to eliminate duplicated assignments to condition code
1017 registers. We have to run this after bypass_jumps, because it
1018 makes it harder for that pass to determine whether a jump can be
1019 bypassed safely. */
1020 cse_condition_code_reg ();
1022 purge_all_dead_edges (0);
1023 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1025 if (tem)
1027 timevar_push (TV_JUMP);
1028 rebuild_jump_labels (get_insns ());
1029 delete_dead_jumptables ();
1030 cleanup_cfg (CLEANUP_EXPENSIVE);
1031 timevar_pop (TV_JUMP);
1033 reg_scan (get_insns (), max_reg_num ());
1034 close_dump_file (DFI_cse2, print_rtl_with_bb, get_insns ());
1035 timevar_pop (TV_CSE2);
1037 ggc_collect ();
1040 /* Perform global cse. */
1041 static void
1042 rest_of_handle_gcse (void)
1044 int save_csb, save_cfj;
1045 int tem2 = 0, tem;
1047 timevar_push (TV_GCSE);
1048 open_dump_file (DFI_gcse, current_function_decl);
1050 tem = gcse_main (get_insns (), dump_file);
1051 rebuild_jump_labels (get_insns ());
1052 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1054 save_csb = flag_cse_skip_blocks;
1055 save_cfj = flag_cse_follow_jumps;
1056 flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
1058 /* If -fexpensive-optimizations, re-run CSE to clean up things done
1059 by gcse. */
1060 if (flag_expensive_optimizations)
1062 timevar_push (TV_CSE);
1063 reg_scan (get_insns (), max_reg_num ());
1064 tem2 = cse_main (get_insns (), max_reg_num (), dump_file);
1065 purge_all_dead_edges (0);
1066 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1067 timevar_pop (TV_CSE);
1068 cse_not_expected = !flag_rerun_cse_after_loop;
1071 /* If gcse or cse altered any jumps, rerun jump optimizations to clean
1072 things up. */
1073 if (tem || tem2)
1075 timevar_push (TV_JUMP);
1076 rebuild_jump_labels (get_insns ());
1077 delete_dead_jumptables ();
1078 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1079 timevar_pop (TV_JUMP);
1082 close_dump_file (DFI_gcse, print_rtl_with_bb, get_insns ());
1083 timevar_pop (TV_GCSE);
1085 ggc_collect ();
1086 flag_cse_skip_blocks = save_csb;
1087 flag_cse_follow_jumps = save_cfj;
1088 #ifdef ENABLE_CHECKING
1089 verify_flow_info ();
1090 #endif
1093 /* Move constant computations out of loops. */
1094 static void
1095 rest_of_handle_loop_optimize (void)
1097 int do_prefetch;
1099 timevar_push (TV_LOOP);
1100 open_dump_file (DFI_loop, current_function_decl);
1102 /* CFG is no longer maintained up-to-date. */
1103 free_bb_for_insn ();
1104 profile_status = PROFILE_ABSENT;
1106 do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
1108 if (flag_rerun_loop_opt)
1110 cleanup_barriers ();
1112 /* We only want to perform unrolling once. */
1113 loop_optimize (get_insns (), dump_file, 0);
1115 /* The first call to loop_optimize makes some instructions
1116 trivially dead. We delete those instructions now in the
1117 hope that doing so will make the heuristics in loop work
1118 better and possibly speed up compilation. */
1119 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1121 /* The regscan pass is currently necessary as the alias
1122 analysis code depends on this information. */
1123 reg_scan (get_insns (), max_reg_num ());
1125 cleanup_barriers ();
1126 loop_optimize (get_insns (), dump_file, do_prefetch);
1128 /* Loop can create trivially dead instructions. */
1129 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1130 find_basic_blocks (get_insns ());
1131 close_dump_file (DFI_loop, print_rtl, get_insns ());
1132 timevar_pop (TV_LOOP);
1134 ggc_collect ();
1137 /* Perform loop optimizations. It might be better to do them a bit
1138 sooner, but we want the profile feedback to work more
1139 efficiently. */
1140 static void
1141 rest_of_handle_loop2 (void)
1143 struct loops *loops;
1144 basic_block bb;
1146 if (!flag_move_loop_invariants
1147 && !flag_unswitch_loops
1148 && !flag_peel_loops
1149 && !flag_unroll_loops
1150 && !flag_branch_on_count_reg)
1151 return;
1153 timevar_push (TV_LOOP);
1154 open_dump_file (DFI_loop2, current_function_decl);
1155 if (dump_file)
1156 dump_flow_info (dump_file);
1158 /* Initialize structures for layout changes. */
1159 cfg_layout_initialize (0);
1161 loops = loop_optimizer_init (dump_file);
1163 if (loops)
1165 /* The optimizations: */
1166 if (flag_move_loop_invariants)
1167 move_loop_invariants (loops);
1169 if (flag_unswitch_loops)
1170 unswitch_loops (loops);
1172 if (flag_peel_loops || flag_unroll_loops)
1173 unroll_and_peel_loops (loops,
1174 (flag_peel_loops ? UAP_PEEL : 0) |
1175 (flag_unroll_loops ? UAP_UNROLL : 0) |
1176 (flag_unroll_all_loops ? UAP_UNROLL_ALL : 0));
1178 #ifdef HAVE_doloop_end
1179 if (flag_branch_on_count_reg && HAVE_doloop_end)
1180 doloop_optimize_loops (loops);
1181 #endif /* HAVE_doloop_end */
1183 loop_optimizer_finalize (loops, dump_file);
1186 free_dominance_info (CDI_DOMINATORS);
1188 /* Finalize layout changes. */
1189 FOR_EACH_BB (bb)
1190 if (bb->next_bb != EXIT_BLOCK_PTR)
1191 bb->rbi->next = bb->next_bb;
1192 cfg_layout_finalize ();
1194 cleanup_cfg (CLEANUP_EXPENSIVE);
1195 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1196 reg_scan (get_insns (), max_reg_num ());
1197 if (dump_file)
1198 dump_flow_info (dump_file);
1199 close_dump_file (DFI_loop2, print_rtl_with_bb, get_insns ());
1200 timevar_pop (TV_LOOP);
1201 ggc_collect ();
1204 static void
1205 rest_of_handle_branch_target_load_optimize (void)
1207 static int warned = 0;
1209 /* Leave this a warning for now so that it is possible to experiment
1210 with running this pass twice. In 3.6, we should either make this
1211 an error, or use separate dump files. */
1212 if (flag_branch_target_load_optimize
1213 && flag_branch_target_load_optimize2
1214 && !warned)
1216 warning ("branch target register load optimization is not intended "
1217 "to be run twice");
1219 warned = 1;
1222 open_dump_file (DFI_branch_target_load, current_function_decl);
1223 branch_target_load_optimize (epilogue_completed);
1224 close_dump_file (DFI_branch_target_load, print_rtl_with_bb, get_insns ());
1225 ggc_collect ();
1228 #ifdef OPTIMIZE_MODE_SWITCHING
1229 static void
1230 rest_of_handle_mode_switching (void)
1232 timevar_push (TV_MODE_SWITCH);
1234 no_new_pseudos = 0;
1235 optimize_mode_switching (NULL);
1236 no_new_pseudos = 1;
1238 timevar_pop (TV_MODE_SWITCH);
1240 #endif
1242 static void
1243 rest_of_handle_jump (void)
1245 ggc_collect ();
1247 timevar_push (TV_JUMP);
1248 open_dump_file (DFI_sibling, current_function_decl);
1250 delete_unreachable_blocks ();
1251 #ifdef ENABLE_CHECKING
1252 verify_flow_info ();
1253 #endif
1255 if (cfun->tail_call_emit)
1256 fixup_tail_calls ();
1258 close_dump_file (DFI_sibling, print_rtl, get_insns ());
1259 timevar_pop (TV_JUMP);
1262 static void
1263 rest_of_handle_eh (void)
1265 insn_locators_initialize ();
1266 /* Complete generation of exception handling code. */
1267 if (doing_eh (0))
1269 timevar_push (TV_JUMP);
1270 open_dump_file (DFI_eh, current_function_decl);
1272 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1274 finish_eh_generation ();
1276 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1278 close_dump_file (DFI_eh, print_rtl, get_insns ());
1279 timevar_pop (TV_JUMP);
1283 static void
1284 rest_of_handle_stack_adjustments (void)
1286 life_analysis (dump_file, PROP_POSTRELOAD);
1287 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE
1288 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1290 /* This is kind of a heuristic. We need to run combine_stack_adjustments
1291 even for machines with possibly nonzero RETURN_POPS_ARGS
1292 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
1293 push instructions will have popping returns. */
1294 #ifndef PUSH_ROUNDING
1295 if (!ACCUMULATE_OUTGOING_ARGS)
1296 #endif
1297 combine_stack_adjustments ();
1300 static void
1301 rest_of_handle_flow2 (void)
1303 timevar_push (TV_FLOW2);
1304 open_dump_file (DFI_flow2, current_function_decl);
1306 /* Re-create the death notes which were deleted during reload. */
1307 #ifdef ENABLE_CHECKING
1308 verify_flow_info ();
1309 #endif
1311 /* If optimizing, then go ahead and split insns now. */
1312 #ifndef STACK_REGS
1313 if (optimize > 0)
1314 #endif
1315 split_all_insns (0);
1317 if (flag_branch_target_load_optimize)
1319 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1320 rest_of_handle_branch_target_load_optimize ();
1321 open_dump_file (DFI_flow2, current_function_decl);
1324 if (optimize)
1325 cleanup_cfg (CLEANUP_EXPENSIVE);
1327 /* On some machines, the prologue and epilogue code, or parts thereof,
1328 can be represented as RTL. Doing so lets us schedule insns between
1329 it and the rest of the code and also allows delayed branch
1330 scheduling to operate in the epilogue. */
1331 thread_prologue_and_epilogue_insns (get_insns ());
1332 epilogue_completed = 1;
1334 if (optimize)
1335 rest_of_handle_stack_adjustments ();
1337 flow2_completed = 1;
1339 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1340 timevar_pop (TV_FLOW2);
1342 ggc_collect ();
1346 static void
1347 rest_of_handle_jump2 (void)
1349 open_dump_file (DFI_jump, current_function_decl);
1351 /* Always do one jump optimization pass to ensure that JUMP_LABEL fields
1352 are initialized and to compute whether control can drop off the end
1353 of the function. */
1355 timevar_push (TV_JUMP);
1356 /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB. Do this
1357 before jump optimization switches branch directions. */
1358 if (flag_guess_branch_prob)
1359 expected_value_to_br_prob ();
1361 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1362 reg_scan (get_insns (), max_reg_num ());
1363 if (dump_file)
1364 dump_flow_info (dump_file);
1365 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_PRE_LOOP
1366 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1368 create_loop_notes ();
1370 purge_line_number_notes (get_insns ());
1372 if (optimize)
1373 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1375 /* Jump optimization, and the removal of NULL pointer checks, may
1376 have reduced the number of instructions substantially. CSE, and
1377 future passes, allocate arrays whose dimensions involve the
1378 maximum instruction UID, so if we can reduce the maximum UID
1379 we'll save big on memory. */
1380 renumber_insns (dump_file);
1382 close_dump_file (DFI_jump, print_rtl_with_bb, get_insns ());
1383 timevar_pop (TV_JUMP);
1385 ggc_collect ();
1388 #ifdef HAVE_peephole2
1389 static void
1390 rest_of_handle_peephole2 (void)
1392 timevar_push (TV_PEEPHOLE2);
1393 open_dump_file (DFI_peephole2, current_function_decl);
1395 peephole2_optimize (dump_file);
1397 close_dump_file (DFI_peephole2, print_rtl_with_bb, get_insns ());
1398 timevar_pop (TV_PEEPHOLE2);
1400 #endif
1402 static void
1403 rest_of_handle_postreload (void)
1405 timevar_push (TV_RELOAD_CSE_REGS);
1406 open_dump_file (DFI_postreload, current_function_decl);
1408 /* Do a very simple CSE pass over just the hard registers. */
1409 reload_cse_regs (get_insns ());
1410 /* reload_cse_regs can eliminate potentially-trapping MEMs.
1411 Remove any EH edges associated with them. */
1412 if (flag_non_call_exceptions)
1413 purge_all_dead_edges (0);
1415 close_dump_file (DFI_postreload, print_rtl_with_bb, get_insns ());
1416 timevar_pop (TV_RELOAD_CSE_REGS);
1419 static void
1420 rest_of_handle_shorten_branches (void)
1422 /* Shorten branches. */
1423 timevar_push (TV_SHORTEN_BRANCH);
1424 shorten_branches (get_insns ());
1425 timevar_pop (TV_SHORTEN_BRANCH);
1428 static void
1429 rest_of_clean_state (void)
1431 rtx insn, next;
1432 coverage_end_function ();
1434 /* It is very important to decompose the RTL instruction chain here:
1435 debug information keeps pointing into CODE_LABEL insns inside the function
1436 body. If these remain pointing to the other insns, we end up preserving
1437 whole RTL chain and attached detailed debug info in memory. */
1438 for (insn = get_insns (); insn; insn = next)
1440 next = NEXT_INSN (insn);
1441 NEXT_INSN (insn) = NULL;
1442 PREV_INSN (insn) = NULL;
1445 /* In case the function was not output,
1446 don't leave any temporary anonymous types
1447 queued up for sdb output. */
1448 #ifdef SDB_DEBUGGING_INFO
1449 if (write_symbols == SDB_DEBUG)
1450 sdbout_types (NULL_TREE);
1451 #endif
1453 reload_completed = 0;
1454 epilogue_completed = 0;
1455 flow2_completed = 0;
1456 no_new_pseudos = 0;
1458 timevar_push (TV_FINAL);
1460 /* Clear out the insn_length contents now that they are no
1461 longer valid. */
1462 init_insn_lengths ();
1464 /* Show no temporary slots allocated. */
1465 init_temp_slots ();
1467 free_basic_block_vars ();
1468 free_bb_for_insn ();
1470 timevar_pop (TV_FINAL);
1472 if (targetm.binds_local_p (current_function_decl))
1474 int pref = cfun->preferred_stack_boundary;
1475 if (cfun->stack_alignment_needed > cfun->preferred_stack_boundary)
1476 pref = cfun->stack_alignment_needed;
1477 cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
1478 = pref;
1481 /* Make sure volatile mem refs aren't considered valid operands for
1482 arithmetic insns. We must call this here if this is a nested inline
1483 function, since the above code leaves us in the init_recog state
1484 (from final.c), and the function context push/pop code does not
1485 save/restore volatile_ok.
1487 ??? Maybe it isn't necessary for expand_start_function to call this
1488 anymore if we do it here? */
1490 init_recog_no_volatile ();
1492 /* We're done with this function. Free up memory if we can. */
1493 free_after_parsing (cfun);
1494 free_after_compilation (cfun);
1498 /* This function is called from the pass manager in tree-optimize.c
1499 after all tree passes have finished for a single function, and we
1500 have expanded the function body from trees to RTL.
1501 Once we are here, we have decided that we're supposed to output
1502 that function, i.e. that we should write assembler code for it.
1504 We run a series of low-level passes here on the function's RTL
1505 representation. Each pass is called via a rest_of_* function. */
1507 static void
1508 rest_of_compilation (void)
1510 /* If we're emitting a nested function, make sure its parent gets
1511 emitted as well. Doing otherwise confuses debug info. */
1513 tree parent;
1514 for (parent = DECL_CONTEXT (current_function_decl);
1515 parent != NULL_TREE;
1516 parent = get_containing_scope (parent))
1517 if (TREE_CODE (parent) == FUNCTION_DECL)
1518 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1521 /* We are now committed to emitting code for this function. Do any
1522 preparation, such as emitting abstract debug info for the inline
1523 before it gets mangled by optimization. */
1524 if (cgraph_function_possibly_inlined_p (current_function_decl))
1525 (*debug_hooks->outlining_inline_function) (current_function_decl);
1527 /* Remove any notes we don't need. That will make iterating
1528 over the instruction sequence faster, and allow the garbage
1529 collector to reclaim the memory used by the notes. */
1530 remove_unnecessary_notes ();
1532 /* Initialize some variables used by the optimizers. */
1533 init_function_for_compilation ();
1535 TREE_ASM_WRITTEN (current_function_decl) = 1;
1537 /* Early return if there were errors. We can run afoul of our
1538 consistency checks, and there's not really much point in fixing them. */
1539 if (rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount)
1540 goto exit_rest_of_compilation;
1542 rest_of_handle_jump ();
1544 rest_of_handle_eh ();
1546 /* Delay emitting hard_reg_initial_value sets until after EH landing pad
1547 generation, which might create new sets. */
1548 emit_initial_value_sets ();
1550 #ifdef FINALIZE_PIC
1551 /* If we are doing position-independent code generation, now
1552 is the time to output special prologues and epilogues.
1553 We do not want to do this earlier, because it just clutters
1554 up inline functions with meaningless insns. */
1555 if (flag_pic)
1556 FINALIZE_PIC;
1557 #endif
1559 /* Copy any shared structure that should not be shared. */
1560 unshare_all_rtl ();
1562 #ifdef SETJMP_VIA_SAVE_AREA
1563 /* This must be performed before virtual register instantiation.
1564 Please be aware that everything in the compiler that can look
1565 at the RTL up to this point must understand that REG_SAVE_AREA
1566 is just like a use of the REG contained inside. */
1567 if (current_function_calls_alloca)
1568 optimize_save_area_alloca ();
1569 #endif
1571 /* Instantiate all virtual registers. */
1572 instantiate_virtual_regs ();
1574 rest_of_handle_jump2 ();
1576 if (optimize > 0)
1577 rest_of_handle_cse ();
1579 if (optimize > 0)
1581 if (flag_gcse)
1582 rest_of_handle_gcse ();
1584 if (flag_loop_optimize)
1585 rest_of_handle_loop_optimize ();
1587 if (flag_gcse)
1588 rest_of_handle_jump_bypass ();
1591 timevar_push (TV_FLOW);
1592 rest_of_handle_cfg ();
1594 if (!flag_tree_based_profiling
1595 && (optimize > 0 || profile_arc_flag
1596 || flag_test_coverage || flag_branch_probabilities))
1598 rtl_register_profile_hooks ();
1599 rtl_register_value_prof_hooks ();
1600 rest_of_handle_branch_prob ();
1602 if (flag_branch_probabilities
1603 && flag_profile_values
1604 && (flag_value_profile_transformations
1605 || flag_speculative_prefetching))
1606 rest_of_handle_value_profile_transformations ();
1608 /* Remove the death notes created for vpt. */
1609 if (flag_profile_values)
1610 count_or_remove_death_notes (NULL, 1);
1613 if (optimize > 0)
1614 rest_of_handle_if_conversion ();
1616 if (optimize > 0 && flag_tracer)
1617 rest_of_handle_tracer ();
1619 if (optimize > 0
1620 && flag_loop_optimize2)
1621 rest_of_handle_loop2 ();
1623 if (optimize > 0 && flag_web)
1624 rest_of_handle_web ();
1626 if (optimize > 0 && flag_rerun_cse_after_loop)
1627 rest_of_handle_cse2 ();
1629 cse_not_expected = 1;
1631 rest_of_handle_life ();
1632 timevar_pop (TV_FLOW);
1634 if (optimize > 0)
1635 rest_of_handle_combine ();
1637 if (optimize > 0 && flag_if_conversion)
1638 rest_of_handle_if_after_combine ();
1640 /* The optimization to partition hot/cold basic blocks into separate
1641 sections of the .o file does not work well with linkonce or with
1642 user defined section attributes. Don't call it if either case
1643 arises. */
1645 if (flag_reorder_blocks_and_partition
1646 && !DECL_ONE_ONLY (current_function_decl)
1647 && !user_defined_section_attribute)
1648 rest_of_handle_partition_blocks ();
1650 if (optimize > 0 && flag_regmove)
1651 rest_of_handle_regmove ();
1653 /* Do unconditional splitting before register allocation to allow machine
1654 description to add extra information not needed previously. */
1655 split_all_insns (1);
1657 #ifdef OPTIMIZE_MODE_SWITCHING
1658 rest_of_handle_mode_switching ();
1659 #endif
1661 /* Any of the several passes since flow1 will have munged register
1662 lifetime data a bit. We need it to be up to date for scheduling
1663 (see handling of reg_known_equiv in init_alias_analysis). */
1664 recompute_reg_usage ();
1666 #ifdef INSN_SCHEDULING
1667 if (optimize > 0 && flag_modulo_sched)
1668 rest_of_handle_sms ();
1670 if (flag_schedule_insns)
1671 rest_of_handle_sched ();
1672 #endif
1674 /* Determine if the current function is a leaf before running reload
1675 since this can impact optimizations done by the prologue and
1676 epilogue thus changing register elimination offsets. */
1677 current_function_is_leaf = leaf_function_p ();
1679 if (rest_of_handle_old_regalloc ())
1680 goto exit_rest_of_compilation;
1682 if (optimize > 0)
1683 rest_of_handle_postreload ();
1685 if (optimize > 0 && flag_gcse_after_reload)
1686 rest_of_handle_gcse2 ();
1688 rest_of_handle_flow2 ();
1690 #ifdef HAVE_peephole2
1691 if (optimize > 0 && flag_peephole2)
1692 rest_of_handle_peephole2 ();
1693 #endif
1695 if (optimize > 0)
1696 rest_of_handle_if_after_reload ();
1698 if (optimize > 0)
1700 if (flag_rename_registers || flag_cprop_registers)
1701 rest_of_handle_regrename ();
1703 rest_of_handle_reorder_blocks ();
1706 if (flag_branch_target_load_optimize2)
1707 rest_of_handle_branch_target_load_optimize ();
1709 #ifdef LEAF_REGISTERS
1710 current_function_uses_only_leaf_regs
1711 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
1712 #endif
1714 #ifdef INSN_SCHEDULING
1715 if (optimize > 0 && flag_schedule_insns_after_reload)
1716 rest_of_handle_sched2 ();
1717 #endif
1719 #ifdef STACK_REGS
1720 rest_of_handle_stack_regs ();
1721 #endif
1723 compute_alignments ();
1725 /* Aggressively duplicate basic blocks ending in computed gotos to the
1726 tails of their predecessors, unless we are optimizing for size. */
1727 if (flag_expensive_optimizations && !optimize_size)
1728 duplicate_computed_gotos ();
1730 if (flag_var_tracking)
1731 rest_of_handle_variable_tracking ();
1733 /* CFG is no longer maintained up-to-date. */
1734 free_bb_for_insn ();
1736 if (targetm.machine_dependent_reorg != 0)
1737 rest_of_handle_machine_reorg ();
1739 purge_line_number_notes (get_insns ());
1740 cleanup_barriers ();
1742 #ifdef DELAY_SLOTS
1743 if (flag_delayed_branch)
1744 rest_of_handle_delay_slots ();
1745 #endif
1747 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
1748 timevar_push (TV_SHORTEN_BRANCH);
1749 split_all_insns_noflow ();
1750 timevar_pop (TV_SHORTEN_BRANCH);
1751 #endif
1753 convert_to_eh_region_ranges ();
1755 rest_of_handle_shorten_branches ();
1757 set_nothrow_function_flags ();
1759 rest_of_handle_final ();
1761 exit_rest_of_compilation:
1763 rest_of_clean_state ();
1766 void
1767 finish_optimization_passes (void)
1769 enum tree_dump_index i;
1770 struct dump_file_info *dfi;
1771 char *name;
1773 timevar_push (TV_DUMP);
1774 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
1776 open_dump_file (DFI_bp, NULL);
1777 end_branch_prob ();
1778 close_dump_file (DFI_bp, NULL, NULL_RTX);
1781 if (optimize > 0 && open_dump_file (DFI_combine, NULL))
1783 dump_combine_total_stats (dump_file);
1784 close_dump_file (DFI_combine, NULL, NULL_RTX);
1787 /* Do whatever is necessary to finish printing the graphs. */
1788 if (graph_dump_format != no_graph)
1789 for (i = DFI_MIN; (dfi = get_dump_file_info (i)) != NULL; ++i)
1790 if (dump_initialized_p (i)
1791 && (dfi->flags & TDF_GRAPH) != 0
1792 && (name = get_dump_file_name (i)) != NULL)
1794 finish_graph_dump_file (name);
1795 free (name);
1798 timevar_pop (TV_DUMP);
1801 struct tree_opt_pass pass_rest_of_compilation =
1803 NULL, /* name */
1804 NULL, /* gate */
1805 rest_of_compilation, /* execute */
1806 NULL, /* sub */
1807 NULL, /* next */
1808 0, /* static_pass_number */
1809 TV_REST_OF_COMPILATION, /* tv_id */
1810 PROP_rtl, /* properties_required */
1811 0, /* properties_provided */
1812 PROP_rtl, /* properties_destroyed */
1813 0, /* todo_flags_start */
1814 TODO_ggc_collect, /* todo_flags_finish */
1815 0 /* letter */