gcc/
[official-gcc.git] / gcc / loop-init.c
blob0e0b0cf4c88fdbe6f0381c67d3e930a3682623d1
1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002, 2003, 2004, 2005 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
9 version.
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
14 for more details.
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, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "obstack.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "cfglayout.h"
31 #include "tree-pass.h"
32 #include "timevar.h"
33 #include "flags.h"
36 /* Initialize loop optimizer. This is used by the tree and RTL loop
37 optimizers. */
39 struct loops *
40 loop_optimizer_init (FILE *dumpfile)
42 struct loops *loops = xcalloc (1, sizeof (struct loops));
43 edge e;
44 edge_iterator ei;
45 static bool first_time = true;
47 if (first_time)
49 first_time = false;
50 init_set_costs ();
53 /* Avoid annoying special cases of edges going to exit
54 block. */
56 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
57 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
58 split_edge (e);
59 else
60 ei_next (&ei);
62 /* Find the loops. */
64 if (flow_loops_find (loops) <= 1)
66 /* No loops. */
67 flow_loops_free (loops);
68 free (loops);
70 return NULL;
73 /* Not going to update these. */
74 free (loops->cfg.rc_order);
75 loops->cfg.rc_order = NULL;
76 free (loops->cfg.dfs_order);
77 loops->cfg.dfs_order = NULL;
79 /* Create pre-headers. */
80 create_preheaders (loops, CP_SIMPLE_PREHEADERS);
82 /* Force all latches to have only single successor. */
83 force_single_succ_latches (loops);
85 /* Mark irreducible loops. */
86 mark_irreducible_loops (loops);
88 /* Dump loops. */
89 flow_loops_dump (loops, dumpfile, NULL, 1);
91 #ifdef ENABLE_CHECKING
92 verify_dominators (CDI_DOMINATORS);
93 verify_loop_structure (loops);
94 #endif
96 return loops;
99 /* Finalize loop optimizer. */
100 void
101 loop_optimizer_finalize (struct loops *loops, FILE *dumpfile)
103 unsigned i;
105 if (!loops)
106 return;
108 for (i = 1; i < loops->num; i++)
109 if (loops->parray[i])
110 free_simple_loop_desc (loops->parray[i]);
112 /* Another dump. */
113 flow_loops_dump (loops, dumpfile, NULL, 1);
115 /* Clean up. */
116 flow_loops_free (loops);
117 free (loops);
119 /* Checking. */
120 #ifdef ENABLE_CHECKING
121 verify_flow_info ();
122 #endif
126 /* Gate for the RTL loop superpass. The actual passes are subpasses.
127 See passes.c for more on that. */
129 static bool
130 gate_handle_loop2 (void)
132 return (optimize > 0 && flag_loop_optimize2
133 && (flag_move_loop_invariants
134 || flag_unswitch_loops
135 || flag_peel_loops
136 || flag_unroll_loops
137 || flag_branch_on_count_reg));
140 struct tree_opt_pass pass_loop2 =
142 "loop2", /* name */
143 gate_handle_loop2, /* gate */
144 NULL, /* execute */
145 NULL, /* sub */
146 NULL, /* next */
147 0, /* static_pass_number */
148 TV_LOOP, /* tv_id */
149 0, /* properties_required */
150 0, /* properties_provided */
151 0, /* properties_destroyed */
152 0, /* todo_flags_start */
153 TODO_dump_func |
154 TODO_ggc_collect, /* todo_flags_finish */
155 'L' /* letter */
159 /* Initialization of the RTL loop passes. */
160 static void
161 rtl_loop_init (void)
163 if (dump_file)
164 dump_flow_info (dump_file);
166 /* Initialize structures for layout changes. */
167 cfg_layout_initialize (0);
169 current_loops = loop_optimizer_init (dump_file);
172 struct tree_opt_pass pass_rtl_loop_init =
174 "loopinit", /* name */
175 NULL, /* gate */
176 rtl_loop_init, /* execute */
177 NULL, /* sub */
178 NULL, /* next */
179 0, /* static_pass_number */
180 TV_LOOP, /* tv_id */
181 0, /* properties_required */
182 0, /* properties_provided */
183 0, /* properties_destroyed */
184 0, /* todo_flags_start */
185 TODO_dump_func, /* todo_flags_finish */
186 'L' /* letter */
190 /* Finalization of the RTL loop passes. */
191 static void
192 rtl_loop_done (void)
194 basic_block bb;
196 if (current_loops)
197 loop_optimizer_finalize (current_loops, dump_file);
199 free_dominance_info (CDI_DOMINATORS);
201 /* Finalize layout changes. */
202 FOR_EACH_BB (bb)
203 if (bb->next_bb != EXIT_BLOCK_PTR)
204 bb->aux = bb->next_bb;
205 cfg_layout_finalize ();
207 cleanup_cfg (CLEANUP_EXPENSIVE);
208 delete_trivially_dead_insns (get_insns (), max_reg_num ());
209 reg_scan (get_insns (), max_reg_num ());
210 if (dump_file)
211 dump_flow_info (dump_file);
213 current_loops = NULL;
216 struct tree_opt_pass pass_rtl_loop_done =
218 "loopdone", /* name */
219 NULL, /* gate */
220 rtl_loop_done, /* execute */
221 NULL, /* sub */
222 NULL, /* next */
223 0, /* static_pass_number */
224 TV_LOOP, /* tv_id */
225 0, /* properties_required */
226 0, /* properties_provided */
227 0, /* properties_destroyed */
228 0, /* todo_flags_start */
229 TODO_dump_func, /* todo_flags_finish */
230 'L' /* letter */
234 /* Loop invariant code motion. */
235 static void
236 rtl_move_loop_invariants (void)
238 if (current_loops && flag_move_loop_invariants)
239 move_loop_invariants (current_loops);
242 struct tree_opt_pass pass_rtl_move_loop_invariants =
244 "loop_invariant", /* name */
245 NULL, /* gate */
246 rtl_move_loop_invariants, /* execute */
247 NULL, /* sub */
248 NULL, /* next */
249 0, /* static_pass_number */
250 TV_LOOP, /* tv_id */
251 0, /* properties_required */
252 0, /* properties_provided */
253 0, /* properties_destroyed */
254 0, /* todo_flags_start */
255 TODO_dump_func, /* todo_flags_finish */
256 'L' /* letter */
260 /* Loop unswitching for RTL. */
261 static void
262 rtl_unswitch (void)
264 if (current_loops && flag_unswitch_loops)
265 unswitch_loops (current_loops);
268 struct tree_opt_pass pass_rtl_unswitch =
270 "loop_unswitch", /* name */
271 NULL, /* gate */
272 rtl_unswitch, /* execute */
273 NULL, /* sub */
274 NULL, /* next */
275 0, /* static_pass_number */
276 TV_LOOP, /* tv_id */
277 0, /* properties_required */
278 0, /* properties_provided */
279 0, /* properties_destroyed */
280 0, /* todo_flags_start */
281 TODO_dump_func, /* todo_flags_finish */
282 'L' /* letter */
286 /* Loop unswitching for RTL. */
287 static void
288 rtl_unroll_and_peel_loops (void)
290 if (current_loops
291 && (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops))
293 int flags = 0;
295 if (flag_peel_loops)
296 flags |= UAP_PEEL;
297 if (flag_unroll_loops)
298 flags |= UAP_UNROLL;
299 if (flag_unroll_all_loops)
300 flags |= UAP_UNROLL_ALL;
302 unroll_and_peel_loops (current_loops, flags);
306 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
308 "loop_unroll", /* name */
309 NULL, /* gate */
310 rtl_unroll_and_peel_loops, /* execute */
311 NULL, /* sub */
312 NULL, /* next */
313 0, /* static_pass_number */
314 TV_LOOP, /* tv_id */
315 0, /* properties_required */
316 0, /* properties_provided */
317 0, /* properties_destroyed */
318 0, /* todo_flags_start */
319 TODO_dump_func, /* todo_flags_finish */
320 'L' /* letter */
324 /* The doloop optimization. */
325 static void
326 rtl_doloop (void)
328 #ifdef HAVE_doloop_end
329 if (current_loops
330 && (flag_branch_on_count_reg && HAVE_doloop_end))
331 doloop_optimize_loops (current_loops);
332 #endif
335 struct tree_opt_pass pass_rtl_doloop =
337 "loop_doloop", /* name */
338 NULL, /* gate */
339 rtl_doloop, /* execute */
340 NULL, /* sub */
341 NULL, /* next */
342 0, /* static_pass_number */
343 TV_LOOP, /* tv_id */
344 0, /* properties_required */
345 0, /* properties_provided */
346 0, /* properties_destroyed */
347 0, /* todo_flags_start */
348 TODO_dump_func, /* todo_flags_finish */
349 'L' /* letter */