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"
36 /* Initialize loop structures. This is used by the tree and RTL loop
37 optimizers. FLAGS specify what properties to compute and/or ensure for
41 loop_optimizer_init (unsigned flags
)
45 gcc_assert (!current_loops
);
46 loops
= XCNEW (struct loops
);
50 flow_loops_find (loops
);
51 current_loops
= loops
;
53 if (number_of_loops () <= 1)
55 /* No loops (the 1 returned by number_of_loops corresponds to the fake
56 loop that we put as a root of the loop tree). */
57 loop_optimizer_finalize ();
61 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
63 /* If the loops may have multiple latches, we cannot canonicalize
64 them further (and most of the loop manipulation functions will
65 not work). However, we avoid modifying cfg, which some
67 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
68 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
69 current_loops
->state
= LOOPS_MAY_HAVE_MULTIPLE_LATCHES
;
72 disambiguate_loops_with_multiple_latches ();
74 /* Create pre-headers. */
75 if (flags
& LOOPS_HAVE_PREHEADERS
)
76 create_preheaders (CP_SIMPLE_PREHEADERS
);
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)
110 FOR_EACH_LOOP (li
, loop
, 0)
112 free_simple_loop_desc (loop
);
116 if (current_loops
->state
& LOOPS_HAVE_RECORDED_EXITS
)
117 release_recorded_exits ();
118 flow_loops_free (current_loops
);
119 free (current_loops
);
120 current_loops
= NULL
;
124 bb
->loop_father
= NULL
;
128 #ifdef ENABLE_CHECKING
134 /* Gate for the RTL loop superpass. The actual passes are subpasses.
135 See passes.c for more on that. */
138 gate_handle_loop2 (void)
141 && (flag_move_loop_invariants
142 || flag_unswitch_loops
145 #ifdef HAVE_doloop_end
146 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
151 struct tree_opt_pass pass_loop2
=
154 gate_handle_loop2
, /* gate */
158 0, /* static_pass_number */
160 0, /* properties_required */
161 0, /* properties_provided */
162 0, /* properties_destroyed */
163 0, /* todo_flags_start */
165 TODO_ggc_collect
, /* todo_flags_finish */
170 /* Initialization of the RTL loop passes. */
174 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
177 dump_flow_info (dump_file
, dump_flags
);
179 loop_optimizer_init (LOOPS_NORMAL
);
183 struct tree_opt_pass pass_rtl_loop_init
=
185 "loop2_init", /* name */
187 rtl_loop_init
, /* execute */
190 0, /* static_pass_number */
192 0, /* properties_required */
193 0, /* properties_provided */
194 0, /* properties_destroyed */
195 0, /* todo_flags_start */
196 TODO_dump_func
, /* todo_flags_finish */
201 /* Finalization of the RTL loop passes. */
206 loop_optimizer_finalize ();
207 free_dominance_info (CDI_DOMINATORS
);
209 cleanup_cfg (CLEANUP_EXPENSIVE
);
210 delete_trivially_dead_insns (get_insns (), max_reg_num ());
211 reg_scan (get_insns (), max_reg_num ());
213 dump_flow_info (dump_file
, dump_flags
);
218 struct tree_opt_pass pass_rtl_loop_done
=
220 "loop2_done", /* name */
222 rtl_loop_done
, /* execute */
225 0, /* static_pass_number */
227 0, /* properties_required */
228 0, /* properties_provided */
229 0, /* properties_destroyed */
230 0, /* todo_flags_start */
231 TODO_dump_func
, /* todo_flags_finish */
236 /* Loop invariant code motion. */
238 gate_rtl_move_loop_invariants (void)
240 return flag_move_loop_invariants
;
244 rtl_move_loop_invariants (void)
247 move_loop_invariants ();
251 struct tree_opt_pass pass_rtl_move_loop_invariants
=
253 "loop2_invariant", /* name */
254 gate_rtl_move_loop_invariants
, /* gate */
255 rtl_move_loop_invariants
, /* execute */
258 0, /* static_pass_number */
260 0, /* properties_required */
261 0, /* properties_provided */
262 0, /* properties_destroyed */
263 0, /* todo_flags_start */
264 TODO_dump_func
, /* todo_flags_finish */
269 /* Loop unswitching for RTL. */
271 gate_rtl_unswitch (void)
273 return flag_unswitch_loops
;
284 struct tree_opt_pass pass_rtl_unswitch
=
286 "loop2_unswitch", /* name */
287 gate_rtl_unswitch
, /* gate */
288 rtl_unswitch
, /* execute */
291 0, /* static_pass_number */
293 0, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 TODO_dump_func
, /* todo_flags_finish */
302 /* Loop unswitching for RTL. */
304 gate_rtl_unroll_and_peel_loops (void)
306 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
310 rtl_unroll_and_peel_loops (void)
318 if (flag_unroll_loops
)
320 if (flag_unroll_all_loops
)
321 flags
|= UAP_UNROLL_ALL
;
323 unroll_and_peel_loops (flags
);
328 struct tree_opt_pass pass_rtl_unroll_and_peel_loops
=
330 "loop2_unroll", /* name */
331 gate_rtl_unroll_and_peel_loops
, /* gate */
332 rtl_unroll_and_peel_loops
, /* execute */
335 0, /* static_pass_number */
337 0, /* properties_required */
338 0, /* properties_provided */
339 0, /* properties_destroyed */
340 0, /* todo_flags_start */
341 TODO_dump_func
, /* todo_flags_finish */
346 /* The doloop optimization. */
348 gate_rtl_doloop (void)
350 #ifdef HAVE_doloop_end
351 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
360 #ifdef HAVE_doloop_end
362 doloop_optimize_loops ();
367 struct tree_opt_pass pass_rtl_doloop
=
369 "loop2_doloop", /* name */
370 gate_rtl_doloop
, /* gate */
371 rtl_doloop
, /* execute */
374 0, /* static_pass_number */
376 0, /* properties_required */
377 0, /* properties_provided */
378 0, /* properties_destroyed */
379 0, /* todo_flags_start */
380 TODO_dump_func
, /* todo_flags_finish */