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
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
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/>. */
22 #include "coretypes.h"
33 #include "hard-reg-set.h"
36 #include "dominance.h"
38 #include "cfgcleanup.h"
39 #include "basic-block.h"
41 #include "tree-pass.h"
45 #include "tree-ssa-loop-niter.h"
46 #include "loop-unroll.h"
49 /* Apply FLAGS to the loop state. */
52 apply_loop_flags (unsigned flags
)
54 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
56 /* If the loops may have multiple latches, we cannot canonicalize
57 them further (and most of the loop manipulation functions will
58 not work). However, we avoid modifying cfg, which some
60 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
61 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
62 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
65 disambiguate_loops_with_multiple_latches ();
67 /* Create pre-headers. */
68 if (flags
& LOOPS_HAVE_PREHEADERS
)
70 int cp_flags
= CP_SIMPLE_PREHEADERS
;
72 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
73 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
75 create_preheaders (cp_flags
);
78 /* Force all latches to have only single successor. */
79 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
80 force_single_succ_latches ();
82 /* Mark irreducible loops. */
83 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
84 mark_irreducible_loops ();
86 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
90 /* Initialize loop structures. This is used by the tree and RTL loop
91 optimizers. FLAGS specify what properties to compute and/or ensure for
95 loop_optimizer_init (unsigned flags
)
97 timevar_push (TV_LOOP_INIT
);
101 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
103 /* Find the loops. */
104 current_loops
= flow_loops_find (NULL
);
108 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
109 bool needs_fixup
= loops_state_satisfies_p (LOOPS_NEED_FIXUP
);
111 gcc_assert (cfun
->curr_properties
& PROP_loops
);
113 /* Ensure that the dominators are computed, like flow_loops_find does. */
114 calculate_dominance_info (CDI_DOMINATORS
);
116 #ifdef ENABLE_CHECKING
118 verify_loop_structure ();
121 /* Clear all flags. */
123 release_recorded_exits ();
124 loops_state_clear (~0U);
128 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
130 loops_state_set (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
131 fix_loop_structure (NULL
);
135 /* Apply flags to loops. */
136 apply_loop_flags (flags
);
139 flow_loops_dump (dump_file
, NULL
, 1);
141 #ifdef ENABLE_CHECKING
142 verify_loop_structure ();
145 timevar_pop (TV_LOOP_INIT
);
148 /* Finalize loop structures. */
151 loop_optimizer_finalize (void)
156 timevar_push (TV_LOOP_FINI
);
158 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
159 release_recorded_exits ();
161 free_numbers_of_iterations_estimates ();
163 /* If we should preserve loop structure, do not free it but clear
164 flags that advanced properties are there as we are not preserving
166 if (cfun
->curr_properties
& PROP_loops
)
168 loops_state_clear (LOOP_CLOSED_SSA
169 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
170 | LOOPS_HAVE_PREHEADERS
171 | LOOPS_HAVE_SIMPLE_LATCHES
172 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
173 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
177 gcc_assert (current_loops
!= NULL
);
179 FOR_EACH_LOOP (loop
, 0)
180 free_simple_loop_desc (loop
);
183 flow_loops_free (current_loops
);
184 ggc_free (current_loops
);
185 current_loops
= NULL
;
187 FOR_ALL_BB_FN (bb
, cfun
)
189 bb
->loop_father
= NULL
;
193 timevar_pop (TV_LOOP_FINI
);
196 /* The structure of loops might have changed. Some loops might get removed
197 (and their headers and latches were set to NULL), loop exists might get
198 removed (thus the loop nesting may be wrong), and some blocks and edges
199 were changed (so the information about bb --> loop mapping does not have
200 to be correct). But still for the remaining loops the header dominates
201 the latch, and loops did not get new subloops (new loops might possibly
202 get created, but we are not interested in them). Fix up the mess.
204 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
207 Returns the number of new discovered loops. */
210 fix_loop_structure (bitmap changed_bbs
)
213 int record_exits
= 0;
215 unsigned old_nloops
, i
;
217 timevar_push (TV_LOOP_INIT
);
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. */
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
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. */
245 && bb_loop_header_p (loop
->header
))
248 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
249 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
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. */
261 loop
->former_header
= loop
->header
;
263 gcc_assert (loop
->former_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. */
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
);
287 /* Finally free deleted loops. */
288 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
289 if (loop
&& loop
->header
== NULL
)
292 && ((unsigned) loop
->former_header
->index
293 < basic_block_info_for_fn (cfun
)->length ()))
295 basic_block former_header
296 = BASIC_BLOCK_FOR_FN (cfun
, loop
->former_header
->index
);
297 /* If the old header still exists we want to check if the
298 original loop is re-discovered or the old header is now
299 part of a newly discovered loop.
300 In both cases we should have avoided removing the loop. */
301 if (former_header
== loop
->former_header
)
303 if (former_header
->loop_father
->header
== former_header
)
304 fprintf (dump_file
, "fix_loop_structure: rediscovered "
305 "removed loop %d as loop %d with old header %d\n",
306 loop
->num
, former_header
->loop_father
->num
,
307 former_header
->index
);
308 else if ((unsigned) former_header
->loop_father
->num
310 fprintf (dump_file
, "fix_loop_structure: header %d of "
311 "removed loop %d is part of the newly "
312 "discovered loop %d with header %d\n",
313 former_header
->index
, loop
->num
,
314 former_header
->loop_father
->num
,
315 former_header
->loop_father
->header
->index
);
318 (*get_loops (cfun
))[i
] = NULL
;
319 flow_loop_free (loop
);
322 loops_state_clear (LOOPS_NEED_FIXUP
);
324 /* Apply flags to loops. */
325 apply_loop_flags (current_loops
->state
| record_exits
);
327 #ifdef ENABLE_CHECKING
328 verify_loop_structure ();
331 timevar_pop (TV_LOOP_INIT
);
333 return number_of_loops (cfun
) - old_nloops
;
336 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
341 const pass_data pass_data_loop2
=
345 OPTGROUP_LOOP
, /* optinfo_flags */
347 0, /* properties_required */
348 0, /* properties_provided */
349 0, /* properties_destroyed */
350 0, /* todo_flags_start */
351 0, /* todo_flags_finish */
354 class pass_loop2
: public rtl_opt_pass
357 pass_loop2 (gcc::context
*ctxt
)
358 : rtl_opt_pass (pass_data_loop2
, ctxt
)
361 /* opt_pass methods: */
362 virtual bool gate (function
*);
364 }; // class pass_loop2
367 pass_loop2::gate (function
*fun
)
370 && (flag_move_loop_invariants
371 || flag_unswitch_loops
373 #ifdef HAVE_doloop_end
374 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
380 /* No longer preserve loops, remove them now. */
381 fun
->curr_properties
&= ~PROP_loops
;
383 loop_optimizer_finalize ();
391 make_pass_loop2 (gcc::context
*ctxt
)
393 return new pass_loop2 (ctxt
);
397 /* Initialization of the RTL loop passes. */
401 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
405 dump_reg_info (dump_file
);
406 dump_flow_info (dump_file
, dump_flags
);
409 loop_optimizer_init (LOOPS_NORMAL
);
415 const pass_data pass_data_rtl_loop_init
=
418 "loop2_init", /* name */
419 OPTGROUP_LOOP
, /* optinfo_flags */
421 0, /* properties_required */
422 0, /* properties_provided */
423 0, /* properties_destroyed */
424 0, /* todo_flags_start */
425 0, /* todo_flags_finish */
428 class pass_rtl_loop_init
: public rtl_opt_pass
431 pass_rtl_loop_init (gcc::context
*ctxt
)
432 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
435 /* opt_pass methods: */
436 virtual unsigned int execute (function
*) { return rtl_loop_init (); }
438 }; // class pass_rtl_loop_init
443 make_pass_rtl_loop_init (gcc::context
*ctxt
)
445 return new pass_rtl_loop_init (ctxt
);
449 /* Finalization of the RTL loop passes. */
453 const pass_data pass_data_rtl_loop_done
=
456 "loop2_done", /* name */
457 OPTGROUP_LOOP
, /* optinfo_flags */
459 0, /* properties_required */
460 0, /* properties_provided */
461 PROP_loops
, /* properties_destroyed */
462 0, /* todo_flags_start */
463 0, /* todo_flags_finish */
466 class pass_rtl_loop_done
: public rtl_opt_pass
469 pass_rtl_loop_done (gcc::context
*ctxt
)
470 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
473 /* opt_pass methods: */
474 virtual unsigned int execute (function
*);
476 }; // class pass_rtl_loop_done
479 pass_rtl_loop_done::execute (function
*fun
)
481 /* No longer preserve loops, remove them now. */
482 fun
->curr_properties
&= ~PROP_loops
;
483 loop_optimizer_finalize ();
484 free_dominance_info (CDI_DOMINATORS
);
489 dump_reg_info (dump_file
);
490 dump_flow_info (dump_file
, dump_flags
);
499 make_pass_rtl_loop_done (gcc::context
*ctxt
)
501 return new pass_rtl_loop_done (ctxt
);
505 /* Loop invariant code motion. */
509 const pass_data pass_data_rtl_move_loop_invariants
=
512 "loop2_invariant", /* name */
513 OPTGROUP_LOOP
, /* optinfo_flags */
514 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
515 0, /* properties_required */
516 0, /* properties_provided */
517 0, /* properties_destroyed */
518 0, /* todo_flags_start */
519 ( TODO_df_verify
| TODO_df_finish
), /* todo_flags_finish */
522 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
525 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
526 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
529 /* opt_pass methods: */
530 virtual bool gate (function
*) { return flag_move_loop_invariants
; }
531 virtual unsigned int execute (function
*fun
)
533 if (number_of_loops (fun
) > 1)
534 move_loop_invariants ();
538 }; // class pass_rtl_move_loop_invariants
543 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
545 return new pass_rtl_move_loop_invariants (ctxt
);
551 const pass_data pass_data_rtl_unroll_loops
=
554 "loop2_unroll", /* name */
555 OPTGROUP_LOOP
, /* optinfo_flags */
556 TV_LOOP_UNROLL
, /* tv_id */
557 0, /* properties_required */
558 0, /* properties_provided */
559 0, /* properties_destroyed */
560 0, /* todo_flags_start */
561 0, /* todo_flags_finish */
564 class pass_rtl_unroll_loops
: public rtl_opt_pass
567 pass_rtl_unroll_loops (gcc::context
*ctxt
)
568 : rtl_opt_pass (pass_data_rtl_unroll_loops
, ctxt
)
571 /* opt_pass methods: */
572 virtual bool gate (function
*)
574 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
577 virtual unsigned int execute (function
*);
579 }; // class pass_rtl_unroll_loops
582 pass_rtl_unroll_loops::execute (function
*fun
)
584 if (number_of_loops (fun
) > 1)
590 if (flag_unroll_loops
)
592 if (flag_unroll_all_loops
)
593 flags
|= UAP_UNROLL_ALL
;
595 unroll_loops (flags
);
603 make_pass_rtl_unroll_loops (gcc::context
*ctxt
)
605 return new pass_rtl_unroll_loops (ctxt
);
611 const pass_data pass_data_rtl_doloop
=
614 "loop2_doloop", /* name */
615 OPTGROUP_LOOP
, /* optinfo_flags */
616 TV_LOOP_DOLOOP
, /* tv_id */
617 0, /* properties_required */
618 0, /* properties_provided */
619 0, /* properties_destroyed */
620 0, /* todo_flags_start */
621 0, /* todo_flags_finish */
624 class pass_rtl_doloop
: public rtl_opt_pass
627 pass_rtl_doloop (gcc::context
*ctxt
)
628 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
631 /* opt_pass methods: */
632 virtual bool gate (function
*);
633 virtual unsigned int execute (function
*);
635 }; // class pass_rtl_doloop
638 pass_rtl_doloop::gate (function
*)
640 #ifdef HAVE_doloop_end
641 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
648 pass_rtl_doloop::execute (function
*fun ATTRIBUTE_UNUSED
)
650 #ifdef HAVE_doloop_end
651 if (number_of_loops (fun
) > 1)
652 doloop_optimize_loops ();
660 make_pass_rtl_doloop (gcc::context
*ctxt
)
662 return new pass_rtl_doloop (ctxt
);