1 /* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002, 2003, 2004, 2005, 2007 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"
25 #include "hard-reg-set.h"
27 #include "basic-block.h"
29 #include "cfglayout.h"
30 #include "tree-pass.h"
35 /* Initialize loop optimizer. This is used by the tree and RTL loop
36 optimizers. FLAGS specify what properties to compute and/or ensure for
40 loop_optimizer_init (unsigned flags
)
42 struct loops
*loops
= XCNEW (struct loops
);
45 static bool first_time
= true;
53 /* Avoid annoying special cases of edges going to exit
56 for (ei
= ei_start (EXIT_BLOCK_PTR
->preds
); (e
= ei_safe_edge (ei
)); )
57 if ((e
->flags
& EDGE_FALLTHRU
) && !single_succ_p (e
->src
))
64 if (flow_loops_find (loops
) <= 1)
67 flow_loops_free (loops
);
73 /* Not going to update these. */
74 free (loops
->cfg
.rc_order
);
75 loops
->cfg
.rc_order
= NULL
;
76 free (loops
->cfg
.dfs_order
);
77 loops
->cfg
.dfs_order
= NULL
;
79 /* Create pre-headers. */
80 if (flags
& LOOPS_HAVE_PREHEADERS
)
81 create_preheaders (loops
, CP_SIMPLE_PREHEADERS
);
83 /* Force all latches to have only single successor. */
84 if (flags
& LOOPS_HAVE_SIMPLE_LATCHES
)
85 force_single_succ_latches (loops
);
87 /* Mark irreducible loops. */
88 if (flags
& LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
)
89 mark_irreducible_loops (loops
);
91 if (flags
& LOOPS_HAVE_MARKED_SINGLE_EXITS
)
92 mark_single_exit_loops (loops
);
95 flow_loops_dump (loops
, dump_file
, NULL
, 1);
97 #ifdef ENABLE_CHECKING
98 verify_dominators (CDI_DOMINATORS
);
99 verify_loop_structure (loops
);
105 /* Finalize loop optimizer. */
107 loop_optimizer_finalize (struct loops
*loops
)
114 for (i
= 1; i
< loops
->num
; i
++)
115 if (loops
->parray
[i
])
116 free_simple_loop_desc (loops
->parray
[i
]);
119 flow_loops_free (loops
);
123 #ifdef ENABLE_CHECKING
129 /* Gate for the RTL loop superpass. The actual passes are subpasses.
130 See passes.c for more on that. */
133 gate_handle_loop2 (void)
136 && (flag_move_loop_invariants
137 || flag_unswitch_loops
140 #ifdef HAVE_doloop_end
141 || (flag_branch_on_count_reg
&& HAVE_doloop_end
)
146 struct tree_opt_pass pass_loop2
=
149 gate_handle_loop2
, /* gate */
153 0, /* static_pass_number */
155 0, /* properties_required */
156 0, /* properties_provided */
157 0, /* properties_destroyed */
158 0, /* todo_flags_start */
160 TODO_ggc_collect
, /* todo_flags_finish */
165 /* Initialization of the RTL loop passes. */
170 dump_flow_info (dump_file
, dump_flags
);
172 /* Initialize structures for layout changes. */
173 cfg_layout_initialize (0);
175 current_loops
= loop_optimizer_init (LOOPS_NORMAL
);
179 struct tree_opt_pass pass_rtl_loop_init
=
181 "loop2_init", /* name */
183 rtl_loop_init
, /* execute */
186 0, /* static_pass_number */
188 0, /* properties_required */
189 0, /* properties_provided */
190 0, /* properties_destroyed */
191 0, /* todo_flags_start */
192 TODO_dump_func
, /* todo_flags_finish */
197 /* Finalization of the RTL loop passes. */
204 loop_optimizer_finalize (current_loops
);
206 free_dominance_info (CDI_DOMINATORS
);
208 /* Finalize layout changes. */
210 if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
211 bb
->aux
= bb
->next_bb
;
212 cfg_layout_finalize ();
214 cleanup_cfg (CLEANUP_EXPENSIVE
);
215 delete_trivially_dead_insns (get_insns (), max_reg_num ());
216 reg_scan (get_insns (), max_reg_num ());
218 dump_flow_info (dump_file
, dump_flags
);
220 current_loops
= NULL
;
224 struct tree_opt_pass pass_rtl_loop_done
=
226 "loop2_done", /* name */
228 rtl_loop_done
, /* execute */
231 0, /* static_pass_number */
233 0, /* properties_required */
234 0, /* properties_provided */
235 0, /* properties_destroyed */
236 0, /* todo_flags_start */
237 TODO_dump_func
, /* todo_flags_finish */
242 /* Loop invariant code motion. */
244 gate_rtl_move_loop_invariants (void)
246 return flag_move_loop_invariants
;
250 rtl_move_loop_invariants (void)
253 move_loop_invariants (current_loops
);
257 struct tree_opt_pass pass_rtl_move_loop_invariants
=
259 "loop2_invariant", /* name */
260 gate_rtl_move_loop_invariants
, /* gate */
261 rtl_move_loop_invariants
, /* execute */
264 0, /* static_pass_number */
266 0, /* properties_required */
267 0, /* properties_provided */
268 0, /* properties_destroyed */
269 0, /* todo_flags_start */
270 TODO_dump_func
, /* todo_flags_finish */
275 /* Loop unswitching for RTL. */
277 gate_rtl_unswitch (void)
279 return flag_unswitch_loops
;
286 unswitch_loops (current_loops
);
290 struct tree_opt_pass pass_rtl_unswitch
=
292 "loop2_unswitch", /* name */
293 gate_rtl_unswitch
, /* gate */
294 rtl_unswitch
, /* execute */
297 0, /* static_pass_number */
299 0, /* properties_required */
300 0, /* properties_provided */
301 0, /* properties_destroyed */
302 0, /* todo_flags_start */
303 TODO_dump_func
, /* todo_flags_finish */
308 /* Loop unswitching for RTL. */
310 gate_rtl_unroll_and_peel_loops (void)
312 return (flag_peel_loops
|| flag_unroll_loops
|| flag_unroll_all_loops
);
316 rtl_unroll_and_peel_loops (void)
324 if (flag_unroll_loops
)
326 if (flag_unroll_all_loops
)
327 flags
|= UAP_UNROLL_ALL
;
329 unroll_and_peel_loops (current_loops
, flags
);
334 struct tree_opt_pass pass_rtl_unroll_and_peel_loops
=
336 "loop2_unroll", /* name */
337 gate_rtl_unroll_and_peel_loops
, /* gate */
338 rtl_unroll_and_peel_loops
, /* execute */
341 0, /* static_pass_number */
343 0, /* properties_required */
344 0, /* properties_provided */
345 0, /* properties_destroyed */
346 0, /* todo_flags_start */
347 TODO_dump_func
, /* todo_flags_finish */
352 /* The doloop optimization. */
354 gate_rtl_doloop (void)
356 #ifdef HAVE_doloop_end
357 return (flag_branch_on_count_reg
&& HAVE_doloop_end
);
366 #ifdef HAVE_doloop_end
368 doloop_optimize_loops (current_loops
);
373 struct tree_opt_pass pass_rtl_doloop
=
375 "loop2_doloop", /* name */
376 gate_rtl_doloop
, /* gate */
377 rtl_doloop
, /* execute */
380 0, /* static_pass_number */
382 0, /* properties_required */
383 0, /* properties_provided */
384 0, /* properties_destroyed */
385 0, /* todo_flags_start */
386 TODO_dump_func
, /* todo_flags_finish */