1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "cfglayout.h"
31 #include "tree-pass.h"
36 /* ICI internal interface */
37 #include "highlev-plugin-internal.h"
40 /* Initialize loop structures. This is used by the tree and RTL loop
41 optimizers. FLAGS specify what properties to compute and/or ensure for
45 loop_optimizer_init (unsigned flags
)
49 gcc_assert (!current_loops
);
50 loops
= GGC_CNEW (struct loops
);
54 flow_loops_find (loops
);
55 current_loops
= loops
;
57 if (flags
& LOOPS_MAY_HAVE_MULTIPLE_LATCHES
)
59 /* If the loops may have multiple latches, we cannot canonicalize
60 them further (and most of the loop manipulation functions will
61 not work). However, we avoid modifying cfg, which some
63 gcc_assert ((flags
& ~(LOOPS_MAY_HAVE_MULTIPLE_LATCHES
64 | LOOPS_HAVE_RECORDED_EXITS
)) == 0);
65 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
68 disambiguate_loops_with_multiple_latches ();
70 /* Create pre-headers. */
71 if (flags
& LOOPS_HAVE_PREHEADERS
)
73 int cp_flags
= CP_SIMPLE_PREHEADERS
;
75 if (flags
& LOOPS_HAVE_FALLTHRU_PREHEADERS
)
76 cp_flags
|= CP_FALLTHRU_PREHEADERS
;
78 create_preheaders (cp_flags
);
81 /* Force all latches to have only single successor. */
82 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
83 force_single_succ_latches ();
85 /* Mark irreducible loops. */
86 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
87 mark_irreducible_loops ();
89 if (flags
& LOOPS_HAVE_RECORDED_EXITS
)
93 flow_loops_dump (dump_file
, NULL
, 1);
95 #ifdef ENABLE_CHECKING
96 verify_dominators (CDI_DOMINATORS
);
97 verify_loop_structure ();
101 /* Finalize loop structures. */
104 loop_optimizer_finalize (void)
110 gcc_assert (current_loops
!= NULL
);
112 FOR_EACH_LOOP (li
, loop
, 0)
114 free_simple_loop_desc (loop
);
118 if (loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS
))
119 release_recorded_exits ();
120 flow_loops_free (current_loops
);
121 ggc_free (current_loops
);
122 current_loops
= NULL
;
126 bb
->loop_father
= NULL
;
130 #ifdef ENABLE_CHECKING
131 /* FIXME: no point to verify flow info after bundling on ia64. Use this
132 hack for achieving this. */
133 if (!reload_completed
)
139 /* Gate for the RTL loop superpass. The actual passes are subpasses.
140 See passes.c for more on that. */
143 gate_handle_loop2 (void)
146 && (flag_move_loop_invariants
147 || flag_unswitch_loops
150 #ifdef HAVE_doloop_end
151 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
156 struct rtl_opt_pass pass_loop2
=
161 gate_handle_loop2
, /* gate */
165 0, /* static_pass_number */
167 0, /* properties_required */
168 0, /* properties_provided */
169 0, /* properties_destroyed */
170 0, /* todo_flags_start */
172 TODO_ggc_collect
/* todo_flags_finish */
177 /* Initialization of the RTL loop passes. */
181 gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT
);
184 dump_flow_info (dump_file
, dump_flags
);
186 loop_optimizer_init (LOOPS_NORMAL
);
190 struct rtl_opt_pass pass_rtl_loop_init
=
194 "loop2_init", /* name */
196 rtl_loop_init
, /* execute */
199 0, /* static_pass_number */
201 0, /* properties_required */
202 0, /* properties_provided */
203 0, /* properties_destroyed */
204 0, /* todo_flags_start */
205 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */
210 /* Finalization of the RTL loop passes. */
215 loop_optimizer_finalize ();
216 free_dominance_info (CDI_DOMINATORS
);
220 dump_flow_info (dump_file
, dump_flags
);
225 struct rtl_opt_pass pass_rtl_loop_done
=
229 "loop2_done", /* name */
231 rtl_loop_done
, /* execute */
234 0, /* static_pass_number */
236 0, /* properties_required */
237 0, /* properties_provided */
238 0, /* properties_destroyed */
239 0, /* todo_flags_start */
240 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */
245 /* Loop invariant code motion. */
247 gate_rtl_move_loop_invariants (void)
249 return flag_move_loop_invariants
;
253 rtl_move_loop_invariants (void)
255 if (number_of_loops () > 1)
256 move_loop_invariants ();
260 struct rtl_opt_pass pass_rtl_move_loop_invariants
=
264 "loop2_invariant", /* name */
265 gate_rtl_move_loop_invariants
, /* gate */
266 rtl_move_loop_invariants
, /* execute */
269 0, /* static_pass_number */
271 0, /* properties_required */
272 0, /* properties_provided */
273 0, /* properties_destroyed */
274 0, /* todo_flags_start */
276 TODO_df_finish
| TODO_verify_rtl_sharing
|
277 TODO_dump_func
/* todo_flags_finish */
282 /* Loop unswitching for RTL. */
284 gate_rtl_unswitch (void)
286 return flag_unswitch_loops
;
292 if (number_of_loops () > 1)
297 struct rtl_opt_pass pass_rtl_unswitch
=
301 "loop2_unswitch", /* name */
302 gate_rtl_unswitch
, /* gate */
303 rtl_unswitch
, /* execute */
306 0, /* static_pass_number */
308 0, /* properties_required */
309 0, /* properties_provided */
310 0, /* properties_destroyed */
311 0, /* todo_flags_start */
312 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
317 /* Loop unswitching for RTL. */
319 gate_rtl_unroll_and_peel_loops (void)
321 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
325 rtl_unroll_and_peel_loops (void)
327 static int ici_flag_peel_loops
;
328 static int ici_flag_unroll_loops
;
329 static int ici_flag_unroll_all_loops
;
331 if (number_of_loops () > 1)
337 register_event_parameter ("loop.flag_peel_loops",
338 &ici_flag_peel_loops
, EP_INT
);
339 register_event_parameter ("loop.flag_unroll_loops",
340 &ici_flag_unroll_loops
, EP_INT
);
341 register_event_parameter ("loop.flag_unroll_all_loops",
342 &ici_flag_unroll_all_loops
, EP_INT
);
344 call_plugin_event ("unroll_parameter_handler");
346 unregister_event_parameter ("loop.flag_peel_loops");
347 unregister_event_parameter ("loop.flag_unroll_loops");
348 unregister_event_parameter ("loop.flag_unroll_all_loops");
352 if (flag_unroll_loops
)
354 if (flag_unroll_all_loops
)
355 flags
|= UAP_UNROLL_ALL
;
357 unroll_and_peel_loops (flags
);
362 struct rtl_opt_pass pass_rtl_unroll_and_peel_loops
=
366 "loop2_unroll", /* name */
367 gate_rtl_unroll_and_peel_loops
, /* gate */
368 rtl_unroll_and_peel_loops
, /* execute */
371 0, /* static_pass_number */
373 0, /* properties_required */
374 0, /* properties_provided */
375 0, /* properties_destroyed */
376 0, /* todo_flags_start */
377 TODO_dump_func
| TODO_verify_rtl_sharing
, /* todo_flags_finish */
382 /* The doloop optimization. */
384 gate_rtl_doloop (void)
386 #ifdef HAVE_doloop_end
387 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
396 #ifdef HAVE_doloop_end
397 if (number_of_loops () > 1)
398 doloop_optimize_loops ();
403 struct rtl_opt_pass pass_rtl_doloop
=
407 "loop2_doloop", /* name */
408 gate_rtl_doloop
, /* gate */
409 rtl_doloop
, /* execute */
412 0, /* static_pass_number */
414 0, /* properties_required */
415 0, /* properties_provided */
416 0, /* properties_destroyed */
417 0, /* todo_flags_start */
418 TODO_dump_func
| TODO_verify_rtl_sharing
/* todo_flags_finish */