PR/56490
[official-gcc.git] / gcc / loop-init.c
blobb1954ca484f79bd46d54a6e0a586db218d1c7e7a
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
103 /* Clear all flags. */
104 loops_state_clear (~0U);
107 /* Apply flags to loops. */
108 apply_loop_flags (flags);
110 /* Dump loops. */
111 flow_loops_dump (dump_file, NULL, 1);
113 #ifdef ENABLE_CHECKING
114 verify_loop_structure ();
115 #endif
117 timevar_pop (TV_LOOP_INIT);
120 /* Finalize loop structures. */
122 void
123 loop_optimizer_finalize (void)
125 loop_iterator li;
126 struct loop *loop;
127 basic_block bb;
129 timevar_push (TV_LOOP_FINI);
131 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
132 release_recorded_exits ();
134 /* If we should preserve loop structure, do not free it but clear
135 flags that advanced properties are there as we are not preserving
136 that in full. */
137 if (cfun->curr_properties & PROP_loops)
139 loops_state_clear (LOOP_CLOSED_SSA
140 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
141 | LOOPS_HAVE_PREHEADERS
142 | LOOPS_HAVE_SIMPLE_LATCHES
143 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
144 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
145 goto loop_fini_done;
148 gcc_assert (current_loops != NULL);
150 FOR_EACH_LOOP (li, loop, 0)
152 free_simple_loop_desc (loop);
155 /* Clean up. */
156 flow_loops_free (current_loops);
157 ggc_free (current_loops);
158 current_loops = NULL;
160 FOR_ALL_BB (bb)
162 bb->loop_father = NULL;
165 loop_fini_done:
166 timevar_pop (TV_LOOP_FINI);
169 /* The structure of loops might have changed. Some loops might get removed
170 (and their headers and latches were set to NULL), loop exists might get
171 removed (thus the loop nesting may be wrong), and some blocks and edges
172 were changed (so the information about bb --> loop mapping does not have
173 to be correct). But still for the remaining loops the header dominates
174 the latch, and loops did not get new subloops (new loops might possibly
175 get created, but we are not interested in them). Fix up the mess.
177 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
178 marked in it.
180 Returns the number of new discovered loops. */
182 unsigned
183 fix_loop_structure (bitmap changed_bbs)
185 basic_block bb;
186 int record_exits = 0;
187 loop_iterator li;
188 struct loop *loop;
189 unsigned old_nloops;
191 timevar_push (TV_LOOP_INIT);
193 /* We need exact and fast dominance info to be available. */
194 gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
196 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
198 release_recorded_exits ();
199 record_exits = LOOPS_HAVE_RECORDED_EXITS;
202 /* Remember the depth of the blocks in the loop hierarchy, so that we can
203 recognize blocks whose loop nesting relationship has changed. */
204 if (changed_bbs)
205 FOR_EACH_BB (bb)
206 bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
208 /* Remove the dead loops from structures. We start from the innermost
209 loops, so that when we remove the loops, we know that the loops inside
210 are preserved, and do not waste time relinking loops that will be
211 removed later. */
212 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
214 /* Detect the case that the loop is no longer present even though
215 it wasn't marked for removal.
216 ??? If we do that we can get away with not marking loops for
217 removal at all. And possibly avoid some spurious removals. */
218 if (loop->header
219 && bb_loop_header_p (loop->header))
220 continue;
222 if (dump_file && (dump_flags & TDF_DETAILS))
223 fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
224 loop->num);
226 while (loop->inner)
228 struct loop *ploop = loop->inner;
229 flow_loop_tree_node_remove (ploop);
230 flow_loop_tree_node_add (loop_outer (loop), ploop);
233 /* Remove the loop and free its data. */
234 delete_loop (loop);
237 /* Remember the number of loops so we can return how many new loops
238 flow_loops_find discovered. */
239 old_nloops = number_of_loops ();
241 /* Re-compute loop structure in-place. */
242 flow_loops_find (current_loops);
244 /* Mark the blocks whose loop has changed. */
245 if (changed_bbs)
247 FOR_EACH_BB (bb)
249 if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
250 bitmap_set_bit (changed_bbs, bb->index);
252 bb->aux = NULL;
256 loops_state_clear (LOOPS_NEED_FIXUP);
258 /* Apply flags to loops. */
259 apply_loop_flags (current_loops->state | record_exits);
261 #ifdef ENABLE_CHECKING
262 verify_loop_structure ();
263 #endif
265 timevar_pop (TV_LOOP_INIT);
267 return number_of_loops () - old_nloops;
270 /* Gate for the RTL loop superpass. The actual passes are subpasses.
271 See passes.c for more on that. */
273 static bool
274 gate_handle_loop2 (void)
276 if (optimize > 0
277 && (flag_move_loop_invariants
278 || flag_unswitch_loops
279 || flag_peel_loops
280 || flag_unroll_loops
281 #ifdef HAVE_doloop_end
282 || (flag_branch_on_count_reg && HAVE_doloop_end)
283 #endif
285 return true;
286 else
288 /* No longer preserve loops, remove them now. */
289 cfun->curr_properties &= ~PROP_loops;
290 if (current_loops)
291 loop_optimizer_finalize ();
292 return false;
296 struct rtl_opt_pass pass_loop2 =
299 RTL_PASS,
300 "loop2", /* name */
301 OPTGROUP_LOOP, /* optinfo_flags */
302 gate_handle_loop2, /* gate */
303 NULL, /* execute */
304 NULL, /* sub */
305 NULL, /* next */
306 0, /* static_pass_number */
307 TV_LOOP, /* tv_id */
308 0, /* properties_required */
309 0, /* properties_provided */
310 0, /* properties_destroyed */
311 0, /* todo_flags_start */
312 TODO_ggc_collect /* todo_flags_finish */
317 /* Initialization of the RTL loop passes. */
318 static unsigned int
319 rtl_loop_init (void)
321 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
323 if (dump_file)
325 dump_reg_info (dump_file);
326 dump_flow_info (dump_file, dump_flags);
329 loop_optimizer_init (LOOPS_NORMAL);
330 return 0;
333 struct rtl_opt_pass pass_rtl_loop_init =
336 RTL_PASS,
337 "loop2_init", /* name */
338 OPTGROUP_LOOP, /* optinfo_flags */
339 NULL, /* gate */
340 rtl_loop_init, /* execute */
341 NULL, /* sub */
342 NULL, /* next */
343 0, /* static_pass_number */
344 TV_LOOP, /* tv_id */
345 0, /* properties_required */
346 0, /* properties_provided */
347 0, /* properties_destroyed */
348 0, /* todo_flags_start */
349 TODO_verify_rtl_sharing /* todo_flags_finish */
354 /* Finalization of the RTL loop passes. */
356 static unsigned int
357 rtl_loop_done (void)
359 /* No longer preserve loops, remove them now. */
360 cfun->curr_properties &= ~PROP_loops;
361 loop_optimizer_finalize ();
362 free_dominance_info (CDI_DOMINATORS);
364 cleanup_cfg (0);
365 if (dump_file)
367 dump_reg_info (dump_file);
368 dump_flow_info (dump_file, dump_flags);
371 return 0;
374 struct rtl_opt_pass pass_rtl_loop_done =
377 RTL_PASS,
378 "loop2_done", /* name */
379 OPTGROUP_LOOP, /* optinfo_flags */
380 NULL, /* gate */
381 rtl_loop_done, /* execute */
382 NULL, /* sub */
383 NULL, /* next */
384 0, /* static_pass_number */
385 TV_LOOP, /* tv_id */
386 0, /* properties_required */
387 0, /* properties_provided */
388 PROP_loops, /* properties_destroyed */
389 0, /* todo_flags_start */
390 TODO_verify_flow
391 | TODO_verify_rtl_sharing /* todo_flags_finish */
396 /* Loop invariant code motion. */
397 static bool
398 gate_rtl_move_loop_invariants (void)
400 return flag_move_loop_invariants;
403 static unsigned int
404 rtl_move_loop_invariants (void)
406 if (number_of_loops () > 1)
407 move_loop_invariants ();
408 return 0;
411 struct rtl_opt_pass pass_rtl_move_loop_invariants =
414 RTL_PASS,
415 "loop2_invariant", /* name */
416 OPTGROUP_LOOP, /* optinfo_flags */
417 gate_rtl_move_loop_invariants, /* gate */
418 rtl_move_loop_invariants, /* execute */
419 NULL, /* sub */
420 NULL, /* next */
421 0, /* static_pass_number */
422 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
423 0, /* properties_required */
424 0, /* properties_provided */
425 0, /* properties_destroyed */
426 0, /* todo_flags_start */
427 TODO_df_verify |
428 TODO_df_finish | TODO_verify_rtl_sharing /* todo_flags_finish */
433 /* Loop unswitching for RTL. */
434 static bool
435 gate_rtl_unswitch (void)
437 return flag_unswitch_loops;
440 static unsigned int
441 rtl_unswitch (void)
443 if (number_of_loops () > 1)
444 unswitch_loops ();
445 return 0;
448 struct rtl_opt_pass pass_rtl_unswitch =
451 RTL_PASS,
452 "loop2_unswitch", /* name */
453 OPTGROUP_LOOP, /* optinfo_flags */
454 gate_rtl_unswitch, /* gate */
455 rtl_unswitch, /* execute */
456 NULL, /* sub */
457 NULL, /* next */
458 0, /* static_pass_number */
459 TV_LOOP_UNSWITCH, /* tv_id */
460 0, /* properties_required */
461 0, /* properties_provided */
462 0, /* properties_destroyed */
463 0, /* todo_flags_start */
464 TODO_verify_rtl_sharing, /* todo_flags_finish */
469 /* Loop unswitching for RTL. */
470 static bool
471 gate_rtl_unroll_and_peel_loops (void)
473 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
476 static unsigned int
477 rtl_unroll_and_peel_loops (void)
479 if (number_of_loops () > 1)
481 int flags = 0;
482 if (dump_file)
483 df_dump (dump_file);
485 if (flag_peel_loops)
486 flags |= UAP_PEEL;
487 if (flag_unroll_loops)
488 flags |= UAP_UNROLL;
489 if (flag_unroll_all_loops)
490 flags |= UAP_UNROLL_ALL;
492 unroll_and_peel_loops (flags);
494 return 0;
497 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops =
500 RTL_PASS,
501 "loop2_unroll", /* name */
502 OPTGROUP_LOOP, /* optinfo_flags */
503 gate_rtl_unroll_and_peel_loops, /* gate */
504 rtl_unroll_and_peel_loops, /* execute */
505 NULL, /* sub */
506 NULL, /* next */
507 0, /* static_pass_number */
508 TV_LOOP_UNROLL, /* tv_id */
509 0, /* properties_required */
510 0, /* properties_provided */
511 0, /* properties_destroyed */
512 0, /* todo_flags_start */
513 TODO_verify_rtl_sharing, /* todo_flags_finish */
518 /* The doloop optimization. */
519 static bool
520 gate_rtl_doloop (void)
522 #ifdef HAVE_doloop_end
523 return (flag_branch_on_count_reg && HAVE_doloop_end);
524 #else
525 return 0;
526 #endif
529 static unsigned int
530 rtl_doloop (void)
532 #ifdef HAVE_doloop_end
533 if (number_of_loops () > 1)
534 doloop_optimize_loops ();
535 #endif
536 return 0;
539 struct rtl_opt_pass pass_rtl_doloop =
542 RTL_PASS,
543 "loop2_doloop", /* name */
544 OPTGROUP_LOOP, /* optinfo_flags */
545 gate_rtl_doloop, /* gate */
546 rtl_doloop, /* execute */
547 NULL, /* sub */
548 NULL, /* next */
549 0, /* static_pass_number */
550 TV_LOOP_DOLOOP, /* tv_id */
551 0, /* properties_required */
552 0, /* properties_provided */
553 0, /* properties_destroyed */
554 0, /* todo_flags_start */
555 TODO_verify_rtl_sharing /* todo_flags_finish */