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)
45 static bool first_time = true;
54 gcc_assert (!current_loops);
55 loops = XCNEW (struct loops);
57 /* Avoid annoying special cases of edges going to exit
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))
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 ();
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_RECORDED_EXITS)
95 flow_loops_dump (dump_file, NULL, 1);
97 #ifdef ENABLE_CHECKING
98 verify_dominators (CDI_DOMINATORS);
99 verify_loop_structure ();
103 /* Finalize loop structures. */
106 loop_optimizer_finalize (void)
115 FOR_EACH_LOOP (li, loop, 0)
117 free_simple_loop_desc (loop);
121 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
122 release_recorded_exits ();
123 flow_loops_free (current_loops);
124 free (current_loops);
125 current_loops = NULL;
129 bb->loop_father = NULL;
133 #ifdef ENABLE_CHECKING
139 /* Gate for the RTL loop superpass. The actual passes are subpasses.
140 See passes.c for more on that. */
143 gate_handle_loop2 (void)
146 && (flag_move_loop_invariants
147 || flag_unswitch_loops
150 #ifdef HAVE_doloop_end
151 || (flag_branch_on_count_reg && HAVE_doloop_end)
156 struct tree_opt_pass pass_loop2 =
159 gate_handle_loop2, /* gate */
163 0, /* static_pass_number */
165 0, /* properties_required */
166 0, /* properties_provided */
167 0, /* properties_destroyed */
168 0, /* todo_flags_start */
170 TODO_ggc_collect, /* todo_flags_finish */
175 /* Initialization of the RTL loop passes. */
180 dump_flow_info (dump_file, dump_flags);
182 /* Initialize structures for layout changes. */
183 cfg_layout_initialize (0);
185 loop_optimizer_init (LOOPS_NORMAL);
189 struct tree_opt_pass pass_rtl_loop_init =
191 "loop2_init", /* name */
193 rtl_loop_init, /* execute */
196 0, /* static_pass_number */
198 0, /* properties_required */
199 0, /* properties_provided */
200 0, /* properties_destroyed */
201 0, /* todo_flags_start */
202 TODO_dump_func, /* todo_flags_finish */
207 /* Finalization of the RTL loop passes. */
214 loop_optimizer_finalize ();
215 free_dominance_info (CDI_DOMINATORS);
217 /* Finalize layout changes. */
219 if (bb->next_bb != EXIT_BLOCK_PTR)
220 bb->aux = bb->next_bb;
221 cfg_layout_finalize ();
223 cleanup_cfg (CLEANUP_EXPENSIVE);
224 delete_trivially_dead_insns (get_insns (), max_reg_num ());
225 reg_scan (get_insns (), max_reg_num ());
227 dump_flow_info (dump_file, dump_flags);
232 struct tree_opt_pass pass_rtl_loop_done =
234 "loop2_done", /* name */
236 rtl_loop_done, /* execute */
239 0, /* static_pass_number */
241 0, /* properties_required */
242 0, /* properties_provided */
243 0, /* properties_destroyed */
244 0, /* todo_flags_start */
245 TODO_dump_func, /* todo_flags_finish */
250 /* Loop invariant code motion. */
252 gate_rtl_move_loop_invariants (void)
254 return flag_move_loop_invariants;
258 rtl_move_loop_invariants (void)
261 move_loop_invariants ();
265 struct tree_opt_pass pass_rtl_move_loop_invariants =
267 "loop2_invariant", /* name */
268 gate_rtl_move_loop_invariants, /* gate */
269 rtl_move_loop_invariants, /* execute */
272 0, /* static_pass_number */
274 0, /* properties_required */
275 0, /* properties_provided */
276 0, /* properties_destroyed */
277 0, /* todo_flags_start */
278 TODO_dump_func, /* todo_flags_finish */
283 /* Loop unswitching for RTL. */
285 gate_rtl_unswitch (void)
287 return flag_unswitch_loops;
298 struct tree_opt_pass pass_rtl_unswitch =
300 "loop2_unswitch", /* name */
301 gate_rtl_unswitch, /* gate */
302 rtl_unswitch, /* execute */
305 0, /* static_pass_number */
307 0, /* properties_required */
308 0, /* properties_provided */
309 0, /* properties_destroyed */
310 0, /* todo_flags_start */
311 TODO_dump_func, /* todo_flags_finish */
316 /* Loop unswitching for RTL. */
318 gate_rtl_unroll_and_peel_loops (void)
320 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
324 rtl_unroll_and_peel_loops (void)
332 if (flag_unroll_loops)
334 if (flag_unroll_all_loops)
335 flags |= UAP_UNROLL_ALL;
337 unroll_and_peel_loops (flags);
342 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
344 "loop2_unroll", /* name */
345 gate_rtl_unroll_and_peel_loops, /* gate */
346 rtl_unroll_and_peel_loops, /* execute */
349 0, /* static_pass_number */
351 0, /* properties_required */
352 0, /* properties_provided */
353 0, /* properties_destroyed */
354 0, /* todo_flags_start */
355 TODO_dump_func, /* todo_flags_finish */
360 /* The doloop optimization. */
362 gate_rtl_doloop (void)
364 #ifdef HAVE_doloop_end
365 return (flag_branch_on_count_reg && HAVE_doloop_end);
374 #ifdef HAVE_doloop_end
376 doloop_optimize_loops ();
381 struct tree_opt_pass pass_rtl_doloop =
383 "loop2_doloop", /* name */
384 gate_rtl_doloop, /* gate */
385 rtl_doloop, /* execute */
388 0, /* static_pass_number */
390 0, /* properties_required */
391 0, /* properties_provided */
392 0, /* properties_destroyed */
393 0, /* todo_flags_start */
394 TODO_dump_func, /* todo_flags_finish */