PR java/29812:
[official-gcc.git] / gcc / loop-init.c
blob1f049c1ad4de9c5175ac1c73ce24dac54e33a6ad
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
9 version.
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
14 for more details.
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
19 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "obstack.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "cfglayout.h"
31 #include "tree-pass.h"
32 #include "timevar.h"
33 #include "flags.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
38 loops. */
40 void
41 loop_optimizer_init (unsigned flags)
43 edge e;
44 edge_iterator ei;
45 static bool first_time = true;
46 struct loops *loops;
48 if (first_time)
50 first_time = false;
51 init_set_costs ();
54 gcc_assert (!current_loops);
55 loops = XCNEW (struct loops);
57 /* Avoid annoying special cases of edges going to exit
58 block. */
60 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
61 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
62 split_edge (e);
63 else
64 ei_next (&ei);
66 /* Find the loops. */
68 flow_loops_find (loops);
69 current_loops = loops;
71 if (number_of_loops () <= 1)
73 /* No loops (the 1 returned by number_of_loops corresponds to the fake
74 loop that we put as a root of the loop tree). */
75 loop_optimizer_finalize ();
76 return;
79 /* Create pre-headers. */
80 if (flags & LOOPS_HAVE_PREHEADERS)
81 create_preheaders (CP_SIMPLE_PREHEADERS);
83 /* Force all latches to have only single successor. */
84 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
85 force_single_succ_latches ();
87 /* Mark irreducible loops. */
88 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
89 mark_irreducible_loops ();
91 if (flags & LOOPS_HAVE_RECORDED_EXITS)
92 record_loop_exits ();
94 /* Dump loops. */
95 flow_loops_dump (dump_file, NULL, 1);
97 #ifdef ENABLE_CHECKING
98 verify_dominators (CDI_DOMINATORS);
99 verify_loop_structure ();
100 #endif
103 /* Finalize loop structures. */
105 void
106 loop_optimizer_finalize (void)
108 loop_iterator li;
109 struct loop *loop;
110 basic_block bb;
112 if (!current_loops)
113 return;
115 FOR_EACH_LOOP (li, loop, 0)
117 free_simple_loop_desc (loop);
120 /* Clean up. */
121 if (current_loops->state & LOOPS_HAVE_RECORDED_EXITS)
122 release_recorded_exits ();
123 flow_loops_free (current_loops);
124 free (current_loops);
125 current_loops = NULL;
127 FOR_ALL_BB (bb)
129 bb->loop_father = NULL;
132 /* Checking. */
133 #ifdef ENABLE_CHECKING
134 verify_flow_info ();
135 #endif
139 /* Gate for the RTL loop superpass. The actual passes are subpasses.
140 See passes.c for more on that. */
142 static bool
143 gate_handle_loop2 (void)
145 return (optimize > 0
146 && (flag_move_loop_invariants
147 || flag_unswitch_loops
148 || flag_peel_loops
149 || flag_unroll_loops
150 #ifdef HAVE_doloop_end
151 || (flag_branch_on_count_reg && HAVE_doloop_end)
152 #endif
156 struct tree_opt_pass pass_loop2 =
158 "loop2", /* name */
159 gate_handle_loop2, /* gate */
160 NULL, /* execute */
161 NULL, /* sub */
162 NULL, /* next */
163 0, /* static_pass_number */
164 TV_LOOP, /* tv_id */
165 0, /* properties_required */
166 0, /* properties_provided */
167 0, /* properties_destroyed */
168 0, /* todo_flags_start */
169 TODO_dump_func |
170 TODO_ggc_collect, /* todo_flags_finish */
171 'L' /* letter */
175 /* Initialization of the RTL loop passes. */
176 static unsigned int
177 rtl_loop_init (void)
179 if (dump_file)
180 dump_flow_info (dump_file, dump_flags);
182 /* Initialize structures for layout changes. */
183 cfg_layout_initialize (0);
185 loop_optimizer_init (LOOPS_NORMAL);
186 return 0;
189 struct tree_opt_pass pass_rtl_loop_init =
191 "loop2_init", /* name */
192 NULL, /* gate */
193 rtl_loop_init, /* execute */
194 NULL, /* sub */
195 NULL, /* next */
196 0, /* static_pass_number */
197 TV_LOOP, /* tv_id */
198 0, /* properties_required */
199 0, /* properties_provided */
200 0, /* properties_destroyed */
201 0, /* todo_flags_start */
202 TODO_dump_func, /* todo_flags_finish */
203 'L' /* letter */
207 /* Finalization of the RTL loop passes. */
209 static unsigned int
210 rtl_loop_done (void)
212 basic_block bb;
214 loop_optimizer_finalize ();
215 free_dominance_info (CDI_DOMINATORS);
217 /* Finalize layout changes. */
218 FOR_EACH_BB (bb)
219 if (bb->next_bb != EXIT_BLOCK_PTR)
220 bb->aux = bb->next_bb;
221 cfg_layout_finalize ();
223 cleanup_cfg (CLEANUP_EXPENSIVE);
224 delete_trivially_dead_insns (get_insns (), max_reg_num ());
225 reg_scan (get_insns (), max_reg_num ());
226 if (dump_file)
227 dump_flow_info (dump_file, dump_flags);
229 return 0;
232 struct tree_opt_pass pass_rtl_loop_done =
234 "loop2_done", /* name */
235 NULL, /* gate */
236 rtl_loop_done, /* execute */
237 NULL, /* sub */
238 NULL, /* next */
239 0, /* static_pass_number */
240 TV_LOOP, /* tv_id */
241 0, /* properties_required */
242 0, /* properties_provided */
243 0, /* properties_destroyed */
244 0, /* todo_flags_start */
245 TODO_dump_func, /* todo_flags_finish */
246 'L' /* letter */
250 /* Loop invariant code motion. */
251 static bool
252 gate_rtl_move_loop_invariants (void)
254 return flag_move_loop_invariants;
257 static unsigned int
258 rtl_move_loop_invariants (void)
260 if (current_loops)
261 move_loop_invariants ();
262 return 0;
265 struct tree_opt_pass pass_rtl_move_loop_invariants =
267 "loop2_invariant", /* name */
268 gate_rtl_move_loop_invariants, /* gate */
269 rtl_move_loop_invariants, /* execute */
270 NULL, /* sub */
271 NULL, /* next */
272 0, /* static_pass_number */
273 TV_LOOP, /* tv_id */
274 0, /* properties_required */
275 0, /* properties_provided */
276 0, /* properties_destroyed */
277 0, /* todo_flags_start */
278 TODO_dump_func, /* todo_flags_finish */
279 'L' /* letter */
283 /* Loop unswitching for RTL. */
284 static bool
285 gate_rtl_unswitch (void)
287 return flag_unswitch_loops;
290 static unsigned int
291 rtl_unswitch (void)
293 if (current_loops)
294 unswitch_loops ();
295 return 0;
298 struct tree_opt_pass pass_rtl_unswitch =
300 "loop2_unswitch", /* name */
301 gate_rtl_unswitch, /* gate */
302 rtl_unswitch, /* execute */
303 NULL, /* sub */
304 NULL, /* next */
305 0, /* static_pass_number */
306 TV_LOOP, /* tv_id */
307 0, /* properties_required */
308 0, /* properties_provided */
309 0, /* properties_destroyed */
310 0, /* todo_flags_start */
311 TODO_dump_func, /* todo_flags_finish */
312 'L' /* letter */
316 /* Loop unswitching for RTL. */
317 static bool
318 gate_rtl_unroll_and_peel_loops (void)
320 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
323 static unsigned int
324 rtl_unroll_and_peel_loops (void)
326 if (current_loops)
328 int flags = 0;
330 if (flag_peel_loops)
331 flags |= UAP_PEEL;
332 if (flag_unroll_loops)
333 flags |= UAP_UNROLL;
334 if (flag_unroll_all_loops)
335 flags |= UAP_UNROLL_ALL;
337 unroll_and_peel_loops (flags);
339 return 0;
342 struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
344 "loop2_unroll", /* name */
345 gate_rtl_unroll_and_peel_loops, /* gate */
346 rtl_unroll_and_peel_loops, /* execute */
347 NULL, /* sub */
348 NULL, /* next */
349 0, /* static_pass_number */
350 TV_LOOP, /* tv_id */
351 0, /* properties_required */
352 0, /* properties_provided */
353 0, /* properties_destroyed */
354 0, /* todo_flags_start */
355 TODO_dump_func, /* todo_flags_finish */
356 'L' /* letter */
360 /* The doloop optimization. */
361 static bool
362 gate_rtl_doloop (void)
364 #ifdef HAVE_doloop_end
365 return (flag_branch_on_count_reg && HAVE_doloop_end);
366 #else
367 return 0;
368 #endif
371 static unsigned int
372 rtl_doloop (void)
374 #ifdef HAVE_doloop_end
375 if (current_loops)
376 doloop_optimize_loops ();
377 #endif
378 return 0;
381 struct tree_opt_pass pass_rtl_doloop =
383 "loop2_doloop", /* name */
384 gate_rtl_doloop, /* gate */
385 rtl_doloop, /* execute */
386 NULL, /* sub */
387 NULL, /* next */
388 0, /* static_pass_number */
389 TV_LOOP, /* tv_id */
390 0, /* properties_required */
391 0, /* properties_provided */
392 0, /* properties_destroyed */
393 0, /* todo_flags_start */
394 TODO_dump_func, /* todo_flags_finish */
395 'L' /* letter */