2013-03-26 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / loop-init.c
blob664ff29dd5b89af91ad44873c81340c842f4f81a
1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002-2013 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
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl.h"
25 #include "regs.h"
26 #include "obstack.h"
27 #include "basic-block.h"
28 #include "cfgloop.h"
29 #include "tree-pass.h"
30 #include "flags.h"
31 #include "df.h"
32 #include "ggc.h"
35 /* Apply FLAGS to the loop state. */
37 static void
38 apply_loop_flags (unsigned flags)
40 if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
42 /* If the loops may have multiple latches, we cannot canonicalize
43 them further (and most of the loop manipulation functions will
44 not work). However, we avoid modifying cfg, which some
45 passes may want. */
46 gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
47 | LOOPS_HAVE_RECORDED_EXITS)) == 0);
48 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
50 else
51 disambiguate_loops_with_multiple_latches ();
53 /* Create pre-headers. */
54 if (flags & LOOPS_HAVE_PREHEADERS)
56 int cp_flags = CP_SIMPLE_PREHEADERS;
58 if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS)
59 cp_flags |= CP_FALLTHRU_PREHEADERS;
61 create_preheaders (cp_flags);
64 /* Force all latches to have only single successor. */
65 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
66 force_single_succ_latches ();
68 /* Mark irreducible loops. */
69 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
70 mark_irreducible_loops ();
72 if (flags & LOOPS_HAVE_RECORDED_EXITS)
73 record_loop_exits ();
76 /* Initialize loop structures. This is used by the tree and RTL loop
77 optimizers. FLAGS specify what properties to compute and/or ensure for
78 loops. */
80 void
81 loop_optimizer_init (unsigned flags)
83 timevar_push (TV_LOOP_INIT);
85 if (!current_loops)
87 gcc_assert (!(cfun->curr_properties & PROP_loops));
89 /* Find the loops. */
90 current_loops = flow_loops_find (NULL);
92 else
94 bool recorded_exits = loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS);
96 gcc_assert (cfun->curr_properties & PROP_loops);
98 /* Ensure that the dominators are computed, like flow_loops_find does. */
99 calculate_dominance_info (CDI_DOMINATORS);
101 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
103 loops_state_clear (~0U);
104 fix_loop_structure (NULL);
107 #ifdef ENABLE_CHECKING
108 else
109 verify_loop_structure ();
110 #endif
112 /* Clear all flags. */
113 if (recorded_exits)
114 release_recorded_exits ();
115 loops_state_clear (~0U);
118 /* Apply flags to loops. */
119 apply_loop_flags (flags);
121 /* Dump loops. */
122 flow_loops_dump (dump_file, NULL, 1);
124 #ifdef ENABLE_CHECKING
125 verify_loop_structure ();
126 #endif
128 timevar_pop (TV_LOOP_INIT);
131 /* Finalize loop structures. */
133 void
134 loop_optimizer_finalize (void)
136 loop_iterator li;
137 struct loop *loop;
138 basic_block bb;
140 timevar_push (TV_LOOP_FINI);
142 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
143 release_recorded_exits ();
145 /* If we should preserve loop structure, do not free it but clear
146 flags that advanced properties are there as we are not preserving
147 that in full. */
148 if (cfun->curr_properties & PROP_loops)
150 loops_state_clear (LOOP_CLOSED_SSA
151 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
152 | LOOPS_HAVE_PREHEADERS
153 | LOOPS_HAVE_SIMPLE_LATCHES
154 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
155 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
156 goto loop_fini_done;
159 gcc_assert (current_loops != NULL);
161 FOR_EACH_LOOP (li, loop, 0)
163 free_simple_loop_desc (loop);
166 /* Clean up. */
167 flow_loops_free (current_loops);
168 ggc_free (current_loops);
169 current_loops = NULL;
171 FOR_ALL_BB (bb)
173 bb->loop_father = NULL;
176 loop_fini_done:
177 timevar_pop (TV_LOOP_FINI);
180 /* The structure of loops might have changed. Some loops might get removed
181 (and their headers and latches were set to NULL), loop exists might get
182 removed (thus the loop nesting may be wrong), and some blocks and edges
183 were changed (so the information about bb --> loop mapping does not have
184 to be correct). But still for the remaining loops the header dominates
185 the latch, and loops did not get new subloops (new loops might possibly
186 get created, but we are not interested in them). Fix up the mess.
188 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
189 marked in it.
191 Returns the number of new discovered loops. */
193 unsigned
194 fix_loop_structure (bitmap changed_bbs)
196 basic_block bb;
197 int record_exits = 0;
198 loop_iterator li;
199 struct loop *loop;
200 unsigned old_nloops, i;
202 timevar_push (TV_LOOP_INIT);
204 /* We need exact and fast dominance info to be available. */
205 gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
207 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
209 release_recorded_exits ();
210 record_exits = LOOPS_HAVE_RECORDED_EXITS;
213 /* Remember the depth of the blocks in the loop hierarchy, so that we can
214 recognize blocks whose loop nesting relationship has changed. */
215 if (changed_bbs)
216 FOR_EACH_BB (bb)
217 bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
219 /* Remove the dead loops from structures. We start from the innermost
220 loops, so that when we remove the loops, we know that the loops inside
221 are preserved, and do not waste time relinking loops that will be
222 removed later. */
223 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
225 /* Detect the case that the loop is no longer present even though
226 it wasn't marked for removal.
227 ??? If we do that we can get away with not marking loops for
228 removal at all. And possibly avoid some spurious removals. */
229 if (loop->header
230 && bb_loop_header_p (loop->header))
231 continue;
233 if (dump_file && (dump_flags & TDF_DETAILS))
234 fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
235 loop->num);
237 while (loop->inner)
239 struct loop *ploop = loop->inner;
240 flow_loop_tree_node_remove (ploop);
241 flow_loop_tree_node_add (loop_outer (loop), ploop);
244 /* Remove the loop. */
245 loop->header = NULL;
246 flow_loop_tree_node_remove (loop);
249 /* Remember the number of loops so we can return how many new loops
250 flow_loops_find discovered. */
251 old_nloops = number_of_loops ();
253 /* Re-compute loop structure in-place. */
254 flow_loops_find (current_loops);
256 /* Mark the blocks whose loop has changed. */
257 if (changed_bbs)
259 FOR_EACH_BB (bb)
261 if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
262 bitmap_set_bit (changed_bbs, bb->index);
264 bb->aux = NULL;
268 /* Finally free deleted loops. */
269 FOR_EACH_VEC_ELT (*get_loops (), i, loop)
270 if (loop && loop->header == NULL)
272 (*get_loops ())[i] = NULL;
273 flow_loop_free (loop);
276 loops_state_clear (LOOPS_NEED_FIXUP);
278 /* Apply flags to loops. */
279 apply_loop_flags (current_loops->state | record_exits);
281 #ifdef ENABLE_CHECKING
282 verify_loop_structure ();
283 #endif
285 timevar_pop (TV_LOOP_INIT);
287 return number_of_loops () - old_nloops;
290 /* Gate for the RTL loop superpass. The actual passes are subpasses.
291 See passes.c for more on that. */
293 static bool
294 gate_handle_loop2 (void)
296 if (optimize > 0
297 && (flag_move_loop_invariants
298 || flag_unswitch_loops
299 || flag_peel_loops
300 || flag_unroll_loops
301 #ifdef HAVE_doloop_end
302 || (flag_branch_on_count_reg && HAVE_doloop_end)
303 #endif
305 return true;
306 else
308 /* No longer preserve loops, remove them now. */
309 cfun->curr_properties &= ~PROP_loops;
310 if (current_loops)
311 loop_optimizer_finalize ();
312 return false;
316 struct rtl_opt_pass pass_loop2 =
319 RTL_PASS,
320 "loop2", /* name */
321 OPTGROUP_LOOP, /* optinfo_flags */
322 gate_handle_loop2, /* gate */
323 NULL, /* execute */
324 NULL, /* sub */
325 NULL, /* next */
326 0, /* static_pass_number */
327 TV_LOOP, /* tv_id */
328 0, /* properties_required */
329 0, /* properties_provided */
330 0, /* properties_destroyed */
331 0, /* todo_flags_start */
332 0 /* todo_flags_finish */
337 /* Initialization of the RTL loop passes. */
338 static unsigned int
339 rtl_loop_init (void)
341 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
343 if (dump_file)
345 dump_reg_info (dump_file);
346 dump_flow_info (dump_file, dump_flags);
349 loop_optimizer_init (LOOPS_NORMAL);
350 return 0;
353 struct rtl_opt_pass pass_rtl_loop_init =
356 RTL_PASS,
357 "loop2_init", /* name */
358 OPTGROUP_LOOP, /* optinfo_flags */
359 NULL, /* gate */
360 rtl_loop_init, /* execute */
361 NULL, /* sub */
362 NULL, /* next */
363 0, /* static_pass_number */
364 TV_LOOP, /* tv_id */
365 0, /* properties_required */
366 0, /* properties_provided */
367 0, /* properties_destroyed */
368 0, /* todo_flags_start */
369 TODO_verify_rtl_sharing /* todo_flags_finish */
374 /* Finalization of the RTL loop passes. */
376 static unsigned int
377 rtl_loop_done (void)
379 /* No longer preserve loops, remove them now. */
380 cfun->curr_properties &= ~PROP_loops;
381 loop_optimizer_finalize ();
382 free_dominance_info (CDI_DOMINATORS);
384 cleanup_cfg (0);
385 if (dump_file)
387 dump_reg_info (dump_file);
388 dump_flow_info (dump_file, dump_flags);
391 return 0;
394 struct rtl_opt_pass pass_rtl_loop_done =
397 RTL_PASS,
398 "loop2_done", /* name */
399 OPTGROUP_LOOP, /* optinfo_flags */
400 NULL, /* gate */
401 rtl_loop_done, /* execute */
402 NULL, /* sub */
403 NULL, /* next */
404 0, /* static_pass_number */
405 TV_LOOP, /* tv_id */
406 0, /* properties_required */
407 0, /* properties_provided */
408 PROP_loops, /* properties_destroyed */
409 0, /* todo_flags_start */
410 TODO_verify_flow
411 | TODO_verify_rtl_sharing /* todo_flags_finish */
416 /* Loop invariant code motion. */
417 static bool
418 gate_rtl_move_loop_invariants (void)
420 return flag_move_loop_invariants;
423 static unsigned int
424 rtl_move_loop_invariants (void)
426 if (number_of_loops () > 1)
427 move_loop_invariants ();
428 return 0;
431 struct rtl_opt_pass pass_rtl_move_loop_invariants =
434 RTL_PASS,
435 "loop2_invariant", /* name */
436 OPTGROUP_LOOP, /* optinfo_flags */
437 gate_rtl_move_loop_invariants, /* gate */
438 rtl_move_loop_invariants, /* execute */
439 NULL, /* sub */
440 NULL, /* next */
441 0, /* static_pass_number */
442 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
443 0, /* properties_required */
444 0, /* properties_provided */
445 0, /* properties_destroyed */
446 0, /* todo_flags_start */
447 TODO_df_verify |
448 TODO_df_finish | TODO_verify_rtl_sharing /* todo_flags_finish */
453 /* Loop unswitching for RTL. */
454 static bool
455 gate_rtl_unswitch (void)
457 return flag_unswitch_loops;
460 static unsigned int
461 rtl_unswitch (void)
463 if (number_of_loops () > 1)
464 unswitch_loops ();
465 return 0;
468 struct rtl_opt_pass pass_rtl_unswitch =
471 RTL_PASS,
472 "loop2_unswitch", /* name */
473 OPTGROUP_LOOP, /* optinfo_flags */
474 gate_rtl_unswitch, /* gate */
475 rtl_unswitch, /* execute */
476 NULL, /* sub */
477 NULL, /* next */
478 0, /* static_pass_number */
479 TV_LOOP_UNSWITCH, /* tv_id */
480 0, /* properties_required */
481 0, /* properties_provided */
482 0, /* properties_destroyed */
483 0, /* todo_flags_start */
484 TODO_verify_rtl_sharing, /* todo_flags_finish */
489 /* Loop unswitching for RTL. */
490 static bool
491 gate_rtl_unroll_and_peel_loops (void)
493 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
496 static unsigned int
497 rtl_unroll_and_peel_loops (void)
499 if (number_of_loops () > 1)
501 int flags = 0;
502 if (dump_file)
503 df_dump (dump_file);
505 if (flag_peel_loops)
506 flags |= UAP_PEEL;
507 if (flag_unroll_loops)
508 flags |= UAP_UNROLL;
509 if (flag_unroll_all_loops)
510 flags |= UAP_UNROLL_ALL;
512 unroll_and_peel_loops (flags);
514 return 0;
517 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops =
520 RTL_PASS,
521 "loop2_unroll", /* name */
522 OPTGROUP_LOOP, /* optinfo_flags */
523 gate_rtl_unroll_and_peel_loops, /* gate */
524 rtl_unroll_and_peel_loops, /* execute */
525 NULL, /* sub */
526 NULL, /* next */
527 0, /* static_pass_number */
528 TV_LOOP_UNROLL, /* tv_id */
529 0, /* properties_required */
530 0, /* properties_provided */
531 0, /* properties_destroyed */
532 0, /* todo_flags_start */
533 TODO_verify_rtl_sharing, /* todo_flags_finish */
538 /* The doloop optimization. */
539 static bool
540 gate_rtl_doloop (void)
542 #ifdef HAVE_doloop_end
543 return (flag_branch_on_count_reg && HAVE_doloop_end);
544 #else
545 return 0;
546 #endif
549 static unsigned int
550 rtl_doloop (void)
552 #ifdef HAVE_doloop_end
553 if (number_of_loops () > 1)
554 doloop_optimize_loops ();
555 #endif
556 return 0;
559 struct rtl_opt_pass pass_rtl_doloop =
562 RTL_PASS,
563 "loop2_doloop", /* name */
564 OPTGROUP_LOOP, /* optinfo_flags */
565 gate_rtl_doloop, /* gate */
566 rtl_doloop, /* execute */
567 NULL, /* sub */
568 NULL, /* next */
569 0, /* static_pass_number */
570 TV_LOOP_DOLOOP, /* tv_id */
571 0, /* properties_required */
572 0, /* properties_provided */
573 0, /* properties_destroyed */
574 0, /* todo_flags_start */
575 TODO_verify_rtl_sharing /* todo_flags_finish */