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 rtl_opt_pass pass_loop2
=
148 gate_handle_loop2
, /* gate */
152 0, /* static_pass_number */
154 0, /* properties_required */
155 0, /* properties_provided */
156 0, /* properties_destroyed */
157 0, /* todo_flags_start */
159 TODO_ggc_collect
/* todo_flags_finish */
164 /* Initialization of the RTL loop passes. */
168 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
171 dump_flow_info (dump_file
, dump_flags
);
173 loop_optimizer_init (LOOPS_NORMAL
);
177 struct rtl_opt_pass pass_rtl_loop_init
=
181 "loop2_init", /* name */
183 rtl_loop_init
, /* execute */
186 0, /* static_pass_number */
188 0, /* properties_required */
189 0, /* properties_provided */
190 0, /* properties_destroyed */
191 0, /* todo_flags_start */
192 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */
197 /* Finalization of the RTL loop passes. */
202 loop_optimizer_finalize ();
203 free_dominance_info (CDI_DOMINATORS
);
207 dump_flow_info (dump_file
, dump_flags
);
212 struct rtl_opt_pass pass_rtl_loop_done
=
216 "loop2_done", /* name */
218 rtl_loop_done
, /* execute */
221 0, /* static_pass_number */
223 0, /* properties_required */
224 0, /* properties_provided */
225 0, /* properties_destroyed */
226 0, /* todo_flags_start */
227 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */
232 /* Loop invariant code motion. */
234 gate_rtl_move_loop_invariants (void)
236 return flag_move_loop_invariants
;
240 rtl_move_loop_invariants (void)
242 if (number_of_loops () > 1)
243 move_loop_invariants ();
247 struct rtl_opt_pass pass_rtl_move_loop_invariants
=
251 "loop2_invariant", /* name */
252 gate_rtl_move_loop_invariants
, /* gate */
253 rtl_move_loop_invariants
, /* execute */
256 0, /* static_pass_number */
258 0, /* properties_required */
259 0, /* properties_provided */
260 0, /* properties_destroyed */
261 0, /* todo_flags_start */
263 TODO_df_finish
| TODO_verify_rtl_sharing
|
264 TODO_dump_func
/* todo_flags_finish */
269 /* Loop unswitching for RTL. */
271 gate_rtl_unswitch (void)
273 return flag_unswitch_loops
;
279 if (number_of_loops () > 1)
284 struct rtl_opt_pass pass_rtl_unswitch
=
288 "loop2_unswitch", /* name */
289 gate_rtl_unswitch
, /* gate */
290 rtl_unswitch
, /* execute */
293 0, /* static_pass_number */
295 0, /* properties_required */
296 0, /* properties_provided */
297 0, /* properties_destroyed */
298 0, /* todo_flags_start */
299 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
304 /* Loop unswitching for RTL. */
306 gate_rtl_unroll_and_peel_loops (void)
308 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
312 rtl_unroll_and_peel_loops (void)
314 if (number_of_loops () > 1)
322 if (flag_unroll_loops
)
324 if (flag_unroll_all_loops
)
325 flags
|= UAP_UNROLL_ALL
;
327 unroll_and_peel_loops (flags
);
332 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops
=
336 "loop2_unroll", /* name */
337 gate_rtl_unroll_and_peel_loops
, /* gate */
338 rtl_unroll_and_peel_loops
, /* execute */
341 0, /* static_pass_number */
343 0, /* properties_required */
344 0, /* properties_provided */
345 0, /* properties_destroyed */
346 0, /* todo_flags_start */
347 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
352 /* The doloop optimization. */
354 gate_rtl_doloop (void)
356 #ifdef HAVE_doloop_end
357 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
366 #ifdef HAVE_doloop_end
367 if (number_of_loops () > 1)
368 doloop_optimize_loops ();
373 struct rtl_opt_pass pass_rtl_doloop
=
377 "loop2_doloop", /* name */
378 gate_rtl_doloop
, /* gate */
379 rtl_doloop
, /* execute */
382 0, /* static_pass_number */
384 0, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */