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_MARKED_SINGLE_EXITS)
92 mark_single_exit_loops ();
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 flow_loops_free (current_loops);
122 free (current_loops);
123 current_loops = NULL;
127 bb->loop_father = NULL;
131 #ifdef ENABLE_CHECKING
137 /* Gate for the RTL loop superpass. The actual passes are subpasses.
138 See passes.c for more on that. */
141 gate_handle_loop2 (void)
144 && (flag_move_loop_invariants
145 || flag_unswitch_loops
148 #ifdef HAVE_doloop_end
149 || (flag_branch_on_count_reg && HAVE_doloop_end)
154 struct tree_opt_pass pass_loop2 =
157 gate_handle_loop2, /* gate */
161 0, /* static_pass_number */
163 0, /* properties_required */
164 0, /* properties_provided */
165 0, /* properties_destroyed */
166 0, /* todo_flags_start */
168 TODO_ggc_collect, /* todo_flags_finish */
173 /* Initialization of the RTL loop passes. */
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);
187 struct tree_opt_pass pass_rtl_loop_init =
189 "loop2_init", /* name */
191 rtl_loop_init, /* execute */
194 0, /* static_pass_number */
196 0, /* properties_required */
197 0, /* properties_provided */
198 0, /* properties_destroyed */
199 0, /* todo_flags_start */
200 TODO_dump_func, /* todo_flags_finish */
205 /* Finalization of the RTL loop passes. */
212 loop_optimizer_finalize ();
213 free_dominance_info (CDI_DOMINATORS);
215 /* Finalize layout changes. */
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 ());
225 dump_flow_info (dump_file, dump_flags);
230 struct tree_opt_pass pass_rtl_loop_done =
232 "loop2_done", /* name */
234 rtl_loop_done, /* execute */
237 0, /* static_pass_number */
239 0, /* properties_required */
240 0, /* properties_provided */
241 0, /* properties_destroyed */
242 0, /* todo_flags_start */
243 TODO_dump_func, /* todo_flags_finish */
248 /* Loop invariant code motion. */
250 gate_rtl_move_loop_invariants (void)
252 return flag_move_loop_invariants;
256 rtl_move_loop_invariants (void)
259 move_loop_invariants ();
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 */
270 0, /* static_pass_number */
272 0, /* properties_required */
273 0, /* properties_provided */
274 0, /* properties_destroyed */
275 0, /* todo_flags_start */
276 TODO_dump_func, /* todo_flags_finish */
281 /* Loop unswitching for RTL. */
283 gate_rtl_unswitch (void)
285 return flag_unswitch_loops;
296 struct tree_opt_pass pass_rtl_unswitch =
298 "loop2_unswitch", /* name */
299 gate_rtl_unswitch, /* gate */
300 rtl_unswitch, /* execute */
303 0, /* static_pass_number */
305 0, /* properties_required */
306 0, /* properties_provided */
307 0, /* properties_destroyed */
308 0, /* todo_flags_start */
309 TODO_dump_func, /* todo_flags_finish */
314 /* Loop unswitching for RTL. */
316 gate_rtl_unroll_and_peel_loops (void)
318 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
322 rtl_unroll_and_peel_loops (void)
330 if (flag_unroll_loops)
332 if (flag_unroll_all_loops)
333 flags |= UAP_UNROLL_ALL;
335 unroll_and_peel_loops (flags);
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 */
347 0, /* static_pass_number */
349 0, /* properties_required */
350 0, /* properties_provided */
351 0, /* properties_destroyed */
352 0, /* todo_flags_start */
353 TODO_dump_func, /* todo_flags_finish */
358 /* The doloop optimization. */
360 gate_rtl_doloop (void)
362 #ifdef HAVE_doloop_end
363 return (flag_branch_on_count_reg && HAVE_doloop_end);
372 #ifdef HAVE_doloop_end
374 doloop_optimize_loops ();
379 struct tree_opt_pass pass_rtl_doloop =
381 "loop2_doloop", /* name */
382 gate_rtl_doloop, /* gate */
383 rtl_doloop, /* execute */
386 0, /* static_pass_number */
388 0, /* properties_required */
389 0, /* properties_provided */
390 0, /* properties_destroyed */
391 0, /* todo_flags_start */
392 TODO_dump_func, /* todo_flags_finish */