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
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"
27 #include "basic-block.h"
29 #include "tree-pass.h"
35 /* Apply FLAGS to the loop state. */
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
46 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
47 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
48 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
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
)
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
81 loop_optimizer_init (unsigned flags
)
83 timevar_push (TV_LOOP_INIT
);
87 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
90 current_loops
= flow_loops_find (NULL
);
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 ();
104 /* Apply flags to loops. */
105 apply_loop_flags (flags
);
108 flow_loops_dump (dump_file
, NULL
, 1);
110 #ifdef ENABLE_CHECKING
111 verify_loop_structure ();
114 timevar_pop (TV_LOOP_INIT
);
117 /* Finalize loop structures. */
120 loop_optimizer_finalize (void)
126 timevar_push (TV_LOOP_FINI
);
128 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
129 release_recorded_exits ();
131 /* If we should preserve loop structure, do not free it but clear
132 flags that advanced properties are there as we are not preserving
134 if (cfun
->curr_properties
& PROP_loops
)
136 loops_state_clear (LOOP_CLOSED_SSA
137 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
138 | LOOPS_HAVE_PREHEADERS
139 | LOOPS_HAVE_SIMPLE_LATCHES
140 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
141 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
145 gcc_assert (current_loops
!= NULL
);
147 FOR_EACH_LOOP (li
, loop
, 0)
149 free_simple_loop_desc (loop
);
153 flow_loops_free (current_loops
);
154 ggc_free (current_loops
);
155 current_loops
= NULL
;
159 bb
->loop_father
= NULL
;
163 timevar_pop (TV_LOOP_FINI
);
166 /* The structure of loops might have changed. Some loops might get removed
167 (and their headers and latches were set to NULL), loop exists might get
168 removed (thus the loop nesting may be wrong), and some blocks and edges
169 were changed (so the information about bb --> loop mapping does not have
170 to be correct). But still for the remaining loops the header dominates
171 the latch, and loops did not get new subloops (new loops might possibly
172 get created, but we are not interested in them). Fix up the mess.
174 If CHANGED_BBS is not NULL, basic blocks whose loop has changed are
178 fix_loop_structure (bitmap changed_bbs
)
181 int record_exits
= 0;
185 timevar_push (TV_LOOP_INIT
);
187 /* We need exact and fast dominance info to be available. */
188 gcc_assert (dom_info_state (CDI_DOMINATORS
) == DOM_OK
);
190 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
192 release_recorded_exits ();
193 record_exits
= LOOPS_HAVE_RECORDED_EXITS
;
196 /* Remember the depth of the blocks in the loop hierarchy, so that we can
197 recognize blocks whose loop nesting relationship has changed. */
200 bb
->aux
= (void *) (size_t) loop_depth (bb
->loop_father
);
202 /* Remove the dead loops from structures. We start from the innermost
203 loops, so that when we remove the loops, we know that the loops inside
204 are preserved, and do not waste time relinking loops that will be
206 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
208 /* Detect the case that the loop is no longer present even though
209 it wasn't marked for removal.
210 ??? If we do that we can get away with not marking loops for
211 removal at all. And possibly avoid some spurious removals. */
213 && bb_loop_header_p (loop
->header
))
216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
217 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
222 struct loop
*ploop
= loop
->inner
;
223 flow_loop_tree_node_remove (ploop
);
224 flow_loop_tree_node_add (loop_outer (loop
), ploop
);
227 /* Remove the loop and free its data. */
231 /* Re-compute loop structure in-place. */
232 flow_loops_find (current_loops
);
234 /* Mark the blocks whose loop has changed. */
239 if ((void *) (size_t) loop_depth (bb
->loop_father
) != bb
->aux
)
240 bitmap_set_bit (changed_bbs
, bb
->index
);
246 loops_state_clear (LOOPS_NEED_FIXUP
);
248 /* Apply flags to loops. */
249 apply_loop_flags (current_loops
->state
| record_exits
);
251 #ifdef ENABLE_CHECKING
252 verify_loop_structure ();
255 timevar_pop (TV_LOOP_INIT
);
258 /* Gate for the RTL loop superpass. The actual passes are subpasses.
259 See passes.c for more on that. */
262 gate_handle_loop2 (void)
265 && (flag_move_loop_invariants
266 || flag_unswitch_loops
269 #ifdef HAVE_doloop_end
270 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
276 /* No longer preserve loops, remove them now. */
277 cfun
->curr_properties
&= ~PROP_loops
;
279 loop_optimizer_finalize ();
284 struct rtl_opt_pass pass_loop2
=
289 OPTGROUP_LOOP
, /* optinfo_flags */
290 gate_handle_loop2
, /* gate */
294 0, /* static_pass_number */
296 0, /* properties_required */
297 0, /* properties_provided */
298 0, /* properties_destroyed */
299 0, /* todo_flags_start */
300 TODO_ggc_collect
/* todo_flags_finish */
305 /* Initialization of the RTL loop passes. */
309 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
313 dump_reg_info (dump_file
);
314 dump_flow_info (dump_file
, dump_flags
);
317 loop_optimizer_init (LOOPS_NORMAL
);
321 struct rtl_opt_pass pass_rtl_loop_init
=
325 "loop2_init", /* name */
326 OPTGROUP_LOOP
, /* optinfo_flags */
328 rtl_loop_init
, /* execute */
331 0, /* static_pass_number */
333 0, /* properties_required */
334 0, /* properties_provided */
335 0, /* properties_destroyed */
336 0, /* todo_flags_start */
337 TODO_verify_rtl_sharing
/* todo_flags_finish */
342 /* Finalization of the RTL loop passes. */
347 /* No longer preserve loops, remove them now. */
348 cfun
->curr_properties
&= ~PROP_loops
;
349 loop_optimizer_finalize ();
350 free_dominance_info (CDI_DOMINATORS
);
355 dump_reg_info (dump_file
);
356 dump_flow_info (dump_file
, dump_flags
);
362 struct rtl_opt_pass pass_rtl_loop_done
=
366 "loop2_done", /* name */
367 OPTGROUP_LOOP
, /* optinfo_flags */
369 rtl_loop_done
, /* execute */
372 0, /* static_pass_number */
374 0, /* properties_required */
375 0, /* properties_provided */
376 PROP_loops
, /* properties_destroyed */
377 0, /* todo_flags_start */
379 | TODO_verify_rtl_sharing
/* todo_flags_finish */
384 /* Loop invariant code motion. */
386 gate_rtl_move_loop_invariants (void)
388 return flag_move_loop_invariants
;
392 rtl_move_loop_invariants (void)
394 if (number_of_loops () > 1)
395 move_loop_invariants ();
399 struct rtl_opt_pass pass_rtl_move_loop_invariants
=
403 "loop2_invariant", /* name */
404 OPTGROUP_LOOP
, /* optinfo_flags */
405 gate_rtl_move_loop_invariants
, /* gate */
406 rtl_move_loop_invariants
, /* execute */
409 0, /* static_pass_number */
410 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
411 0, /* properties_required */
412 0, /* properties_provided */
413 0, /* properties_destroyed */
414 0, /* todo_flags_start */
416 TODO_df_finish
| TODO_verify_rtl_sharing
/* todo_flags_finish */
421 /* Loop unswitching for RTL. */
423 gate_rtl_unswitch (void)
425 return flag_unswitch_loops
;
431 if (number_of_loops () > 1)
436 struct rtl_opt_pass pass_rtl_unswitch
=
440 "loop2_unswitch", /* name */
441 OPTGROUP_LOOP
, /* optinfo_flags */
442 gate_rtl_unswitch
, /* gate */
443 rtl_unswitch
, /* execute */
446 0, /* static_pass_number */
447 TV_LOOP_UNSWITCH
, /* tv_id */
448 0, /* properties_required */
449 0, /* properties_provided */
450 0, /* properties_destroyed */
451 0, /* todo_flags_start */
452 TODO_verify_rtl_sharing
, /* todo_flags_finish */
457 /* Loop unswitching for RTL. */
459 gate_rtl_unroll_and_peel_loops (void)
461 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
465 rtl_unroll_and_peel_loops (void)
467 if (number_of_loops () > 1)
475 if (flag_unroll_loops
)
477 if (flag_unroll_all_loops
)
478 flags
|= UAP_UNROLL_ALL
;
480 unroll_and_peel_loops (flags
);
485 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops
=
489 "loop2_unroll", /* name */
490 OPTGROUP_LOOP
, /* optinfo_flags */
491 gate_rtl_unroll_and_peel_loops
, /* gate */
492 rtl_unroll_and_peel_loops
, /* execute */
495 0, /* static_pass_number */
496 TV_LOOP_UNROLL
, /* tv_id */
497 0, /* properties_required */
498 0, /* properties_provided */
499 0, /* properties_destroyed */
500 0, /* todo_flags_start */
501 TODO_verify_rtl_sharing
, /* todo_flags_finish */
506 /* The doloop optimization. */
508 gate_rtl_doloop (void)
510 #ifdef HAVE_doloop_end
511 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
520 #ifdef HAVE_doloop_end
521 if (number_of_loops () > 1)
522 doloop_optimize_loops ();
527 struct rtl_opt_pass pass_rtl_doloop
=
531 "loop2_doloop", /* name */
532 OPTGROUP_LOOP
, /* optinfo_flags */
533 gate_rtl_doloop
, /* gate */
534 rtl_doloop
, /* execute */
537 0, /* static_pass_number */
538 TV_LOOP_DOLOOP
, /* tv_id */
539 0, /* properties_required */
540 0, /* properties_provided */
541 0, /* properties_destroyed */
542 0, /* todo_flags_start */
543 TODO_verify_rtl_sharing
/* todo_flags_finish */