2015-07-02 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / loop-init.c
blob5959145b9697d2a4e2d770dea20bcecde817219a
1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002-2015 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 "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "regs.h"
29 #include "obstack.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "cfgcleanup.h"
36 #include "basic-block.h"
37 #include "cfgloop.h"
38 #include "tree-pass.h"
39 #include "flags.h"
40 #include "df.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "loop-unroll.h"
43 #include "tree-scalar-evolution.h"
46 /* Apply FLAGS to the loop state. */
48 static void
49 apply_loop_flags (unsigned flags)
51 if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
53 /* If the loops may have multiple latches, we cannot canonicalize
54 them further (and most of the loop manipulation functions will
55 not work). However, we avoid modifying cfg, which some
56 passes may want. */
57 gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
58 | LOOPS_HAVE_RECORDED_EXITS)) == 0);
59 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
61 else
62 disambiguate_loops_with_multiple_latches ();
64 /* Create pre-headers. */
65 if (flags & LOOPS_HAVE_PREHEADERS)
67 int cp_flags = CP_SIMPLE_PREHEADERS;
69 if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS)
70 cp_flags |= CP_FALLTHRU_PREHEADERS;
72 create_preheaders (cp_flags);
75 /* Force all latches to have only single successor. */
76 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
77 force_single_succ_latches ();
79 /* Mark irreducible loops. */
80 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
81 mark_irreducible_loops ();
83 if (flags & LOOPS_HAVE_RECORDED_EXITS)
84 record_loop_exits ();
87 /* Initialize loop structures. This is used by the tree and RTL loop
88 optimizers. FLAGS specify what properties to compute and/or ensure for
89 loops. */
91 void
92 loop_optimizer_init (unsigned flags)
94 timevar_push (TV_LOOP_INIT);
96 if (!current_loops)
98 gcc_assert (!(cfun->curr_properties & PROP_loops));
100 /* Find the loops. */
101 current_loops = flow_loops_find (NULL);
103 else
105 bool recorded_exits = loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS);
106 bool needs_fixup = loops_state_satisfies_p (LOOPS_NEED_FIXUP);
108 gcc_assert (cfun->curr_properties & PROP_loops);
110 /* Ensure that the dominators are computed, like flow_loops_find does. */
111 calculate_dominance_info (CDI_DOMINATORS);
113 #ifdef ENABLE_CHECKING
114 if (!needs_fixup)
115 verify_loop_structure ();
116 #endif
118 /* Clear all flags. */
119 if (recorded_exits)
120 release_recorded_exits ();
121 loops_state_clear (~0U);
123 if (needs_fixup)
125 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
126 re-applies flags. */
127 loops_state_set (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
128 fix_loop_structure (NULL);
132 /* Apply flags to loops. */
133 apply_loop_flags (flags);
135 /* Dump loops. */
136 flow_loops_dump (dump_file, NULL, 1);
138 #ifdef ENABLE_CHECKING
139 verify_loop_structure ();
140 #endif
142 timevar_pop (TV_LOOP_INIT);
145 /* Finalize loop structures. */
147 void
148 loop_optimizer_finalize (void)
150 struct loop *loop;
151 basic_block bb;
153 timevar_push (TV_LOOP_FINI);
155 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
156 release_recorded_exits ();
158 free_numbers_of_iterations_estimates ();
160 /* If we should preserve loop structure, do not free it but clear
161 flags that advanced properties are there as we are not preserving
162 that in full. */
163 if (cfun->curr_properties & PROP_loops)
165 loops_state_clear (LOOP_CLOSED_SSA
166 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
167 | LOOPS_HAVE_PREHEADERS
168 | LOOPS_HAVE_SIMPLE_LATCHES
169 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
170 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
171 goto loop_fini_done;
174 gcc_assert (current_loops != NULL);
176 FOR_EACH_LOOP (loop, 0)
177 free_simple_loop_desc (loop);
179 /* Clean up. */
180 flow_loops_free (current_loops);
181 ggc_free (current_loops);
182 current_loops = NULL;
184 FOR_ALL_BB_FN (bb, cfun)
186 bb->loop_father = NULL;
189 loop_fini_done:
190 timevar_pop (TV_LOOP_FINI);
193 /* The structure of loops might have changed. Some loops might get removed
194 (and their headers and latches were set to NULL), loop exists might get
195 removed (thus the loop nesting may be wrong), and some blocks and edges
196 were changed (so the information about bb --> loop mapping does not have
197 to be correct). But still for the remaining loops the header dominates
198 the latch, and loops did not get new subloops (new loops might possibly
199 get created, but we are not interested in them). Fix up the mess.
201 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
202 marked in it.
204 Returns the number of new discovered loops. */
206 unsigned
207 fix_loop_structure (bitmap changed_bbs)
209 basic_block bb;
210 int record_exits = 0;
211 struct loop *loop;
212 unsigned old_nloops, i;
214 timevar_push (TV_LOOP_INIT);
216 if (dump_file && (dump_flags & TDF_DETAILS))
217 fprintf (dump_file, "fix_loop_structure: fixing up loops for function\n");
219 /* We need exact and fast dominance info to be available. */
220 gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
222 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
224 release_recorded_exits ();
225 record_exits = LOOPS_HAVE_RECORDED_EXITS;
228 /* Remember the depth of the blocks in the loop hierarchy, so that we can
229 recognize blocks whose loop nesting relationship has changed. */
230 if (changed_bbs)
231 FOR_EACH_BB_FN (bb, cfun)
232 bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
234 /* Remove the dead loops from structures. We start from the innermost
235 loops, so that when we remove the loops, we know that the loops inside
236 are preserved, and do not waste time relinking loops that will be
237 removed later. */
238 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
240 /* Detect the case that the loop is no longer present even though
241 it wasn't marked for removal.
242 ??? If we do that we can get away with not marking loops for
243 removal at all. And possibly avoid some spurious removals. */
244 if (loop->header
245 && bb_loop_header_p (loop->header))
246 continue;
248 if (dump_file && (dump_flags & TDF_DETAILS))
249 fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
250 loop->num);
252 while (loop->inner)
254 struct loop *ploop = loop->inner;
255 flow_loop_tree_node_remove (ploop);
256 flow_loop_tree_node_add (loop_outer (loop), ploop);
259 /* Remove the loop. */
260 if (loop->header)
261 loop->former_header = loop->header;
262 else
263 gcc_assert (loop->former_header != NULL);
264 loop->header = NULL;
265 flow_loop_tree_node_remove (loop);
268 /* Remember the number of loops so we can return how many new loops
269 flow_loops_find discovered. */
270 old_nloops = number_of_loops (cfun);
272 /* Re-compute loop structure in-place. */
273 flow_loops_find (current_loops);
275 /* Mark the blocks whose loop has changed. */
276 if (changed_bbs)
278 FOR_EACH_BB_FN (bb, cfun)
280 if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
281 bitmap_set_bit (changed_bbs, bb->index);
283 bb->aux = NULL;
287 /* Finally free deleted loops. */
288 bool any_deleted = false;
289 FOR_EACH_VEC_ELT (*get_loops (cfun), i, loop)
290 if (loop && loop->header == NULL)
292 if (dump_file
293 && ((unsigned) loop->former_header->index
294 < basic_block_info_for_fn (cfun)->length ()))
296 basic_block former_header
297 = BASIC_BLOCK_FOR_FN (cfun, loop->former_header->index);
298 /* If the old header still exists we want to check if the
299 original loop is re-discovered or the old header is now
300 part of a newly discovered loop.
301 In both cases we should have avoided removing the loop. */
302 if (former_header == loop->former_header)
304 if (former_header->loop_father->header == former_header)
305 fprintf (dump_file, "fix_loop_structure: rediscovered "
306 "removed loop %d as loop %d with old header %d\n",
307 loop->num, former_header->loop_father->num,
308 former_header->index);
309 else if ((unsigned) former_header->loop_father->num
310 >= old_nloops)
311 fprintf (dump_file, "fix_loop_structure: header %d of "
312 "removed loop %d is part of the newly "
313 "discovered loop %d with header %d\n",
314 former_header->index, loop->num,
315 former_header->loop_father->num,
316 former_header->loop_father->header->index);
319 (*get_loops (cfun))[i] = NULL;
320 flow_loop_free (loop);
321 any_deleted = true;
324 /* If we deleted loops then the cached scalar evolutions refering to
325 those loops become invalid. */
326 if (any_deleted && scev_initialized_p ())
327 scev_reset_htab ();
329 loops_state_clear (LOOPS_NEED_FIXUP);
331 /* Apply flags to loops. */
332 apply_loop_flags (current_loops->state | record_exits);
334 #ifdef ENABLE_CHECKING
335 verify_loop_structure ();
336 #endif
338 timevar_pop (TV_LOOP_INIT);
340 return number_of_loops (cfun) - old_nloops;
343 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
344 more on that. */
346 namespace {
348 const pass_data pass_data_loop2 =
350 RTL_PASS, /* type */
351 "loop2", /* name */
352 OPTGROUP_LOOP, /* optinfo_flags */
353 TV_LOOP, /* tv_id */
354 0, /* properties_required */
355 0, /* properties_provided */
356 0, /* properties_destroyed */
357 0, /* todo_flags_start */
358 0, /* todo_flags_finish */
361 class pass_loop2 : public rtl_opt_pass
363 public:
364 pass_loop2 (gcc::context *ctxt)
365 : rtl_opt_pass (pass_data_loop2, ctxt)
368 /* opt_pass methods: */
369 virtual bool gate (function *);
371 }; // class pass_loop2
373 bool
374 pass_loop2::gate (function *fun)
376 if (optimize > 0
377 && (flag_move_loop_invariants
378 || flag_unswitch_loops
379 || flag_unroll_loops
380 #ifdef HAVE_doloop_end
381 || (flag_branch_on_count_reg && HAVE_doloop_end)
382 #endif
384 return true;
385 else
387 /* No longer preserve loops, remove them now. */
388 fun->curr_properties &= ~PROP_loops;
389 if (current_loops)
390 loop_optimizer_finalize ();
391 return false;
395 } // anon namespace
397 rtl_opt_pass *
398 make_pass_loop2 (gcc::context *ctxt)
400 return new pass_loop2 (ctxt);
404 /* Initialization of the RTL loop passes. */
405 static unsigned int
406 rtl_loop_init (void)
408 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
410 if (dump_file)
412 dump_reg_info (dump_file);
413 dump_flow_info (dump_file, dump_flags);
416 loop_optimizer_init (LOOPS_NORMAL);
417 return 0;
420 namespace {
422 const pass_data pass_data_rtl_loop_init =
424 RTL_PASS, /* type */
425 "loop2_init", /* name */
426 OPTGROUP_LOOP, /* optinfo_flags */
427 TV_LOOP, /* tv_id */
428 0, /* properties_required */
429 0, /* properties_provided */
430 0, /* properties_destroyed */
431 0, /* todo_flags_start */
432 0, /* todo_flags_finish */
435 class pass_rtl_loop_init : public rtl_opt_pass
437 public:
438 pass_rtl_loop_init (gcc::context *ctxt)
439 : rtl_opt_pass (pass_data_rtl_loop_init, ctxt)
442 /* opt_pass methods: */
443 virtual unsigned int execute (function *) { return rtl_loop_init (); }
445 }; // class pass_rtl_loop_init
447 } // anon namespace
449 rtl_opt_pass *
450 make_pass_rtl_loop_init (gcc::context *ctxt)
452 return new pass_rtl_loop_init (ctxt);
456 /* Finalization of the RTL loop passes. */
458 namespace {
460 const pass_data pass_data_rtl_loop_done =
462 RTL_PASS, /* type */
463 "loop2_done", /* name */
464 OPTGROUP_LOOP, /* optinfo_flags */
465 TV_LOOP, /* tv_id */
466 0, /* properties_required */
467 0, /* properties_provided */
468 PROP_loops, /* properties_destroyed */
469 0, /* todo_flags_start */
470 0, /* todo_flags_finish */
473 class pass_rtl_loop_done : public rtl_opt_pass
475 public:
476 pass_rtl_loop_done (gcc::context *ctxt)
477 : rtl_opt_pass (pass_data_rtl_loop_done, ctxt)
480 /* opt_pass methods: */
481 virtual unsigned int execute (function *);
483 }; // class pass_rtl_loop_done
485 unsigned int
486 pass_rtl_loop_done::execute (function *fun)
488 /* No longer preserve loops, remove them now. */
489 fun->curr_properties &= ~PROP_loops;
490 loop_optimizer_finalize ();
491 free_dominance_info (CDI_DOMINATORS);
493 cleanup_cfg (0);
494 if (dump_file)
496 dump_reg_info (dump_file);
497 dump_flow_info (dump_file, dump_flags);
500 return 0;
503 } // anon namespace
505 rtl_opt_pass *
506 make_pass_rtl_loop_done (gcc::context *ctxt)
508 return new pass_rtl_loop_done (ctxt);
512 /* Loop invariant code motion. */
514 namespace {
516 const pass_data pass_data_rtl_move_loop_invariants =
518 RTL_PASS, /* type */
519 "loop2_invariant", /* name */
520 OPTGROUP_LOOP, /* optinfo_flags */
521 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
522 0, /* properties_required */
523 0, /* properties_provided */
524 0, /* properties_destroyed */
525 0, /* todo_flags_start */
526 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
529 class pass_rtl_move_loop_invariants : public rtl_opt_pass
531 public:
532 pass_rtl_move_loop_invariants (gcc::context *ctxt)
533 : rtl_opt_pass (pass_data_rtl_move_loop_invariants, ctxt)
536 /* opt_pass methods: */
537 virtual bool gate (function *) { return flag_move_loop_invariants; }
538 virtual unsigned int execute (function *fun)
540 if (number_of_loops (fun) > 1)
541 move_loop_invariants ();
542 return 0;
545 }; // class pass_rtl_move_loop_invariants
547 } // anon namespace
549 rtl_opt_pass *
550 make_pass_rtl_move_loop_invariants (gcc::context *ctxt)
552 return new pass_rtl_move_loop_invariants (ctxt);
556 namespace {
558 const pass_data pass_data_rtl_unroll_loops =
560 RTL_PASS, /* type */
561 "loop2_unroll", /* name */
562 OPTGROUP_LOOP, /* optinfo_flags */
563 TV_LOOP_UNROLL, /* tv_id */
564 0, /* properties_required */
565 0, /* properties_provided */
566 0, /* properties_destroyed */
567 0, /* todo_flags_start */
568 0, /* todo_flags_finish */
571 class pass_rtl_unroll_loops : public rtl_opt_pass
573 public:
574 pass_rtl_unroll_loops (gcc::context *ctxt)
575 : rtl_opt_pass (pass_data_rtl_unroll_loops, ctxt)
578 /* opt_pass methods: */
579 virtual bool gate (function *)
581 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
584 virtual unsigned int execute (function *);
586 }; // class pass_rtl_unroll_loops
588 unsigned int
589 pass_rtl_unroll_loops::execute (function *fun)
591 if (number_of_loops (fun) > 1)
593 int flags = 0;
594 if (dump_file)
595 df_dump (dump_file);
597 if (flag_unroll_loops)
598 flags |= UAP_UNROLL;
599 if (flag_unroll_all_loops)
600 flags |= UAP_UNROLL_ALL;
602 unroll_loops (flags);
604 return 0;
607 } // anon namespace
609 rtl_opt_pass *
610 make_pass_rtl_unroll_loops (gcc::context *ctxt)
612 return new pass_rtl_unroll_loops (ctxt);
616 namespace {
618 const pass_data pass_data_rtl_doloop =
620 RTL_PASS, /* type */
621 "loop2_doloop", /* name */
622 OPTGROUP_LOOP, /* optinfo_flags */
623 TV_LOOP_DOLOOP, /* tv_id */
624 0, /* properties_required */
625 0, /* properties_provided */
626 0, /* properties_destroyed */
627 0, /* todo_flags_start */
628 0, /* todo_flags_finish */
631 class pass_rtl_doloop : public rtl_opt_pass
633 public:
634 pass_rtl_doloop (gcc::context *ctxt)
635 : rtl_opt_pass (pass_data_rtl_doloop, ctxt)
638 /* opt_pass methods: */
639 virtual bool gate (function *);
640 virtual unsigned int execute (function *);
642 }; // class pass_rtl_doloop
644 bool
645 pass_rtl_doloop::gate (function *)
647 #ifdef HAVE_doloop_end
648 return (flag_branch_on_count_reg && HAVE_doloop_end);
649 #else
650 return false;
651 #endif
654 unsigned int
655 pass_rtl_doloop::execute (function *fun ATTRIBUTE_UNUSED)
657 #ifdef HAVE_doloop_end
658 if (number_of_loops (fun) > 1)
659 doloop_optimize_loops ();
660 #endif
661 return 0;
664 } // anon namespace
666 rtl_opt_pass *
667 make_pass_rtl_doloop (gcc::context *ctxt)
669 return new pass_rtl_doloop (ctxt);