* omp-low.c (lower_omp_target): Remove unreachable code & merge
[official-gcc.git] / gcc / loop-init.c
blob6f1565bdfa18fe557b625776103ef97050cc8e69
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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "cfghooks.h"
28 #include "df.h"
29 #include "regs.h"
30 #include "alias.h"
31 #include "cfgcleanup.h"
32 #include "cfgloop.h"
33 #include "tree-pass.h"
34 #include "flags.h"
35 #include "tree-ssa-loop-niter.h"
36 #include "loop-unroll.h"
37 #include "tree-scalar-evolution.h"
40 /* Apply FLAGS to the loop state. */
42 static void
43 apply_loop_flags (unsigned flags)
45 if (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES)
47 /* If the loops may have multiple latches, we cannot canonicalize
48 them further (and most of the loop manipulation functions will
49 not work). However, we avoid modifying cfg, which some
50 passes may want. */
51 gcc_assert ((flags & ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
52 | LOOPS_HAVE_RECORDED_EXITS)) == 0);
53 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
55 else
56 disambiguate_loops_with_multiple_latches ();
58 /* Create pre-headers. */
59 if (flags & LOOPS_HAVE_PREHEADERS)
61 int cp_flags = CP_SIMPLE_PREHEADERS;
63 if (flags & LOOPS_HAVE_FALLTHRU_PREHEADERS)
64 cp_flags |= CP_FALLTHRU_PREHEADERS;
66 create_preheaders (cp_flags);
69 /* Force all latches to have only single successor. */
70 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
71 force_single_succ_latches ();
73 /* Mark irreducible loops. */
74 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
75 mark_irreducible_loops ();
77 if (flags & LOOPS_HAVE_RECORDED_EXITS)
78 record_loop_exits ();
81 /* Initialize loop structures. This is used by the tree and RTL loop
82 optimizers. FLAGS specify what properties to compute and/or ensure for
83 loops. */
85 void
86 loop_optimizer_init (unsigned flags)
88 timevar_push (TV_LOOP_INIT);
90 if (!current_loops)
92 gcc_assert (!(cfun->curr_properties & PROP_loops));
94 /* Find the loops. */
95 current_loops = flow_loops_find (NULL);
97 else
99 bool recorded_exits = loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS);
100 bool needs_fixup = loops_state_satisfies_p (LOOPS_NEED_FIXUP);
102 gcc_assert (cfun->curr_properties & PROP_loops);
104 /* Ensure that the dominators are computed, like flow_loops_find does. */
105 calculate_dominance_info (CDI_DOMINATORS);
107 if (!needs_fixup)
108 checking_verify_loop_structure ();
110 /* Clear all flags. */
111 if (recorded_exits)
112 release_recorded_exits (cfun);
113 loops_state_clear (~0U);
115 if (needs_fixup)
117 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
118 re-applies flags. */
119 loops_state_set (flags & LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
120 fix_loop_structure (NULL);
124 /* Apply flags to loops. */
125 apply_loop_flags (flags);
127 /* Dump loops. */
128 flow_loops_dump (dump_file, NULL, 1);
130 checking_verify_loop_structure ();
132 timevar_pop (TV_LOOP_INIT);
135 /* Finalize loop structures. */
137 void
138 loop_optimizer_finalize (struct function *fn)
140 struct loop *loop;
141 basic_block bb;
143 timevar_push (TV_LOOP_FINI);
145 if (loops_state_satisfies_p (fn, LOOPS_HAVE_RECORDED_EXITS))
146 release_recorded_exits (fn);
148 free_numbers_of_iterations_estimates (fn);
150 /* If we should preserve loop structure, do not free it but clear
151 flags that advanced properties are there as we are not preserving
152 that in full. */
153 if (fn->curr_properties & PROP_loops)
155 loops_state_clear (fn, LOOP_CLOSED_SSA
156 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
157 | LOOPS_HAVE_PREHEADERS
158 | LOOPS_HAVE_SIMPLE_LATCHES
159 | LOOPS_HAVE_FALLTHRU_PREHEADERS);
160 loops_state_set (fn, LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
161 goto loop_fini_done;
164 FOR_EACH_LOOP_FN (fn, loop, 0)
165 free_simple_loop_desc (loop);
167 /* Clean up. */
168 flow_loops_free (loops_for_fn (fn));
169 ggc_free (loops_for_fn (fn));
170 set_loops_for_fn (fn, NULL);
172 FOR_ALL_BB_FN (bb, fn)
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 if (dump_file && (dump_flags & TDF_DETAILS))
205 fprintf (dump_file, "fix_loop_structure: fixing up loops for function\n");
207 /* We need exact and fast dominance info to be available. */
208 gcc_assert (dom_info_state (CDI_DOMINATORS) == DOM_OK);
210 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
212 release_recorded_exits (cfun);
213 record_exits = LOOPS_HAVE_RECORDED_EXITS;
216 /* Remember the depth of the blocks in the loop hierarchy, so that we can
217 recognize blocks whose loop nesting relationship has changed. */
218 if (changed_bbs)
219 FOR_EACH_BB_FN (bb, cfun)
220 bb->aux = (void *) (size_t) loop_depth (bb->loop_father);
222 /* Remove the dead loops from structures. We start from the innermost
223 loops, so that when we remove the loops, we know that the loops inside
224 are preserved, and do not waste time relinking loops that will be
225 removed later. */
226 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
228 /* Detect the case that the loop is no longer present even though
229 it wasn't marked for removal.
230 ??? If we do that we can get away with not marking loops for
231 removal at all. And possibly avoid some spurious removals. */
232 if (loop->header
233 && bb_loop_header_p (loop->header))
234 continue;
236 if (dump_file && (dump_flags & TDF_DETAILS))
237 fprintf (dump_file, "fix_loop_structure: removing loop %d\n",
238 loop->num);
240 while (loop->inner)
242 struct loop *ploop = loop->inner;
243 flow_loop_tree_node_remove (ploop);
244 flow_loop_tree_node_add (loop_outer (loop), ploop);
247 /* Remove the loop. */
248 if (loop->header)
249 loop->former_header = loop->header;
250 else
251 gcc_assert (loop->former_header != NULL);
252 loop->header = NULL;
253 flow_loop_tree_node_remove (loop);
256 /* Remember the number of loops so we can return how many new loops
257 flow_loops_find discovered. */
258 old_nloops = number_of_loops (cfun);
260 /* Re-compute loop structure in-place. */
261 flow_loops_find (current_loops);
263 /* Mark the blocks whose loop has changed. */
264 if (changed_bbs)
266 FOR_EACH_BB_FN (bb, cfun)
268 if ((void *) (size_t) loop_depth (bb->loop_father) != bb->aux)
269 bitmap_set_bit (changed_bbs, bb->index);
271 bb->aux = NULL;
275 /* Finally free deleted loops. */
276 bool any_deleted = false;
277 FOR_EACH_VEC_ELT (*get_loops (cfun), i, loop)
278 if (loop && loop->header == NULL)
280 if (dump_file
281 && ((unsigned) loop->former_header->index
282 < basic_block_info_for_fn (cfun)->length ()))
284 basic_block former_header
285 = BASIC_BLOCK_FOR_FN (cfun, loop->former_header->index);
286 /* If the old header still exists we want to check if the
287 original loop is re-discovered or the old header is now
288 part of a newly discovered loop.
289 In both cases we should have avoided removing the loop. */
290 if (former_header == loop->former_header)
292 if (former_header->loop_father->header == former_header)
293 fprintf (dump_file, "fix_loop_structure: rediscovered "
294 "removed loop %d as loop %d with old header %d\n",
295 loop->num, former_header->loop_father->num,
296 former_header->index);
297 else if ((unsigned) former_header->loop_father->num
298 >= old_nloops)
299 fprintf (dump_file, "fix_loop_structure: header %d of "
300 "removed loop %d is part of the newly "
301 "discovered loop %d with header %d\n",
302 former_header->index, loop->num,
303 former_header->loop_father->num,
304 former_header->loop_father->header->index);
307 (*get_loops (cfun))[i] = NULL;
308 flow_loop_free (loop);
309 any_deleted = true;
312 /* If we deleted loops then the cached scalar evolutions refering to
313 those loops become invalid. */
314 if (any_deleted && scev_initialized_p ())
315 scev_reset_htab ();
317 loops_state_clear (LOOPS_NEED_FIXUP);
319 /* Apply flags to loops. */
320 apply_loop_flags (current_loops->state | record_exits);
322 checking_verify_loop_structure ();
324 timevar_pop (TV_LOOP_INIT);
326 return number_of_loops (cfun) - old_nloops;
329 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
330 more on that. */
332 namespace {
334 const pass_data pass_data_loop2 =
336 RTL_PASS, /* type */
337 "loop2", /* name */
338 OPTGROUP_LOOP, /* optinfo_flags */
339 TV_LOOP, /* tv_id */
340 0, /* properties_required */
341 0, /* properties_provided */
342 0, /* properties_destroyed */
343 0, /* todo_flags_start */
344 0, /* todo_flags_finish */
347 class pass_loop2 : public rtl_opt_pass
349 public:
350 pass_loop2 (gcc::context *ctxt)
351 : rtl_opt_pass (pass_data_loop2, ctxt)
354 /* opt_pass methods: */
355 virtual bool gate (function *);
357 }; // class pass_loop2
359 bool
360 pass_loop2::gate (function *fun)
362 if (optimize > 0
363 && (flag_move_loop_invariants
364 || flag_unswitch_loops
365 || flag_unroll_loops
366 || (flag_branch_on_count_reg
367 && targetm.have_doloop_end ())))
368 return true;
369 else
371 /* No longer preserve loops, remove them now. */
372 fun->curr_properties &= ~PROP_loops;
373 if (current_loops)
374 loop_optimizer_finalize ();
375 return false;
379 } // anon namespace
381 rtl_opt_pass *
382 make_pass_loop2 (gcc::context *ctxt)
384 return new pass_loop2 (ctxt);
388 /* Initialization of the RTL loop passes. */
389 static unsigned int
390 rtl_loop_init (void)
392 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
394 if (dump_file)
396 dump_reg_info (dump_file);
397 dump_flow_info (dump_file, dump_flags);
400 loop_optimizer_init (LOOPS_NORMAL);
401 return 0;
404 namespace {
406 const pass_data pass_data_rtl_loop_init =
408 RTL_PASS, /* type */
409 "loop2_init", /* name */
410 OPTGROUP_LOOP, /* optinfo_flags */
411 TV_LOOP, /* tv_id */
412 0, /* properties_required */
413 0, /* properties_provided */
414 0, /* properties_destroyed */
415 0, /* todo_flags_start */
416 0, /* todo_flags_finish */
419 class pass_rtl_loop_init : public rtl_opt_pass
421 public:
422 pass_rtl_loop_init (gcc::context *ctxt)
423 : rtl_opt_pass (pass_data_rtl_loop_init, ctxt)
426 /* opt_pass methods: */
427 virtual unsigned int execute (function *) { return rtl_loop_init (); }
429 }; // class pass_rtl_loop_init
431 } // anon namespace
433 rtl_opt_pass *
434 make_pass_rtl_loop_init (gcc::context *ctxt)
436 return new pass_rtl_loop_init (ctxt);
440 /* Finalization of the RTL loop passes. */
442 namespace {
444 const pass_data pass_data_rtl_loop_done =
446 RTL_PASS, /* type */
447 "loop2_done", /* name */
448 OPTGROUP_LOOP, /* optinfo_flags */
449 TV_LOOP, /* tv_id */
450 0, /* properties_required */
451 0, /* properties_provided */
452 PROP_loops, /* properties_destroyed */
453 0, /* todo_flags_start */
454 0, /* todo_flags_finish */
457 class pass_rtl_loop_done : public rtl_opt_pass
459 public:
460 pass_rtl_loop_done (gcc::context *ctxt)
461 : rtl_opt_pass (pass_data_rtl_loop_done, ctxt)
464 /* opt_pass methods: */
465 virtual unsigned int execute (function *);
467 }; // class pass_rtl_loop_done
469 unsigned int
470 pass_rtl_loop_done::execute (function *fun)
472 /* No longer preserve loops, remove them now. */
473 fun->curr_properties &= ~PROP_loops;
474 loop_optimizer_finalize ();
475 free_dominance_info (CDI_DOMINATORS);
477 cleanup_cfg (0);
478 if (dump_file)
480 dump_reg_info (dump_file);
481 dump_flow_info (dump_file, dump_flags);
484 return 0;
487 } // anon namespace
489 rtl_opt_pass *
490 make_pass_rtl_loop_done (gcc::context *ctxt)
492 return new pass_rtl_loop_done (ctxt);
496 /* Loop invariant code motion. */
498 namespace {
500 const pass_data pass_data_rtl_move_loop_invariants =
502 RTL_PASS, /* type */
503 "loop2_invariant", /* name */
504 OPTGROUP_LOOP, /* optinfo_flags */
505 TV_LOOP_MOVE_INVARIANTS, /* tv_id */
506 0, /* properties_required */
507 0, /* properties_provided */
508 0, /* properties_destroyed */
509 0, /* todo_flags_start */
510 ( TODO_df_verify | TODO_df_finish ), /* todo_flags_finish */
513 class pass_rtl_move_loop_invariants : public rtl_opt_pass
515 public:
516 pass_rtl_move_loop_invariants (gcc::context *ctxt)
517 : rtl_opt_pass (pass_data_rtl_move_loop_invariants, ctxt)
520 /* opt_pass methods: */
521 virtual bool gate (function *) { return flag_move_loop_invariants; }
522 virtual unsigned int execute (function *fun)
524 if (number_of_loops (fun) > 1)
525 move_loop_invariants ();
526 return 0;
529 }; // class pass_rtl_move_loop_invariants
531 } // anon namespace
533 rtl_opt_pass *
534 make_pass_rtl_move_loop_invariants (gcc::context *ctxt)
536 return new pass_rtl_move_loop_invariants (ctxt);
540 namespace {
542 const pass_data pass_data_rtl_unroll_loops =
544 RTL_PASS, /* type */
545 "loop2_unroll", /* name */
546 OPTGROUP_LOOP, /* optinfo_flags */
547 TV_LOOP_UNROLL, /* tv_id */
548 0, /* properties_required */
549 0, /* properties_provided */
550 0, /* properties_destroyed */
551 0, /* todo_flags_start */
552 0, /* todo_flags_finish */
555 class pass_rtl_unroll_loops : public rtl_opt_pass
557 public:
558 pass_rtl_unroll_loops (gcc::context *ctxt)
559 : rtl_opt_pass (pass_data_rtl_unroll_loops, ctxt)
562 /* opt_pass methods: */
563 virtual bool gate (function *)
565 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
568 virtual unsigned int execute (function *);
570 }; // class pass_rtl_unroll_loops
572 unsigned int
573 pass_rtl_unroll_loops::execute (function *fun)
575 if (number_of_loops (fun) > 1)
577 int flags = 0;
578 if (dump_file)
579 df_dump (dump_file);
581 if (flag_unroll_loops)
582 flags |= UAP_UNROLL;
583 if (flag_unroll_all_loops)
584 flags |= UAP_UNROLL_ALL;
586 unroll_loops (flags);
588 return 0;
591 } // anon namespace
593 rtl_opt_pass *
594 make_pass_rtl_unroll_loops (gcc::context *ctxt)
596 return new pass_rtl_unroll_loops (ctxt);
600 namespace {
602 const pass_data pass_data_rtl_doloop =
604 RTL_PASS, /* type */
605 "loop2_doloop", /* name */
606 OPTGROUP_LOOP, /* optinfo_flags */
607 TV_LOOP_DOLOOP, /* tv_id */
608 0, /* properties_required */
609 0, /* properties_provided */
610 0, /* properties_destroyed */
611 0, /* todo_flags_start */
612 0, /* todo_flags_finish */
615 class pass_rtl_doloop : public rtl_opt_pass
617 public:
618 pass_rtl_doloop (gcc::context *ctxt)
619 : rtl_opt_pass (pass_data_rtl_doloop, ctxt)
622 /* opt_pass methods: */
623 virtual bool gate (function *);
624 virtual unsigned int execute (function *);
626 }; // class pass_rtl_doloop
628 bool
629 pass_rtl_doloop::gate (function *)
631 return (flag_branch_on_count_reg && targetm.have_doloop_end ());
634 unsigned int
635 pass_rtl_doloop::execute (function *fun)
637 if (number_of_loops (fun) > 1)
638 doloop_optimize_loops ();
639 return 0;
642 } // anon namespace
644 rtl_opt_pass *
645 make_pass_rtl_doloop (gcc::context *ctxt)
647 return new pass_rtl_doloop (ctxt);