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"
31 #include "hard-reg-set.h"
33 #include "dominance.h"
35 #include "cfgcleanup.h"
36 #include "basic-block.h"
38 #include "tree-pass.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "loop-unroll.h"
43 #include "tree-scalar-evolution.h"
46 /* Apply FLAGS to the loop state. */
49 apply_loop_flags (unsigned flags
)
51 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
53 /* If the loops may have multiple latches, we cannot canonicalize
54 them further (and most of the loop manipulation functions will
55 not work). However, we avoid modifying cfg, which some
57 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
58 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
59 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
62 disambiguate_loops_with_multiple_latches ();
64 /* Create pre-headers. */
65 if (flags
& LOOPS_HAVE_PREHEADERS
)
67 int cp_flags
= CP_SIMPLE_PREHEADERS
;
69 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
70 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
72 create_preheaders (cp_flags
);
75 /* Force all latches to have only single successor. */
76 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
77 force_single_succ_latches ();
79 /* Mark irreducible loops. */
80 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
81 mark_irreducible_loops ();
83 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
87 /* Initialize loop structures. This is used by the tree and RTL loop
88 optimizers. FLAGS specify what properties to compute and/or ensure for
92 loop_optimizer_init (unsigned flags
)
94 timevar_push (TV_LOOP_INIT
);
98 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
100 /* Find the loops. */
101 current_loops
= flow_loops_find (NULL
);
105 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
106 bool needs_fixup
= loops_state_satisfies_p (LOOPS_NEED_FIXUP
);
108 gcc_assert (cfun
->curr_properties
& PROP_loops
);
110 /* Ensure that the dominators are computed, like flow_loops_find does. */
111 calculate_dominance_info (CDI_DOMINATORS
);
113 #ifdef ENABLE_CHECKING
115 verify_loop_structure ();
118 /* Clear all flags. */
120 release_recorded_exits ();
121 loops_state_clear (~0U);
125 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
127 loops_state_set (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
128 fix_loop_structure (NULL
);
132 /* Apply flags to loops. */
133 apply_loop_flags (flags
);
136 flow_loops_dump (dump_file
, NULL
, 1);
138 #ifdef ENABLE_CHECKING
139 verify_loop_structure ();
142 timevar_pop (TV_LOOP_INIT
);
145 /* Finalize loop structures. */
148 loop_optimizer_finalize (void)
153 timevar_push (TV_LOOP_FINI
);
155 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
156 release_recorded_exits ();
158 free_numbers_of_iterations_estimates ();
160 /* If we should preserve loop structure, do not free it but clear
161 flags that advanced properties are there as we are not preserving
163 if (cfun
->curr_properties
& PROP_loops
)
165 loops_state_clear (LOOP_CLOSED_SSA
166 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
167 | LOOPS_HAVE_PREHEADERS
168 | LOOPS_HAVE_SIMPLE_LATCHES
169 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
170 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
174 gcc_assert (current_loops
!= NULL
);
176 FOR_EACH_LOOP (loop
, 0)
177 free_simple_loop_desc (loop
);
180 flow_loops_free (current_loops
);
181 ggc_free (current_loops
);
182 current_loops
= NULL
;
184 FOR_ALL_BB_FN (bb
, cfun
)
186 bb
->loop_father
= NULL
;
190 timevar_pop (TV_LOOP_FINI
);
193 /* The structure of loops might have changed. Some loops might get removed
194 (and their headers and latches were set to NULL), loop exists might get
195 removed (thus the loop nesting may be wrong), and some blocks and edges
196 were changed (so the information about bb --> loop mapping does not have
197 to be correct). But still for the remaining loops the header dominates
198 the latch, and loops did not get new subloops (new loops might possibly
199 get created, but we are not interested in them). Fix up the mess.
201 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
204 Returns the number of new discovered loops. */
207 fix_loop_structure (bitmap changed_bbs
)
210 int record_exits
= 0;
212 unsigned old_nloops
, i
;
214 timevar_push (TV_LOOP_INIT
);
216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
217 fprintf (dump_file
, "fix_loop_structure: fixing up loops for function\n");
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 bool any_deleted
= false;
289 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
290 if (loop
&& loop
->header
== NULL
)
293 && ((unsigned) loop
->former_header
->index
294 < basic_block_info_for_fn (cfun
)->length ()))
296 basic_block former_header
297 = BASIC_BLOCK_FOR_FN (cfun
, loop
->former_header
->index
);
298 /* If the old header still exists we want to check if the
299 original loop is re-discovered or the old header is now
300 part of a newly discovered loop.
301 In both cases we should have avoided removing the loop. */
302 if (former_header
== loop
->former_header
)
304 if (former_header
->loop_father
->header
== former_header
)
305 fprintf (dump_file
, "fix_loop_structure: rediscovered "
306 "removed loop %d as loop %d with old header %d\n",
307 loop
->num
, former_header
->loop_father
->num
,
308 former_header
->index
);
309 else if ((unsigned) former_header
->loop_father
->num
311 fprintf (dump_file
, "fix_loop_structure: header %d of "
312 "removed loop %d is part of the newly "
313 "discovered loop %d with header %d\n",
314 former_header
->index
, loop
->num
,
315 former_header
->loop_father
->num
,
316 former_header
->loop_father
->header
->index
);
319 (*get_loops (cfun
))[i
] = NULL
;
320 flow_loop_free (loop
);
324 /* If we deleted loops then the cached scalar evolutions refering to
325 those loops become invalid. */
326 if (any_deleted
&& scev_initialized_p ())
329 loops_state_clear (LOOPS_NEED_FIXUP
);
331 /* Apply flags to loops. */
332 apply_loop_flags (current_loops
->state
| record_exits
);
334 #ifdef ENABLE_CHECKING
335 verify_loop_structure ();
338 timevar_pop (TV_LOOP_INIT
);
340 return number_of_loops (cfun
) - old_nloops
;
343 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
348 const pass_data pass_data_loop2
=
352 OPTGROUP_LOOP
, /* optinfo_flags */
354 0, /* properties_required */
355 0, /* properties_provided */
356 0, /* properties_destroyed */
357 0, /* todo_flags_start */
358 0, /* todo_flags_finish */
361 class pass_loop2
: public rtl_opt_pass
364 pass_loop2 (gcc::context
*ctxt
)
365 : rtl_opt_pass (pass_data_loop2
, ctxt
)
368 /* opt_pass methods: */
369 virtual bool gate (function
*);
371 }; // class pass_loop2
374 pass_loop2::gate (function
*fun
)
377 && (flag_move_loop_invariants
378 || flag_unswitch_loops
380 #ifdef HAVE_doloop_end
381 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
387 /* No longer preserve loops, remove them now. */
388 fun
->curr_properties
&= ~PROP_loops
;
390 loop_optimizer_finalize ();
398 make_pass_loop2 (gcc::context
*ctxt
)
400 return new pass_loop2 (ctxt
);
404 /* Initialization of the RTL loop passes. */
408 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
412 dump_reg_info (dump_file
);
413 dump_flow_info (dump_file
, dump_flags
);
416 loop_optimizer_init (LOOPS_NORMAL
);
422 const pass_data pass_data_rtl_loop_init
=
425 "loop2_init", /* name */
426 OPTGROUP_LOOP
, /* optinfo_flags */
428 0, /* properties_required */
429 0, /* properties_provided */
430 0, /* properties_destroyed */
431 0, /* todo_flags_start */
432 0, /* todo_flags_finish */
435 class pass_rtl_loop_init
: public rtl_opt_pass
438 pass_rtl_loop_init (gcc::context
*ctxt
)
439 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
442 /* opt_pass methods: */
443 virtual unsigned int execute (function
*) { return rtl_loop_init (); }
445 }; // class pass_rtl_loop_init
450 make_pass_rtl_loop_init (gcc::context
*ctxt
)
452 return new pass_rtl_loop_init (ctxt
);
456 /* Finalization of the RTL loop passes. */
460 const pass_data pass_data_rtl_loop_done
=
463 "loop2_done", /* name */
464 OPTGROUP_LOOP
, /* optinfo_flags */
466 0, /* properties_required */
467 0, /* properties_provided */
468 PROP_loops
, /* properties_destroyed */
469 0, /* todo_flags_start */
470 0, /* todo_flags_finish */
473 class pass_rtl_loop_done
: public rtl_opt_pass
476 pass_rtl_loop_done (gcc::context
*ctxt
)
477 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
480 /* opt_pass methods: */
481 virtual unsigned int execute (function
*);
483 }; // class pass_rtl_loop_done
486 pass_rtl_loop_done::execute (function
*fun
)
488 /* No longer preserve loops, remove them now. */
489 fun
->curr_properties
&= ~PROP_loops
;
490 loop_optimizer_finalize ();
491 free_dominance_info (CDI_DOMINATORS
);
496 dump_reg_info (dump_file
);
497 dump_flow_info (dump_file
, dump_flags
);
506 make_pass_rtl_loop_done (gcc::context
*ctxt
)
508 return new pass_rtl_loop_done (ctxt
);
512 /* Loop invariant code motion. */
516 const pass_data pass_data_rtl_move_loop_invariants
=
519 "loop2_invariant", /* name */
520 OPTGROUP_LOOP
, /* optinfo_flags */
521 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
522 0, /* properties_required */
523 0, /* properties_provided */
524 0, /* properties_destroyed */
525 0, /* todo_flags_start */
526 ( TODO_df_verify
| TODO_df_finish
), /* todo_flags_finish */
529 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
532 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
533 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
536 /* opt_pass methods: */
537 virtual bool gate (function
*) { return flag_move_loop_invariants
; }
538 virtual unsigned int execute (function
*fun
)
540 if (number_of_loops (fun
) > 1)
541 move_loop_invariants ();
545 }; // class pass_rtl_move_loop_invariants
550 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
552 return new pass_rtl_move_loop_invariants (ctxt
);
558 const pass_data pass_data_rtl_unroll_loops
=
561 "loop2_unroll", /* name */
562 OPTGROUP_LOOP
, /* optinfo_flags */
563 TV_LOOP_UNROLL
, /* tv_id */
564 0, /* properties_required */
565 0, /* properties_provided */
566 0, /* properties_destroyed */
567 0, /* todo_flags_start */
568 0, /* todo_flags_finish */
571 class pass_rtl_unroll_loops
: public rtl_opt_pass
574 pass_rtl_unroll_loops (gcc::context
*ctxt
)
575 : rtl_opt_pass (pass_data_rtl_unroll_loops
, ctxt
)
578 /* opt_pass methods: */
579 virtual bool gate (function
*)
581 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
584 virtual unsigned int execute (function
*);
586 }; // class pass_rtl_unroll_loops
589 pass_rtl_unroll_loops::execute (function
*fun
)
591 if (number_of_loops (fun
) > 1)
597 if (flag_unroll_loops
)
599 if (flag_unroll_all_loops
)
600 flags
|= UAP_UNROLL_ALL
;
602 unroll_loops (flags
);
610 make_pass_rtl_unroll_loops (gcc::context
*ctxt
)
612 return new pass_rtl_unroll_loops (ctxt
);
618 const pass_data pass_data_rtl_doloop
=
621 "loop2_doloop", /* name */
622 OPTGROUP_LOOP
, /* optinfo_flags */
623 TV_LOOP_DOLOOP
, /* tv_id */
624 0, /* properties_required */
625 0, /* properties_provided */
626 0, /* properties_destroyed */
627 0, /* todo_flags_start */
628 0, /* todo_flags_finish */
631 class pass_rtl_doloop
: public rtl_opt_pass
634 pass_rtl_doloop (gcc::context
*ctxt
)
635 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
638 /* opt_pass methods: */
639 virtual bool gate (function
*);
640 virtual unsigned int execute (function
*);
642 }; // class pass_rtl_doloop
645 pass_rtl_doloop::gate (function
*)
647 #ifdef HAVE_doloop_end
648 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
655 pass_rtl_doloop::execute (function
*fun ATTRIBUTE_UNUSED
)
657 #ifdef HAVE_doloop_end
658 if (number_of_loops (fun
) > 1)
659 doloop_optimize_loops ();
667 make_pass_rtl_doloop (gcc::context
*ctxt
)
669 return new pass_rtl_doloop (ctxt
);