1 /* Loop optimizer initialization routines.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
29 #include "cfglayout.h"
31 static void fixup_loop_exit_succesor (basic_block, basic_block);
33 /* Initialize loop optimizer. */
36 loop_optimizer_init (FILE *dumpfile)
38 struct loops *loops = xcalloc (1, sizeof (struct loops));
41 /* Initialize structures for layout changes. */
42 cfg_layout_initialize ();
44 /* Avoid annoying special cases of edges going to exit
46 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
47 if ((e->flags & EDGE_FALLTHRU) && e->src->succ->succ_next)
52 if (flow_loops_find (loops, LOOP_TREE) <= 1)
57 flow_loops_free (loops);
58 free_dominance_info (CDI_DOMINATORS);
63 if (bb->next_bb != EXIT_BLOCK_PTR)
64 bb->rbi->next = bb->next_bb;
65 cfg_layout_finalize ();
69 /* Not going to update these. */
70 free (loops->cfg.rc_order);
71 loops->cfg.rc_order = NULL;
72 free (loops->cfg.dfs_order);
73 loops->cfg.dfs_order = NULL;
75 /* Create pre-headers. */
76 create_preheaders (loops, CP_SIMPLE_PREHEADERS);
78 /* Force all latches to have only single successor. */
79 force_single_succ_latches (loops);
81 /* Mark irreducible loops. */
82 mark_irreducible_loops (loops);
85 flow_loops_dump (loops, dumpfile, NULL, 1);
87 #ifdef ENABLE_CHECKING
88 verify_dominators (CDI_DOMINATORS);
89 verify_loop_structure (loops);
95 /* The first basic block is moved after the second in the reorder chain. */
97 fixup_loop_exit_succesor (basic_block exit_succ, basic_block latch)
99 basic_block bb = exit_succ;
100 basic_block bb1 = latch;
102 if (!(bb && bb->rbi->next))
109 if (bb && bb->rbi->next)
111 basic_block c = ENTRY_BLOCK_PTR->next_bb;
113 while (c->rbi->next != bb)
116 c->rbi->next = bb->rbi->next;
119 if(bb1->rbi->next == NULL)
129 tmp = bb1->rbi->next;
135 /* Finalize loop optimizer. */
137 loop_optimizer_finalize (struct loops *loops, FILE *dumpfile)
142 /* Finalize layout changes. */
145 if (bb->next_bb != EXIT_BLOCK_PTR)
146 bb->rbi->next = bb->next_bb;
149 flow_loops_dump (loops, dumpfile, NULL, 1);
151 /* For loops ending with a branch and count instruction, move the basic
152 block found before the unrolling on the fallthru path of this branch,
153 after the unrolled code. This will allow branch simplification. */
154 for (i = 1; i < loops->num; i++)
156 struct loop *loop = loops->parray[i];
157 struct loop_desc *desc;
158 basic_block loop_real_latch, bb, bb_exit;
168 if (loop->lpt_decision.decision != LPT_UNROLL_RUNTIME)
174 if (loop->latch->pred == NULL)
177 loop_real_latch = loop->latch->pred->src;
182 if (!is_bct_cond (BB_END (loop_real_latch)))
185 for (e = loop_real_latch->succ; e ; e = e->succ_next)
186 if (e->flags & EDGE_FALLTHRU)
196 /* Leave the case of the bb_exit falling through to exit to
197 fixed_fallthru_exit_predecessor */
198 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
199 if (e->flags & EDGE_FALLTHRU)
205 fixup_loop_exit_succesor (bb_exit, loop->latch);
209 flow_loops_free (loops);
210 free_dominance_info (CDI_DOMINATORS);
213 /* Finalize changes. */
214 cfg_layout_finalize ();
217 #ifdef ENABLE_CHECKING