* lto-partition.c (add_symbol_to_partition_1,
[official-gcc.git] / gcc / loop-init.c
blob2c2c269b83aa5b3e4d89cd5c707a7bd09c10ba20
1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002-2014 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 "tree.h"
26 #include "regs.h"
27 #include "obstack.h"
28 #include "basic-block.h"
29 #include "cfgloop.h"
30 #include "tree-pass.h"
31 #include "flags.h"
32 #include "df.h"
33 #include "ggc.h"
34 #include "tree-ssa-loop-niter.h"
37 /* Apply FLAGS to the loop state. */
39 static void
40 apply_loop_flags (unsigned flags)
42 if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
44 /* If the loops may have multiple latches, we cannot canonicalize
45 them further (and most of the loop manipulation functions will
46 not work). However, we avoid modifying cfg, which some
47 passes may want. */
48 gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
49 | LOOPS_HAVE_RECORDED_EXITS)) == 0);
50 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
52 else
53 disambiguate_loops_with_multiple_latches ();
55 /* Create pre-headers. */
56 if (flags & LOOPS_HAVE_PREHEADERS)
58 int cp_flags = CP_SIMPLE_PREHEADERS;
60 if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS)
61 cp_flags |= CP_FALLTHRU_PREHEADERS;
63 create_preheaders (cp_flags);
66 /* Force all latches to have only single successor. */
67 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
68 force_single_succ_latches ();
70 /* Mark irreducible loops. */
71 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
72 mark_irreducible_loops ();
74 if (flags & LOOPS_HAVE_RECORDED_EXITS)
75 record_loop_exits ();
78 /* Initialize loop structures. This is used by the tree and RTL loop
79 optimizers. FLAGS specify what properties to compute and/or ensure for
80 loops. */
82 void
83 loop_optimizer_init (unsigned flags)
85 timevar_push (TV_LOOP_INIT);
87 if (!current_loops)
89 gcc_assert (!(cfun->curr_properties & PROP_loops));
91 /* Find the loops. */
92 current_loops = flow_loops_find (NULL);
94 else
96 bool recorded_exits = loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS);
98 gcc_assert (cfun->curr_properties & PROP_loops);
100 /* Ensure that the dominators are computed, like flow_loops_find does. */
101 calculate_dominance_info (CDI_DOMINATORS);
103 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
105 loops_state_clear (~0U);
106 fix_loop_structure (NULL);
109 #ifdef ENABLE_CHECKING
110 else
111 verify_loop_structure ();
112 #endif
114 /* Clear all flags. */
115 if (recorded_exits)
116 release_recorded_exits ();
117 loops_state_clear (~0U);
120 /* Apply flags to loops. */
121 apply_loop_flags (flags);
123 /* Dump loops. */
124 flow_loops_dump (dump_file, NULL, 1);
126 #ifdef ENABLE_CHECKING
127 verify_loop_structure ();
128 #endif
130 timevar_pop (TV_LOOP_INIT);
133 /* Finalize loop structures. */
135 void
136 loop_optimizer_finalize (void)
138 struct loop *loop;
139 basic_block bb;
141 timevar_push (TV_LOOP_FINI);
143 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
144 release_recorded_exits ();
146 free_numbers_of_iterations_estimates ();
148 /* If we should preserve loop structure, do not free it but clear
149 flags that advanced properties are there as we are not preserving
150 that in full. */
151 if (cfun->curr_properties & PROP_loops)
153 loops_state_clear (LOOP_CLOSED_SSA
154 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
155 | LOOPS_HAVE_PREHEADERS
156 | LOOPS_HAVE_SIMPLE_LATCHES
157 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
158 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
159 goto loop_fini_done;
162 gcc_assert (current_loops != NULL);
164 FOR_EACH_LOOP (loop, 0)
165 free_simple_loop_desc (loop);
167 /* Clean up. */
168 flow_loops_free (current_loops);
169 ggc_free (current_loops);
170 current_loops = NULL;
172 FOR_ALL_BB_FN (bb, cfun)
174 bb->loop_father = NULL;
177 loop_fini_done:
178 timevar_pop (TV_LOOP_FINI);
181 /* The structure of loops might have changed. Some loops might get removed
182 (and their headers and latches were set to NULL), loop exists might get
183 removed (thus the loop nesting may be wrong), and some blocks and edges
184 were changed (so the information about bb --> loop mapping does not have
185 to be correct). But still for the remaining loops the header dominates
186 the latch, and loops did not get new subloops (new loops might possibly
187 get created, but we are not interested in them). Fix up the mess.
189 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
190 marked in it.
192 Returns the number of new discovered loops. */
194 unsigned
195 fix_loop_structure (bitmap changed_bbs)
197 basic_block bb;
198 int record_exits = 0;
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_FN (bb, cfun)
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 (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 (cfun);
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_FN (bb, cfun)
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 (cfun), i, loop)
270 if (loop && loop->header == NULL)
272 (*get_loops (cfun))[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 (cfun) - 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 namespace {
318 const pass_data pass_data_loop2 =
320 RTL_PASS, /* type */
321 "loop2", /* name */
322 OPTGROUP_LOOP, /* optinfo_flags */
323 true, /* has_gate */
324 false, /* has_execute */
325 TV_LOOP, /* tv_id */
326 0, /* properties_required */
327 0, /* properties_provided */
328 0, /* properties_destroyed */
329 0, /* todo_flags_start */
330 0, /* todo_flags_finish */
333 class pass_loop2 : public rtl_opt_pass
335 public:
336 pass_loop2 (gcc::context *ctxt)
337 : rtl_opt_pass (pass_data_loop2, ctxt)
340 /* opt_pass methods: */
341 bool gate () { return gate_handle_loop2 (); }
343 }; // class pass_loop2
345 } // anon namespace
347 rtl_opt_pass *
348 make_pass_loop2 (gcc::context *ctxt)
350 return new pass_loop2 (ctxt);
354 /* Initialization of the RTL loop passes. */
355 static unsigned int
356 rtl_loop_init (void)
358 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
360 if (dump_file)
362 dump_reg_info (dump_file);
363 dump_flow_info (dump_file, dump_flags);
366 loop_optimizer_init (LOOPS_NORMAL);
367 return 0;
370 namespace {
372 const pass_data pass_data_rtl_loop_init =
374 RTL_PASS, /* type */
375 "loop2_init", /* name */
376 OPTGROUP_LOOP, /* optinfo_flags */
377 false, /* has_gate */
378 true, /* has_execute */
379 TV_LOOP, /* tv_id */
380 0, /* properties_required */
381 0, /* properties_provided */
382 0, /* properties_destroyed */
383 0, /* todo_flags_start */
384 TODO_verify_rtl_sharing, /* todo_flags_finish */
387 class pass_rtl_loop_init : public rtl_opt_pass
389 public:
390 pass_rtl_loop_init (gcc::context *ctxt)
391 : rtl_opt_pass (pass_data_rtl_loop_init, ctxt)
394 /* opt_pass methods: */
395 unsigned int execute () { return rtl_loop_init (); }
397 }; // class pass_rtl_loop_init
399 } // anon namespace
401 rtl_opt_pass *
402 make_pass_rtl_loop_init (gcc::context *ctxt)
404 return new pass_rtl_loop_init (ctxt);
408 /* Finalization of the RTL loop passes. */
410 static unsigned int
411 rtl_loop_done (void)
413 /* No longer preserve loops, remove them now. */
414 cfun->curr_properties &= ~PROP_loops;
415 loop_optimizer_finalize ();
416 free_dominance_info (CDI_DOMINATORS);
418 cleanup_cfg (0);
419 if (dump_file)
421 dump_reg_info (dump_file);
422 dump_flow_info (dump_file, dump_flags);
425 return 0;
428 namespace {
430 const pass_data pass_data_rtl_loop_done =
432 RTL_PASS, /* type */
433 "loop2_done", /* name */
434 OPTGROUP_LOOP, /* optinfo_flags */
435 false, /* has_gate */
436 true, /* has_execute */
437 TV_LOOP, /* tv_id */
438 0, /* properties_required */
439 0, /* properties_provided */
440 PROP_loops, /* properties_destroyed */
441 0, /* todo_flags_start */
442 ( TODO_verify_flow | TODO_verify_rtl_sharing ), /* todo_flags_finish */
445 class pass_rtl_loop_done : public rtl_opt_pass
447 public:
448 pass_rtl_loop_done (gcc::context *ctxt)
449 : rtl_opt_pass (pass_data_rtl_loop_done, ctxt)
452 /* opt_pass methods: */
453 unsigned int execute () { return rtl_loop_done (); }
455 }; // class pass_rtl_loop_done
457 } // anon namespace
459 rtl_opt_pass *
460 make_pass_rtl_loop_done (gcc::context *ctxt)
462 return new pass_rtl_loop_done (ctxt);
466 /* Loop invariant code motion. */
467 static bool
468 gate_rtl_move_loop_invariants (void)
470 return flag_move_loop_invariants;
473 static unsigned int
474 rtl_move_loop_invariants (void)
476 if (number_of_loops (cfun) > 1)
477 move_loop_invariants ();
478 return 0;
481 namespace {
483 const pass_data pass_data_rtl_move_loop_invariants =
485 RTL_PASS, /* type */
486 "loop2_invariant", /* name */
487 OPTGROUP_LOOP, /* optinfo_flags */
488 true, /* has_gate */
489 true, /* has_execute */
490 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
491 0, /* properties_required */
492 0, /* properties_provided */
493 0, /* properties_destroyed */
494 0, /* todo_flags_start */
495 ( TODO_df_verify | TODO_df_finish
496 | TODO_verify_rtl_sharing ), /* todo_flags_finish */
499 class pass_rtl_move_loop_invariants : public rtl_opt_pass
501 public:
502 pass_rtl_move_loop_invariants (gcc::context *ctxt)
503 : rtl_opt_pass (pass_data_rtl_move_loop_invariants, ctxt)
506 /* opt_pass methods: */
507 bool gate () { return gate_rtl_move_loop_invariants (); }
508 unsigned int execute () { return rtl_move_loop_invariants (); }
510 }; // class pass_rtl_move_loop_invariants
512 } // anon namespace
514 rtl_opt_pass *
515 make_pass_rtl_move_loop_invariants (gcc::context *ctxt)
517 return new pass_rtl_move_loop_invariants (ctxt);
521 /* Loop unswitching for RTL. */
522 static bool
523 gate_rtl_unswitch (void)
525 return flag_unswitch_loops;
528 static unsigned int
529 rtl_unswitch (void)
531 if (number_of_loops (cfun) > 1)
532 unswitch_loops ();
533 return 0;
536 namespace {
538 const pass_data pass_data_rtl_unswitch =
540 RTL_PASS, /* type */
541 "loop2_unswitch", /* name */
542 OPTGROUP_LOOP, /* optinfo_flags */
543 true, /* has_gate */
544 true, /* has_execute */
545 TV_LOOP_UNSWITCH, /* tv_id */
546 0, /* properties_required */
547 0, /* properties_provided */
548 0, /* properties_destroyed */
549 0, /* todo_flags_start */
550 TODO_verify_rtl_sharing, /* todo_flags_finish */
553 class pass_rtl_unswitch : public rtl_opt_pass
555 public:
556 pass_rtl_unswitch (gcc::context *ctxt)
557 : rtl_opt_pass (pass_data_rtl_unswitch, ctxt)
560 /* opt_pass methods: */
561 bool gate () { return gate_rtl_unswitch (); }
562 unsigned int execute () { return rtl_unswitch (); }
564 }; // class pass_rtl_unswitch
566 } // anon namespace
568 rtl_opt_pass *
569 make_pass_rtl_unswitch (gcc::context *ctxt)
571 return new pass_rtl_unswitch (ctxt);
575 /* Loop unswitching for RTL. */
576 static bool
577 gate_rtl_unroll_and_peel_loops (void)
579 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
582 static unsigned int
583 rtl_unroll_and_peel_loops (void)
585 if (number_of_loops (cfun) > 1)
587 int flags = 0;
588 if (dump_file)
589 df_dump (dump_file);
591 if (flag_peel_loops)
592 flags |= UAP_PEEL;
593 if (flag_unroll_loops)
594 flags |= UAP_UNROLL;
595 if (flag_unroll_all_loops)
596 flags |= UAP_UNROLL_ALL;
598 unroll_and_peel_loops (flags);
600 return 0;
603 namespace {
605 const pass_data pass_data_rtl_unroll_and_peel_loops =
607 RTL_PASS, /* type */
608 "loop2_unroll", /* name */
609 OPTGROUP_LOOP, /* optinfo_flags */
610 true, /* has_gate */
611 true, /* has_execute */
612 TV_LOOP_UNROLL, /* tv_id */
613 0, /* properties_required */
614 0, /* properties_provided */
615 0, /* properties_destroyed */
616 0, /* todo_flags_start */
617 TODO_verify_rtl_sharing, /* todo_flags_finish */
620 class pass_rtl_unroll_and_peel_loops : public rtl_opt_pass
622 public:
623 pass_rtl_unroll_and_peel_loops (gcc::context *ctxt)
624 : rtl_opt_pass (pass_data_rtl_unroll_and_peel_loops, ctxt)
627 /* opt_pass methods: */
628 bool gate () { return gate_rtl_unroll_and_peel_loops (); }
629 unsigned int execute () { return rtl_unroll_and_peel_loops (); }
631 }; // class pass_rtl_unroll_and_peel_loops
633 } // anon namespace
635 rtl_opt_pass *
636 make_pass_rtl_unroll_and_peel_loops (gcc::context *ctxt)
638 return new pass_rtl_unroll_and_peel_loops (ctxt);
642 /* The doloop optimization. */
643 static bool
644 gate_rtl_doloop (void)
646 #ifdef HAVE_doloop_end
647 return (flag_branch_on_count_reg && HAVE_doloop_end);
648 #else
649 return 0;
650 #endif
653 static unsigned int
654 rtl_doloop (void)
656 #ifdef HAVE_doloop_end
657 if (number_of_loops (cfun) > 1)
658 doloop_optimize_loops ();
659 #endif
660 return 0;
663 namespace {
665 const pass_data pass_data_rtl_doloop =
667 RTL_PASS, /* type */
668 "loop2_doloop", /* name */
669 OPTGROUP_LOOP, /* optinfo_flags */
670 true, /* has_gate */
671 true, /* has_execute */
672 TV_LOOP_DOLOOP, /* tv_id */
673 0, /* properties_required */
674 0, /* properties_provided */
675 0, /* properties_destroyed */
676 0, /* todo_flags_start */
677 TODO_verify_rtl_sharing, /* todo_flags_finish */
680 class pass_rtl_doloop : public rtl_opt_pass
682 public:
683 pass_rtl_doloop (gcc::context *ctxt)
684 : rtl_opt_pass (pass_data_rtl_doloop, ctxt)
687 /* opt_pass methods: */
688 bool gate () { return gate_rtl_doloop (); }
689 unsigned int execute () { return rtl_doloop (); }
691 }; // class pass_rtl_doloop
693 } // anon namespace
695 rtl_opt_pass *
696 make_pass_rtl_doloop (gcc::context *ctxt)
698 return new pass_rtl_doloop (ctxt);