2006-12-16 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[official-gcc.git] / gcc / loop-init.c
blob32e56b32a6ae69c26262ffd165a6e07d2f9df436
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 structures. This is used by the tree and RTL loop
37 optimizers. FLAGS specify what properties to compute and/or ensure for
38 loops. */
40 void
41 loop_optimizer_init (unsigned flags)
43 edge e;
44 edge_iterator ei;
45 static bool first_time = true;
46 struct loops *loops;
48 if (first_time)
50 first_time = false;
51 init_set_costs ();
54 gcc_assert (!current_loops);
55 loops = XCNEW (struct loops);
57 /* Avoid annoying special cases of edges going to exit
58 block. */
60 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
61 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
62 split_edge (e);
63 else
64 ei_next (&ei);
66 /* Find the loops. */
68 flow_loops_find (loops);
69 current_loops = loops;
71 if (number_of_loops () <= 1)
73 /* No loops (the 1 returned by number_of_loops corresponds to the fake
74 loop that we put as a root of the loop tree). */
75 loop_optimizer_finalize ();
76 return;
79 /* Create pre-headers. */
80 if (flags & LOOPS_HAVE_PREHEADERS)
81 create_preheaders (CP_SIMPLE_PREHEADERS);
83 /* Force all latches to have only single successor. */
84 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
85 force_single_succ_latches ();
87 /* Mark irreducible loops. */
88 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
89 mark_irreducible_loops ();
91 if (flags & LOOPS_HAVE_MARKED_SINGLE_EXITS)
92 mark_single_exit_loops ();
94 /* Dump loops. */
95 flow_loops_dump (dump_file, NULL, 1);
97 #ifdef ENABLE_CHECKING
98 verify_dominators (CDI_DOMINATORS);
99 verify_loop_structure ();
100 #endif
103 /* Finalize loop structures. */
105 void
106 loop_optimizer_finalize (void)
108 loop_iterator li;
109 struct loop *loop;
110 basic_block bb;
112 if (!current_loops)
113 return;
115 FOR_EACH_LOOP (li, loop, 0)
117 free_simple_loop_desc (loop);
120 /* Clean up. */
121 flow_loops_free (current_loops);
122 free (current_loops);
123 current_loops = NULL;
125 FOR_ALL_BB (bb)
127 bb->loop_father = NULL;
130 /* Checking. */
131 #ifdef ENABLE_CHECKING
132 verify_flow_info ();
133 #endif
137 /* Gate for the RTL loop superpass. The actual passes are subpasses.
138 See passes.c for more on that. */
140 static bool
141 gate_handle_loop2 (void)
143 return (optimize > 0
144 && (flag_move_loop_invariants
145 || flag_unswitch_loops
146 || flag_peel_loops
147 || flag_unroll_loops
148 #ifdef HAVE_doloop_end
149 || (flag_branch_on_count_reg && HAVE_doloop_end)
150 #endif
154 struct tree_opt_pass pass_loop2 =
156 "loop2", /* name */
157 gate_handle_loop2, /* gate */
158 NULL, /* execute */
159 NULL, /* sub */
160 NULL, /* next */
161 0, /* static_pass_number */
162 TV_LOOP, /* tv_id */
163 0, /* properties_required */
164 0, /* properties_provided */
165 0, /* properties_destroyed */
166 0, /* todo_flags_start */
167 TODO_dump_func |
168 TODO_ggc_collect, /* todo_flags_finish */
169 'L' /* letter */
173 /* Initialization of the RTL loop passes. */
174 static unsigned int
175 rtl_loop_init (void)
177 if (dump_file)
178 dump_flow_info (dump_file, dump_flags);
180 /* Initialize structures for layout changes. */
181 cfg_layout_initialize (0);
183 loop_optimizer_init (LOOPS_NORMAL);
184 return 0;
187 struct tree_opt_pass pass_rtl_loop_init =
189 "loop2_init", /* name */
190 NULL, /* gate */
191 rtl_loop_init, /* execute */
192 NULL, /* sub */
193 NULL, /* next */
194 0, /* static_pass_number */
195 TV_LOOP, /* tv_id */
196 0, /* properties_required */
197 0, /* properties_provided */
198 0, /* properties_destroyed */
199 0, /* todo_flags_start */
200 TODO_dump_func, /* todo_flags_finish */
201 'L' /* letter */
205 /* Finalization of the RTL loop passes. */
207 static unsigned int
208 rtl_loop_done (void)
210 basic_block bb;
212 loop_optimizer_finalize ();
213 free_dominance_info (CDI_DOMINATORS);
215 /* Finalize layout changes. */
216 FOR_EACH_BB (bb)
217 if (bb->next_bb != EXIT_BLOCK_PTR)
218 bb->aux = bb->next_bb;
219 cfg_layout_finalize ();
221 cleanup_cfg (CLEANUP_EXPENSIVE);
222 delete_trivially_dead_insns (get_insns (), max_reg_num ());
223 reg_scan (get_insns (), max_reg_num ());
224 if (dump_file)
225 dump_flow_info (dump_file, dump_flags);
227 return 0;
230 struct tree_opt_pass pass_rtl_loop_done =
232 "loop2_done", /* name */
233 NULL, /* gate */
234 rtl_loop_done, /* execute */
235 NULL, /* sub */
236 NULL, /* next */
237 0, /* static_pass_number */
238 TV_LOOP, /* tv_id */
239 0, /* properties_required */
240 0, /* properties_provided */
241 0, /* properties_destroyed */
242 0, /* todo_flags_start */
243 TODO_dump_func, /* todo_flags_finish */
244 'L' /* letter */
248 /* Loop invariant code motion. */
249 static bool
250 gate_rtl_move_loop_invariants (void)
252 return flag_move_loop_invariants;
255 static unsigned int
256 rtl_move_loop_invariants (void)
258 if (current_loops)
259 move_loop_invariants ();
260 return 0;
263 struct tree_opt_pass pass_rtl_move_loop_invariants =
265 "loop2_invariant", /* name */
266 gate_rtl_move_loop_invariants, /* gate */
267 rtl_move_loop_invariants, /* execute */
268 NULL, /* sub */
269 NULL, /* next */
270 0, /* static_pass_number */
271 TV_LOOP, /* tv_id */
272 0, /* properties_required */
273 0, /* properties_provided */
274 0, /* properties_destroyed */
275 0, /* todo_flags_start */
276 TODO_dump_func, /* todo_flags_finish */
277 'L' /* letter */
281 /* Loop unswitching for RTL. */
282 static bool
283 gate_rtl_unswitch (void)
285 return flag_unswitch_loops;
288 static unsigned int
289 rtl_unswitch (void)
291 if (current_loops)
292 unswitch_loops ();
293 return 0;
296 struct tree_opt_pass pass_rtl_unswitch =
298 "loop2_unswitch", /* name */
299 gate_rtl_unswitch, /* gate */
300 rtl_unswitch, /* execute */
301 NULL, /* sub */
302 NULL, /* next */
303 0, /* static_pass_number */
304 TV_LOOP, /* tv_id */
305 0, /* properties_required */
306 0, /* properties_provided */
307 0, /* properties_destroyed */
308 0, /* todo_flags_start */
309 TODO_dump_func, /* todo_flags_finish */
310 'L' /* letter */
314 /* Loop unswitching for RTL. */
315 static bool
316 gate_rtl_unroll_and_peel_loops (void)
318 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
321 static unsigned int
322 rtl_unroll_and_peel_loops (void)
324 if (current_loops)
326 int flags = 0;
328 if (flag_peel_loops)
329 flags |= UAP_PEEL;
330 if (flag_unroll_loops)
331 flags |= UAP_UNROLL;
332 if (flag_unroll_all_loops)
333 flags |= UAP_UNROLL_ALL;
335 unroll_and_peel_loops (flags);
337 return 0;
340 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
342 "loop2_unroll", /* name */
343 gate_rtl_unroll_and_peel_loops, /* gate */
344 rtl_unroll_and_peel_loops, /* execute */
345 NULL, /* sub */
346 NULL, /* next */
347 0, /* static_pass_number */
348 TV_LOOP, /* tv_id */
349 0, /* properties_required */
350 0, /* properties_provided */
351 0, /* properties_destroyed */
352 0, /* todo_flags_start */
353 TODO_dump_func, /* todo_flags_finish */
354 'L' /* letter */
358 /* The doloop optimization. */
359 static bool
360 gate_rtl_doloop (void)
362 #ifdef HAVE_doloop_end
363 return (flag_branch_on_count_reg && HAVE_doloop_end);
364 #else
365 return 0;
366 #endif
369 static unsigned int
370 rtl_doloop (void)
372 #ifdef HAVE_doloop_end
373 if (current_loops)
374 doloop_optimize_loops ();
375 #endif
376 return 0;
379 struct tree_opt_pass pass_rtl_doloop =
381 "loop2_doloop", /* name */
382 gate_rtl_doloop, /* gate */
383 rtl_doloop, /* execute */
384 NULL, /* sub */
385 NULL, /* next */
386 0, /* static_pass_number */
387 TV_LOOP, /* tv_id */
388 0, /* properties_required */
389 0, /* properties_provided */
390 0, /* properties_destroyed */
391 0, /* todo_flags_start */
392 TODO_dump_func, /* todo_flags_finish */
393 'L' /* letter */