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
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"
28 #include "double-int.h"
38 #include "hard-reg-set.h"
41 #include "dominance.h"
43 #include "cfgcleanup.h"
44 #include "basic-block.h"
46 #include "tree-pass.h"
50 #include "tree-ssa-loop-niter.h"
51 #include "loop-unroll.h"
54 /* Apply FLAGS to the loop state. */
57 apply_loop_flags (unsigned flags
)
59 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
61 /* If the loops may have multiple latches, we cannot canonicalize
62 them further (and most of the loop manipulation functions will
63 not work). However, we avoid modifying cfg, which some
65 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
66 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
67 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
70 disambiguate_loops_with_multiple_latches ();
72 /* Create pre-headers. */
73 if (flags
& LOOPS_HAVE_PREHEADERS
)
75 int cp_flags
= CP_SIMPLE_PREHEADERS
;
77 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
78 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
80 create_preheaders (cp_flags
);
83 /* Force all latches to have only single successor. */
84 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
85 force_single_succ_latches ();
87 /* Mark irreducible loops. */
88 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
89 mark_irreducible_loops ();
91 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
95 /* Initialize loop structures. This is used by the tree and RTL loop
96 optimizers. FLAGS specify what properties to compute and/or ensure for
100 loop_optimizer_init (unsigned flags
)
102 timevar_push (TV_LOOP_INIT
);
106 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
108 /* Find the loops. */
109 current_loops
= flow_loops_find (NULL
);
113 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
114 bool needs_fixup
= loops_state_satisfies_p (LOOPS_NEED_FIXUP
);
116 gcc_assert (cfun
->curr_properties
& PROP_loops
);
118 /* Ensure that the dominators are computed, like flow_loops_find does. */
119 calculate_dominance_info (CDI_DOMINATORS
);
121 #ifdef ENABLE_CHECKING
123 verify_loop_structure ();
126 /* Clear all flags. */
128 release_recorded_exits ();
129 loops_state_clear (~0U);
133 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
135 loops_state_set (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
136 fix_loop_structure (NULL
);
140 /* Apply flags to loops. */
141 apply_loop_flags (flags
);
144 flow_loops_dump (dump_file
, NULL
, 1);
146 #ifdef ENABLE_CHECKING
147 verify_loop_structure ();
150 timevar_pop (TV_LOOP_INIT
);
153 /* Finalize loop structures. */
156 loop_optimizer_finalize (void)
161 timevar_push (TV_LOOP_FINI
);
163 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
164 release_recorded_exits ();
166 free_numbers_of_iterations_estimates ();
168 /* If we should preserve loop structure, do not free it but clear
169 flags that advanced properties are there as we are not preserving
171 if (cfun
->curr_properties
& PROP_loops
)
173 loops_state_clear (LOOP_CLOSED_SSA
174 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
175 | LOOPS_HAVE_PREHEADERS
176 | LOOPS_HAVE_SIMPLE_LATCHES
177 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
178 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
182 gcc_assert (current_loops
!= NULL
);
184 FOR_EACH_LOOP (loop
, 0)
185 free_simple_loop_desc (loop
);
188 flow_loops_free (current_loops
);
189 ggc_free (current_loops
);
190 current_loops
= NULL
;
192 FOR_ALL_BB_FN (bb
, cfun
)
194 bb
->loop_father
= NULL
;
198 timevar_pop (TV_LOOP_FINI
);
201 /* The structure of loops might have changed. Some loops might get removed
202 (and their headers and latches were set to NULL), loop exists might get
203 removed (thus the loop nesting may be wrong), and some blocks and edges
204 were changed (so the information about bb --> loop mapping does not have
205 to be correct). But still for the remaining loops the header dominates
206 the latch, and loops did not get new subloops (new loops might possibly
207 get created, but we are not interested in them). Fix up the mess.
209 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
212 Returns the number of new discovered loops. */
215 fix_loop_structure (bitmap changed_bbs
)
218 int record_exits
= 0;
220 unsigned old_nloops
, i
;
222 timevar_push (TV_LOOP_INIT
);
224 /* We need exact and fast dominance info to be available. */
225 gcc_assert (dom_info_state (CDI_DOMINATORS
) == DOM_OK
);
227 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
229 release_recorded_exits ();
230 record_exits
= LOOPS_HAVE_RECORDED_EXITS
;
233 /* Remember the depth of the blocks in the loop hierarchy, so that we can
234 recognize blocks whose loop nesting relationship has changed. */
236 FOR_EACH_BB_FN (bb
, cfun
)
237 bb
->aux
= (void *) (size_t) loop_depth (bb
->loop_father
);
239 /* Remove the dead loops from structures. We start from the innermost
240 loops, so that when we remove the loops, we know that the loops inside
241 are preserved, and do not waste time relinking loops that will be
243 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
245 /* Detect the case that the loop is no longer present even though
246 it wasn't marked for removal.
247 ??? If we do that we can get away with not marking loops for
248 removal at all. And possibly avoid some spurious removals. */
250 && bb_loop_header_p (loop
->header
))
253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
254 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
259 struct loop
*ploop
= loop
->inner
;
260 flow_loop_tree_node_remove (ploop
);
261 flow_loop_tree_node_add (loop_outer (loop
), ploop
);
264 /* Remove the loop. */
266 loop
->former_header
= loop
->header
;
268 gcc_assert (loop
->former_header
!= NULL
);
270 flow_loop_tree_node_remove (loop
);
273 /* Remember the number of loops so we can return how many new loops
274 flow_loops_find discovered. */
275 old_nloops
= number_of_loops (cfun
);
277 /* Re-compute loop structure in-place. */
278 flow_loops_find (current_loops
);
280 /* Mark the blocks whose loop has changed. */
283 FOR_EACH_BB_FN (bb
, cfun
)
285 if ((void *) (size_t) loop_depth (bb
->loop_father
) != bb
->aux
)
286 bitmap_set_bit (changed_bbs
, bb
->index
);
292 /* Finally free deleted loops. */
293 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
294 if (loop
&& loop
->header
== NULL
)
297 && ((unsigned) loop
->former_header
->index
298 < basic_block_info_for_fn (cfun
)->length ()))
300 basic_block former_header
301 = BASIC_BLOCK_FOR_FN (cfun
, loop
->former_header
->index
);
302 /* If the old header still exists we want to check if the
303 original loop is re-discovered or the old header is now
304 part of a newly discovered loop.
305 In both cases we should have avoided removing the loop. */
306 if (former_header
== loop
->former_header
)
308 if (former_header
->loop_father
->header
== former_header
)
309 fprintf (dump_file
, "fix_loop_structure: rediscovered "
310 "removed loop %d as loop %d with old header %d\n",
311 loop
->num
, former_header
->loop_father
->num
,
312 former_header
->index
);
313 else if ((unsigned) former_header
->loop_father
->num
315 fprintf (dump_file
, "fix_loop_structure: header %d of "
316 "removed loop %d is part of the newly "
317 "discovered loop %d with header %d\n",
318 former_header
->index
, loop
->num
,
319 former_header
->loop_father
->num
,
320 former_header
->loop_father
->header
->index
);
323 (*get_loops (cfun
))[i
] = NULL
;
324 flow_loop_free (loop
);
327 loops_state_clear (LOOPS_NEED_FIXUP
);
329 /* Apply flags to loops. */
330 apply_loop_flags (current_loops
->state
| record_exits
);
332 #ifdef ENABLE_CHECKING
333 verify_loop_structure ();
336 timevar_pop (TV_LOOP_INIT
);
338 return number_of_loops (cfun
) - old_nloops
;
341 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
346 const pass_data pass_data_loop2
=
350 OPTGROUP_LOOP
, /* optinfo_flags */
352 0, /* properties_required */
353 0, /* properties_provided */
354 0, /* properties_destroyed */
355 0, /* todo_flags_start */
356 0, /* todo_flags_finish */
359 class pass_loop2
: public rtl_opt_pass
362 pass_loop2 (gcc::context
*ctxt
)
363 : rtl_opt_pass (pass_data_loop2
, ctxt
)
366 /* opt_pass methods: */
367 virtual bool gate (function
*);
369 }; // class pass_loop2
372 pass_loop2::gate (function
*fun
)
375 && (flag_move_loop_invariants
376 || flag_unswitch_loops
378 #ifdef HAVE_doloop_end
379 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
385 /* No longer preserve loops, remove them now. */
386 fun
->curr_properties
&= ~PROP_loops
;
388 loop_optimizer_finalize ();
396 make_pass_loop2 (gcc::context
*ctxt
)
398 return new pass_loop2 (ctxt
);
402 /* Initialization of the RTL loop passes. */
406 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
410 dump_reg_info (dump_file
);
411 dump_flow_info (dump_file
, dump_flags
);
414 loop_optimizer_init (LOOPS_NORMAL
);
420 const pass_data pass_data_rtl_loop_init
=
423 "loop2_init", /* name */
424 OPTGROUP_LOOP
, /* optinfo_flags */
426 0, /* properties_required */
427 0, /* properties_provided */
428 0, /* properties_destroyed */
429 0, /* todo_flags_start */
430 0, /* todo_flags_finish */
433 class pass_rtl_loop_init
: public rtl_opt_pass
436 pass_rtl_loop_init (gcc::context
*ctxt
)
437 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
440 /* opt_pass methods: */
441 virtual unsigned int execute (function
*) { return rtl_loop_init (); }
443 }; // class pass_rtl_loop_init
448 make_pass_rtl_loop_init (gcc::context
*ctxt
)
450 return new pass_rtl_loop_init (ctxt
);
454 /* Finalization of the RTL loop passes. */
458 const pass_data pass_data_rtl_loop_done
=
461 "loop2_done", /* name */
462 OPTGROUP_LOOP
, /* optinfo_flags */
464 0, /* properties_required */
465 0, /* properties_provided */
466 PROP_loops
, /* properties_destroyed */
467 0, /* todo_flags_start */
468 0, /* todo_flags_finish */
471 class pass_rtl_loop_done
: public rtl_opt_pass
474 pass_rtl_loop_done (gcc::context
*ctxt
)
475 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
478 /* opt_pass methods: */
479 virtual unsigned int execute (function
*);
481 }; // class pass_rtl_loop_done
484 pass_rtl_loop_done::execute (function
*fun
)
486 /* No longer preserve loops, remove them now. */
487 fun
->curr_properties
&= ~PROP_loops
;
488 loop_optimizer_finalize ();
489 free_dominance_info (CDI_DOMINATORS
);
494 dump_reg_info (dump_file
);
495 dump_flow_info (dump_file
, dump_flags
);
504 make_pass_rtl_loop_done (gcc::context
*ctxt
)
506 return new pass_rtl_loop_done (ctxt
);
510 /* Loop invariant code motion. */
514 const pass_data pass_data_rtl_move_loop_invariants
=
517 "loop2_invariant", /* name */
518 OPTGROUP_LOOP
, /* optinfo_flags */
519 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
520 0, /* properties_required */
521 0, /* properties_provided */
522 0, /* properties_destroyed */
523 0, /* todo_flags_start */
524 ( TODO_df_verify
| TODO_df_finish
), /* todo_flags_finish */
527 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
530 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
531 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
534 /* opt_pass methods: */
535 virtual bool gate (function
*) { return flag_move_loop_invariants
; }
536 virtual unsigned int execute (function
*fun
)
538 if (number_of_loops (fun
) > 1)
539 move_loop_invariants ();
543 }; // class pass_rtl_move_loop_invariants
548 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
550 return new pass_rtl_move_loop_invariants (ctxt
);
556 const pass_data pass_data_rtl_unroll_loops
=
559 "loop2_unroll", /* name */
560 OPTGROUP_LOOP
, /* optinfo_flags */
561 TV_LOOP_UNROLL
, /* tv_id */
562 0, /* properties_required */
563 0, /* properties_provided */
564 0, /* properties_destroyed */
565 0, /* todo_flags_start */
566 0, /* todo_flags_finish */
569 class pass_rtl_unroll_loops
: public rtl_opt_pass
572 pass_rtl_unroll_loops (gcc::context
*ctxt
)
573 : rtl_opt_pass (pass_data_rtl_unroll_loops
, ctxt
)
576 /* opt_pass methods: */
577 virtual bool gate (function
*)
579 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
582 virtual unsigned int execute (function
*);
584 }; // class pass_rtl_unroll_loops
587 pass_rtl_unroll_loops::execute (function
*fun
)
589 if (number_of_loops (fun
) > 1)
595 if (flag_unroll_loops
)
597 if (flag_unroll_all_loops
)
598 flags
|= UAP_UNROLL_ALL
;
600 unroll_loops (flags
);
608 make_pass_rtl_unroll_loops (gcc::context
*ctxt
)
610 return new pass_rtl_unroll_loops (ctxt
);
616 const pass_data pass_data_rtl_doloop
=
619 "loop2_doloop", /* name */
620 OPTGROUP_LOOP
, /* optinfo_flags */
621 TV_LOOP_DOLOOP
, /* tv_id */
622 0, /* properties_required */
623 0, /* properties_provided */
624 0, /* properties_destroyed */
625 0, /* todo_flags_start */
626 0, /* todo_flags_finish */
629 class pass_rtl_doloop
: public rtl_opt_pass
632 pass_rtl_doloop (gcc::context
*ctxt
)
633 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
636 /* opt_pass methods: */
637 virtual bool gate (function
*);
638 virtual unsigned int execute (function
*);
640 }; // class pass_rtl_doloop
643 pass_rtl_doloop::gate (function
*)
645 #ifdef HAVE_doloop_end
646 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
653 pass_rtl_doloop::execute (function
*fun ATTRIBUTE_UNUSED
)
655 #ifdef HAVE_doloop_end
656 if (number_of_loops (fun
) > 1)
657 doloop_optimize_loops ();
665 make_pass_rtl_doloop (gcc::context
*ctxt
)
667 return new pass_rtl_doloop (ctxt
);