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"
37 /* Initialize loop structures. This is used by the tree and RTL loop
38 optimizers. FLAGS specify what properties to compute and/or ensure for
42 loop_optimizer_init (unsigned flags)
46 gcc_assert (!current_loops);
47 loops = GGC_CNEW (struct loops);
51 flow_loops_find (loops);
52 current_loops = loops;
54 if (number_of_loops () <= 1)
56 /* No loops (the 1 returned by number_of_loops corresponds to the fake
57 loop that we put as a root of the loop tree). */
58 loop_optimizer_finalize ();
62 if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
64 /* If the loops may have multiple latches, we cannot canonicalize
65 them further (and most of the loop manipulation functions will
66 not work). However, we avoid modifying cfg, which some
68 gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
69 | LOOPS_HAVE_RECORDED_EXITS)) == 0);
70 current_loops->state = LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
73 disambiguate_loops_with_multiple_latches ();
75 /* Create pre-headers. */
76 if (flags & LOOPS_HAVE_PREHEADERS)
77 create_preheaders (CP_SIMPLE_PREHEADERS);
79 /* Force all latches to have only single successor. */
80 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
81 force_single_succ_latches ();
83 /* Mark irreducible loops. */
84 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
85 mark_irreducible_loops ();
87 if (flags & LOOPS_HAVE_RECORDED_EXITS)
91 flow_loops_dump (dump_file, NULL, 1);
93 #ifdef ENABLE_CHECKING
94 verify_dominators (CDI_DOMINATORS);
95 verify_loop_structure ();
99 /* Finalize loop structures. */
102 loop_optimizer_finalize (void)
111 FOR_EACH_LOOP (li, loop, 0)
113 free_simple_loop_desc (loop);
117 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
118 release_recorded_exits ();
119 flow_loops_free (current_loops);
120 ggc_free (current_loops);
121 current_loops = NULL;
125 bb->loop_father = NULL;
129 #ifdef ENABLE_CHECKING
135 /* Gate for the RTL loop superpass. The actual passes are subpasses.
136 See passes.c for more on that. */
139 gate_handle_loop2 (void)
142 && (flag_move_loop_invariants
143 || flag_unswitch_loops
146 #ifdef HAVE_doloop_end
147 || (flag_branch_on_count_reg && HAVE_doloop_end)
152 struct tree_opt_pass pass_loop2 =
155 gate_handle_loop2, /* gate */
159 0, /* static_pass_number */
161 0, /* properties_required */
162 0, /* properties_provided */
163 0, /* properties_destroyed */
164 0, /* todo_flags_start */
166 TODO_ggc_collect, /* todo_flags_finish */
171 /* Initialization of the RTL loop passes. */
175 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
178 dump_flow_info (dump_file, dump_flags);
180 loop_optimizer_init (LOOPS_NORMAL);
184 struct tree_opt_pass pass_rtl_loop_init =
186 "loop2_init", /* name */
188 rtl_loop_init, /* execute */
191 0, /* static_pass_number */
193 0, /* properties_required */
194 0, /* properties_provided */
195 0, /* properties_destroyed */
196 0, /* todo_flags_start */
197 TODO_dump_func, /* todo_flags_finish */
202 /* Finalization of the RTL loop passes. */
207 loop_optimizer_finalize ();
208 free_dominance_info (CDI_DOMINATORS);
210 cleanup_cfg (CLEANUP_EXPENSIVE);
211 delete_trivially_dead_insns (get_insns (), max_reg_num ());
212 reg_scan (get_insns (), max_reg_num ());
214 dump_flow_info (dump_file, dump_flags);
219 struct tree_opt_pass pass_rtl_loop_done =
221 "loop2_done", /* name */
223 rtl_loop_done, /* execute */
226 0, /* static_pass_number */
228 0, /* properties_required */
229 0, /* properties_provided */
230 0, /* properties_destroyed */
231 0, /* todo_flags_start */
232 TODO_dump_func, /* todo_flags_finish */
237 /* Loop invariant code motion. */
239 gate_rtl_move_loop_invariants (void)
241 return flag_move_loop_invariants;
245 rtl_move_loop_invariants (void)
248 move_loop_invariants ();
252 struct tree_opt_pass pass_rtl_move_loop_invariants =
254 "loop2_invariant", /* name */
255 gate_rtl_move_loop_invariants, /* gate */
256 rtl_move_loop_invariants, /* execute */
259 0, /* static_pass_number */
261 0, /* properties_required */
262 0, /* properties_provided */
263 0, /* properties_destroyed */
264 0, /* todo_flags_start */
265 TODO_dump_func, /* todo_flags_finish */
270 /* Loop unswitching for RTL. */
272 gate_rtl_unswitch (void)
274 return flag_unswitch_loops;
285 struct tree_opt_pass pass_rtl_unswitch =
287 "loop2_unswitch", /* name */
288 gate_rtl_unswitch, /* gate */
289 rtl_unswitch, /* execute */
292 0, /* static_pass_number */
294 0, /* properties_required */
295 0, /* properties_provided */
296 0, /* properties_destroyed */
297 0, /* todo_flags_start */
298 TODO_dump_func, /* todo_flags_finish */
303 /* Loop unswitching for RTL. */
305 gate_rtl_unroll_and_peel_loops (void)
307 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
311 rtl_unroll_and_peel_loops (void)
319 if (flag_unroll_loops)
321 if (flag_unroll_all_loops)
322 flags |= UAP_UNROLL_ALL;
324 unroll_and_peel_loops (flags);
329 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
331 "loop2_unroll", /* name */
332 gate_rtl_unroll_and_peel_loops, /* gate */
333 rtl_unroll_and_peel_loops, /* execute */
336 0, /* static_pass_number */
338 0, /* properties_required */
339 0, /* properties_provided */
340 0, /* properties_destroyed */
341 0, /* todo_flags_start */
342 TODO_dump_func, /* todo_flags_finish */
347 /* The doloop optimization. */
349 gate_rtl_doloop (void)
351 #ifdef HAVE_doloop_end
352 return (flag_branch_on_count_reg && HAVE_doloop_end);
361 #ifdef HAVE_doloop_end
363 doloop_optimize_loops ();
368 struct tree_opt_pass pass_rtl_doloop =
370 "loop2_doloop", /* name */
371 gate_rtl_doloop, /* gate */
372 rtl_doloop, /* execute */
375 0, /* static_pass_number */
377 0, /* properties_required */
378 0, /* properties_provided */
379 0, /* properties_destroyed */
380 0, /* todo_flags_start */
381 TODO_dump_func, /* todo_flags_finish */