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 "cfgcleanup.h"
33 #include "tree-pass.h"
35 #include "tree-ssa-loop-niter.h"
36 #include "loop-unroll.h"
37 #include "tree-scalar-evolution.h"
40 /* Apply FLAGS to the loop state. */
43 apply_loop_flags (unsigned flags
)
45 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
47 /* If the loops may have multiple latches, we cannot canonicalize
48 them further (and most of the loop manipulation functions will
49 not work). However, we avoid modifying cfg, which some
51 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
52 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
53 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
56 disambiguate_loops_with_multiple_latches ();
58 /* Create pre-headers. */
59 if (flags
& LOOPS_HAVE_PREHEADERS
)
61 int cp_flags
= CP_SIMPLE_PREHEADERS
;
63 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
64 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
66 create_preheaders (cp_flags
);
69 /* Force all latches to have only single successor. */
70 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
71 force_single_succ_latches ();
73 /* Mark irreducible loops. */
74 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
75 mark_irreducible_loops ();
77 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
81 /* Initialize loop structures. This is used by the tree and RTL loop
82 optimizers. FLAGS specify what properties to compute and/or ensure for
86 loop_optimizer_init (unsigned flags
)
88 timevar_push (TV_LOOP_INIT
);
92 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
95 current_loops
= flow_loops_find (NULL
);
99 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
100 bool needs_fixup
= loops_state_satisfies_p (LOOPS_NEED_FIXUP
);
102 gcc_assert (cfun
->curr_properties
& PROP_loops
);
104 /* Ensure that the dominators are computed, like flow_loops_find does. */
105 calculate_dominance_info (CDI_DOMINATORS
);
108 checking_verify_loop_structure ();
110 /* Clear all flags. */
112 release_recorded_exits (cfun
);
113 loops_state_clear (~0U);
117 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
119 loops_state_set (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
120 fix_loop_structure (NULL
);
124 /* Apply flags to loops. */
125 apply_loop_flags (flags
);
128 flow_loops_dump (dump_file
, NULL
, 1);
130 checking_verify_loop_structure ();
132 timevar_pop (TV_LOOP_INIT
);
135 /* Finalize loop structures. */
138 loop_optimizer_finalize (struct function
*fn
)
143 timevar_push (TV_LOOP_FINI
);
145 if (loops_state_satisfies_p (fn
, LOOPS_HAVE_RECORDED_EXITS
))
146 release_recorded_exits (fn
);
148 free_numbers_of_iterations_estimates (fn
);
150 /* If we should preserve loop structure, do not free it but clear
151 flags that advanced properties are there as we are not preserving
153 if (fn
->curr_properties
& PROP_loops
)
155 loops_state_clear (fn
, LOOP_CLOSED_SSA
156 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
157 | LOOPS_HAVE_PREHEADERS
158 | LOOPS_HAVE_SIMPLE_LATCHES
159 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
160 loops_state_set (fn
, LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
164 FOR_EACH_LOOP_FN (fn
, loop
, 0)
165 free_simple_loop_desc (loop
);
168 flow_loops_free (loops_for_fn (fn
));
169 ggc_free (loops_for_fn (fn
));
170 set_loops_for_fn (fn
, NULL
);
172 FOR_ALL_BB_FN (bb
, fn
)
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 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
205 fprintf (dump_file
, "fix_loop_structure: fixing up loops for function\n");
207 /* We need exact and fast dominance info to be available. */
208 gcc_assert (dom_info_state (CDI_DOMINATORS
) == DOM_OK
);
210 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
212 release_recorded_exits (cfun
);
213 record_exits
= LOOPS_HAVE_RECORDED_EXITS
;
216 /* Remember the depth of the blocks in the loop hierarchy, so that we can
217 recognize blocks whose loop nesting relationship has changed. */
219 FOR_EACH_BB_FN (bb
, cfun
)
220 bb
->aux
= (void *) (size_t) loop_depth (bb
->loop_father
);
222 /* Remove the dead loops from structures. We start from the innermost
223 loops, so that when we remove the loops, we know that the loops inside
224 are preserved, and do not waste time relinking loops that will be
226 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
228 /* Detect the case that the loop is no longer present even though
229 it wasn't marked for removal.
230 ??? If we do that we can get away with not marking loops for
231 removal at all. And possibly avoid some spurious removals. */
233 && bb_loop_header_p (loop
->header
))
236 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
237 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
242 struct loop
*ploop
= loop
->inner
;
243 flow_loop_tree_node_remove (ploop
);
244 flow_loop_tree_node_add (loop_outer (loop
), ploop
);
247 /* Remove the loop. */
249 loop
->former_header
= loop
->header
;
251 gcc_assert (loop
->former_header
!= NULL
);
253 flow_loop_tree_node_remove (loop
);
256 /* Remember the number of loops so we can return how many new loops
257 flow_loops_find discovered. */
258 old_nloops
= number_of_loops (cfun
);
260 /* Re-compute loop structure in-place. */
261 flow_loops_find (current_loops
);
263 /* Mark the blocks whose loop has changed. */
266 FOR_EACH_BB_FN (bb
, cfun
)
268 if ((void *) (size_t) loop_depth (bb
->loop_father
) != bb
->aux
)
269 bitmap_set_bit (changed_bbs
, bb
->index
);
275 /* Finally free deleted loops. */
276 bool any_deleted
= false;
277 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
278 if (loop
&& loop
->header
== NULL
)
281 && ((unsigned) loop
->former_header
->index
282 < basic_block_info_for_fn (cfun
)->length ()))
284 basic_block former_header
285 = BASIC_BLOCK_FOR_FN (cfun
, loop
->former_header
->index
);
286 /* If the old header still exists we want to check if the
287 original loop is re-discovered or the old header is now
288 part of a newly discovered loop.
289 In both cases we should have avoided removing the loop. */
290 if (former_header
== loop
->former_header
)
292 if (former_header
->loop_father
->header
== former_header
)
293 fprintf (dump_file
, "fix_loop_structure: rediscovered "
294 "removed loop %d as loop %d with old header %d\n",
295 loop
->num
, former_header
->loop_father
->num
,
296 former_header
->index
);
297 else if ((unsigned) former_header
->loop_father
->num
299 fprintf (dump_file
, "fix_loop_structure: header %d of "
300 "removed loop %d is part of the newly "
301 "discovered loop %d with header %d\n",
302 former_header
->index
, loop
->num
,
303 former_header
->loop_father
->num
,
304 former_header
->loop_father
->header
->index
);
307 (*get_loops (cfun
))[i
] = NULL
;
308 flow_loop_free (loop
);
312 /* If we deleted loops then the cached scalar evolutions refering to
313 those loops become invalid. */
314 if (any_deleted
&& scev_initialized_p ())
317 loops_state_clear (LOOPS_NEED_FIXUP
);
319 /* Apply flags to loops. */
320 apply_loop_flags (current_loops
->state
| record_exits
);
322 checking_verify_loop_structure ();
324 timevar_pop (TV_LOOP_INIT
);
326 return number_of_loops (cfun
) - old_nloops
;
329 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
334 const pass_data pass_data_loop2
=
338 OPTGROUP_LOOP
, /* optinfo_flags */
340 0, /* properties_required */
341 0, /* properties_provided */
342 0, /* properties_destroyed */
343 0, /* todo_flags_start */
344 0, /* todo_flags_finish */
347 class pass_loop2
: public rtl_opt_pass
350 pass_loop2 (gcc::context
*ctxt
)
351 : rtl_opt_pass (pass_data_loop2
, ctxt
)
354 /* opt_pass methods: */
355 virtual bool gate (function
*);
357 }; // class pass_loop2
360 pass_loop2::gate (function
*fun
)
363 && (flag_move_loop_invariants
364 || flag_unswitch_loops
366 || (flag_branch_on_count_reg
367 && targetm
.have_doloop_end ())))
371 /* No longer preserve loops, remove them now. */
372 fun
->curr_properties
&= ~PROP_loops
;
374 loop_optimizer_finalize ();
382 make_pass_loop2 (gcc::context
*ctxt
)
384 return new pass_loop2 (ctxt
);
388 /* Initialization of the RTL loop passes. */
392 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
396 dump_reg_info (dump_file
);
397 dump_flow_info (dump_file
, dump_flags
);
400 loop_optimizer_init (LOOPS_NORMAL
);
406 const pass_data pass_data_rtl_loop_init
=
409 "loop2_init", /* name */
410 OPTGROUP_LOOP
, /* optinfo_flags */
412 0, /* properties_required */
413 0, /* properties_provided */
414 0, /* properties_destroyed */
415 0, /* todo_flags_start */
416 0, /* todo_flags_finish */
419 class pass_rtl_loop_init
: public rtl_opt_pass
422 pass_rtl_loop_init (gcc::context
*ctxt
)
423 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
426 /* opt_pass methods: */
427 virtual unsigned int execute (function
*) { return rtl_loop_init (); }
429 }; // class pass_rtl_loop_init
434 make_pass_rtl_loop_init (gcc::context
*ctxt
)
436 return new pass_rtl_loop_init (ctxt
);
440 /* Finalization of the RTL loop passes. */
444 const pass_data pass_data_rtl_loop_done
=
447 "loop2_done", /* name */
448 OPTGROUP_LOOP
, /* optinfo_flags */
450 0, /* properties_required */
451 0, /* properties_provided */
452 PROP_loops
, /* properties_destroyed */
453 0, /* todo_flags_start */
454 0, /* todo_flags_finish */
457 class pass_rtl_loop_done
: public rtl_opt_pass
460 pass_rtl_loop_done (gcc::context
*ctxt
)
461 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
464 /* opt_pass methods: */
465 virtual unsigned int execute (function
*);
467 }; // class pass_rtl_loop_done
470 pass_rtl_loop_done::execute (function
*fun
)
472 /* No longer preserve loops, remove them now. */
473 fun
->curr_properties
&= ~PROP_loops
;
474 loop_optimizer_finalize ();
475 free_dominance_info (CDI_DOMINATORS
);
480 dump_reg_info (dump_file
);
481 dump_flow_info (dump_file
, dump_flags
);
490 make_pass_rtl_loop_done (gcc::context
*ctxt
)
492 return new pass_rtl_loop_done (ctxt
);
496 /* Loop invariant code motion. */
500 const pass_data pass_data_rtl_move_loop_invariants
=
503 "loop2_invariant", /* name */
504 OPTGROUP_LOOP
, /* optinfo_flags */
505 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
506 0, /* properties_required */
507 0, /* properties_provided */
508 0, /* properties_destroyed */
509 0, /* todo_flags_start */
510 ( TODO_df_verify
| TODO_df_finish
), /* todo_flags_finish */
513 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
516 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
517 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
520 /* opt_pass methods: */
521 virtual bool gate (function
*) { return flag_move_loop_invariants
; }
522 virtual unsigned int execute (function
*fun
)
524 if (number_of_loops (fun
) > 1)
525 move_loop_invariants ();
529 }; // class pass_rtl_move_loop_invariants
534 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
536 return new pass_rtl_move_loop_invariants (ctxt
);
542 const pass_data pass_data_rtl_unroll_loops
=
545 "loop2_unroll", /* name */
546 OPTGROUP_LOOP
, /* optinfo_flags */
547 TV_LOOP_UNROLL
, /* tv_id */
548 0, /* properties_required */
549 0, /* properties_provided */
550 0, /* properties_destroyed */
551 0, /* todo_flags_start */
552 0, /* todo_flags_finish */
555 class pass_rtl_unroll_loops
: public rtl_opt_pass
558 pass_rtl_unroll_loops (gcc::context
*ctxt
)
559 : rtl_opt_pass (pass_data_rtl_unroll_loops
, ctxt
)
562 /* opt_pass methods: */
563 virtual bool gate (function
*)
565 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
568 virtual unsigned int execute (function
*);
570 }; // class pass_rtl_unroll_loops
573 pass_rtl_unroll_loops::execute (function
*fun
)
575 if (number_of_loops (fun
) > 1)
581 if (flag_unroll_loops
)
583 if (flag_unroll_all_loops
)
584 flags
|= UAP_UNROLL_ALL
;
586 unroll_loops (flags
);
594 make_pass_rtl_unroll_loops (gcc::context
*ctxt
)
596 return new pass_rtl_unroll_loops (ctxt
);
602 const pass_data pass_data_rtl_doloop
=
605 "loop2_doloop", /* name */
606 OPTGROUP_LOOP
, /* optinfo_flags */
607 TV_LOOP_DOLOOP
, /* tv_id */
608 0, /* properties_required */
609 0, /* properties_provided */
610 0, /* properties_destroyed */
611 0, /* todo_flags_start */
612 0, /* todo_flags_finish */
615 class pass_rtl_doloop
: public rtl_opt_pass
618 pass_rtl_doloop (gcc::context
*ctxt
)
619 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
622 /* opt_pass methods: */
623 virtual bool gate (function
*);
624 virtual unsigned int execute (function
*);
626 }; // class pass_rtl_doloop
629 pass_rtl_doloop::gate (function
*)
631 return (flag_branch_on_count_reg
&& targetm
.have_doloop_end ());
635 pass_rtl_doloop::execute (function
*fun
)
637 if (number_of_loops (fun
) > 1)
638 doloop_optimize_loops ();
645 make_pass_rtl_doloop (gcc::context
*ctxt
)
647 return new pass_rtl_doloop (ctxt
);