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
)
70 int cp_flags
= CP_SIMPLE_PREHEADERS
;
72 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
73 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
75 create_preheaders (cp_flags
);
78 /* Force all latches to have only single successor. */
79 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
80 force_single_succ_latches ();
82 /* Mark irreducible loops. */
83 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
84 mark_irreducible_loops ();
86 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
90 flow_loops_dump (dump_file
, NULL
, 1);
92 #ifdef ENABLE_CHECKING
93 verify_dominators (CDI_DOMINATORS
);
94 verify_loop_structure ();
98 /* Finalize loop structures. */
101 loop_optimizer_finalize (void)
107 gcc_assert (current_loops
!= NULL
);
109 FOR_EACH_LOOP (li
, loop
, 0)
111 free_simple_loop_desc (loop
);
115 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
116 release_recorded_exits ();
117 flow_loops_free (current_loops
);
118 ggc_free (current_loops
);
119 current_loops
= NULL
;
123 bb
->loop_father
= NULL
;
127 #ifdef ENABLE_CHECKING
128 /* FIXME: no point to verify flow info after bundling on ia64. Use this
129 hack for achieving this. */
130 if (!reload_completed
)
136 /* Gate for the RTL loop superpass. The actual passes are subpasses.
137 See passes.c for more on that. */
140 gate_handle_loop2 (void)
143 && (flag_move_loop_invariants
144 || flag_unswitch_loops
147 #ifdef HAVE_doloop_end
148 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
153 struct rtl_opt_pass pass_loop2
=
158 gate_handle_loop2
, /* gate */
162 0, /* static_pass_number */
164 0, /* properties_required */
165 0, /* properties_provided */
166 0, /* properties_destroyed */
167 0, /* todo_flags_start */
169 TODO_ggc_collect
/* todo_flags_finish */
174 /* Initialization of the RTL loop passes. */
178 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
181 dump_flow_info (dump_file
, dump_flags
);
183 loop_optimizer_init (LOOPS_NORMAL
);
187 struct rtl_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_verify_rtl_sharing
/* todo_flags_finish */
207 /* Finalization of the RTL loop passes. */
212 loop_optimizer_finalize ();
213 free_dominance_info (CDI_DOMINATORS
);
217 dump_flow_info (dump_file
, dump_flags
);
222 struct rtl_opt_pass pass_rtl_loop_done
=
226 "loop2_done", /* name */
228 rtl_loop_done
, /* execute */
231 0, /* static_pass_number */
233 0, /* properties_required */
234 0, /* properties_provided */
235 0, /* properties_destroyed */
236 0, /* todo_flags_start */
237 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */
242 /* Loop invariant code motion. */
244 gate_rtl_move_loop_invariants (void)
246 return flag_move_loop_invariants
;
250 rtl_move_loop_invariants (void)
252 if (number_of_loops () > 1)
253 move_loop_invariants ();
257 struct rtl_opt_pass pass_rtl_move_loop_invariants
=
261 "loop2_invariant", /* name */
262 gate_rtl_move_loop_invariants
, /* gate */
263 rtl_move_loop_invariants
, /* execute */
266 0, /* static_pass_number */
268 0, /* properties_required */
269 0, /* properties_provided */
270 0, /* properties_destroyed */
271 0, /* todo_flags_start */
273 TODO_df_finish
| TODO_verify_rtl_sharing
|
274 TODO_dump_func
/* todo_flags_finish */
279 /* Loop unswitching for RTL. */
281 gate_rtl_unswitch (void)
283 return flag_unswitch_loops
;
289 if (number_of_loops () > 1)
294 struct rtl_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_verify_rtl_sharing
, /* 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)
324 if (number_of_loops () > 1)
332 if (flag_unroll_loops
)
334 if (flag_unroll_all_loops
)
335 flags
|= UAP_UNROLL_ALL
;
337 unroll_and_peel_loops (flags
);
342 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops
=
346 "loop2_unroll", /* name */
347 gate_rtl_unroll_and_peel_loops
, /* gate */
348 rtl_unroll_and_peel_loops
, /* execute */
351 0, /* static_pass_number */
353 0, /* properties_required */
354 0, /* properties_provided */
355 0, /* properties_destroyed */
356 0, /* todo_flags_start */
357 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
362 /* The doloop optimization. */
364 gate_rtl_doloop (void)
366 #ifdef HAVE_doloop_end
367 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
376 #ifdef HAVE_doloop_end
377 if (number_of_loops () > 1)
378 doloop_optimize_loops ();
383 struct rtl_opt_pass pass_rtl_doloop
=
387 "loop2_doloop", /* name */
388 gate_rtl_doloop
, /* gate */
389 rtl_doloop
, /* execute */
392 0, /* static_pass_number */
394 0, /* properties_required */
395 0, /* properties_provided */
396 0, /* properties_destroyed */
397 0, /* todo_flags_start */
398 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */