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"
28 #include "basic-block.h"
30 #include "tree-pass.h"
34 #include "tree-ssa-loop-niter.h"
37 /* Apply FLAGS to the loop state. */
40 apply_loop_flags (unsigned flags
)
42 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
44 /* If the loops may have multiple latches, we cannot canonicalize
45 them further (and most of the loop manipulation functions will
46 not work). However, we avoid modifying cfg, which some
48 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
49 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
50 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
53 disambiguate_loops_with_multiple_latches ();
55 /* Create pre-headers. */
56 if (flags
& LOOPS_HAVE_PREHEADERS
)
58 int cp_flags
= CP_SIMPLE_PREHEADERS
;
60 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
61 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
63 create_preheaders (cp_flags
);
66 /* Force all latches to have only single successor. */
67 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
68 force_single_succ_latches ();
70 /* Mark irreducible loops. */
71 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
72 mark_irreducible_loops ();
74 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
78 /* Initialize loop structures. This is used by the tree and RTL loop
79 optimizers. FLAGS specify what properties to compute and/or ensure for
83 loop_optimizer_init (unsigned flags
)
85 timevar_push (TV_LOOP_INIT
);
89 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
92 current_loops
= flow_loops_find (NULL
);
96 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
98 gcc_assert (cfun
->curr_properties
& PROP_loops
);
100 /* Ensure that the dominators are computed, like flow_loops_find does. */
101 calculate_dominance_info (CDI_DOMINATORS
);
103 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP
))
105 loops_state_clear (~0U);
106 fix_loop_structure (NULL
);
109 #ifdef ENABLE_CHECKING
111 verify_loop_structure ();
114 /* Clear all flags. */
116 release_recorded_exits ();
117 loops_state_clear (~0U);
120 /* Apply flags to loops. */
121 apply_loop_flags (flags
);
124 flow_loops_dump (dump_file
, NULL
, 1);
126 #ifdef ENABLE_CHECKING
127 verify_loop_structure ();
130 timevar_pop (TV_LOOP_INIT
);
133 /* Finalize loop structures. */
136 loop_optimizer_finalize (void)
141 timevar_push (TV_LOOP_FINI
);
143 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
144 release_recorded_exits ();
146 free_numbers_of_iterations_estimates ();
148 /* If we should preserve loop structure, do not free it but clear
149 flags that advanced properties are there as we are not preserving
151 if (cfun
->curr_properties
& PROP_loops
)
153 loops_state_clear (LOOP_CLOSED_SSA
154 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
155 | LOOPS_HAVE_PREHEADERS
156 | LOOPS_HAVE_SIMPLE_LATCHES
157 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
158 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
162 gcc_assert (current_loops
!= NULL
);
164 FOR_EACH_LOOP (loop
, 0)
165 free_simple_loop_desc (loop
);
168 flow_loops_free (current_loops
);
169 ggc_free (current_loops
);
170 current_loops
= NULL
;
172 FOR_ALL_BB_FN (bb
, cfun
)
174 bb
->loop_father
= NULL
;
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
192 Returns the number of new discovered loops. */
195 fix_loop_structure (bitmap changed_bbs
)
198 int record_exits
= 0;
200 unsigned old_nloops
, i
;
202 timevar_push (TV_LOOP_INIT
);
204 /* We need exact and fast dominance info to be available. */
205 gcc_assert (dom_info_state (CDI_DOMINATORS
) == DOM_OK
);
207 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
209 release_recorded_exits ();
210 record_exits
= LOOPS_HAVE_RECORDED_EXITS
;
213 /* Remember the depth of the blocks in the loop hierarchy, so that we can
214 recognize blocks whose loop nesting relationship has changed. */
216 FOR_EACH_BB_FN (bb
, cfun
)
217 bb
->aux
= (void *) (size_t) loop_depth (bb
->loop_father
);
219 /* Remove the dead loops from structures. We start from the innermost
220 loops, so that when we remove the loops, we know that the loops inside
221 are preserved, and do not waste time relinking loops that will be
223 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
225 /* Detect the case that the loop is no longer present even though
226 it wasn't marked for removal.
227 ??? If we do that we can get away with not marking loops for
228 removal at all. And possibly avoid some spurious removals. */
230 && bb_loop_header_p (loop
->header
))
233 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
234 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
239 struct loop
*ploop
= loop
->inner
;
240 flow_loop_tree_node_remove (ploop
);
241 flow_loop_tree_node_add (loop_outer (loop
), ploop
);
244 /* Remove the loop. */
246 flow_loop_tree_node_remove (loop
);
249 /* Remember the number of loops so we can return how many new loops
250 flow_loops_find discovered. */
251 old_nloops
= number_of_loops (cfun
);
253 /* Re-compute loop structure in-place. */
254 flow_loops_find (current_loops
);
256 /* Mark the blocks whose loop has changed. */
259 FOR_EACH_BB_FN (bb
, cfun
)
261 if ((void *) (size_t) loop_depth (bb
->loop_father
) != bb
->aux
)
262 bitmap_set_bit (changed_bbs
, bb
->index
);
268 /* Finally free deleted loops. */
269 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
270 if (loop
&& loop
->header
== NULL
)
272 (*get_loops (cfun
))[i
] = NULL
;
273 flow_loop_free (loop
);
276 loops_state_clear (LOOPS_NEED_FIXUP
);
278 /* Apply flags to loops. */
279 apply_loop_flags (current_loops
->state
| record_exits
);
281 #ifdef ENABLE_CHECKING
282 verify_loop_structure ();
285 timevar_pop (TV_LOOP_INIT
);
287 return number_of_loops (cfun
) - old_nloops
;
290 /* Gate for the RTL loop superpass. The actual passes are subpasses.
291 See passes.c for more on that. */
294 gate_handle_loop2 (void)
297 && (flag_move_loop_invariants
298 || flag_unswitch_loops
301 #ifdef HAVE_doloop_end
302 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
308 /* No longer preserve loops, remove them now. */
309 cfun
->curr_properties
&= ~PROP_loops
;
311 loop_optimizer_finalize ();
318 const pass_data pass_data_loop2
=
322 OPTGROUP_LOOP
, /* optinfo_flags */
324 false, /* has_execute */
326 0, /* properties_required */
327 0, /* properties_provided */
328 0, /* properties_destroyed */
329 0, /* todo_flags_start */
330 0, /* todo_flags_finish */
333 class pass_loop2
: public rtl_opt_pass
336 pass_loop2 (gcc::context
*ctxt
)
337 : rtl_opt_pass (pass_data_loop2
, ctxt
)
340 /* opt_pass methods: */
341 bool gate () { return gate_handle_loop2 (); }
343 }; // class pass_loop2
348 make_pass_loop2 (gcc::context
*ctxt
)
350 return new pass_loop2 (ctxt
);
354 /* Initialization of the RTL loop passes. */
358 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
362 dump_reg_info (dump_file
);
363 dump_flow_info (dump_file
, dump_flags
);
366 loop_optimizer_init (LOOPS_NORMAL
);
372 const pass_data pass_data_rtl_loop_init
=
375 "loop2_init", /* name */
376 OPTGROUP_LOOP
, /* optinfo_flags */
377 false, /* has_gate */
378 true, /* has_execute */
380 0, /* properties_required */
381 0, /* properties_provided */
382 0, /* properties_destroyed */
383 0, /* todo_flags_start */
384 TODO_verify_rtl_sharing
, /* todo_flags_finish */
387 class pass_rtl_loop_init
: public rtl_opt_pass
390 pass_rtl_loop_init (gcc::context
*ctxt
)
391 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
394 /* opt_pass methods: */
395 unsigned int execute () { return rtl_loop_init (); }
397 }; // class pass_rtl_loop_init
402 make_pass_rtl_loop_init (gcc::context
*ctxt
)
404 return new pass_rtl_loop_init (ctxt
);
408 /* Finalization of the RTL loop passes. */
413 /* No longer preserve loops, remove them now. */
414 cfun
->curr_properties
&= ~PROP_loops
;
415 loop_optimizer_finalize ();
416 free_dominance_info (CDI_DOMINATORS
);
421 dump_reg_info (dump_file
);
422 dump_flow_info (dump_file
, dump_flags
);
430 const pass_data pass_data_rtl_loop_done
=
433 "loop2_done", /* name */
434 OPTGROUP_LOOP
, /* optinfo_flags */
435 false, /* has_gate */
436 true, /* has_execute */
438 0, /* properties_required */
439 0, /* properties_provided */
440 PROP_loops
, /* properties_destroyed */
441 0, /* todo_flags_start */
442 ( TODO_verify_flow
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
445 class pass_rtl_loop_done
: public rtl_opt_pass
448 pass_rtl_loop_done (gcc::context
*ctxt
)
449 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
452 /* opt_pass methods: */
453 unsigned int execute () { return rtl_loop_done (); }
455 }; // class pass_rtl_loop_done
460 make_pass_rtl_loop_done (gcc::context
*ctxt
)
462 return new pass_rtl_loop_done (ctxt
);
466 /* Loop invariant code motion. */
468 gate_rtl_move_loop_invariants (void)
470 return flag_move_loop_invariants
;
474 rtl_move_loop_invariants (void)
476 if (number_of_loops (cfun
) > 1)
477 move_loop_invariants ();
483 const pass_data pass_data_rtl_move_loop_invariants
=
486 "loop2_invariant", /* name */
487 OPTGROUP_LOOP
, /* optinfo_flags */
489 true, /* has_execute */
490 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
491 0, /* properties_required */
492 0, /* properties_provided */
493 0, /* properties_destroyed */
494 0, /* todo_flags_start */
495 ( TODO_df_verify
| TODO_df_finish
496 | TODO_verify_rtl_sharing
), /* todo_flags_finish */
499 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
502 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
503 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
506 /* opt_pass methods: */
507 bool gate () { return gate_rtl_move_loop_invariants (); }
508 unsigned int execute () { return rtl_move_loop_invariants (); }
510 }; // class pass_rtl_move_loop_invariants
515 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
517 return new pass_rtl_move_loop_invariants (ctxt
);
521 /* Loop unswitching for RTL. */
523 gate_rtl_unswitch (void)
525 return flag_unswitch_loops
;
531 if (number_of_loops (cfun
) > 1)
538 const pass_data pass_data_rtl_unswitch
=
541 "loop2_unswitch", /* name */
542 OPTGROUP_LOOP
, /* optinfo_flags */
544 true, /* has_execute */
545 TV_LOOP_UNSWITCH
, /* tv_id */
546 0, /* properties_required */
547 0, /* properties_provided */
548 0, /* properties_destroyed */
549 0, /* todo_flags_start */
550 TODO_verify_rtl_sharing
, /* todo_flags_finish */
553 class pass_rtl_unswitch
: public rtl_opt_pass
556 pass_rtl_unswitch (gcc::context
*ctxt
)
557 : rtl_opt_pass (pass_data_rtl_unswitch
, ctxt
)
560 /* opt_pass methods: */
561 bool gate () { return gate_rtl_unswitch (); }
562 unsigned int execute () { return rtl_unswitch (); }
564 }; // class pass_rtl_unswitch
569 make_pass_rtl_unswitch (gcc::context
*ctxt
)
571 return new pass_rtl_unswitch (ctxt
);
575 /* Loop unswitching for RTL. */
577 gate_rtl_unroll_and_peel_loops (void)
579 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
583 rtl_unroll_and_peel_loops (void)
585 if (number_of_loops (cfun
) > 1)
593 if (flag_unroll_loops
)
595 if (flag_unroll_all_loops
)
596 flags
|= UAP_UNROLL_ALL
;
598 unroll_and_peel_loops (flags
);
605 const pass_data pass_data_rtl_unroll_and_peel_loops
=
608 "loop2_unroll", /* name */
609 OPTGROUP_LOOP
, /* optinfo_flags */
611 true, /* has_execute */
612 TV_LOOP_UNROLL
, /* tv_id */
613 0, /* properties_required */
614 0, /* properties_provided */
615 0, /* properties_destroyed */
616 0, /* todo_flags_start */
617 TODO_verify_rtl_sharing
, /* todo_flags_finish */
620 class pass_rtl_unroll_and_peel_loops
: public rtl_opt_pass
623 pass_rtl_unroll_and_peel_loops (gcc::context
*ctxt
)
624 : rtl_opt_pass (pass_data_rtl_unroll_and_peel_loops
, ctxt
)
627 /* opt_pass methods: */
628 bool gate () { return gate_rtl_unroll_and_peel_loops (); }
629 unsigned int execute () { return rtl_unroll_and_peel_loops (); }
631 }; // class pass_rtl_unroll_and_peel_loops
636 make_pass_rtl_unroll_and_peel_loops (gcc::context
*ctxt
)
638 return new pass_rtl_unroll_and_peel_loops (ctxt
);
642 /* The doloop optimization. */
644 gate_rtl_doloop (void)
646 #ifdef HAVE_doloop_end
647 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
656 #ifdef HAVE_doloop_end
657 if (number_of_loops (cfun
) > 1)
658 doloop_optimize_loops ();
665 const pass_data pass_data_rtl_doloop
=
668 "loop2_doloop", /* name */
669 OPTGROUP_LOOP
, /* optinfo_flags */
671 true, /* has_execute */
672 TV_LOOP_DOLOOP
, /* tv_id */
673 0, /* properties_required */
674 0, /* properties_provided */
675 0, /* properties_destroyed */
676 0, /* todo_flags_start */
677 TODO_verify_rtl_sharing
, /* todo_flags_finish */
680 class pass_rtl_doloop
: public rtl_opt_pass
683 pass_rtl_doloop (gcc::context
*ctxt
)
684 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
687 /* opt_pass methods: */
688 bool gate () { return gate_rtl_doloop (); }
689 unsigned int execute () { return rtl_doloop (); }
691 }; // class pass_rtl_doloop
696 make_pass_rtl_doloop (gcc::context
*ctxt
)
698 return new pass_rtl_doloop (ctxt
);