* rtl.h (insn_note): Remove NOTE_INSN_PREDICTION.
[official-gcc.git] / gcc / passes.c
blobb124b3eafcdb75b71cef0b7e6a87db9590ffb2f0
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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 "loop.h"
64 #include "regs.h"
65 #include "timevar.h"
66 #include "diagnostic.h"
67 #include "params.h"
68 #include "reload.h"
69 #include "dwarf2asm.h"
70 #include "integrate.h"
71 #include "real.h"
72 #include "debug.h"
73 #include "target.h"
74 #include "langhooks.h"
75 #include "cfglayout.h"
76 #include "cfgloop.h"
77 #include "hosthooks.h"
78 #include "cgraph.h"
79 #include "opts.h"
80 #include "coverage.h"
81 #include "value-prof.h"
82 #include "alloc-pool.h"
83 #include "tree-pass.h"
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 /* Describes a dump file. */
113 struct dump_file_info
115 /* The unique extension to apply, e.g. ".jump". */
116 const char *const extension;
118 /* The -d<c> character that enables this dump file. */
119 char const debug_switch;
121 /* True if there is a corresponding graph dump file. */
122 char const graph_dump_p;
124 /* True if the user selected this dump. */
125 char enabled;
127 /* True if the files have been initialized (ie truncated). */
128 char initialized;
131 /* Enumerate the extant dump files. */
133 enum dump_file_index
135 DFI_cgraph,
136 DFI_rtl,
137 DFI_sibling,
138 DFI_eh,
139 DFI_jump,
140 DFI_null,
141 DFI_cse,
142 DFI_gcse,
143 DFI_loop,
144 DFI_bypass,
145 DFI_cfg,
146 DFI_bp,
147 DFI_vpt,
148 DFI_ce1,
149 DFI_tracer,
150 DFI_loop2,
151 DFI_web,
152 DFI_cse2,
153 DFI_life,
154 DFI_combine,
155 DFI_ce2,
156 DFI_regmove,
157 DFI_sms,
158 DFI_sched,
159 DFI_lreg,
160 DFI_greg,
161 DFI_postreload,
162 DFI_gcse2,
163 DFI_flow2,
164 DFI_peephole2,
165 DFI_ce3,
166 DFI_rnreg,
167 DFI_bbro,
168 DFI_branch_target_load,
169 DFI_sched2,
170 DFI_stack,
171 DFI_vartrack,
172 DFI_mach,
173 DFI_dbr,
174 DFI_MAX
177 /* Describes all the dump files. Should be kept in order of the
178 pass and in sync with dump_file_index above.
180 Remaining -d letters:
182 " e q "
183 " F K O Q WXY "
186 static struct dump_file_info dump_file_tbl[DFI_MAX] =
188 { "cgraph", 'U', 0, 0, 0 },
189 { "rtl", 'r', 0, 0, 0 },
190 { "sibling", 'i', 0, 0, 0 },
191 { "eh", 'h', 0, 0, 0 },
192 { "jump", 'j', 0, 0, 0 },
193 { "null", 'u', 0, 0, 0 },
194 { "cse", 's', 0, 0, 0 },
195 { "gcse", 'G', 1, 0, 0 },
196 { "loop", 'L', 1, 0, 0 },
197 { "bypass", 'G', 1, 0, 0 }, /* Yes, duplicate enable switch. */
198 { "cfg", 'f', 1, 0, 0 },
199 { "bp", 'b', 1, 0, 0 },
200 { "vpt", 'V', 1, 0, 0 },
201 { "ce1", 'C', 1, 0, 0 },
202 { "tracer", 'T', 1, 0, 0 },
203 { "loop2", 'L', 1, 0, 0 },
204 { "web", 'Z', 0, 0, 0 },
205 { "cse2", 't', 1, 0, 0 },
206 { "life", 'f', 1, 0, 0 }, /* Yes, duplicate enable switch. */
207 { "combine", 'c', 1, 0, 0 },
208 { "ce2", 'C', 1, 0, 0 },
209 { "regmove", 'N', 1, 0, 0 },
210 { "sms", 'm', 0, 0, 0 },
211 { "sched", 'S', 1, 0, 0 },
212 { "lreg", 'l', 1, 0, 0 },
213 { "greg", 'g', 1, 0, 0 },
214 { "postreload", 'o', 1, 0, 0 },
215 { "gcse2", 'J', 0, 0, 0 },
216 { "flow2", 'w', 1, 0, 0 },
217 { "peephole2", 'z', 1, 0, 0 },
218 { "ce3", 'E', 1, 0, 0 },
219 { "rnreg", 'n', 1, 0, 0 },
220 { "bbro", 'B', 1, 0, 0 },
221 { "btl", 'd', 1, 0, 0 }, /* Yes, duplicate enable switch. */
222 { "sched2", 'R', 1, 0, 0 },
223 { "stack", 'k', 1, 0, 0 },
224 { "vartrack", 'V', 1, 0, 0 }, /* Yes, duplicate enable switch. */
225 { "mach", 'M', 1, 0, 0 },
226 { "dbr", 'd', 0, 0, 0 },
229 /* Routine to open a dump file. Return true if the dump file is enabled. */
231 static int
232 open_dump_file (enum dump_file_index index, tree decl)
234 char *dump_name;
235 const char *open_arg;
236 char seq[16];
238 if (! dump_file_tbl[index].enabled)
239 return 0;
241 timevar_push (TV_DUMP);
242 if (dump_file != NULL)
243 fclose (dump_file);
245 sprintf (seq, DUMPFILE_FORMAT, index);
247 if (! dump_file_tbl[index].initialized)
249 /* If we've not initialized the files, do so now. */
250 if (graph_dump_format != no_graph
251 && dump_file_tbl[index].graph_dump_p)
253 dump_name = concat (seq, dump_file_tbl[index].extension, NULL);
254 clean_graph_dump_file (dump_base_name, dump_name);
255 free (dump_name);
257 dump_file_tbl[index].initialized = 1;
258 open_arg = "w";
260 else
261 open_arg = "a";
263 dump_name = concat (dump_base_name, seq,
264 dump_file_tbl[index].extension, NULL);
266 dump_file = fopen (dump_name, open_arg);
267 if (dump_file == NULL)
268 fatal_error ("can't open %s: %m", dump_name);
270 free (dump_name);
272 if (decl)
273 fprintf (dump_file, "\n;; Function %s%s\n\n",
274 lang_hooks.decl_printable_name (decl, 2),
275 cfun->function_frequency == FUNCTION_FREQUENCY_HOT
276 ? " (hot)"
277 : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
278 ? " (unlikely executed)"
279 : "");
281 timevar_pop (TV_DUMP);
282 return 1;
285 /* Routine to close a dump file. */
287 static void
288 close_dump_file (enum dump_file_index index,
289 void (*func) (FILE *, rtx),
290 rtx insns)
292 if (! dump_file)
293 return;
295 timevar_push (TV_DUMP);
296 if (insns
297 && graph_dump_format != no_graph
298 && dump_file_tbl[index].graph_dump_p)
300 char seq[16];
301 char *suffix;
303 sprintf (seq, DUMPFILE_FORMAT, index);
304 suffix = concat (seq, dump_file_tbl[index].extension, NULL);
305 print_rtl_graph_with_bb (dump_base_name, suffix, insns);
306 free (suffix);
309 if (func && insns)
310 func (dump_file, insns);
312 fflush (dump_file);
313 fclose (dump_file);
315 dump_file = NULL;
316 timevar_pop (TV_DUMP);
319 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
320 and TYPE_DECL nodes.
322 This does nothing for local (non-static) variables, unless the
323 variable is a register variable with an ASMSPEC. In that case, or
324 if the variable is not an automatic, it sets up the RTL and
325 outputs any assembler code (label definition, storage allocation
326 and initialization).
328 DECL is the declaration. If ASMSPEC is nonzero, it specifies
329 the assembler symbol name to be used. TOP_LEVEL is nonzero
330 if this declaration is not within a function. */
332 void
333 rest_of_decl_compilation (tree decl,
334 const char *asmspec,
335 int top_level,
336 int at_end)
338 /* We deferred calling assemble_alias so that we could collect
339 other attributes such as visibility. Emit the alias now. */
341 tree alias;
342 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
343 if (alias)
345 alias = TREE_VALUE (TREE_VALUE (alias));
346 alias = get_identifier (TREE_STRING_POINTER (alias));
347 assemble_alias (decl, alias);
351 /* Forward declarations for nested functions are not "external",
352 but we need to treat them as if they were. */
353 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
354 || TREE_CODE (decl) == FUNCTION_DECL)
356 timevar_push (TV_VARCONST);
358 if (asmspec)
359 make_decl_rtl (decl, asmspec);
361 /* Don't output anything when a tentative file-scope definition
362 is seen. But at end of compilation, do output code for them.
364 We do output all variables when unit-at-a-time is active and rely on
365 callgraph code to defer them except for forward declarations
366 (see gcc.c-torture/compile/920624-1.c) */
367 if ((at_end
368 || !DECL_DEFER_OUTPUT (decl)
369 || (flag_unit_at_a_time && DECL_INITIAL (decl)))
370 && !DECL_EXTERNAL (decl))
372 if (flag_unit_at_a_time && !cgraph_global_info_ready
373 && TREE_CODE (decl) != FUNCTION_DECL && top_level
374 /* If we defer processing of decls that have had their
375 DECL_RTL set above (say, in make_decl_rtl),
376 check_global_declarations() will clear it before
377 assemble_variable has a chance to act on it. This
378 would remove all traces of the register name in a
379 global register variable, for example. */
380 && !DECL_RTL_SET_P (decl))
381 cgraph_varpool_finalize_decl (decl);
382 else
383 assemble_variable (decl, top_level, at_end, 0);
386 #ifdef ASM_FINISH_DECLARE_OBJECT
387 if (decl == last_assemble_variable_decl)
389 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
390 top_level, at_end);
392 #endif
394 timevar_pop (TV_VARCONST);
396 else if (DECL_REGISTER (decl) && asmspec != 0)
398 if (decode_reg_name (asmspec) >= 0)
400 SET_DECL_RTL (decl, NULL_RTX);
401 make_decl_rtl (decl, asmspec);
403 else
405 error ("%Hinvalid register name `%s' for register variable",
406 &DECL_SOURCE_LOCATION (decl), asmspec);
407 DECL_REGISTER (decl) = 0;
408 if (!top_level)
409 expand_decl (decl);
412 else if (TREE_CODE (decl) == TYPE_DECL)
414 timevar_push (TV_SYMOUT);
415 debug_hooks->type_decl (decl, !top_level);
416 timevar_pop (TV_SYMOUT);
420 /* Called after finishing a record, union or enumeral type. */
422 void
423 rest_of_type_compilation (tree type, int toplev)
425 /* Avoid confusing the debug information machinery when there are
426 errors. */
427 if (errorcount != 0 || sorrycount != 0)
428 return;
430 timevar_push (TV_SYMOUT);
431 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
432 timevar_pop (TV_SYMOUT);
435 /* Turn the RTL into assembly. */
436 static void
437 rest_of_handle_final (void)
439 timevar_push (TV_FINAL);
441 rtx x;
442 const char *fnname;
444 /* Get the function's name, as described by its RTL. This may be
445 different from the DECL_NAME name used in the source file. */
447 x = DECL_RTL (current_function_decl);
448 if (!MEM_P (x))
449 abort ();
450 x = XEXP (x, 0);
451 if (GET_CODE (x) != SYMBOL_REF)
452 abort ();
453 fnname = XSTR (x, 0);
455 assemble_start_function (current_function_decl, fnname);
456 final_start_function (get_insns (), asm_out_file, optimize);
457 final (get_insns (), asm_out_file, optimize, 0);
458 final_end_function ();
460 #ifdef TARGET_UNWIND_INFO
461 /* ??? The IA-64 ".handlerdata" directive must be issued before
462 the ".endp" directive that closes the procedure descriptor. */
463 output_function_exception_table ();
464 #endif
466 assemble_end_function (current_function_decl, fnname);
468 #ifndef TARGET_UNWIND_INFO
469 /* Otherwise, it feels unclean to switch sections in the middle. */
470 output_function_exception_table ();
471 #endif
473 if (! quiet_flag)
474 fflush (asm_out_file);
476 /* Release all memory allocated by flow. */
477 free_basic_block_vars ();
479 /* Release all memory held by regsets now. */
480 regset_release_memory ();
483 /* Write DBX symbols if requested. */
485 /* Note that for those inline functions where we don't initially
486 know for certain that we will be generating an out-of-line copy,
487 the first invocation of this routine (rest_of_compilation) will
488 skip over this code by doing a `goto exit_rest_of_compilation;'.
489 Later on, wrapup_global_declarations will (indirectly) call
490 rest_of_compilation again for those inline functions that need
491 to have out-of-line copies generated. During that call, we
492 *will* be routed past here. */
494 timevar_push (TV_SYMOUT);
495 (*debug_hooks->function_decl) (current_function_decl);
496 timevar_pop (TV_SYMOUT);
498 ggc_collect ();
499 timevar_pop (TV_FINAL);
502 #ifdef DELAY_SLOTS
503 /* Run delay slot optimization. */
504 static void
505 rest_of_handle_delay_slots (void)
507 timevar_push (TV_DBR_SCHED);
508 open_dump_file (DFI_dbr, current_function_decl);
510 dbr_schedule (get_insns (), dump_file);
512 close_dump_file (DFI_dbr, print_rtl, get_insns ());
514 ggc_collect ();
516 timevar_pop (TV_DBR_SCHED);
518 #endif
520 #ifdef STACK_REGS
521 /* Convert register usage from flat register file usage to a stack
522 register file. */
523 static void
524 rest_of_handle_stack_regs (void)
526 #if defined (HAVE_ATTR_length)
527 /* If flow2 creates new instructions which need splitting
528 and scheduling after reload is not done, they might not be
529 split until final which doesn't allow splitting
530 if HAVE_ATTR_length. */
531 #ifdef INSN_SCHEDULING
532 if (optimize && !flag_schedule_insns_after_reload)
533 #else
534 if (optimize)
535 #endif
537 timevar_push (TV_SHORTEN_BRANCH);
538 split_all_insns (1);
539 timevar_pop (TV_SHORTEN_BRANCH);
541 #endif
543 timevar_push (TV_REG_STACK);
544 open_dump_file (DFI_stack, current_function_decl);
546 if (reg_to_stack (dump_file) && optimize)
548 if (cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK
549 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0))
550 && (flag_reorder_blocks || flag_reorder_blocks_and_partition))
552 reorder_basic_blocks ();
553 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_POST_REGSTACK);
557 close_dump_file (DFI_stack, print_rtl_with_bb, get_insns ());
559 ggc_collect ();
560 timevar_pop (TV_REG_STACK);
562 #endif
564 /* Track the variables, ie. compute where the variable is stored at each position in function. */
565 static void
566 rest_of_handle_variable_tracking (void)
568 timevar_push (TV_VAR_TRACKING);
569 open_dump_file (DFI_vartrack, current_function_decl);
571 variable_tracking_main ();
573 close_dump_file (DFI_vartrack, print_rtl_with_bb, get_insns ());
574 timevar_pop (TV_VAR_TRACKING);
577 /* Machine dependent reorg pass. */
578 static void
579 rest_of_handle_machine_reorg (void)
581 timevar_push (TV_MACH_DEP);
582 open_dump_file (DFI_mach, current_function_decl);
584 targetm.machine_dependent_reorg ();
586 close_dump_file (DFI_mach, print_rtl, get_insns ());
588 ggc_collect ();
589 timevar_pop (TV_MACH_DEP);
593 /* Run new register allocator. Return TRUE if we must exit
594 rest_of_compilation upon return. */
595 static bool
596 rest_of_handle_new_regalloc (void)
598 int failure;
600 timevar_push (TV_LOCAL_ALLOC);
601 open_dump_file (DFI_lreg, current_function_decl);
603 delete_trivially_dead_insns (get_insns (), max_reg_num ());
604 reg_alloc ();
606 timevar_pop (TV_LOCAL_ALLOC);
607 close_dump_file (DFI_lreg, NULL, NULL);
609 /* XXX clean up the whole mess to bring live info in shape again. */
610 timevar_push (TV_GLOBAL_ALLOC);
611 open_dump_file (DFI_greg, current_function_decl);
613 build_insn_chain (get_insns ());
614 failure = reload (get_insns (), 0);
616 timevar_pop (TV_GLOBAL_ALLOC);
618 ggc_collect ();
620 if (dump_file_tbl[DFI_greg].enabled)
622 timevar_push (TV_DUMP);
623 dump_global_regs (dump_file);
624 timevar_pop (TV_DUMP);
625 close_dump_file (DFI_greg, print_rtl_with_bb, get_insns ());
628 if (failure)
629 return true;
631 reload_completed = 1;
633 return false;
636 /* Run old register allocator. Return TRUE if we must exit
637 rest_of_compilation upon return. */
638 static bool
639 rest_of_handle_old_regalloc (void)
641 int failure;
642 int rebuild_notes;
644 timevar_push (TV_LOCAL_ALLOC);
645 open_dump_file (DFI_lreg, current_function_decl);
647 /* Allocate the reg_renumber array. */
648 allocate_reg_info (max_regno, FALSE, TRUE);
650 /* And the reg_equiv_memory_loc array. */
651 VARRAY_GROW (reg_equiv_memory_loc_varray, max_regno);
652 reg_equiv_memory_loc = &VARRAY_RTX (reg_equiv_memory_loc_varray, 0);
654 allocate_initial_values (reg_equiv_memory_loc);
656 regclass (get_insns (), max_reg_num (), dump_file);
657 rebuild_notes = local_alloc ();
659 timevar_pop (TV_LOCAL_ALLOC);
661 /* Local allocation may have turned an indirect jump into a direct
662 jump. If so, we must rebuild the JUMP_LABEL fields of jumping
663 instructions. */
664 if (rebuild_notes)
666 timevar_push (TV_JUMP);
668 rebuild_jump_labels (get_insns ());
669 purge_all_dead_edges (0);
671 timevar_pop (TV_JUMP);
674 if (dump_file_tbl[DFI_lreg].enabled)
676 timevar_push (TV_DUMP);
677 dump_flow_info (dump_file);
678 dump_local_alloc (dump_file);
679 timevar_pop (TV_DUMP);
682 close_dump_file (DFI_lreg, print_rtl_with_bb, get_insns ());
684 ggc_collect ();
686 timevar_push (TV_GLOBAL_ALLOC);
687 open_dump_file (DFI_greg, current_function_decl);
689 /* If optimizing, allocate remaining pseudo-regs. Do the reload
690 pass fixing up any insns that are invalid. */
692 if (optimize)
693 failure = global_alloc (dump_file);
694 else
696 build_insn_chain (get_insns ());
697 failure = reload (get_insns (), 0);
700 if (dump_file_tbl[DFI_greg].enabled)
702 timevar_push (TV_DUMP);
703 dump_global_regs (dump_file);
704 timevar_pop (TV_DUMP);
706 close_dump_file (DFI_greg, print_rtl_with_bb, get_insns ());
709 ggc_collect ();
711 timevar_pop (TV_GLOBAL_ALLOC);
713 return failure;
716 /* Run the regrename and cprop passes. */
717 static void
718 rest_of_handle_regrename (void)
720 timevar_push (TV_RENAME_REGISTERS);
721 open_dump_file (DFI_rnreg, current_function_decl);
723 if (flag_rename_registers)
724 regrename_optimize ();
725 if (flag_cprop_registers)
726 copyprop_hardreg_forward ();
728 close_dump_file (DFI_rnreg, print_rtl_with_bb, get_insns ());
729 timevar_pop (TV_RENAME_REGISTERS);
732 /* Reorder basic blocks. */
733 static void
734 rest_of_handle_reorder_blocks (void)
736 bool changed;
737 open_dump_file (DFI_bbro, current_function_decl);
739 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
740 splitting possibly introduced more crossjumping opportunities. */
741 changed = cleanup_cfg (CLEANUP_EXPENSIVE
742 | (!HAVE_conditional_execution
743 ? CLEANUP_UPDATE_LIFE : 0));
745 if (flag_sched2_use_traces && flag_schedule_insns_after_reload)
746 tracer ();
747 if (flag_reorder_blocks || flag_reorder_blocks_and_partition)
748 reorder_basic_blocks ();
749 if (flag_reorder_blocks || flag_reorder_blocks_and_partition
750 || (flag_sched2_use_traces && flag_schedule_insns_after_reload))
751 changed |= cleanup_cfg (CLEANUP_EXPENSIVE
752 | (!HAVE_conditional_execution
753 ? CLEANUP_UPDATE_LIFE : 0));
755 /* On conditional execution targets we can not update the life cheaply, so
756 we deffer the updating to after both cleanups. This may lose some cases
757 but should not be terribly bad. */
758 if (changed && HAVE_conditional_execution)
759 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
760 PROP_DEATH_NOTES);
761 close_dump_file (DFI_bbro, print_rtl_with_bb, get_insns ());
764 /* Partition hot and cold basic blocks. */
765 static void
766 rest_of_handle_partition_blocks (void)
768 no_new_pseudos = 0;
769 partition_hot_cold_basic_blocks ();
770 allocate_reg_life_data ();
771 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
772 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
773 no_new_pseudos = 1;
776 #ifdef INSN_SCHEDULING
777 /* Run instruction scheduler. */
778 /* Perform SMS module scheduling. */
779 static void
780 rest_of_handle_sms (void)
782 timevar_push (TV_SMS);
783 open_dump_file (DFI_sms, current_function_decl);
785 /* We want to be able to create new pseudos. */
786 no_new_pseudos = 0;
787 sms_schedule (dump_file);
788 close_dump_file (DFI_sms, print_rtl, get_insns ());
791 /* Update the life information, because we add pseudos. */
792 max_regno = max_reg_num ();
793 allocate_reg_info (max_regno, FALSE, FALSE);
794 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
795 (PROP_DEATH_NOTES
796 | PROP_KILL_DEAD_CODE
797 | PROP_SCAN_DEAD_CODE));
798 no_new_pseudos = 1;
800 ggc_collect ();
801 timevar_pop (TV_SMS);
804 /* Run instruction scheduler. */
805 static void
806 rest_of_handle_sched (void)
808 timevar_push (TV_SCHED);
810 /* Print function header into sched dump now
811 because doing the sched analysis makes some of the dump. */
812 open_dump_file (DFI_sched, current_function_decl);
814 /* Do control and data sched analysis,
815 and write some of the results to dump file. */
817 schedule_insns (dump_file);
819 close_dump_file (DFI_sched, print_rtl_with_bb, get_insns ());
821 ggc_collect ();
822 timevar_pop (TV_SCHED);
825 /* Run second scheduling pass after reload. */
826 static void
827 rest_of_handle_sched2 (void)
829 timevar_push (TV_SCHED2);
830 open_dump_file (DFI_sched2, current_function_decl);
832 /* Do control and data sched analysis again,
833 and write some more of the results to dump file. */
835 split_all_insns (1);
837 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
839 schedule_ebbs (dump_file);
840 /* No liveness updating code yet, but it should be easy to do.
841 reg-stack recomputes the liveness when needed for now. */
842 count_or_remove_death_notes (NULL, 1);
843 cleanup_cfg (CLEANUP_EXPENSIVE);
845 else
846 schedule_insns (dump_file);
848 close_dump_file (DFI_sched2, print_rtl_with_bb, get_insns ());
850 ggc_collect ();
852 timevar_pop (TV_SCHED2);
854 #endif
856 static void
857 rest_of_handle_gcse2 (void)
859 timevar_push (TV_RELOAD_CSE_REGS);
860 open_dump_file (DFI_gcse2, current_function_decl);
862 gcse_after_reload_main (get_insns (), dump_file);
863 rebuild_jump_labels (get_insns ());
864 delete_trivially_dead_insns (get_insns (), max_reg_num ());
865 close_dump_file (DFI_gcse2, print_rtl_with_bb, get_insns ());
867 ggc_collect ();
869 #ifdef ENABLE_CHECKING
870 verify_flow_info ();
871 #endif
873 timevar_pop (TV_RELOAD_CSE_REGS);
876 /* Register allocation pre-pass, to reduce number of moves necessary
877 for two-address machines. */
878 static void
879 rest_of_handle_regmove (void)
881 timevar_push (TV_REGMOVE);
882 open_dump_file (DFI_regmove, current_function_decl);
884 regmove_optimize (get_insns (), max_reg_num (), dump_file);
886 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
887 close_dump_file (DFI_regmove, print_rtl_with_bb, get_insns ());
889 ggc_collect ();
890 timevar_pop (TV_REGMOVE);
893 /* Run tracer. */
894 static void
895 rest_of_handle_tracer (void)
897 open_dump_file (DFI_tracer, current_function_decl);
898 if (dump_file)
899 dump_flow_info (dump_file);
900 tracer ();
901 cleanup_cfg (CLEANUP_EXPENSIVE);
902 reg_scan (get_insns (), max_reg_num (), 0);
903 close_dump_file (DFI_tracer, print_rtl_with_bb, get_insns ());
906 /* If-conversion and CFG cleanup. */
907 static void
908 rest_of_handle_if_conversion (void)
910 timevar_push (TV_IFCVT);
911 open_dump_file (DFI_ce1, current_function_decl);
913 if (flag_if_conversion)
915 if (dump_file)
916 dump_flow_info (dump_file);
917 cleanup_cfg (CLEANUP_EXPENSIVE);
918 reg_scan (get_insns (), max_reg_num (), 0);
919 if_convert (0);
922 timevar_push (TV_JUMP);
923 cleanup_cfg (CLEANUP_EXPENSIVE);
924 reg_scan (get_insns (), max_reg_num (), 0);
925 timevar_pop (TV_JUMP);
927 close_dump_file (DFI_ce1, print_rtl_with_bb, get_insns ());
928 timevar_pop (TV_IFCVT);
931 /* Rerun if-conversion, as combine may have simplified things enough
932 to now meet sequence length restrictions. */
933 static void
934 rest_of_handle_if_after_combine (void)
936 timevar_push (TV_IFCVT);
937 open_dump_file (DFI_ce2, current_function_decl);
939 no_new_pseudos = 0;
940 if_convert (1);
941 no_new_pseudos = 1;
943 close_dump_file (DFI_ce2, print_rtl_with_bb, get_insns ());
944 timevar_pop (TV_IFCVT);
947 static void
948 rest_of_handle_if_after_reload (void)
950 timevar_push (TV_IFCVT2);
951 open_dump_file (DFI_ce3, current_function_decl);
953 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
954 splitting possibly introduced more crossjumping opportunities. */
955 cleanup_cfg (CLEANUP_EXPENSIVE
956 | CLEANUP_UPDATE_LIFE
957 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
958 if (flag_if_conversion2)
959 if_convert (1);
960 close_dump_file (DFI_ce3, print_rtl_with_bb, get_insns ());
961 timevar_pop (TV_IFCVT2);
964 static void
965 rest_of_handle_web (void)
967 open_dump_file (DFI_web, current_function_decl);
968 timevar_push (TV_WEB);
969 web_main ();
970 delete_trivially_dead_insns (get_insns (), max_reg_num ());
971 cleanup_cfg (CLEANUP_EXPENSIVE);
973 timevar_pop (TV_WEB);
974 close_dump_file (DFI_web, print_rtl_with_bb, get_insns ());
975 reg_scan (get_insns (), max_reg_num (), 0);
978 /* Do branch profiling and static profile estimation passes. */
979 static void
980 rest_of_handle_branch_prob (void)
982 struct loops loops;
984 timevar_push (TV_BRANCH_PROB);
985 open_dump_file (DFI_bp, current_function_decl);
987 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
988 branch_prob ();
990 /* Discover and record the loop depth at the head of each basic
991 block. The loop infrastructure does the real job for us. */
992 flow_loops_find (&loops, LOOP_TREE);
994 if (dump_file)
995 flow_loops_dump (&loops, dump_file, NULL, 0);
997 /* Estimate using heuristics if no profiling info is available. */
998 if (flag_guess_branch_prob)
999 estimate_probability (&loops);
1001 flow_loops_free (&loops);
1002 free_dominance_info (CDI_DOMINATORS);
1003 close_dump_file (DFI_bp, print_rtl_with_bb, get_insns ());
1004 timevar_pop (TV_BRANCH_PROB);
1007 /* Do optimizations based on expression value profiles. */
1008 static void
1009 rest_of_handle_value_profile_transformations (void)
1011 open_dump_file (DFI_vpt, current_function_decl);
1012 timevar_push (TV_VPT);
1014 if (value_profile_transformations ())
1015 cleanup_cfg (CLEANUP_EXPENSIVE);
1017 timevar_pop (TV_VPT);
1018 close_dump_file (DFI_vpt, print_rtl_with_bb, get_insns ());
1021 /* Do control and data flow analysis; write some of the results to the
1022 dump file. */
1023 static void
1024 rest_of_handle_cfg (void)
1026 open_dump_file (DFI_cfg, current_function_decl);
1027 if (dump_file)
1028 dump_flow_info (dump_file);
1029 if (optimize)
1030 cleanup_cfg (CLEANUP_EXPENSIVE
1031 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1033 /* It may make more sense to mark constant functions after dead code is
1034 eliminated by life_analysis, but we need to do it early, as -fprofile-arcs
1035 may insert code making function non-constant, but we still must consider
1036 it as constant, otherwise -fbranch-probabilities will not read data back.
1038 life_analysis rarely eliminates modification of external memory.
1040 if (optimize)
1042 /* Alias analysis depends on this information and mark_constant_function
1043 depends on alias analysis. */
1044 reg_scan (get_insns (), max_reg_num (), 1);
1045 mark_constant_function ();
1048 close_dump_file (DFI_cfg, print_rtl_with_bb, get_insns ());
1051 /* Perform jump bypassing and control flow optimizations. */
1052 static void
1053 rest_of_handle_jump_bypass (void)
1055 timevar_push (TV_BYPASS);
1056 open_dump_file (DFI_bypass, current_function_decl);
1058 cleanup_cfg (CLEANUP_EXPENSIVE);
1059 reg_scan (get_insns (), max_reg_num (), 1);
1061 if (bypass_jumps (dump_file))
1063 rebuild_jump_labels (get_insns ());
1064 cleanup_cfg (CLEANUP_EXPENSIVE);
1065 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1068 close_dump_file (DFI_bypass, print_rtl_with_bb, get_insns ());
1069 timevar_pop (TV_BYPASS);
1071 ggc_collect ();
1073 #ifdef ENABLE_CHECKING
1074 verify_flow_info ();
1075 #endif
1078 /* Try combining insns through substitution. */
1079 static void
1080 rest_of_handle_combine (void)
1082 int rebuild_jump_labels_after_combine = 0;
1084 timevar_push (TV_COMBINE);
1085 open_dump_file (DFI_combine, current_function_decl);
1087 rebuild_jump_labels_after_combine
1088 = combine_instructions (get_insns (), max_reg_num ());
1090 /* Combining insns may have turned an indirect jump into a
1091 direct jump. Rebuild the JUMP_LABEL fields of jumping
1092 instructions. */
1093 if (rebuild_jump_labels_after_combine)
1095 timevar_push (TV_JUMP);
1096 rebuild_jump_labels (get_insns ());
1097 timevar_pop (TV_JUMP);
1099 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
1102 close_dump_file (DFI_combine, print_rtl_with_bb, get_insns ());
1103 timevar_pop (TV_COMBINE);
1105 ggc_collect ();
1108 /* Perform life analysis. */
1109 static void
1110 rest_of_handle_life (void)
1112 open_dump_file (DFI_life, current_function_decl);
1113 regclass_init ();
1115 #ifdef ENABLE_CHECKING
1116 verify_flow_info ();
1117 #endif
1118 life_analysis (dump_file, PROP_FINAL);
1119 if (optimize)
1120 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_UPDATE_LIFE
1121 | CLEANUP_LOG_LINKS
1122 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1124 if (extra_warnings)
1126 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
1127 setjmp_args_warning ();
1130 if (optimize)
1132 if (!flag_new_regalloc && initialize_uninitialized_subregs ())
1134 /* Insns were inserted, and possibly pseudos created, so
1135 things might look a bit different. */
1136 allocate_reg_life_data ();
1137 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1138 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
1142 no_new_pseudos = 1;
1144 close_dump_file (DFI_life, print_rtl_with_bb, get_insns ());
1146 ggc_collect ();
1149 /* Perform common subexpression elimination. Nonzero value from
1150 `cse_main' means that jumps were simplified and some code may now
1151 be unreachable, so do jump optimization again. */
1152 static void
1153 rest_of_handle_cse (void)
1155 int tem;
1157 open_dump_file (DFI_cse, current_function_decl);
1158 if (dump_file)
1159 dump_flow_info (dump_file);
1160 timevar_push (TV_CSE);
1162 reg_scan (get_insns (), max_reg_num (), 1);
1164 tem = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1165 if (tem)
1166 rebuild_jump_labels (get_insns ());
1167 if (purge_all_dead_edges (0))
1168 delete_unreachable_blocks ();
1170 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1172 /* If we are not running more CSE passes, then we are no longer
1173 expecting CSE to be run. But always rerun it in a cheap mode. */
1174 cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
1176 if (tem || optimize > 1)
1177 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1179 timevar_pop (TV_CSE);
1180 close_dump_file (DFI_cse, print_rtl_with_bb, get_insns ());
1182 ggc_collect ();
1185 /* Run second CSE pass after loop optimizations. */
1186 static void
1187 rest_of_handle_cse2 (void)
1189 int tem;
1191 timevar_push (TV_CSE2);
1192 open_dump_file (DFI_cse2, current_function_decl);
1193 if (dump_file)
1194 dump_flow_info (dump_file);
1195 /* CFG is no longer maintained up-to-date. */
1196 tem = cse_main (get_insns (), max_reg_num (), 1, dump_file);
1198 /* Run a pass to eliminate duplicated assignments to condition code
1199 registers. We have to run this after bypass_jumps, because it
1200 makes it harder for that pass to determine whether a jump can be
1201 bypassed safely. */
1202 cse_condition_code_reg ();
1204 purge_all_dead_edges (0);
1205 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1207 if (tem)
1209 timevar_push (TV_JUMP);
1210 rebuild_jump_labels (get_insns ());
1211 cleanup_cfg (CLEANUP_EXPENSIVE);
1212 timevar_pop (TV_JUMP);
1214 reg_scan (get_insns (), max_reg_num (), 0);
1215 close_dump_file (DFI_cse2, print_rtl_with_bb, get_insns ());
1216 timevar_pop (TV_CSE2);
1218 ggc_collect ();
1221 /* Perform global cse. */
1222 static void
1223 rest_of_handle_gcse (void)
1225 int save_csb, save_cfj;
1226 int tem2 = 0, tem;
1228 timevar_push (TV_GCSE);
1229 open_dump_file (DFI_gcse, current_function_decl);
1231 tem = gcse_main (get_insns (), dump_file);
1232 rebuild_jump_labels (get_insns ());
1233 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1235 save_csb = flag_cse_skip_blocks;
1236 save_cfj = flag_cse_follow_jumps;
1237 flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
1239 /* If -fexpensive-optimizations, re-run CSE to clean up things done
1240 by gcse. */
1241 if (flag_expensive_optimizations)
1243 timevar_push (TV_CSE);
1244 reg_scan (get_insns (), max_reg_num (), 1);
1245 tem2 = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1246 purge_all_dead_edges (0);
1247 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1248 timevar_pop (TV_CSE);
1249 cse_not_expected = !flag_rerun_cse_after_loop;
1252 /* If gcse or cse altered any jumps, rerun jump optimizations to clean
1253 things up. Then possibly re-run CSE again. */
1254 while (tem || tem2)
1256 tem = tem2 = 0;
1257 timevar_push (TV_JUMP);
1258 rebuild_jump_labels (get_insns ());
1259 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1260 timevar_pop (TV_JUMP);
1262 if (flag_expensive_optimizations)
1264 timevar_push (TV_CSE);
1265 reg_scan (get_insns (), max_reg_num (), 1);
1266 tem2 = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1267 purge_all_dead_edges (0);
1268 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1269 timevar_pop (TV_CSE);
1273 close_dump_file (DFI_gcse, print_rtl_with_bb, get_insns ());
1274 timevar_pop (TV_GCSE);
1276 ggc_collect ();
1277 flag_cse_skip_blocks = save_csb;
1278 flag_cse_follow_jumps = save_cfj;
1279 #ifdef ENABLE_CHECKING
1280 verify_flow_info ();
1281 #endif
1284 /* Move constant computations out of loops. */
1285 static void
1286 rest_of_handle_loop_optimize (void)
1288 int do_unroll, do_prefetch;
1290 timevar_push (TV_LOOP);
1291 delete_dead_jumptables ();
1292 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1293 open_dump_file (DFI_loop, current_function_decl);
1295 /* CFG is no longer maintained up-to-date. */
1296 free_bb_for_insn ();
1298 if (flag_unroll_loops)
1299 do_unroll = LOOP_AUTO_UNROLL; /* Having two unrollers is useless. */
1300 else
1301 do_unroll = flag_old_unroll_loops ? LOOP_UNROLL : LOOP_AUTO_UNROLL;
1302 do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
1304 if (flag_rerun_loop_opt)
1306 cleanup_barriers ();
1308 /* We only want to perform unrolling once. */
1309 loop_optimize (get_insns (), dump_file, do_unroll);
1310 do_unroll = 0;
1312 /* The first call to loop_optimize makes some instructions
1313 trivially dead. We delete those instructions now in the
1314 hope that doing so will make the heuristics in loop work
1315 better and possibly speed up compilation. */
1316 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1318 /* The regscan pass is currently necessary as the alias
1319 analysis code depends on this information. */
1320 reg_scan (get_insns (), max_reg_num (), 1);
1322 cleanup_barriers ();
1323 loop_optimize (get_insns (), dump_file, do_unroll | do_prefetch);
1325 /* Loop can create trivially dead instructions. */
1326 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1327 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1328 close_dump_file (DFI_loop, print_rtl, get_insns ());
1329 timevar_pop (TV_LOOP);
1331 ggc_collect ();
1334 /* Perform loop optimizations. It might be better to do them a bit
1335 sooner, but we want the profile feedback to work more
1336 efficiently. */
1337 static void
1338 rest_of_handle_loop2 (void)
1340 struct loops *loops;
1341 basic_block bb;
1343 if (!flag_move_loop_invariants
1344 && !flag_unswitch_loops
1345 && !flag_peel_loops
1346 && !flag_unroll_loops
1347 && !flag_branch_on_count_reg)
1348 return;
1350 timevar_push (TV_LOOP);
1351 open_dump_file (DFI_loop2, current_function_decl);
1352 if (dump_file)
1353 dump_flow_info (dump_file);
1355 /* Initialize structures for layout changes. */
1356 cfg_layout_initialize ();
1358 loops = loop_optimizer_init (dump_file);
1360 if (loops)
1362 /* The optimizations: */
1363 if (flag_move_loop_invariants)
1364 move_loop_invariants (loops);
1366 if (flag_unswitch_loops)
1367 unswitch_loops (loops);
1369 if (flag_peel_loops || flag_unroll_loops)
1370 unroll_and_peel_loops (loops,
1371 (flag_peel_loops ? UAP_PEEL : 0) |
1372 (flag_unroll_loops ? UAP_UNROLL : 0) |
1373 (flag_unroll_all_loops ? UAP_UNROLL_ALL : 0));
1375 #ifdef HAVE_doloop_end
1376 if (flag_branch_on_count_reg && HAVE_doloop_end)
1377 doloop_optimize_loops (loops);
1378 #endif /* HAVE_doloop_end */
1380 loop_optimizer_finalize (loops, dump_file);
1383 free_dominance_info (CDI_DOMINATORS);
1385 /* Finalize layout changes. */
1386 FOR_EACH_BB (bb)
1387 if (bb->next_bb != EXIT_BLOCK_PTR)
1388 bb->rbi->next = bb->next_bb;
1389 cfg_layout_finalize ();
1391 cleanup_cfg (CLEANUP_EXPENSIVE);
1392 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1393 reg_scan (get_insns (), max_reg_num (), 0);
1394 if (dump_file)
1395 dump_flow_info (dump_file);
1396 close_dump_file (DFI_loop2, print_rtl_with_bb, get_insns ());
1397 timevar_pop (TV_LOOP);
1398 ggc_collect ();
1401 static void
1402 rest_of_handle_branch_target_load_optimize (void)
1404 static int warned = 0;
1406 /* Leave this a warning for now so that it is possible to experiment
1407 with running this pass twice. In 3.6, we should either make this
1408 an error, or use separate dump files. */
1409 if (flag_branch_target_load_optimize
1410 && flag_branch_target_load_optimize2
1411 && !warned)
1413 warning ("branch target register load optimization is not intended "
1414 "to be run twice");
1416 warned = 1;
1419 open_dump_file (DFI_branch_target_load, current_function_decl);
1420 branch_target_load_optimize (epilogue_completed);
1421 close_dump_file (DFI_branch_target_load, print_rtl_with_bb, get_insns ());
1422 ggc_collect ();
1425 #ifdef OPTIMIZE_MODE_SWITCHING
1426 static void
1427 rest_of_handle_mode_switching (void)
1429 timevar_push (TV_MODE_SWITCH);
1431 no_new_pseudos = 0;
1432 optimize_mode_switching (NULL);
1433 no_new_pseudos = 1;
1435 timevar_pop (TV_MODE_SWITCH);
1437 #endif
1439 static void
1440 rest_of_handle_jump (void)
1442 ggc_collect ();
1444 timevar_push (TV_JUMP);
1445 open_dump_file (DFI_sibling, current_function_decl);
1447 /* ??? We may get caled either via tree_rest_of_compilation when the CFG
1448 is already built or directly (for instance from coverage code).
1449 The direct callers shall be updated. */
1450 if (!basic_block_info)
1452 init_flow ();
1453 rebuild_jump_labels (get_insns ());
1454 find_exception_handler_labels ();
1455 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1458 /* ??? We may get called either via tree_rest_of_compilation when the CFG
1459 is already built or directly (for instance from coverage code).
1460 The direct callers shall be updated. */
1461 if (!basic_block_info)
1463 init_flow ();
1464 rebuild_jump_labels (get_insns ());
1465 find_exception_handler_labels ();
1466 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1468 delete_unreachable_blocks ();
1469 #ifdef ENABLE_CHECKING
1470 verify_flow_info ();
1471 #endif
1472 timevar_pop (TV_JUMP);
1475 static void
1476 rest_of_handle_eh (void)
1478 insn_locators_initialize ();
1479 /* Complete generation of exception handling code. */
1480 if (doing_eh (0))
1482 timevar_push (TV_JUMP);
1483 open_dump_file (DFI_eh, current_function_decl);
1485 finish_eh_generation ();
1487 close_dump_file (DFI_eh, print_rtl, get_insns ());
1488 timevar_pop (TV_JUMP);
1493 static void
1494 rest_of_handle_prologue_epilogue (void)
1496 if (optimize && !flow2_completed)
1497 cleanup_cfg (CLEANUP_EXPENSIVE);
1499 /* On some machines, the prologue and epilogue code, or parts thereof,
1500 can be represented as RTL. Doing so lets us schedule insns between
1501 it and the rest of the code and also allows delayed branch
1502 scheduling to operate in the epilogue. */
1503 thread_prologue_and_epilogue_insns (get_insns ());
1504 epilogue_completed = 1;
1506 if (optimize && flow2_completed)
1507 life_analysis (dump_file, PROP_POSTRELOAD);
1510 static void
1511 rest_of_handle_stack_adjustments (void)
1513 life_analysis (dump_file, PROP_POSTRELOAD);
1514 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE
1515 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1517 /* This is kind of a heuristic. We need to run combine_stack_adjustments
1518 even for machines with possibly nonzero RETURN_POPS_ARGS
1519 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
1520 push instructions will have popping returns. */
1521 #ifndef PUSH_ROUNDING
1522 if (!ACCUMULATE_OUTGOING_ARGS)
1523 #endif
1524 combine_stack_adjustments ();
1527 static void
1528 rest_of_handle_flow2 (void)
1530 timevar_push (TV_FLOW2);
1531 open_dump_file (DFI_flow2, current_function_decl);
1533 /* Re-create the death notes which were deleted during reload. */
1534 #ifdef ENABLE_CHECKING
1535 verify_flow_info ();
1536 #endif
1538 /* If optimizing, then go ahead and split insns now. */
1539 #ifndef STACK_REGS
1540 if (optimize > 0)
1541 #endif
1542 split_all_insns (0);
1544 if (flag_branch_target_load_optimize)
1545 rest_of_handle_branch_target_load_optimize ();
1547 if (!targetm.late_rtl_prologue_epilogue)
1548 rest_of_handle_prologue_epilogue ();
1550 if (optimize)
1551 rest_of_handle_stack_adjustments ();
1553 flow2_completed = 1;
1555 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1556 timevar_pop (TV_FLOW2);
1558 ggc_collect ();
1562 static void
1563 rest_of_handle_jump2 (void)
1565 open_dump_file (DFI_jump, current_function_decl);
1567 /* Always do one jump optimization pass to ensure that JUMP_LABEL fields
1568 are initialized and to compute whether control can drop off the end
1569 of the function. */
1571 timevar_push (TV_JUMP);
1572 /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB. Do this
1573 before jump optimization switches branch directions. */
1574 if (flag_guess_branch_prob)
1575 expected_value_to_br_prob ();
1577 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1578 reg_scan (get_insns (), max_reg_num (), 0);
1579 if (dump_file)
1580 dump_flow_info (dump_file);
1581 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_PRE_LOOP
1582 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1584 create_loop_notes ();
1586 purge_line_number_notes (get_insns ());
1588 if (optimize)
1589 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1591 /* Jump optimization, and the removal of NULL pointer checks, may
1592 have reduced the number of instructions substantially. CSE, and
1593 future passes, allocate arrays whose dimensions involve the
1594 maximum instruction UID, so if we can reduce the maximum UID
1595 we'll save big on memory. */
1596 renumber_insns (dump_file);
1598 close_dump_file (DFI_jump, print_rtl_with_bb, get_insns ());
1599 timevar_pop (TV_JUMP);
1601 ggc_collect ();
1604 #ifdef HAVE_peephole2
1605 static void
1606 rest_of_handle_peephole2 (void)
1608 timevar_push (TV_PEEPHOLE2);
1609 open_dump_file (DFI_peephole2, current_function_decl);
1611 peephole2_optimize (dump_file);
1613 close_dump_file (DFI_peephole2, print_rtl_with_bb, get_insns ());
1614 timevar_pop (TV_PEEPHOLE2);
1616 #endif
1618 static void
1619 rest_of_handle_postreload (void)
1621 timevar_push (TV_RELOAD_CSE_REGS);
1622 open_dump_file (DFI_postreload, current_function_decl);
1624 /* Do a very simple CSE pass over just the hard registers. */
1625 reload_cse_regs (get_insns ());
1626 /* reload_cse_regs can eliminate potentially-trapping MEMs.
1627 Remove any EH edges associated with them. */
1628 if (flag_non_call_exceptions)
1629 purge_all_dead_edges (0);
1631 close_dump_file (DFI_postreload, print_rtl_with_bb, get_insns ());
1632 timevar_pop (TV_RELOAD_CSE_REGS);
1635 static void
1636 rest_of_handle_shorten_branches (void)
1638 /* Shorten branches. */
1639 timevar_push (TV_SHORTEN_BRANCH);
1640 shorten_branches (get_insns ());
1641 timevar_pop (TV_SHORTEN_BRANCH);
1644 static void
1645 rest_of_clean_state (void)
1647 coverage_end_function ();
1649 /* In case the function was not output,
1650 don't leave any temporary anonymous types
1651 queued up for sdb output. */
1652 #ifdef SDB_DEBUGGING_INFO
1653 if (write_symbols == SDB_DEBUG)
1654 sdbout_types (NULL_TREE);
1655 #endif
1657 reload_completed = 0;
1658 epilogue_completed = 0;
1659 flow2_completed = 0;
1660 no_new_pseudos = 0;
1662 timevar_push (TV_FINAL);
1664 /* Clear out the insn_length contents now that they are no
1665 longer valid. */
1666 init_insn_lengths ();
1668 /* Show no temporary slots allocated. */
1669 init_temp_slots ();
1671 free_basic_block_vars ();
1672 free_bb_for_insn ();
1674 timevar_pop (TV_FINAL);
1676 if (targetm.binds_local_p (current_function_decl))
1678 int pref = cfun->preferred_stack_boundary;
1679 if (cfun->recursive_call_emit
1680 && cfun->stack_alignment_needed > cfun->preferred_stack_boundary)
1681 pref = cfun->stack_alignment_needed;
1682 cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
1683 = pref;
1686 /* Make sure volatile mem refs aren't considered valid operands for
1687 arithmetic insns. We must call this here if this is a nested inline
1688 function, since the above code leaves us in the init_recog state
1689 (from final.c), and the function context push/pop code does not
1690 save/restore volatile_ok.
1692 ??? Maybe it isn't necessary for expand_start_function to call this
1693 anymore if we do it here? */
1695 init_recog_no_volatile ();
1697 /* We're done with this function. Free up memory if we can. */
1698 free_after_parsing (cfun);
1702 /* This is called from finish_function (within langhooks.parse_file)
1703 after each top-level definition is parsed.
1704 It is supposed to compile that function or variable
1705 and output the assembler code for it.
1706 After we return, the tree storage is freed. */
1708 void
1709 rest_of_compilation (void)
1711 /* There's no need to defer outputting this function any more; we
1712 know we want to output it. */
1713 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1715 /* Register rtl specific functions for cfg. */
1716 rtl_register_cfg_hooks ();
1718 /* Now that we're out of the frontend, we shouldn't have any more
1719 CONCATs anywhere. */
1720 generating_concat_p = 0;
1722 /* When processing delayed functions, prepare_function_start () won't
1723 have been run to re-initialize it. */
1724 cse_not_expected = ! optimize;
1726 finalize_block_changes ();
1728 /* Dump the rtl code if we are dumping rtl. */
1729 if (open_dump_file (DFI_rtl, current_function_decl))
1730 close_dump_file (DFI_rtl, print_rtl, get_insns ());
1732 /* Convert from NOTE_INSN_EH_REGION style notes, and do other
1733 sorts of eh initialization. Delay this until after the
1734 initial rtl dump so that we can see the original nesting. */
1735 convert_from_eh_region_ranges ();
1737 /* If we're emitting a nested function, make sure its parent gets
1738 emitted as well. Doing otherwise confuses debug info. */
1740 tree parent;
1741 for (parent = DECL_CONTEXT (current_function_decl);
1742 parent != NULL_TREE;
1743 parent = get_containing_scope (parent))
1744 if (TREE_CODE (parent) == FUNCTION_DECL)
1745 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1748 /* We are now committed to emitting code for this function. Do any
1749 preparation, such as emitting abstract debug info for the inline
1750 before it gets mangled by optimization. */
1751 if (cgraph_function_possibly_inlined_p (current_function_decl))
1752 (*debug_hooks->outlining_inline_function) (current_function_decl);
1754 /* Remove any notes we don't need. That will make iterating
1755 over the instruction sequence faster, and allow the garbage
1756 collector to reclaim the memory used by the notes. */
1757 remove_unnecessary_notes ();
1759 /* Initialize some variables used by the optimizers. */
1760 init_function_for_compilation ();
1762 TREE_ASM_WRITTEN (current_function_decl) = 1;
1764 /* Now that integrate will no longer see our rtl, we need not
1765 distinguish between the return value of this function and the
1766 return value of called functions. Also, we can remove all SETs
1767 of subregs of hard registers; they are only here because of
1768 integrate. Also, we can now initialize pseudos intended to
1769 carry magic hard reg data throughout the function.
1771 FIXME: All this looks thoroughly obsolete... maybe we can
1772 get rid of both these lines unconditionally? */
1773 rtx_equal_function_value_matters = 0;
1774 purge_hard_subreg_sets (get_insns ());
1776 /* Early return if there were errors. We can run afoul of our
1777 consistency checks, and there's not really much point in fixing them. */
1778 if (rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount)
1779 goto exit_rest_of_compilation;
1781 rest_of_handle_jump ();
1783 if (cfun->tail_call_emit)
1784 fixup_tail_calls ();
1786 rest_of_handle_eh ();
1788 /* Delay emitting hard_reg_initial_value sets until after EH landing pad
1789 generation, which might create new sets. */
1790 emit_initial_value_sets ();
1792 #ifdef FINALIZE_PIC
1793 /* If we are doing position-independent code generation, now
1794 is the time to output special prologues and epilogues.
1795 We do not want to do this earlier, because it just clutters
1796 up inline functions with meaningless insns. */
1797 if (flag_pic)
1798 FINALIZE_PIC;
1799 #endif
1801 /* Copy any shared structure that should not be shared. */
1802 unshare_all_rtl ();
1804 #ifdef SETJMP_VIA_SAVE_AREA
1805 /* This must be performed before virtual register instantiation.
1806 Please be aware that everything in the compiler that can look
1807 at the RTL up to this point must understand that REG_SAVE_AREA
1808 is just like a use of the REG contained inside. */
1809 if (current_function_calls_alloca)
1810 optimize_save_area_alloca ();
1811 #endif
1813 /* Instantiate all virtual registers. */
1814 instantiate_virtual_regs ();
1816 rest_of_handle_jump2 ();
1818 if (optimize > 0)
1819 rest_of_handle_cse ();
1821 if (optimize > 0)
1823 if (flag_gcse)
1824 rest_of_handle_gcse ();
1826 if (flag_loop_optimize)
1827 rest_of_handle_loop_optimize ();
1829 if (flag_gcse)
1830 rest_of_handle_jump_bypass ();
1833 timevar_push (TV_FLOW);
1834 rest_of_handle_cfg ();
1836 if (!flag_tree_based_profiling
1837 && (optimize > 0 || profile_arc_flag
1838 || flag_test_coverage || flag_branch_probabilities))
1840 rtl_register_profile_hooks ();
1841 rtl_register_value_prof_hooks ();
1842 rest_of_handle_branch_prob ();
1844 if (flag_branch_probabilities
1845 && flag_profile_values
1846 && flag_value_profile_transformations)
1847 rest_of_handle_value_profile_transformations ();
1849 /* Remove the death notes created for vpt. */
1850 if (flag_profile_values)
1851 count_or_remove_death_notes (NULL, 1);
1854 if (optimize > 0)
1855 rest_of_handle_if_conversion ();
1857 if (optimize > 0 && flag_tracer)
1858 rest_of_handle_tracer ();
1860 if (optimize > 0
1861 && flag_loop_optimize2)
1862 rest_of_handle_loop2 ();
1864 if (optimize > 0 && flag_web)
1865 rest_of_handle_web ();
1867 if (optimize > 0 && flag_rerun_cse_after_loop)
1868 rest_of_handle_cse2 ();
1870 cse_not_expected = 1;
1872 rest_of_handle_life ();
1873 timevar_pop (TV_FLOW);
1875 if (optimize > 0)
1876 rest_of_handle_combine ();
1878 if (optimize > 0 && flag_if_conversion)
1879 rest_of_handle_if_after_combine ();
1881 /* The optimization to partition hot/cold basic blocks into separate
1882 sections of the .o file does not work well with exception handling.
1883 Don't call it if there are exceptions. */
1885 if (optimize > 0 && flag_reorder_blocks_and_partition && !flag_exceptions)
1886 rest_of_handle_partition_blocks ();
1888 if (optimize > 0 && (flag_regmove || flag_expensive_optimizations))
1889 rest_of_handle_regmove ();
1891 /* Do unconditional splitting before register allocation to allow machine
1892 description to add extra information not needed previously. */
1893 split_all_insns (1);
1895 #ifdef OPTIMIZE_MODE_SWITCHING
1896 rest_of_handle_mode_switching ();
1897 #endif
1899 /* Any of the several passes since flow1 will have munged register
1900 lifetime data a bit. We need it to be up to date for scheduling
1901 (see handling of reg_known_equiv in init_alias_analysis). */
1902 recompute_reg_usage (get_insns (), !optimize_size);
1904 #ifdef INSN_SCHEDULING
1905 if (optimize > 0 && flag_modulo_sched)
1906 rest_of_handle_sms ();
1908 if (flag_schedule_insns)
1909 rest_of_handle_sched ();
1910 #endif
1912 /* Determine if the current function is a leaf before running reload
1913 since this can impact optimizations done by the prologue and
1914 epilogue thus changing register elimination offsets. */
1915 current_function_is_leaf = leaf_function_p ();
1917 if (flag_new_regalloc)
1919 if (rest_of_handle_new_regalloc ())
1920 goto exit_rest_of_compilation;
1922 else
1924 if (rest_of_handle_old_regalloc ())
1925 goto exit_rest_of_compilation;
1928 if (optimize > 0)
1929 rest_of_handle_postreload ();
1931 if (optimize > 0 && flag_gcse_after_reload)
1932 rest_of_handle_gcse2 ();
1934 rest_of_handle_flow2 ();
1936 #ifdef HAVE_peephole2
1937 if (optimize > 0 && flag_peephole2)
1938 rest_of_handle_peephole2 ();
1939 #endif
1941 if (optimize > 0)
1942 rest_of_handle_if_after_reload ();
1944 if (optimize > 0)
1946 if (flag_rename_registers || flag_cprop_registers)
1947 rest_of_handle_regrename ();
1949 rest_of_handle_reorder_blocks ();
1952 if (flag_branch_target_load_optimize2)
1953 rest_of_handle_branch_target_load_optimize ();
1955 #ifdef LEAF_REGISTERS
1956 current_function_uses_only_leaf_regs
1957 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
1958 #endif
1960 if (targetm.late_rtl_prologue_epilogue)
1961 rest_of_handle_prologue_epilogue ();
1963 #ifdef INSN_SCHEDULING
1964 if (optimize > 0 && flag_schedule_insns_after_reload)
1965 rest_of_handle_sched2 ();
1966 #endif
1968 #ifdef STACK_REGS
1969 rest_of_handle_stack_regs ();
1970 #endif
1972 compute_alignments ();
1974 if (flag_var_tracking)
1975 rest_of_handle_variable_tracking ();
1977 /* CFG is no longer maintained up-to-date. */
1978 free_bb_for_insn ();
1980 if (targetm.machine_dependent_reorg != 0)
1981 rest_of_handle_machine_reorg ();
1983 purge_line_number_notes (get_insns ());
1984 cleanup_barriers ();
1986 #ifdef DELAY_SLOTS
1987 if (flag_delayed_branch)
1988 rest_of_handle_delay_slots ();
1989 #endif
1991 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
1992 timevar_push (TV_SHORTEN_BRANCH);
1993 split_all_insns_noflow ();
1994 timevar_pop (TV_SHORTEN_BRANCH);
1995 #endif
1997 convert_to_eh_region_ranges ();
1999 rest_of_handle_shorten_branches ();
2001 set_nothrow_function_flags ();
2003 rest_of_handle_final ();
2005 exit_rest_of_compilation:
2007 rest_of_clean_state ();
2010 void
2011 init_optimization_passes (void)
2013 open_dump_file (DFI_cgraph, NULL);
2014 cgraph_dump_file = dump_file;
2015 dump_file = NULL;
2018 void
2019 finish_optimization_passes (void)
2021 timevar_push (TV_DUMP);
2022 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
2024 open_dump_file (DFI_bp, NULL);
2025 end_branch_prob ();
2026 close_dump_file (DFI_bp, NULL, NULL_RTX);
2029 if (optimize > 0 && open_dump_file (DFI_combine, NULL))
2031 dump_combine_total_stats (dump_file);
2032 close_dump_file (DFI_combine, NULL, NULL_RTX);
2035 dump_file = cgraph_dump_file;
2036 cgraph_dump_file = NULL;
2037 close_dump_file (DFI_cgraph, NULL, NULL_RTX);
2039 /* Do whatever is necessary to finish printing the graphs. */
2040 if (graph_dump_format != no_graph)
2042 int i;
2044 for (i = 0; i < (int) DFI_MAX; ++i)
2045 if (dump_file_tbl[i].initialized && dump_file_tbl[i].graph_dump_p)
2047 char seq[16];
2048 char *suffix;
2050 sprintf (seq, DUMPFILE_FORMAT, i);
2051 suffix = concat (seq, dump_file_tbl[i].extension, NULL);
2052 finish_graph_dump_file (dump_base_name, suffix);
2053 free (suffix);
2057 timevar_pop (TV_DUMP);
2060 bool
2061 enable_rtl_dump_file (int letter)
2063 bool matched = false;
2064 int i;
2066 if (letter == 'a')
2068 for (i = 0; i < (int) DFI_MAX; ++i)
2069 dump_file_tbl[i].enabled = 1;
2070 matched = true;
2072 else
2074 for (i = 0; i < (int) DFI_MAX; ++i)
2075 if (letter == dump_file_tbl[i].debug_switch)
2077 dump_file_tbl[i].enabled = 1;
2078 matched = true;
2082 return matched;
2085 struct tree_opt_pass pass_rest_of_compilation =
2087 "rest of compilation", /* name */
2088 NULL, /* gate */
2089 rest_of_compilation, /* execute */
2090 NULL, /* sub */
2091 NULL, /* next */
2092 0, /* static_pass_number */
2093 TV_REST_OF_COMPILATION, /* tv_id */
2094 PROP_rtl, /* properties_required */
2095 0, /* properties_provided */
2096 PROP_rtl, /* properties_destroyed */
2097 0, /* todo_flags_start */
2098 TODO_ggc_collect /* todo_flags_finish */