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"
52 #include "tree-scalar-evolution.h"
55 /* Apply FLAGS to the loop state. */
58 apply_loop_flags (unsigned flags
)
60 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
62 /* If the loops may have multiple latches, we cannot canonicalize
63 them further (and most of the loop manipulation functions will
64 not work). However, we avoid modifying cfg, which some
66 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
67 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
68 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
71 disambiguate_loops_with_multiple_latches ();
73 /* Create pre-headers. */
74 if (flags
& LOOPS_HAVE_PREHEADERS
)
76 int cp_flags
= CP_SIMPLE_PREHEADERS
;
78 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
79 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
81 create_preheaders (cp_flags
);
84 /* Force all latches to have only single successor. */
85 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
86 force_single_succ_latches ();
88 /* Mark irreducible loops. */
89 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
90 mark_irreducible_loops ();
92 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
96 /* Initialize loop structures. This is used by the tree and RTL loop
97 optimizers. FLAGS specify what properties to compute and/or ensure for
101 loop_optimizer_init (unsigned flags
)
103 timevar_push (TV_LOOP_INIT
);
107 gcc_assert (!(cfun
->curr_properties
& PROP_loops
));
109 /* Find the loops. */
110 current_loops
= flow_loops_find (NULL
);
114 bool recorded_exits
= loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
);
115 bool needs_fixup
= loops_state_satisfies_p (LOOPS_NEED_FIXUP
);
117 gcc_assert (cfun
->curr_properties
& PROP_loops
);
119 /* Ensure that the dominators are computed, like flow_loops_find does. */
120 calculate_dominance_info (CDI_DOMINATORS
);
122 #ifdef ENABLE_CHECKING
124 verify_loop_structure ();
127 /* Clear all flags. */
129 release_recorded_exits ();
130 loops_state_clear (~0U);
134 /* Apply LOOPS_MAY_HAVE_MULTIPLE_LATCHES early as fix_loop_structure
136 loops_state_set (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
137 fix_loop_structure (NULL
);
141 /* Apply flags to loops. */
142 apply_loop_flags (flags
);
145 flow_loops_dump (dump_file
, NULL
, 1);
147 #ifdef ENABLE_CHECKING
148 verify_loop_structure ();
151 timevar_pop (TV_LOOP_INIT
);
154 /* Finalize loop structures. */
157 loop_optimizer_finalize (void)
162 timevar_push (TV_LOOP_FINI
);
164 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
165 release_recorded_exits ();
167 free_numbers_of_iterations_estimates ();
169 /* If we should preserve loop structure, do not free it but clear
170 flags that advanced properties are there as we are not preserving
172 if (cfun
->curr_properties
& PROP_loops
)
174 loops_state_clear (LOOP_CLOSED_SSA
175 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
176 | LOOPS_HAVE_PREHEADERS
177 | LOOPS_HAVE_SIMPLE_LATCHES
178 | LOOPS_HAVE_FALLTHRU_PREHEADERS
);
179 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
183 gcc_assert (current_loops
!= NULL
);
185 FOR_EACH_LOOP (loop
, 0)
186 free_simple_loop_desc (loop
);
189 flow_loops_free (current_loops
);
190 ggc_free (current_loops
);
191 current_loops
= NULL
;
193 FOR_ALL_BB_FN (bb
, cfun
)
195 bb
->loop_father
= NULL
;
199 timevar_pop (TV_LOOP_FINI
);
202 /* The structure of loops might have changed. Some loops might get removed
203 (and their headers and latches were set to NULL), loop exists might get
204 removed (thus the loop nesting may be wrong), and some blocks and edges
205 were changed (so the information about bb --> loop mapping does not have
206 to be correct). But still for the remaining loops the header dominates
207 the latch, and loops did not get new subloops (new loops might possibly
208 get created, but we are not interested in them). Fix up the mess.
210 If CHANGED_BBS is not NULL, basic blocks whose loop depth has changed are
213 Returns the number of new discovered loops. */
216 fix_loop_structure (bitmap changed_bbs
)
219 int record_exits
= 0;
221 unsigned old_nloops
, i
;
223 timevar_push (TV_LOOP_INIT
);
225 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
226 fprintf (dump_file
, "fix_loop_structure: fixing up loops for function\n");
228 /* We need exact and fast dominance info to be available. */
229 gcc_assert (dom_info_state (CDI_DOMINATORS
) == DOM_OK
);
231 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
233 release_recorded_exits ();
234 record_exits
= LOOPS_HAVE_RECORDED_EXITS
;
237 /* Remember the depth of the blocks in the loop hierarchy, so that we can
238 recognize blocks whose loop nesting relationship has changed. */
240 FOR_EACH_BB_FN (bb
, cfun
)
241 bb
->aux
= (void *) (size_t) loop_depth (bb
->loop_father
);
243 /* Remove the dead loops from structures. We start from the innermost
244 loops, so that when we remove the loops, we know that the loops inside
245 are preserved, and do not waste time relinking loops that will be
247 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
249 /* Detect the case that the loop is no longer present even though
250 it wasn't marked for removal.
251 ??? If we do that we can get away with not marking loops for
252 removal at all. And possibly avoid some spurious removals. */
254 && bb_loop_header_p (loop
->header
))
257 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
258 fprintf (dump_file
, "fix_loop_structure: removing loop %d\n",
263 struct loop
*ploop
= loop
->inner
;
264 flow_loop_tree_node_remove (ploop
);
265 flow_loop_tree_node_add (loop_outer (loop
), ploop
);
268 /* Remove the loop. */
270 loop
->former_header
= loop
->header
;
272 gcc_assert (loop
->former_header
!= NULL
);
274 flow_loop_tree_node_remove (loop
);
277 /* Remember the number of loops so we can return how many new loops
278 flow_loops_find discovered. */
279 old_nloops
= number_of_loops (cfun
);
281 /* Re-compute loop structure in-place. */
282 flow_loops_find (current_loops
);
284 /* Mark the blocks whose loop has changed. */
287 FOR_EACH_BB_FN (bb
, cfun
)
289 if ((void *) (size_t) loop_depth (bb
->loop_father
) != bb
->aux
)
290 bitmap_set_bit (changed_bbs
, bb
->index
);
296 /* Finally free deleted loops. */
297 bool any_deleted
= false;
298 FOR_EACH_VEC_ELT (*get_loops (cfun
), i
, loop
)
299 if (loop
&& loop
->header
== NULL
)
302 && ((unsigned) loop
->former_header
->index
303 < basic_block_info_for_fn (cfun
)->length ()))
305 basic_block former_header
306 = BASIC_BLOCK_FOR_FN (cfun
, loop
->former_header
->index
);
307 /* If the old header still exists we want to check if the
308 original loop is re-discovered or the old header is now
309 part of a newly discovered loop.
310 In both cases we should have avoided removing the loop. */
311 if (former_header
== loop
->former_header
)
313 if (former_header
->loop_father
->header
== former_header
)
314 fprintf (dump_file
, "fix_loop_structure: rediscovered "
315 "removed loop %d as loop %d with old header %d\n",
316 loop
->num
, former_header
->loop_father
->num
,
317 former_header
->index
);
318 else if ((unsigned) former_header
->loop_father
->num
320 fprintf (dump_file
, "fix_loop_structure: header %d of "
321 "removed loop %d is part of the newly "
322 "discovered loop %d with header %d\n",
323 former_header
->index
, loop
->num
,
324 former_header
->loop_father
->num
,
325 former_header
->loop_father
->header
->index
);
328 (*get_loops (cfun
))[i
] = NULL
;
329 flow_loop_free (loop
);
333 /* If we deleted loops then the cached scalar evolutions refering to
334 those loops become invalid. */
335 if (any_deleted
&& scev_initialized_p ())
338 loops_state_clear (LOOPS_NEED_FIXUP
);
340 /* Apply flags to loops. */
341 apply_loop_flags (current_loops
->state
| record_exits
);
343 #ifdef ENABLE_CHECKING
344 verify_loop_structure ();
347 timevar_pop (TV_LOOP_INIT
);
349 return number_of_loops (cfun
) - old_nloops
;
352 /* The RTL loop superpass. The actual passes are subpasses. See passes.c for
357 const pass_data pass_data_loop2
=
361 OPTGROUP_LOOP
, /* optinfo_flags */
363 0, /* properties_required */
364 0, /* properties_provided */
365 0, /* properties_destroyed */
366 0, /* todo_flags_start */
367 0, /* todo_flags_finish */
370 class pass_loop2
: public rtl_opt_pass
373 pass_loop2 (gcc::context
*ctxt
)
374 : rtl_opt_pass (pass_data_loop2
, ctxt
)
377 /* opt_pass methods: */
378 virtual bool gate (function
*);
380 }; // class pass_loop2
383 pass_loop2::gate (function
*fun
)
386 && (flag_move_loop_invariants
387 || flag_unswitch_loops
389 #ifdef HAVE_doloop_end
390 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
396 /* No longer preserve loops, remove them now. */
397 fun
->curr_properties
&= ~PROP_loops
;
399 loop_optimizer_finalize ();
407 make_pass_loop2 (gcc::context
*ctxt
)
409 return new pass_loop2 (ctxt
);
413 /* Initialization of the RTL loop passes. */
417 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
421 dump_reg_info (dump_file
);
422 dump_flow_info (dump_file
, dump_flags
);
425 loop_optimizer_init (LOOPS_NORMAL
);
431 const pass_data pass_data_rtl_loop_init
=
434 "loop2_init", /* name */
435 OPTGROUP_LOOP
, /* optinfo_flags */
437 0, /* properties_required */
438 0, /* properties_provided */
439 0, /* properties_destroyed */
440 0, /* todo_flags_start */
441 0, /* todo_flags_finish */
444 class pass_rtl_loop_init
: public rtl_opt_pass
447 pass_rtl_loop_init (gcc::context
*ctxt
)
448 : rtl_opt_pass (pass_data_rtl_loop_init
, ctxt
)
451 /* opt_pass methods: */
452 virtual unsigned int execute (function
*) { return rtl_loop_init (); }
454 }; // class pass_rtl_loop_init
459 make_pass_rtl_loop_init (gcc::context
*ctxt
)
461 return new pass_rtl_loop_init (ctxt
);
465 /* Finalization of the RTL loop passes. */
469 const pass_data pass_data_rtl_loop_done
=
472 "loop2_done", /* name */
473 OPTGROUP_LOOP
, /* optinfo_flags */
475 0, /* properties_required */
476 0, /* properties_provided */
477 PROP_loops
, /* properties_destroyed */
478 0, /* todo_flags_start */
479 0, /* todo_flags_finish */
482 class pass_rtl_loop_done
: public rtl_opt_pass
485 pass_rtl_loop_done (gcc::context
*ctxt
)
486 : rtl_opt_pass (pass_data_rtl_loop_done
, ctxt
)
489 /* opt_pass methods: */
490 virtual unsigned int execute (function
*);
492 }; // class pass_rtl_loop_done
495 pass_rtl_loop_done::execute (function
*fun
)
497 /* No longer preserve loops, remove them now. */
498 fun
->curr_properties
&= ~PROP_loops
;
499 loop_optimizer_finalize ();
500 free_dominance_info (CDI_DOMINATORS
);
505 dump_reg_info (dump_file
);
506 dump_flow_info (dump_file
, dump_flags
);
515 make_pass_rtl_loop_done (gcc::context
*ctxt
)
517 return new pass_rtl_loop_done (ctxt
);
521 /* Loop invariant code motion. */
525 const pass_data pass_data_rtl_move_loop_invariants
=
528 "loop2_invariant", /* name */
529 OPTGROUP_LOOP
, /* optinfo_flags */
530 TV_LOOP_MOVE_INVARIANTS
, /* tv_id */
531 0, /* properties_required */
532 0, /* properties_provided */
533 0, /* properties_destroyed */
534 0, /* todo_flags_start */
535 ( TODO_df_verify
| TODO_df_finish
), /* todo_flags_finish */
538 class pass_rtl_move_loop_invariants
: public rtl_opt_pass
541 pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
542 : rtl_opt_pass (pass_data_rtl_move_loop_invariants
, ctxt
)
545 /* opt_pass methods: */
546 virtual bool gate (function
*) { return flag_move_loop_invariants
; }
547 virtual unsigned int execute (function
*fun
)
549 if (number_of_loops (fun
) > 1)
550 move_loop_invariants ();
554 }; // class pass_rtl_move_loop_invariants
559 make_pass_rtl_move_loop_invariants (gcc::context
*ctxt
)
561 return new pass_rtl_move_loop_invariants (ctxt
);
567 const pass_data pass_data_rtl_unroll_loops
=
570 "loop2_unroll", /* name */
571 OPTGROUP_LOOP
, /* optinfo_flags */
572 TV_LOOP_UNROLL
, /* tv_id */
573 0, /* properties_required */
574 0, /* properties_provided */
575 0, /* properties_destroyed */
576 0, /* todo_flags_start */
577 0, /* todo_flags_finish */
580 class pass_rtl_unroll_loops
: public rtl_opt_pass
583 pass_rtl_unroll_loops (gcc::context
*ctxt
)
584 : rtl_opt_pass (pass_data_rtl_unroll_loops
, ctxt
)
587 /* opt_pass methods: */
588 virtual bool gate (function
*)
590 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
593 virtual unsigned int execute (function
*);
595 }; // class pass_rtl_unroll_loops
598 pass_rtl_unroll_loops::execute (function
*fun
)
600 if (number_of_loops (fun
) > 1)
606 if (flag_unroll_loops
)
608 if (flag_unroll_all_loops
)
609 flags
|= UAP_UNROLL_ALL
;
611 unroll_loops (flags
);
619 make_pass_rtl_unroll_loops (gcc::context
*ctxt
)
621 return new pass_rtl_unroll_loops (ctxt
);
627 const pass_data pass_data_rtl_doloop
=
630 "loop2_doloop", /* name */
631 OPTGROUP_LOOP
, /* optinfo_flags */
632 TV_LOOP_DOLOOP
, /* tv_id */
633 0, /* properties_required */
634 0, /* properties_provided */
635 0, /* properties_destroyed */
636 0, /* todo_flags_start */
637 0, /* todo_flags_finish */
640 class pass_rtl_doloop
: public rtl_opt_pass
643 pass_rtl_doloop (gcc::context
*ctxt
)
644 : rtl_opt_pass (pass_data_rtl_doloop
, ctxt
)
647 /* opt_pass methods: */
648 virtual bool gate (function
*);
649 virtual unsigned int execute (function
*);
651 }; // class pass_rtl_doloop
654 pass_rtl_doloop::gate (function
*)
656 #ifdef HAVE_doloop_end
657 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
664 pass_rtl_doloop::execute (function
*fun ATTRIBUTE_UNUSED
)
666 #ifdef HAVE_doloop_end
667 if (number_of_loops (fun
) > 1)
668 doloop_optimize_loops ();
676 make_pass_rtl_doloop (gcc::context
*ctxt
)
678 return new pass_rtl_doloop (ctxt
);