PR libstdc++/56278
[official-gcc.git] / gcc / loop-init.c
blobd64c110c1b5dc1d6ae35b756df871cbbda58b668
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 gcc_assert (cfun->curr_properties & PROP_loops);
96 /* Ensure that the dominators are computed, like flow_loops_find does. */
97 calculate_dominance_info (CDI_DOMINATORS);
99 #ifdef ENABLE_CHECKING
100 verify_loop_structure ();
101 #endif
104 /* Apply flags to loops. */
105 apply_loop_flags (flags);
107 /* Dump loops. */
108 flow_loops_dump (dump_file, NULL, 1);
110 #ifdef ENABLE_CHECKING
111 verify_loop_structure ();
112 #endif
114 timevar_pop (TV_LOOP_INIT);
117 /* Finalize loop structures. */
119 void
120 loop_optimizer_finalize (void)
122 loop_iterator li;
123 struct loop *loop;
124 basic_block bb;
126 timevar_push (TV_LOOP_FINI);
128 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
129 release_recorded_exits ();
131 /* If we should preserve loop structure, do not free it but clear
132 flags that advanced properties are there as we are not preserving
133 that in full. */
134 if (cfun->curr_properties & PROP_loops)
136 loops_state_clear (LOOP_CLOSED_SSA
137 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
138 | LOOPS_HAVE_PREHEADERS
139 | LOOPS_HAVE_SIMPLE_LATCHES
140 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
141 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
142 goto loop_fini_done;
145 gcc_assert (current_loops != NULL);
147 FOR_EACH_LOOP (li, loop, 0)
149 free_simple_loop_desc (loop);
152 /* Clean up. */
153 flow_loops_free (current_loops);
154 ggc_free (current_loops);
155 current_loops = NULL;
157 FOR_ALL_BB (bb)
159 bb->loop_father = NULL;
162 loop_fini_done:
163 timevar_pop (TV_LOOP_FINI);
166 /* The structure of loops might have changed. Some loops might get removed
167 (and their headers and latches were set to NULL), loop exists might get
168 removed (thus the loop nesting may be wrong), and some blocks and edges
169 were changed (so the information about bb --> loop mapping does not have
170 to be correct). But still for the remaining loops the header dominates
171 the latch, and loops did not get new subloops (new loops might possibly
172 get created, but we are not interested in them). Fix up the mess.
174 If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
175 marked in it. */
177 void
178 fix_loop_structure (bitmap changed_bbs)
180 basic_block bb;
181 int record_exits = 0;
182 loop_iterator li;
183 struct loop *loop;
185 timevar_push (TV_LOOP_INIT);
187 /* We need exact and fast dominance info to be available. */
188 gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
190 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
192 release_recorded_exits ();
193 record_exits = LOOPS_HAVE_RECORDED_EXITS;
196 /* Remember the depth of the blocks in the loop hierarchy, so that we can
197 recognize blocks whose loop nesting relationship has changed. */
198 if (changed_bbs)
199 FOR_EACH_BB (bb)
200 bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
202 /* Remove the dead loops from structures. We start from the innermost
203 loops, so that when we remove the loops, we know that the loops inside
204 are preserved, and do not waste time relinking loops that will be
205 removed later. */
206 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
208 /* Detect the case that the loop is no longer present even though
209 it wasn't marked for removal.
210 ??? If we do that we can get away with not marking loops for
211 removal at all. And possibly avoid some spurious removals. */
212 if (loop->header
213 && bb_loop_header_p (loop->header))
214 continue;
216 if (dump_file && (dump_flags & TDF_DETAILS))
217 fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
218 loop->num);
220 while (loop->inner)
222 struct loop *ploop = loop->inner;
223 flow_loop_tree_node_remove (ploop);
224 flow_loop_tree_node_add (loop_outer (loop), ploop);
227 /* Remove the loop and free its data. */
228 delete_loop (loop);
231 /* Re-compute loop structure in-place. */
232 flow_loops_find (current_loops);
234 /* Mark the blocks whose loop has changed. */
235 if (changed_bbs)
237 FOR_EACH_BB (bb)
239 if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
240 bitmap_set_bit (changed_bbs, bb->index);
242 bb->aux = NULL;
246 loops_state_clear (LOOPS_NEED_FIXUP);
248 /* Apply flags to loops. */
249 apply_loop_flags (current_loops->state | record_exits);
251 #ifdef ENABLE_CHECKING
252 verify_loop_structure ();
253 #endif
255 timevar_pop (TV_LOOP_INIT);
258 /* Gate for the RTL loop superpass. The actual passes are subpasses.
259 See passes.c for more on that. */
261 static bool
262 gate_handle_loop2 (void)
264 if (optimize > 0
265 && (flag_move_loop_invariants
266 || flag_unswitch_loops
267 || flag_peel_loops
268 || flag_unroll_loops
269 #ifdef HAVE_doloop_end
270 || (flag_branch_on_count_reg && HAVE_doloop_end)
271 #endif
273 return true;
274 else
276 /* No longer preserve loops, remove them now. */
277 cfun->curr_properties &= ~PROP_loops;
278 if (current_loops)
279 loop_optimizer_finalize ();
280 return false;
284 struct rtl_opt_pass pass_loop2 =
287 RTL_PASS,
288 "loop2", /* name */
289 OPTGROUP_LOOP, /* optinfo_flags */
290 gate_handle_loop2, /* gate */
291 NULL, /* execute */
292 NULL, /* sub */
293 NULL, /* next */
294 0, /* static_pass_number */
295 TV_LOOP, /* tv_id */
296 0, /* properties_required */
297 0, /* properties_provided */
298 0, /* properties_destroyed */
299 0, /* todo_flags_start */
300 TODO_ggc_collect /* todo_flags_finish */
305 /* Initialization of the RTL loop passes. */
306 static unsigned int
307 rtl_loop_init (void)
309 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
311 if (dump_file)
313 dump_reg_info (dump_file);
314 dump_flow_info (dump_file, dump_flags);
317 loop_optimizer_init (LOOPS_NORMAL);
318 return 0;
321 struct rtl_opt_pass pass_rtl_loop_init =
324 RTL_PASS,
325 "loop2_init", /* name */
326 OPTGROUP_LOOP, /* optinfo_flags */
327 NULL, /* gate */
328 rtl_loop_init, /* execute */
329 NULL, /* sub */
330 NULL, /* next */
331 0, /* static_pass_number */
332 TV_LOOP, /* tv_id */
333 0, /* properties_required */
334 0, /* properties_provided */
335 0, /* properties_destroyed */
336 0, /* todo_flags_start */
337 TODO_verify_rtl_sharing /* todo_flags_finish */
342 /* Finalization of the RTL loop passes. */
344 static unsigned int
345 rtl_loop_done (void)
347 /* No longer preserve loops, remove them now. */
348 cfun->curr_properties &= ~PROP_loops;
349 loop_optimizer_finalize ();
350 free_dominance_info (CDI_DOMINATORS);
352 cleanup_cfg (0);
353 if (dump_file)
355 dump_reg_info (dump_file);
356 dump_flow_info (dump_file, dump_flags);
359 return 0;
362 struct rtl_opt_pass pass_rtl_loop_done =
365 RTL_PASS,
366 "loop2_done", /* name */
367 OPTGROUP_LOOP, /* optinfo_flags */
368 NULL, /* gate */
369 rtl_loop_done, /* execute */
370 NULL, /* sub */
371 NULL, /* next */
372 0, /* static_pass_number */
373 TV_LOOP, /* tv_id */
374 0, /* properties_required */
375 0, /* properties_provided */
376 PROP_loops, /* properties_destroyed */
377 0, /* todo_flags_start */
378 TODO_verify_flow
379 | TODO_verify_rtl_sharing /* todo_flags_finish */
384 /* Loop invariant code motion. */
385 static bool
386 gate_rtl_move_loop_invariants (void)
388 return flag_move_loop_invariants;
391 static unsigned int
392 rtl_move_loop_invariants (void)
394 if (number_of_loops () > 1)
395 move_loop_invariants ();
396 return 0;
399 struct rtl_opt_pass pass_rtl_move_loop_invariants =
402 RTL_PASS,
403 "loop2_invariant", /* name */
404 OPTGROUP_LOOP, /* optinfo_flags */
405 gate_rtl_move_loop_invariants, /* gate */
406 rtl_move_loop_invariants, /* execute */
407 NULL, /* sub */
408 NULL, /* next */
409 0, /* static_pass_number */
410 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
411 0, /* properties_required */
412 0, /* properties_provided */
413 0, /* properties_destroyed */
414 0, /* todo_flags_start */
415 TODO_df_verify |
416 TODO_df_finish | TODO_verify_rtl_sharing /* todo_flags_finish */
421 /* Loop unswitching for RTL. */
422 static bool
423 gate_rtl_unswitch (void)
425 return flag_unswitch_loops;
428 static unsigned int
429 rtl_unswitch (void)
431 if (number_of_loops () > 1)
432 unswitch_loops ();
433 return 0;
436 struct rtl_opt_pass pass_rtl_unswitch =
439 RTL_PASS,
440 "loop2_unswitch", /* name */
441 OPTGROUP_LOOP, /* optinfo_flags */
442 gate_rtl_unswitch, /* gate */
443 rtl_unswitch, /* execute */
444 NULL, /* sub */
445 NULL, /* next */
446 0, /* static_pass_number */
447 TV_LOOP_UNSWITCH, /* tv_id */
448 0, /* properties_required */
449 0, /* properties_provided */
450 0, /* properties_destroyed */
451 0, /* todo_flags_start */
452 TODO_verify_rtl_sharing, /* todo_flags_finish */
457 /* Loop unswitching for RTL. */
458 static bool
459 gate_rtl_unroll_and_peel_loops (void)
461 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
464 static unsigned int
465 rtl_unroll_and_peel_loops (void)
467 if (number_of_loops () > 1)
469 int flags = 0;
470 if (dump_file)
471 df_dump (dump_file);
473 if (flag_peel_loops)
474 flags |= UAP_PEEL;
475 if (flag_unroll_loops)
476 flags |= UAP_UNROLL;
477 if (flag_unroll_all_loops)
478 flags |= UAP_UNROLL_ALL;
480 unroll_and_peel_loops (flags);
482 return 0;
485 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops =
488 RTL_PASS,
489 "loop2_unroll", /* name */
490 OPTGROUP_LOOP, /* optinfo_flags */
491 gate_rtl_unroll_and_peel_loops, /* gate */
492 rtl_unroll_and_peel_loops, /* execute */
493 NULL, /* sub */
494 NULL, /* next */
495 0, /* static_pass_number */
496 TV_LOOP_UNROLL, /* tv_id */
497 0, /* properties_required */
498 0, /* properties_provided */
499 0, /* properties_destroyed */
500 0, /* todo_flags_start */
501 TODO_verify_rtl_sharing, /* todo_flags_finish */
506 /* The doloop optimization. */
507 static bool
508 gate_rtl_doloop (void)
510 #ifdef HAVE_doloop_end
511 return (flag_branch_on_count_reg && HAVE_doloop_end);
512 #else
513 return 0;
514 #endif
517 static unsigned int
518 rtl_doloop (void)
520 #ifdef HAVE_doloop_end
521 if (number_of_loops () > 1)
522 doloop_optimize_loops ();
523 #endif
524 return 0;
527 struct rtl_opt_pass pass_rtl_doloop =
530 RTL_PASS,
531 "loop2_doloop", /* name */
532 OPTGROUP_LOOP, /* optinfo_flags */
533 gate_rtl_doloop, /* gate */
534 rtl_doloop, /* execute */
535 NULL, /* sub */
536 NULL, /* next */
537 0, /* static_pass_number */
538 TV_LOOP_DOLOOP, /* tv_id */
539 0, /* properties_required */
540 0, /* properties_provided */
541 0, /* properties_destroyed */
542 0, /* todo_flags_start */
543 TODO_verify_rtl_sharing /* todo_flags_finish */