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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "hard-reg-set.h"
27 #include "basic-block.h"
29 #include "cfglayout.h"
30 #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 (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
56 /* If the loops may have multiple latches, we cannot canonicalize
57 them further (and most of the loop manipulation functions will
58 not work). However, we avoid modifying cfg, which some
60 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
61 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
62 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
65 disambiguate_loops_with_multiple_latches ();
67 /* Create pre-headers. */
68 if (flags
& LOOPS_HAVE_PREHEADERS
)
69 create_preheaders (CP_SIMPLE_PREHEADERS
);
71 /* Force all latches to have only single successor. */
72 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
73 force_single_succ_latches ();
75 /* Mark irreducible loops. */
76 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
77 mark_irreducible_loops ();
79 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
83 flow_loops_dump (dump_file
, NULL
, 1);
85 #ifdef ENABLE_CHECKING
86 verify_dominators (CDI_DOMINATORS
);
87 verify_loop_structure ();
91 /* Finalize loop structures. */
94 loop_optimizer_finalize (void)
100 gcc_assert (current_loops
!= NULL
);
102 FOR_EACH_LOOP (li
, loop
, 0)
104 free_simple_loop_desc (loop
);
108 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
109 release_recorded_exits ();
110 flow_loops_free (current_loops
);
111 ggc_free (current_loops
);
112 current_loops
= NULL
;
116 bb
->loop_father
= NULL
;
120 #ifdef ENABLE_CHECKING
126 /* Gate for the RTL loop superpass. The actual passes are subpasses.
127 See passes.c for more on that. */
130 gate_handle_loop2 (void)
133 && (flag_move_loop_invariants
134 || flag_unswitch_loops
137 #ifdef HAVE_doloop_end
138 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
143 struct tree_opt_pass pass_loop2
=
146 gate_handle_loop2
, /* gate */
150 0, /* static_pass_number */
152 0, /* properties_required */
153 0, /* properties_provided */
154 0, /* properties_destroyed */
155 0, /* todo_flags_start */
157 TODO_ggc_collect
, /* todo_flags_finish */
162 /* Initialization of the RTL loop passes. */
166 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
169 dump_flow_info (dump_file
, dump_flags
);
171 loop_optimizer_init (LOOPS_NORMAL
);
175 struct tree_opt_pass pass_rtl_loop_init
=
177 "loop2_init", /* name */
179 rtl_loop_init
, /* execute */
182 0, /* static_pass_number */
184 0, /* properties_required */
185 0, /* properties_provided */
186 0, /* properties_destroyed */
187 0, /* todo_flags_start */
188 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
193 /* Finalization of the RTL loop passes. */
198 loop_optimizer_finalize ();
199 free_dominance_info (CDI_DOMINATORS
);
203 dump_flow_info (dump_file
, dump_flags
);
208 struct tree_opt_pass pass_rtl_loop_done
=
210 "loop2_done", /* name */
212 rtl_loop_done
, /* execute */
215 0, /* static_pass_number */
217 0, /* properties_required */
218 0, /* properties_provided */
219 0, /* properties_destroyed */
220 0, /* todo_flags_start */
221 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
226 /* Loop invariant code motion. */
228 gate_rtl_move_loop_invariants (void)
230 return flag_move_loop_invariants
;
234 rtl_move_loop_invariants (void)
236 if (number_of_loops () > 1)
237 move_loop_invariants ();
241 struct tree_opt_pass pass_rtl_move_loop_invariants
=
243 "loop2_invariant", /* name */
244 gate_rtl_move_loop_invariants
, /* gate */
245 rtl_move_loop_invariants
, /* execute */
248 0, /* static_pass_number */
250 0, /* properties_required */
251 0, /* properties_provided */
252 0, /* properties_destroyed */
253 0, /* todo_flags_start */
255 TODO_df_finish
| TODO_verify_rtl_sharing
|
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_verify_rtl_sharing
, /* 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_verify_rtl_sharing
, /* 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_verify_rtl_sharing
, /* todo_flags_finish */