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
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, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "cfglayout.h"
31 #include "tree-pass.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
41 loop_optimizer_init (unsigned flags)
47 gcc_assert (!current_loops);
48 loops = XCNEW (struct loops);
50 /* Avoid annoying special cases of edges going to exit
53 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
54 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
61 flow_loops_find (loops);
62 current_loops = loops;
64 if (number_of_loops () <= 1)
66 /* No loops (the 1 returned by number_of_loops corresponds to the fake
67 loop that we put as a root of the loop tree). */
68 loop_optimizer_finalize ();
72 /* Create pre-headers. */
73 if (flags & LOOPS_HAVE_PREHEADERS)
74 create_preheaders (CP_SIMPLE_PREHEADERS);
76 /* Force all latches to have only single successor. */
77 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
78 force_single_succ_latches ();
80 /* Mark irreducible loops. */
81 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
82 mark_irreducible_loops ();
84 if (flags & LOOPS_HAVE_RECORDED_EXITS)
88 flow_loops_dump (dump_file, NULL, 1);
90 #ifdef ENABLE_CHECKING
91 verify_dominators (CDI_DOMINATORS);
92 verify_loop_structure ();
96 /* Finalize loop structures. */
99 loop_optimizer_finalize (void)
108 FOR_EACH_LOOP (li, loop, 0)
110 free_simple_loop_desc (loop);
114 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
115 release_recorded_exits ();
116 flow_loops_free (current_loops);
117 free (current_loops);
118 current_loops = NULL;
122 bb->loop_father = NULL;
126 #ifdef ENABLE_CHECKING
132 /* Gate for the RTL loop superpass. The actual passes are subpasses.
133 See passes.c for more on that. */
136 gate_handle_loop2 (void)
139 && (flag_move_loop_invariants
140 || flag_unswitch_loops
143 #ifdef HAVE_doloop_end
144 || (flag_branch_on_count_reg && HAVE_doloop_end)
149 struct tree_opt_pass pass_loop2 =
152 gate_handle_loop2, /* gate */
156 0, /* static_pass_number */
158 0, /* properties_required */
159 0, /* properties_provided */
160 0, /* properties_destroyed */
161 0, /* todo_flags_start */
163 TODO_ggc_collect, /* todo_flags_finish */
168 /* Initialization of the RTL loop passes. */
173 dump_flow_info (dump_file, dump_flags);
175 /* Initialize structures for layout changes. */
176 cfg_layout_initialize (0);
178 loop_optimizer_init (LOOPS_NORMAL);
182 struct tree_opt_pass pass_rtl_loop_init =
184 "loop2_init", /* name */
186 rtl_loop_init, /* execute */
189 0, /* static_pass_number */
191 0, /* properties_required */
192 0, /* properties_provided */
193 0, /* properties_destroyed */
194 0, /* todo_flags_start */
195 TODO_dump_func, /* todo_flags_finish */
200 /* Finalization of the RTL loop passes. */
207 loop_optimizer_finalize ();
208 free_dominance_info (CDI_DOMINATORS);
210 /* Finalize layout changes. */
212 if (bb->next_bb != EXIT_BLOCK_PTR)
213 bb->aux = bb->next_bb;
214 cfg_layout_finalize ();
216 cleanup_cfg (CLEANUP_EXPENSIVE);
217 delete_trivially_dead_insns (get_insns (), max_reg_num ());
218 reg_scan (get_insns (), max_reg_num ());
220 dump_flow_info (dump_file, dump_flags);
225 struct tree_opt_pass pass_rtl_loop_done =
227 "loop2_done", /* name */
229 rtl_loop_done, /* execute */
232 0, /* static_pass_number */
234 0, /* properties_required */
235 0, /* properties_provided */
236 0, /* properties_destroyed */
237 0, /* todo_flags_start */
238 TODO_dump_func, /* todo_flags_finish */
243 /* Loop invariant code motion. */
245 gate_rtl_move_loop_invariants (void)
247 return flag_move_loop_invariants;
251 rtl_move_loop_invariants (void)
254 move_loop_invariants ();
258 struct tree_opt_pass pass_rtl_move_loop_invariants =
260 "loop2_invariant", /* name */
261 gate_rtl_move_loop_invariants, /* gate */
262 rtl_move_loop_invariants, /* execute */
265 0, /* static_pass_number */
267 0, /* properties_required */
268 0, /* properties_provided */
269 0, /* properties_destroyed */
270 0, /* todo_flags_start */
271 TODO_dump_func, /* todo_flags_finish */
276 /* Loop unswitching for RTL. */
278 gate_rtl_unswitch (void)
280 return flag_unswitch_loops;
291 struct tree_opt_pass pass_rtl_unswitch =
293 "loop2_unswitch", /* name */
294 gate_rtl_unswitch, /* gate */
295 rtl_unswitch, /* execute */
298 0, /* static_pass_number */
300 0, /* properties_required */
301 0, /* properties_provided */
302 0, /* properties_destroyed */
303 0, /* todo_flags_start */
304 TODO_dump_func, /* todo_flags_finish */
309 /* Loop unswitching for RTL. */
311 gate_rtl_unroll_and_peel_loops (void)
313 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
317 rtl_unroll_and_peel_loops (void)
325 if (flag_unroll_loops)
327 if (flag_unroll_all_loops)
328 flags |= UAP_UNROLL_ALL;
330 unroll_and_peel_loops (flags);
335 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
337 "loop2_unroll", /* name */
338 gate_rtl_unroll_and_peel_loops, /* gate */
339 rtl_unroll_and_peel_loops, /* execute */
342 0, /* static_pass_number */
344 0, /* properties_required */
345 0, /* properties_provided */
346 0, /* properties_destroyed */
347 0, /* todo_flags_start */
348 TODO_dump_func, /* todo_flags_finish */
353 /* The doloop optimization. */
355 gate_rtl_doloop (void)
357 #ifdef HAVE_doloop_end
358 return (flag_branch_on_count_reg && HAVE_doloop_end);
367 #ifdef HAVE_doloop_end
369 doloop_optimize_loops ();
374 struct tree_opt_pass pass_rtl_doloop =
376 "loop2_doloop", /* name */
377 gate_rtl_doloop, /* gate */
378 rtl_doloop, /* execute */
381 0, /* static_pass_number */
383 0, /* properties_required */
384 0, /* properties_provided */
385 0, /* properties_destroyed */
386 0, /* todo_flags_start */
387 TODO_dump_func, /* todo_flags_finish */