* config/arm/arm.c (emit_sfm): Only emit a single frame adjustment.
[official-gcc.git] / gcc / passes.c
blob4b50a1f5a4112cc43073e11de475a79e09b54736
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 (0);
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 unsigned int liveness_flags;
739 open_dump_file (DFI_bbro, current_function_decl);
741 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
742 splitting possibly introduced more crossjumping opportunities. */
743 liveness_flags = (!HAVE_conditional_execution ? CLEANUP_UPDATE_LIFE : 0);
744 changed = cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
746 if (flag_sched2_use_traces && flag_schedule_insns_after_reload)
747 tracer (liveness_flags);
748 if (flag_reorder_blocks || flag_reorder_blocks_and_partition)
749 reorder_basic_blocks (liveness_flags);
750 if (flag_reorder_blocks || flag_reorder_blocks_and_partition
751 || (flag_sched2_use_traces && flag_schedule_insns_after_reload))
752 changed |= cleanup_cfg (CLEANUP_EXPENSIVE | liveness_flags);
754 /* On conditional execution targets we can not update the life cheaply, so
755 we deffer the updating to after both cleanups. This may lose some cases
756 but should not be terribly bad. */
757 if (changed && HAVE_conditional_execution)
758 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
759 PROP_DEATH_NOTES);
760 close_dump_file (DFI_bbro, print_rtl_with_bb, get_insns ());
763 /* Partition hot and cold basic blocks. */
764 static void
765 rest_of_handle_partition_blocks (void)
767 no_new_pseudos = 0;
768 partition_hot_cold_basic_blocks ();
769 allocate_reg_life_data ();
770 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
771 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
772 no_new_pseudos = 1;
775 #ifdef INSN_SCHEDULING
776 /* Run instruction scheduler. */
777 /* Perform SMS module scheduling. */
778 static void
779 rest_of_handle_sms (void)
781 timevar_push (TV_SMS);
782 open_dump_file (DFI_sms, current_function_decl);
784 /* We want to be able to create new pseudos. */
785 no_new_pseudos = 0;
786 sms_schedule (dump_file);
787 close_dump_file (DFI_sms, print_rtl, get_insns ());
790 /* Update the life information, because we add pseudos. */
791 max_regno = max_reg_num ();
792 allocate_reg_info (max_regno, FALSE, FALSE);
793 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
794 (PROP_DEATH_NOTES
795 | PROP_KILL_DEAD_CODE
796 | PROP_SCAN_DEAD_CODE));
797 no_new_pseudos = 1;
799 ggc_collect ();
800 timevar_pop (TV_SMS);
803 /* Run instruction scheduler. */
804 static void
805 rest_of_handle_sched (void)
807 timevar_push (TV_SCHED);
809 /* Print function header into sched dump now
810 because doing the sched analysis makes some of the dump. */
811 open_dump_file (DFI_sched, current_function_decl);
813 /* Do control and data sched analysis,
814 and write some of the results to dump file. */
816 schedule_insns (dump_file);
818 close_dump_file (DFI_sched, print_rtl_with_bb, get_insns ());
820 ggc_collect ();
821 timevar_pop (TV_SCHED);
824 /* Run second scheduling pass after reload. */
825 static void
826 rest_of_handle_sched2 (void)
828 timevar_push (TV_SCHED2);
829 open_dump_file (DFI_sched2, current_function_decl);
831 /* Do control and data sched analysis again,
832 and write some more of the results to dump file. */
834 split_all_insns (1);
836 if (flag_sched2_use_superblocks || flag_sched2_use_traces)
838 schedule_ebbs (dump_file);
839 /* No liveness updating code yet, but it should be easy to do.
840 reg-stack recomputes the liveness when needed for now. */
841 count_or_remove_death_notes (NULL, 1);
842 cleanup_cfg (CLEANUP_EXPENSIVE);
844 else
845 schedule_insns (dump_file);
847 close_dump_file (DFI_sched2, print_rtl_with_bb, get_insns ());
849 ggc_collect ();
851 timevar_pop (TV_SCHED2);
853 #endif
855 static void
856 rest_of_handle_gcse2 (void)
858 timevar_push (TV_RELOAD_CSE_REGS);
859 open_dump_file (DFI_gcse2, current_function_decl);
861 gcse_after_reload_main (get_insns (), dump_file);
862 rebuild_jump_labels (get_insns ());
863 delete_trivially_dead_insns (get_insns (), max_reg_num ());
864 close_dump_file (DFI_gcse2, print_rtl_with_bb, get_insns ());
866 ggc_collect ();
868 #ifdef ENABLE_CHECKING
869 verify_flow_info ();
870 #endif
872 timevar_pop (TV_RELOAD_CSE_REGS);
875 /* Register allocation pre-pass, to reduce number of moves necessary
876 for two-address machines. */
877 static void
878 rest_of_handle_regmove (void)
880 timevar_push (TV_REGMOVE);
881 open_dump_file (DFI_regmove, current_function_decl);
883 regmove_optimize (get_insns (), max_reg_num (), dump_file);
885 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
886 close_dump_file (DFI_regmove, print_rtl_with_bb, get_insns ());
888 ggc_collect ();
889 timevar_pop (TV_REGMOVE);
892 /* Run tracer. */
893 static void
894 rest_of_handle_tracer (void)
896 open_dump_file (DFI_tracer, current_function_decl);
897 if (dump_file)
898 dump_flow_info (dump_file);
899 tracer (0);
900 cleanup_cfg (CLEANUP_EXPENSIVE);
901 reg_scan (get_insns (), max_reg_num (), 0);
902 close_dump_file (DFI_tracer, print_rtl_with_bb, get_insns ());
905 /* If-conversion and CFG cleanup. */
906 static void
907 rest_of_handle_if_conversion (void)
909 timevar_push (TV_IFCVT);
910 open_dump_file (DFI_ce1, current_function_decl);
912 if (flag_if_conversion)
914 if (dump_file)
915 dump_flow_info (dump_file);
916 cleanup_cfg (CLEANUP_EXPENSIVE);
917 reg_scan (get_insns (), max_reg_num (), 0);
918 if_convert (0);
921 timevar_push (TV_JUMP);
922 cleanup_cfg (CLEANUP_EXPENSIVE);
923 reg_scan (get_insns (), max_reg_num (), 0);
924 timevar_pop (TV_JUMP);
926 close_dump_file (DFI_ce1, print_rtl_with_bb, get_insns ());
927 timevar_pop (TV_IFCVT);
930 /* Rerun if-conversion, as combine may have simplified things enough
931 to now meet sequence length restrictions. */
932 static void
933 rest_of_handle_if_after_combine (void)
935 timevar_push (TV_IFCVT);
936 open_dump_file (DFI_ce2, current_function_decl);
938 no_new_pseudos = 0;
939 if_convert (1);
940 no_new_pseudos = 1;
942 close_dump_file (DFI_ce2, print_rtl_with_bb, get_insns ());
943 timevar_pop (TV_IFCVT);
946 static void
947 rest_of_handle_if_after_reload (void)
949 timevar_push (TV_IFCVT2);
950 open_dump_file (DFI_ce3, current_function_decl);
952 /* Last attempt to optimize CFG, as scheduling, peepholing and insn
953 splitting possibly introduced more crossjumping opportunities. */
954 cleanup_cfg (CLEANUP_EXPENSIVE
955 | CLEANUP_UPDATE_LIFE
956 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
957 if (flag_if_conversion2)
958 if_convert (1);
959 close_dump_file (DFI_ce3, print_rtl_with_bb, get_insns ());
960 timevar_pop (TV_IFCVT2);
963 static void
964 rest_of_handle_web (void)
966 open_dump_file (DFI_web, current_function_decl);
967 timevar_push (TV_WEB);
968 web_main ();
969 delete_trivially_dead_insns (get_insns (), max_reg_num ());
970 cleanup_cfg (CLEANUP_EXPENSIVE);
972 timevar_pop (TV_WEB);
973 close_dump_file (DFI_web, print_rtl_with_bb, get_insns ());
974 reg_scan (get_insns (), max_reg_num (), 0);
977 /* Do branch profiling and static profile estimation passes. */
978 static void
979 rest_of_handle_branch_prob (void)
981 struct loops loops;
983 timevar_push (TV_BRANCH_PROB);
984 open_dump_file (DFI_bp, current_function_decl);
986 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
987 branch_prob ();
989 /* Discover and record the loop depth at the head of each basic
990 block. The loop infrastructure does the real job for us. */
991 flow_loops_find (&loops, LOOP_TREE);
993 if (dump_file)
994 flow_loops_dump (&loops, dump_file, NULL, 0);
996 /* Estimate using heuristics if no profiling info is available. */
997 if (flag_guess_branch_prob)
998 estimate_probability (&loops);
1000 flow_loops_free (&loops);
1001 free_dominance_info (CDI_DOMINATORS);
1002 close_dump_file (DFI_bp, print_rtl_with_bb, get_insns ());
1003 timevar_pop (TV_BRANCH_PROB);
1006 /* Do optimizations based on expression value profiles. */
1007 static void
1008 rest_of_handle_value_profile_transformations (void)
1010 open_dump_file (DFI_vpt, current_function_decl);
1011 timevar_push (TV_VPT);
1013 if (value_profile_transformations ())
1014 cleanup_cfg (CLEANUP_EXPENSIVE);
1016 timevar_pop (TV_VPT);
1017 close_dump_file (DFI_vpt, print_rtl_with_bb, get_insns ());
1020 /* Do control and data flow analysis; write some of the results to the
1021 dump file. */
1022 static void
1023 rest_of_handle_cfg (void)
1025 open_dump_file (DFI_cfg, current_function_decl);
1026 if (dump_file)
1027 dump_flow_info (dump_file);
1028 if (optimize)
1029 cleanup_cfg (CLEANUP_EXPENSIVE
1030 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1032 /* It may make more sense to mark constant functions after dead code is
1033 eliminated by life_analysis, but we need to do it early, as -fprofile-arcs
1034 may insert code making function non-constant, but we still must consider
1035 it as constant, otherwise -fbranch-probabilities will not read data back.
1037 life_analysis rarely eliminates modification of external memory.
1039 if (optimize)
1041 /* Alias analysis depends on this information and mark_constant_function
1042 depends on alias analysis. */
1043 reg_scan (get_insns (), max_reg_num (), 1);
1044 mark_constant_function ();
1047 close_dump_file (DFI_cfg, print_rtl_with_bb, get_insns ());
1050 /* Perform jump bypassing and control flow optimizations. */
1051 static void
1052 rest_of_handle_jump_bypass (void)
1054 timevar_push (TV_BYPASS);
1055 open_dump_file (DFI_bypass, current_function_decl);
1057 cleanup_cfg (CLEANUP_EXPENSIVE);
1058 reg_scan (get_insns (), max_reg_num (), 1);
1060 if (bypass_jumps (dump_file))
1062 rebuild_jump_labels (get_insns ());
1063 cleanup_cfg (CLEANUP_EXPENSIVE);
1064 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1067 close_dump_file (DFI_bypass, print_rtl_with_bb, get_insns ());
1068 timevar_pop (TV_BYPASS);
1070 ggc_collect ();
1072 #ifdef ENABLE_CHECKING
1073 verify_flow_info ();
1074 #endif
1077 /* Try combining insns through substitution. */
1078 static void
1079 rest_of_handle_combine (void)
1081 int rebuild_jump_labels_after_combine = 0;
1083 timevar_push (TV_COMBINE);
1084 open_dump_file (DFI_combine, current_function_decl);
1086 rebuild_jump_labels_after_combine
1087 = combine_instructions (get_insns (), max_reg_num ());
1089 /* Combining insns may have turned an indirect jump into a
1090 direct jump. Rebuild the JUMP_LABEL fields of jumping
1091 instructions. */
1092 if (rebuild_jump_labels_after_combine)
1094 timevar_push (TV_JUMP);
1095 rebuild_jump_labels (get_insns ());
1096 timevar_pop (TV_JUMP);
1098 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
1101 close_dump_file (DFI_combine, print_rtl_with_bb, get_insns ());
1102 timevar_pop (TV_COMBINE);
1104 ggc_collect ();
1107 /* Perform life analysis. */
1108 static void
1109 rest_of_handle_life (void)
1111 open_dump_file (DFI_life, current_function_decl);
1112 regclass_init ();
1114 #ifdef ENABLE_CHECKING
1115 verify_flow_info ();
1116 #endif
1117 life_analysis (dump_file, PROP_FINAL);
1118 if (optimize)
1119 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_UPDATE_LIFE
1120 | CLEANUP_LOG_LINKS
1121 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1123 if (extra_warnings)
1125 setjmp_vars_warning (DECL_INITIAL (current_function_decl));
1126 setjmp_args_warning ();
1129 if (optimize)
1131 if (!flag_new_regalloc && initialize_uninitialized_subregs ())
1133 /* Insns were inserted, and possibly pseudos created, so
1134 things might look a bit different. */
1135 allocate_reg_life_data ();
1136 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1137 PROP_LOG_LINKS | PROP_REG_INFO | PROP_DEATH_NOTES);
1141 no_new_pseudos = 1;
1143 close_dump_file (DFI_life, print_rtl_with_bb, get_insns ());
1145 ggc_collect ();
1148 /* Perform common subexpression elimination. Nonzero value from
1149 `cse_main' means that jumps were simplified and some code may now
1150 be unreachable, so do jump optimization again. */
1151 static void
1152 rest_of_handle_cse (void)
1154 int tem;
1156 open_dump_file (DFI_cse, current_function_decl);
1157 if (dump_file)
1158 dump_flow_info (dump_file);
1159 timevar_push (TV_CSE);
1161 reg_scan (get_insns (), max_reg_num (), 1);
1163 tem = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1164 if (tem)
1165 rebuild_jump_labels (get_insns ());
1166 if (purge_all_dead_edges (0))
1167 delete_unreachable_blocks ();
1169 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1171 /* If we are not running more CSE passes, then we are no longer
1172 expecting CSE to be run. But always rerun it in a cheap mode. */
1173 cse_not_expected = !flag_rerun_cse_after_loop && !flag_gcse;
1175 if (tem || optimize > 1)
1176 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1178 timevar_pop (TV_CSE);
1179 close_dump_file (DFI_cse, print_rtl_with_bb, get_insns ());
1181 ggc_collect ();
1184 /* Run second CSE pass after loop optimizations. */
1185 static void
1186 rest_of_handle_cse2 (void)
1188 int tem;
1190 timevar_push (TV_CSE2);
1191 open_dump_file (DFI_cse2, current_function_decl);
1192 if (dump_file)
1193 dump_flow_info (dump_file);
1194 /* CFG is no longer maintained up-to-date. */
1195 tem = cse_main (get_insns (), max_reg_num (), 1, dump_file);
1197 /* Run a pass to eliminate duplicated assignments to condition code
1198 registers. We have to run this after bypass_jumps, because it
1199 makes it harder for that pass to determine whether a jump can be
1200 bypassed safely. */
1201 cse_condition_code_reg ();
1203 purge_all_dead_edges (0);
1204 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1206 if (tem)
1208 timevar_push (TV_JUMP);
1209 rebuild_jump_labels (get_insns ());
1210 cleanup_cfg (CLEANUP_EXPENSIVE);
1211 timevar_pop (TV_JUMP);
1213 reg_scan (get_insns (), max_reg_num (), 0);
1214 close_dump_file (DFI_cse2, print_rtl_with_bb, get_insns ());
1215 timevar_pop (TV_CSE2);
1217 ggc_collect ();
1220 /* Perform global cse. */
1221 static void
1222 rest_of_handle_gcse (void)
1224 int save_csb, save_cfj;
1225 int tem2 = 0, tem;
1227 timevar_push (TV_GCSE);
1228 open_dump_file (DFI_gcse, current_function_decl);
1230 tem = gcse_main (get_insns (), dump_file);
1231 rebuild_jump_labels (get_insns ());
1232 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1234 save_csb = flag_cse_skip_blocks;
1235 save_cfj = flag_cse_follow_jumps;
1236 flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
1238 /* If -fexpensive-optimizations, re-run CSE to clean up things done
1239 by gcse. */
1240 if (flag_expensive_optimizations)
1242 timevar_push (TV_CSE);
1243 reg_scan (get_insns (), max_reg_num (), 1);
1244 tem2 = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1245 purge_all_dead_edges (0);
1246 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1247 timevar_pop (TV_CSE);
1248 cse_not_expected = !flag_rerun_cse_after_loop;
1251 /* If gcse or cse altered any jumps, rerun jump optimizations to clean
1252 things up. Then possibly re-run CSE again. */
1253 while (tem || tem2)
1255 tem = tem2 = 0;
1256 timevar_push (TV_JUMP);
1257 rebuild_jump_labels (get_insns ());
1258 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1259 timevar_pop (TV_JUMP);
1261 if (flag_expensive_optimizations)
1263 timevar_push (TV_CSE);
1264 reg_scan (get_insns (), max_reg_num (), 1);
1265 tem2 = cse_main (get_insns (), max_reg_num (), 0, dump_file);
1266 purge_all_dead_edges (0);
1267 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1268 timevar_pop (TV_CSE);
1272 close_dump_file (DFI_gcse, print_rtl_with_bb, get_insns ());
1273 timevar_pop (TV_GCSE);
1275 ggc_collect ();
1276 flag_cse_skip_blocks = save_csb;
1277 flag_cse_follow_jumps = save_cfj;
1278 #ifdef ENABLE_CHECKING
1279 verify_flow_info ();
1280 #endif
1283 /* Move constant computations out of loops. */
1284 static void
1285 rest_of_handle_loop_optimize (void)
1287 int do_unroll, do_prefetch;
1289 timevar_push (TV_LOOP);
1290 delete_dead_jumptables ();
1291 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1292 open_dump_file (DFI_loop, current_function_decl);
1294 /* CFG is no longer maintained up-to-date. */
1295 free_bb_for_insn ();
1297 if (flag_unroll_loops)
1298 do_unroll = LOOP_AUTO_UNROLL; /* Having two unrollers is useless. */
1299 else
1300 do_unroll = flag_old_unroll_loops ? LOOP_UNROLL : LOOP_AUTO_UNROLL;
1301 do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
1303 if (flag_rerun_loop_opt)
1305 cleanup_barriers ();
1307 /* We only want to perform unrolling once. */
1308 loop_optimize (get_insns (), dump_file, do_unroll);
1309 do_unroll = 0;
1311 /* The first call to loop_optimize makes some instructions
1312 trivially dead. We delete those instructions now in the
1313 hope that doing so will make the heuristics in loop work
1314 better and possibly speed up compilation. */
1315 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1317 /* The regscan pass is currently necessary as the alias
1318 analysis code depends on this information. */
1319 reg_scan (get_insns (), max_reg_num (), 1);
1321 cleanup_barriers ();
1322 loop_optimize (get_insns (), dump_file, do_unroll | do_prefetch);
1324 /* Loop can create trivially dead instructions. */
1325 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1326 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1327 close_dump_file (DFI_loop, print_rtl, get_insns ());
1328 timevar_pop (TV_LOOP);
1330 ggc_collect ();
1333 /* Perform loop optimizations. It might be better to do them a bit
1334 sooner, but we want the profile feedback to work more
1335 efficiently. */
1336 static void
1337 rest_of_handle_loop2 (void)
1339 struct loops *loops;
1340 basic_block bb;
1342 if (!flag_move_loop_invariants
1343 && !flag_unswitch_loops
1344 && !flag_peel_loops
1345 && !flag_unroll_loops
1346 && !flag_branch_on_count_reg)
1347 return;
1349 timevar_push (TV_LOOP);
1350 open_dump_file (DFI_loop2, current_function_decl);
1351 if (dump_file)
1352 dump_flow_info (dump_file);
1354 /* Initialize structures for layout changes. */
1355 cfg_layout_initialize (0);
1357 loops = loop_optimizer_init (dump_file);
1359 if (loops)
1361 /* The optimizations: */
1362 if (flag_move_loop_invariants)
1363 move_loop_invariants (loops);
1365 if (flag_unswitch_loops)
1366 unswitch_loops (loops);
1368 if (flag_peel_loops || flag_unroll_loops)
1369 unroll_and_peel_loops (loops,
1370 (flag_peel_loops ? UAP_PEEL : 0) |
1371 (flag_unroll_loops ? UAP_UNROLL : 0) |
1372 (flag_unroll_all_loops ? UAP_UNROLL_ALL : 0));
1374 #ifdef HAVE_doloop_end
1375 if (flag_branch_on_count_reg && HAVE_doloop_end)
1376 doloop_optimize_loops (loops);
1377 #endif /* HAVE_doloop_end */
1379 loop_optimizer_finalize (loops, dump_file);
1382 free_dominance_info (CDI_DOMINATORS);
1384 /* Finalize layout changes. */
1385 FOR_EACH_BB (bb)
1386 if (bb->next_bb != EXIT_BLOCK_PTR)
1387 bb->rbi->next = bb->next_bb;
1388 cfg_layout_finalize ();
1390 cleanup_cfg (CLEANUP_EXPENSIVE);
1391 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1392 reg_scan (get_insns (), max_reg_num (), 0);
1393 if (dump_file)
1394 dump_flow_info (dump_file);
1395 close_dump_file (DFI_loop2, print_rtl_with_bb, get_insns ());
1396 timevar_pop (TV_LOOP);
1397 ggc_collect ();
1400 static void
1401 rest_of_handle_branch_target_load_optimize (void)
1403 static int warned = 0;
1405 /* Leave this a warning for now so that it is possible to experiment
1406 with running this pass twice. In 3.6, we should either make this
1407 an error, or use separate dump files. */
1408 if (flag_branch_target_load_optimize
1409 && flag_branch_target_load_optimize2
1410 && !warned)
1412 warning ("branch target register load optimization is not intended "
1413 "to be run twice");
1415 warned = 1;
1418 open_dump_file (DFI_branch_target_load, current_function_decl);
1419 branch_target_load_optimize (epilogue_completed);
1420 close_dump_file (DFI_branch_target_load, print_rtl_with_bb, get_insns ());
1421 ggc_collect ();
1424 #ifdef OPTIMIZE_MODE_SWITCHING
1425 static void
1426 rest_of_handle_mode_switching (void)
1428 timevar_push (TV_MODE_SWITCH);
1430 no_new_pseudos = 0;
1431 optimize_mode_switching (NULL);
1432 no_new_pseudos = 1;
1434 timevar_pop (TV_MODE_SWITCH);
1436 #endif
1438 static void
1439 rest_of_handle_jump (void)
1441 ggc_collect ();
1443 timevar_push (TV_JUMP);
1444 open_dump_file (DFI_sibling, current_function_decl);
1446 /* ??? We may get caled either via tree_rest_of_compilation when the CFG
1447 is already built or directly (for instance from coverage code).
1448 The direct callers shall be updated. */
1449 if (!basic_block_info)
1451 init_flow ();
1452 rebuild_jump_labels (get_insns ());
1453 find_exception_handler_labels ();
1454 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1457 /* ??? We may get called either via tree_rest_of_compilation when the CFG
1458 is already built or directly (for instance from coverage code).
1459 The direct callers shall be updated. */
1460 if (!basic_block_info)
1462 init_flow ();
1463 rebuild_jump_labels (get_insns ());
1464 find_exception_handler_labels ();
1465 find_basic_blocks (get_insns (), max_reg_num (), dump_file);
1467 delete_unreachable_blocks ();
1468 #ifdef ENABLE_CHECKING
1469 verify_flow_info ();
1470 #endif
1471 timevar_pop (TV_JUMP);
1474 static void
1475 rest_of_handle_eh (void)
1477 insn_locators_initialize ();
1478 /* Complete generation of exception handling code. */
1479 if (doing_eh (0))
1481 timevar_push (TV_JUMP);
1482 open_dump_file (DFI_eh, current_function_decl);
1484 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1486 finish_eh_generation ();
1488 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
1490 close_dump_file (DFI_eh, print_rtl, get_insns ());
1491 timevar_pop (TV_JUMP);
1496 static void
1497 rest_of_handle_prologue_epilogue (void)
1499 if (optimize && !flow2_completed)
1500 cleanup_cfg (CLEANUP_EXPENSIVE);
1502 /* On some machines, the prologue and epilogue code, or parts thereof,
1503 can be represented as RTL. Doing so lets us schedule insns between
1504 it and the rest of the code and also allows delayed branch
1505 scheduling to operate in the epilogue. */
1506 thread_prologue_and_epilogue_insns (get_insns ());
1507 epilogue_completed = 1;
1509 if (optimize && flow2_completed)
1510 life_analysis (dump_file, PROP_POSTRELOAD);
1513 static void
1514 rest_of_handle_stack_adjustments (void)
1516 life_analysis (dump_file, PROP_POSTRELOAD);
1517 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE
1518 | (flag_crossjumping ? CLEANUP_CROSSJUMP : 0));
1520 /* This is kind of a heuristic. We need to run combine_stack_adjustments
1521 even for machines with possibly nonzero RETURN_POPS_ARGS
1522 and ACCUMULATE_OUTGOING_ARGS. We expect that only ports having
1523 push instructions will have popping returns. */
1524 #ifndef PUSH_ROUNDING
1525 if (!ACCUMULATE_OUTGOING_ARGS)
1526 #endif
1527 combine_stack_adjustments ();
1530 static void
1531 rest_of_handle_flow2 (void)
1533 timevar_push (TV_FLOW2);
1534 open_dump_file (DFI_flow2, current_function_decl);
1536 /* Re-create the death notes which were deleted during reload. */
1537 #ifdef ENABLE_CHECKING
1538 verify_flow_info ();
1539 #endif
1541 /* If optimizing, then go ahead and split insns now. */
1542 #ifndef STACK_REGS
1543 if (optimize > 0)
1544 #endif
1545 split_all_insns (0);
1547 if (flag_branch_target_load_optimize)
1548 rest_of_handle_branch_target_load_optimize ();
1550 if (!targetm.late_rtl_prologue_epilogue)
1551 rest_of_handle_prologue_epilogue ();
1553 if (optimize)
1554 rest_of_handle_stack_adjustments ();
1556 flow2_completed = 1;
1558 close_dump_file (DFI_flow2, print_rtl_with_bb, get_insns ());
1559 timevar_pop (TV_FLOW2);
1561 ggc_collect ();
1565 static void
1566 rest_of_handle_jump2 (void)
1568 open_dump_file (DFI_jump, current_function_decl);
1570 /* Always do one jump optimization pass to ensure that JUMP_LABEL fields
1571 are initialized and to compute whether control can drop off the end
1572 of the function. */
1574 timevar_push (TV_JUMP);
1575 /* Turn NOTE_INSN_EXPECTED_VALUE into REG_BR_PROB. Do this
1576 before jump optimization switches branch directions. */
1577 if (flag_guess_branch_prob)
1578 expected_value_to_br_prob ();
1580 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1581 reg_scan (get_insns (), max_reg_num (), 0);
1582 if (dump_file)
1583 dump_flow_info (dump_file);
1584 cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0) | CLEANUP_PRE_LOOP
1585 | (flag_thread_jumps ? CLEANUP_THREADING : 0));
1587 create_loop_notes ();
1589 purge_line_number_notes (get_insns ());
1591 if (optimize)
1592 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_PRE_LOOP);
1594 /* Jump optimization, and the removal of NULL pointer checks, may
1595 have reduced the number of instructions substantially. CSE, and
1596 future passes, allocate arrays whose dimensions involve the
1597 maximum instruction UID, so if we can reduce the maximum UID
1598 we'll save big on memory. */
1599 renumber_insns (dump_file);
1601 close_dump_file (DFI_jump, print_rtl_with_bb, get_insns ());
1602 timevar_pop (TV_JUMP);
1604 ggc_collect ();
1607 #ifdef HAVE_peephole2
1608 static void
1609 rest_of_handle_peephole2 (void)
1611 timevar_push (TV_PEEPHOLE2);
1612 open_dump_file (DFI_peephole2, current_function_decl);
1614 peephole2_optimize (dump_file);
1616 close_dump_file (DFI_peephole2, print_rtl_with_bb, get_insns ());
1617 timevar_pop (TV_PEEPHOLE2);
1619 #endif
1621 static void
1622 rest_of_handle_postreload (void)
1624 timevar_push (TV_RELOAD_CSE_REGS);
1625 open_dump_file (DFI_postreload, current_function_decl);
1627 /* Do a very simple CSE pass over just the hard registers. */
1628 reload_cse_regs (get_insns ());
1629 /* reload_cse_regs can eliminate potentially-trapping MEMs.
1630 Remove any EH edges associated with them. */
1631 if (flag_non_call_exceptions)
1632 purge_all_dead_edges (0);
1634 close_dump_file (DFI_postreload, print_rtl_with_bb, get_insns ());
1635 timevar_pop (TV_RELOAD_CSE_REGS);
1638 static void
1639 rest_of_handle_shorten_branches (void)
1641 /* Shorten branches. */
1642 timevar_push (TV_SHORTEN_BRANCH);
1643 shorten_branches (get_insns ());
1644 timevar_pop (TV_SHORTEN_BRANCH);
1647 static void
1648 rest_of_clean_state (void)
1650 coverage_end_function ();
1652 /* In case the function was not output,
1653 don't leave any temporary anonymous types
1654 queued up for sdb output. */
1655 #ifdef SDB_DEBUGGING_INFO
1656 if (write_symbols == SDB_DEBUG)
1657 sdbout_types (NULL_TREE);
1658 #endif
1660 reload_completed = 0;
1661 epilogue_completed = 0;
1662 flow2_completed = 0;
1663 no_new_pseudos = 0;
1665 timevar_push (TV_FINAL);
1667 /* Clear out the insn_length contents now that they are no
1668 longer valid. */
1669 init_insn_lengths ();
1671 /* Show no temporary slots allocated. */
1672 init_temp_slots ();
1674 free_basic_block_vars ();
1675 free_bb_for_insn ();
1677 timevar_pop (TV_FINAL);
1679 if (targetm.binds_local_p (current_function_decl))
1681 int pref = cfun->preferred_stack_boundary;
1682 if (cfun->recursive_call_emit
1683 && cfun->stack_alignment_needed > cfun->preferred_stack_boundary)
1684 pref = cfun->stack_alignment_needed;
1685 cgraph_rtl_info (current_function_decl)->preferred_incoming_stack_boundary
1686 = pref;
1689 /* Make sure volatile mem refs aren't considered valid operands for
1690 arithmetic insns. We must call this here if this is a nested inline
1691 function, since the above code leaves us in the init_recog state
1692 (from final.c), and the function context push/pop code does not
1693 save/restore volatile_ok.
1695 ??? Maybe it isn't necessary for expand_start_function to call this
1696 anymore if we do it here? */
1698 init_recog_no_volatile ();
1700 /* We're done with this function. Free up memory if we can. */
1701 free_after_parsing (cfun);
1705 /* This function is called from the pass manager in tree-optimize.c
1706 after all tree passes have finished for a single function, and we
1707 have expanded the function body from trees to RTL.
1708 Once we are here, we have decided that we're supposed to output
1709 that function, ie. that we should write assembler code for it.
1711 We run a series of low-level passes here on the function's RTL
1712 representation. Each pass is called via a rest_of_* function. */
1714 void
1715 rest_of_compilation (void)
1717 /* There's no need to defer outputting this function any more; we
1718 know we want to output it. */
1719 DECL_DEFER_OUTPUT (current_function_decl) = 0;
1721 /* Now that we're done expanding trees to RTL, we shouldn't have any
1722 more CONCATs anywhere. */
1723 generating_concat_p = 0;
1725 /* When processing delayed functions, prepare_function_start () won't
1726 have been run to re-initialize it. */
1727 cse_not_expected = ! optimize;
1729 finalize_block_changes ();
1731 /* Dump the rtl code if we are dumping rtl. */
1732 if (open_dump_file (DFI_rtl, current_function_decl))
1733 close_dump_file (DFI_rtl, print_rtl, get_insns ());
1735 /* Convert from NOTE_INSN_EH_REGION style notes, and do other
1736 sorts of eh initialization. Delay this until after the
1737 initial rtl dump so that we can see the original nesting. */
1738 convert_from_eh_region_ranges ();
1740 /* If we're emitting a nested function, make sure its parent gets
1741 emitted as well. Doing otherwise confuses debug info. */
1743 tree parent;
1744 for (parent = DECL_CONTEXT (current_function_decl);
1745 parent != NULL_TREE;
1746 parent = get_containing_scope (parent))
1747 if (TREE_CODE (parent) == FUNCTION_DECL)
1748 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1751 /* We are now committed to emitting code for this function. Do any
1752 preparation, such as emitting abstract debug info for the inline
1753 before it gets mangled by optimization. */
1754 if (cgraph_function_possibly_inlined_p (current_function_decl))
1755 (*debug_hooks->outlining_inline_function) (current_function_decl);
1757 /* Remove any notes we don't need. That will make iterating
1758 over the instruction sequence faster, and allow the garbage
1759 collector to reclaim the memory used by the notes. */
1760 remove_unnecessary_notes ();
1762 /* Initialize some variables used by the optimizers. */
1763 init_function_for_compilation ();
1765 TREE_ASM_WRITTEN (current_function_decl) = 1;
1767 /* Early return if there were errors. We can run afoul of our
1768 consistency checks, and there's not really much point in fixing them. */
1769 if (rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount)
1770 goto exit_rest_of_compilation;
1772 rest_of_handle_jump ();
1774 if (cfun->tail_call_emit)
1775 fixup_tail_calls ();
1777 rest_of_handle_eh ();
1779 /* Delay emitting hard_reg_initial_value sets until after EH landing pad
1780 generation, which might create new sets. */
1781 emit_initial_value_sets ();
1783 #ifdef FINALIZE_PIC
1784 /* If we are doing position-independent code generation, now
1785 is the time to output special prologues and epilogues.
1786 We do not want to do this earlier, because it just clutters
1787 up inline functions with meaningless insns. */
1788 if (flag_pic)
1789 FINALIZE_PIC;
1790 #endif
1792 /* Copy any shared structure that should not be shared. */
1793 unshare_all_rtl ();
1795 #ifdef SETJMP_VIA_SAVE_AREA
1796 /* This must be performed before virtual register instantiation.
1797 Please be aware that everything in the compiler that can look
1798 at the RTL up to this point must understand that REG_SAVE_AREA
1799 is just like a use of the REG contained inside. */
1800 if (current_function_calls_alloca)
1801 optimize_save_area_alloca ();
1802 #endif
1804 /* Instantiate all virtual registers. */
1805 instantiate_virtual_regs ();
1807 rest_of_handle_jump2 ();
1809 if (optimize > 0)
1810 rest_of_handle_cse ();
1812 if (optimize > 0)
1814 if (flag_gcse)
1815 rest_of_handle_gcse ();
1817 if (flag_loop_optimize)
1818 rest_of_handle_loop_optimize ();
1820 if (flag_gcse)
1821 rest_of_handle_jump_bypass ();
1824 timevar_push (TV_FLOW);
1825 rest_of_handle_cfg ();
1827 if (!flag_tree_based_profiling
1828 && (optimize > 0 || profile_arc_flag
1829 || flag_test_coverage || flag_branch_probabilities))
1831 rtl_register_profile_hooks ();
1832 rtl_register_value_prof_hooks ();
1833 rest_of_handle_branch_prob ();
1835 if (flag_branch_probabilities
1836 && flag_profile_values
1837 && flag_value_profile_transformations)
1838 rest_of_handle_value_profile_transformations ();
1840 /* Remove the death notes created for vpt. */
1841 if (flag_profile_values)
1842 count_or_remove_death_notes (NULL, 1);
1845 if (optimize > 0)
1846 rest_of_handle_if_conversion ();
1848 if (optimize > 0 && flag_tracer)
1849 rest_of_handle_tracer ();
1851 if (optimize > 0
1852 && flag_loop_optimize2)
1853 rest_of_handle_loop2 ();
1855 if (optimize > 0 && flag_web)
1856 rest_of_handle_web ();
1858 if (optimize > 0 && flag_rerun_cse_after_loop)
1859 rest_of_handle_cse2 ();
1861 cse_not_expected = 1;
1863 rest_of_handle_life ();
1864 timevar_pop (TV_FLOW);
1866 if (optimize > 0)
1867 rest_of_handle_combine ();
1869 if (optimize > 0 && flag_if_conversion)
1870 rest_of_handle_if_after_combine ();
1872 /* The optimization to partition hot/cold basic blocks into separate
1873 sections of the .o file does not work well with exception handling.
1874 Don't call it if there are exceptions. */
1876 if (optimize > 0 && flag_reorder_blocks_and_partition && !flag_exceptions)
1877 rest_of_handle_partition_blocks ();
1879 if (optimize > 0 && (flag_regmove || flag_expensive_optimizations))
1880 rest_of_handle_regmove ();
1882 /* Do unconditional splitting before register allocation to allow machine
1883 description to add extra information not needed previously. */
1884 split_all_insns (1);
1886 #ifdef OPTIMIZE_MODE_SWITCHING
1887 rest_of_handle_mode_switching ();
1888 #endif
1890 /* Any of the several passes since flow1 will have munged register
1891 lifetime data a bit. We need it to be up to date for scheduling
1892 (see handling of reg_known_equiv in init_alias_analysis). */
1893 recompute_reg_usage (get_insns (), !optimize_size);
1895 #ifdef INSN_SCHEDULING
1896 if (optimize > 0 && flag_modulo_sched)
1897 rest_of_handle_sms ();
1899 if (flag_schedule_insns)
1900 rest_of_handle_sched ();
1901 #endif
1903 /* Determine if the current function is a leaf before running reload
1904 since this can impact optimizations done by the prologue and
1905 epilogue thus changing register elimination offsets. */
1906 current_function_is_leaf = leaf_function_p ();
1908 if (flag_new_regalloc)
1910 if (rest_of_handle_new_regalloc ())
1911 goto exit_rest_of_compilation;
1913 else
1915 if (rest_of_handle_old_regalloc ())
1916 goto exit_rest_of_compilation;
1919 if (optimize > 0)
1920 rest_of_handle_postreload ();
1922 if (optimize > 0 && flag_gcse_after_reload)
1923 rest_of_handle_gcse2 ();
1925 rest_of_handle_flow2 ();
1927 #ifdef HAVE_peephole2
1928 if (optimize > 0 && flag_peephole2)
1929 rest_of_handle_peephole2 ();
1930 #endif
1932 if (optimize > 0)
1933 rest_of_handle_if_after_reload ();
1935 if (optimize > 0)
1937 if (flag_rename_registers || flag_cprop_registers)
1938 rest_of_handle_regrename ();
1940 rest_of_handle_reorder_blocks ();
1943 if (flag_branch_target_load_optimize2)
1944 rest_of_handle_branch_target_load_optimize ();
1946 #ifdef LEAF_REGISTERS
1947 current_function_uses_only_leaf_regs
1948 = optimize > 0 && only_leaf_regs_used () && leaf_function_p ();
1949 #endif
1951 if (targetm.late_rtl_prologue_epilogue)
1952 rest_of_handle_prologue_epilogue ();
1954 #ifdef INSN_SCHEDULING
1955 if (optimize > 0 && flag_schedule_insns_after_reload)
1956 rest_of_handle_sched2 ();
1957 #endif
1959 #ifdef STACK_REGS
1960 rest_of_handle_stack_regs ();
1961 #endif
1963 compute_alignments ();
1965 if (flag_var_tracking)
1966 rest_of_handle_variable_tracking ();
1968 /* CFG is no longer maintained up-to-date. */
1969 free_bb_for_insn ();
1971 if (targetm.machine_dependent_reorg != 0)
1972 rest_of_handle_machine_reorg ();
1974 purge_line_number_notes (get_insns ());
1975 cleanup_barriers ();
1977 #ifdef DELAY_SLOTS
1978 if (flag_delayed_branch)
1979 rest_of_handle_delay_slots ();
1980 #endif
1982 #if defined (HAVE_ATTR_length) && !defined (STACK_REGS)
1983 timevar_push (TV_SHORTEN_BRANCH);
1984 split_all_insns_noflow ();
1985 timevar_pop (TV_SHORTEN_BRANCH);
1986 #endif
1988 convert_to_eh_region_ranges ();
1990 rest_of_handle_shorten_branches ();
1992 set_nothrow_function_flags ();
1994 rest_of_handle_final ();
1996 exit_rest_of_compilation:
1998 rest_of_clean_state ();
2001 void
2002 init_optimization_passes (void)
2004 open_dump_file (DFI_cgraph, NULL);
2005 cgraph_dump_file = dump_file;
2006 dump_file = NULL;
2009 void
2010 finish_optimization_passes (void)
2012 timevar_push (TV_DUMP);
2013 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
2015 open_dump_file (DFI_bp, NULL);
2016 end_branch_prob ();
2017 close_dump_file (DFI_bp, NULL, NULL_RTX);
2020 if (optimize > 0 && open_dump_file (DFI_combine, NULL))
2022 dump_combine_total_stats (dump_file);
2023 close_dump_file (DFI_combine, NULL, NULL_RTX);
2026 dump_file = cgraph_dump_file;
2027 cgraph_dump_file = NULL;
2028 close_dump_file (DFI_cgraph, NULL, NULL_RTX);
2030 /* Do whatever is necessary to finish printing the graphs. */
2031 if (graph_dump_format != no_graph)
2033 int i;
2035 for (i = 0; i < (int) DFI_MAX; ++i)
2036 if (dump_file_tbl[i].initialized && dump_file_tbl[i].graph_dump_p)
2038 char seq[16];
2039 char *suffix;
2041 sprintf (seq, DUMPFILE_FORMAT, i);
2042 suffix = concat (seq, dump_file_tbl[i].extension, NULL);
2043 finish_graph_dump_file (dump_base_name, suffix);
2044 free (suffix);
2048 timevar_pop (TV_DUMP);
2051 bool
2052 enable_rtl_dump_file (int letter)
2054 bool matched = false;
2055 int i;
2057 if (letter == 'a')
2059 for (i = 0; i < (int) DFI_MAX; ++i)
2060 dump_file_tbl[i].enabled = 1;
2061 matched = true;
2063 else
2065 for (i = 0; i < (int) DFI_MAX; ++i)
2066 if (letter == dump_file_tbl[i].debug_switch)
2068 dump_file_tbl[i].enabled = 1;
2069 matched = true;
2073 return matched;
2076 struct tree_opt_pass pass_rest_of_compilation =
2078 "rest of compilation", /* name */
2079 NULL, /* gate */
2080 rest_of_compilation, /* execute */
2081 NULL, /* sub */
2082 NULL, /* next */
2083 0, /* static_pass_number */
2084 TV_REST_OF_COMPILATION, /* tv_id */
2085 PROP_rtl, /* properties_required */
2086 0, /* properties_provided */
2087 PROP_rtl, /* properties_destroyed */
2088 0, /* todo_flags_start */
2089 TODO_ggc_collect /* todo_flags_finish */