1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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"
38 /* Initialize loop structures. This is used by the tree and RTL loop
39 optimizers. FLAGS specify what properties to compute and/or ensure for
43 loop_optimizer_init (unsigned flags
)
47 gcc_assert (!current_loops
);
48 loops
= GGC_CNEW (struct loops
);
52 flow_loops_find (loops
);
53 current_loops
= loops
;
55 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
57 /* If the loops may have multiple latches, we cannot canonicalize
58 them further (and most of the loop manipulation functions will
59 not work). However, we avoid modifying cfg, which some
61 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
62 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
63 current_loops
->state
= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
66 disambiguate_loops_with_multiple_latches ();
68 /* Create pre-headers. */
69 if (flags
& LOOPS_HAVE_PREHEADERS
)
70 create_preheaders (CP_SIMPLE_PREHEADERS
);
72 /* Force all latches to have only single successor. */
73 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
74 force_single_succ_latches ();
76 /* Mark irreducible loops. */
77 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
78 mark_irreducible_loops ();
80 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
84 flow_loops_dump (dump_file
, NULL
, 1);
86 #ifdef ENABLE_CHECKING
87 verify_dominators (CDI_DOMINATORS
);
88 verify_loop_structure ();
92 /* Finalize loop structures. */
95 loop_optimizer_finalize (void)
101 gcc_assert (current_loops
!= NULL
);
103 FOR_EACH_LOOP (li
, loop
, 0)
105 free_simple_loop_desc (loop
);
109 if (current_loops
->state
& LOOPS_HAVE_RECORDED_EXITS
)
110 release_recorded_exits ();
111 flow_loops_free (current_loops
);
112 ggc_free (current_loops
);
113 current_loops
= NULL
;
117 bb
->loop_father
= NULL
;
121 #ifdef ENABLE_CHECKING
127 /* Gate for the RTL loop superpass. The actual passes are subpasses.
128 See passes.c for more on that. */
131 gate_handle_loop2 (void)
134 && (flag_move_loop_invariants
135 || flag_unswitch_loops
138 #ifdef HAVE_doloop_end
139 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
144 struct tree_opt_pass pass_loop2
=
147 gate_handle_loop2
, /* gate */
151 0, /* static_pass_number */
153 0, /* properties_required */
154 0, /* properties_provided */
155 0, /* properties_destroyed */
156 0, /* todo_flags_start */
158 TODO_ggc_collect
, /* todo_flags_finish */
163 /* Initialization of the RTL loop passes. */
167 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
170 dump_flow_info (dump_file
, dump_flags
);
172 loop_optimizer_init (LOOPS_NORMAL
);
176 struct tree_opt_pass pass_rtl_loop_init
=
178 "loop2_init", /* name */
180 rtl_loop_init
, /* execute */
183 0, /* static_pass_number */
185 0, /* properties_required */
186 0, /* properties_provided */
187 0, /* properties_destroyed */
188 0, /* todo_flags_start */
189 TODO_dump_func
, /* todo_flags_finish */
194 /* Finalization of the RTL loop passes. */
199 loop_optimizer_finalize ();
200 free_dominance_info (CDI_DOMINATORS
);
204 dump_flow_info (dump_file
, dump_flags
);
209 struct tree_opt_pass pass_rtl_loop_done
=
211 "loop2_done", /* name */
213 rtl_loop_done
, /* execute */
216 0, /* static_pass_number */
218 0, /* properties_required */
219 0, /* properties_provided */
220 0, /* properties_destroyed */
221 0, /* todo_flags_start */
222 TODO_dump_func
, /* todo_flags_finish */
227 /* Loop invariant code motion. */
229 gate_rtl_move_loop_invariants (void)
231 return flag_move_loop_invariants
;
235 rtl_move_loop_invariants (void)
237 if (number_of_loops () > 1)
238 move_loop_invariants ();
242 struct tree_opt_pass pass_rtl_move_loop_invariants
=
244 "loop2_invariant", /* name */
245 gate_rtl_move_loop_invariants
, /* gate */
246 rtl_move_loop_invariants
, /* execute */
249 0, /* static_pass_number */
251 0, /* properties_required */
252 0, /* properties_provided */
253 0, /* properties_destroyed */
254 0, /* todo_flags_start */
255 TODO_df_finish
| /* This is shutting down the instance in loop_invariant.c */
256 TODO_dump_func
, /* todo_flags_finish */
261 /* Loop unswitching for RTL. */
263 gate_rtl_unswitch (void)
265 return flag_unswitch_loops
;
271 if (number_of_loops () > 1)
276 struct tree_opt_pass pass_rtl_unswitch
=
278 "loop2_unswitch", /* name */
279 gate_rtl_unswitch
, /* gate */
280 rtl_unswitch
, /* execute */
283 0, /* static_pass_number */
285 0, /* properties_required */
286 0, /* properties_provided */
287 0, /* properties_destroyed */
288 0, /* todo_flags_start */
289 TODO_dump_func
, /* todo_flags_finish */
294 /* Loop unswitching for RTL. */
296 gate_rtl_unroll_and_peel_loops (void)
298 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
302 rtl_unroll_and_peel_loops (void)
304 if (number_of_loops () > 1)
312 if (flag_unroll_loops
)
314 if (flag_unroll_all_loops
)
315 flags
|= UAP_UNROLL_ALL
;
317 unroll_and_peel_loops (flags
);
322 struct tree_opt_pass pass_rtl_unroll_and_peel_loops
=
324 "loop2_unroll", /* name */
325 gate_rtl_unroll_and_peel_loops
, /* gate */
326 rtl_unroll_and_peel_loops
, /* execute */
329 0, /* static_pass_number */
331 0, /* properties_required */
332 0, /* properties_provided */
333 0, /* properties_destroyed */
334 0, /* todo_flags_start */
335 TODO_dump_func
, /* todo_flags_finish */
340 /* The doloop optimization. */
342 gate_rtl_doloop (void)
344 #ifdef HAVE_doloop_end
345 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
354 #ifdef HAVE_doloop_end
355 if (number_of_loops () > 1)
356 doloop_optimize_loops ();
361 struct tree_opt_pass pass_rtl_doloop
=
363 "loop2_doloop", /* name */
364 gate_rtl_doloop
, /* gate */
365 rtl_doloop
, /* execute */
368 0, /* static_pass_number */
370 0, /* properties_required */
371 0, /* properties_provided */
372 0, /* properties_destroyed */
373 0, /* todo_flags_start */
374 TODO_dump_func
, /* todo_flags_finish */