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