1/* Loop optimizer initialization routines and RTL loop optimization passes.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "rtl.h"
26#include "hard-reg-set.h"
27#include "obstack.h"
28#include "basic-block.h"
29#include "cfgloop.h"
30#include "cfglayout.h"
31#include "tree-pass.h"
32#include "timevar.h"
33#include "flags.h"
34
35
36/* Initialize loop optimizer. This is used by the tree and RTL loop
37 optimizers. FLAGS specify what properties to compute and/or ensure for
38 loops. */
39
40struct loops *
41loop_optimizer_init (unsigned flags)
42{
43 struct loops *loops = XCNEW (struct loops);
44 edge e;
45 edge_iterator ei;
46
47 /* Avoid annoying special cases of edges going to exit
48 block. */
49
50 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
51 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
52 split_edge (e);
53 else
54 ei_next (&ei);
55
56 /* Find the loops. */
57
58 if (flow_loops_find (loops) <= 1)
59 {
60 /* No loops. */
61 flow_loops_free (loops);
62 free (loops);
63
64 return NULL;
65 }
66
67 /* Not going to update these. */
68 free (loops->cfg.rc_order);
69 loops->cfg.rc_order = NULL;
70 free (loops->cfg.dfs_order);
71 loops->cfg.dfs_order = NULL;
72
73 /* Create pre-headers. */
74 if (flags & LOOPS_HAVE_PREHEADERS)
75 create_preheaders (loops, CP_SIMPLE_PREHEADERS);
76
77 /* Force all latches to have only single successor. */
78 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
79 force_single_succ_latches (loops);
80
81 /* Mark irreducible loops. */
82 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
83 mark_irreducible_loops (loops);
84
85 if (flags & LOOPS_HAVE_MARKED_SINGLE_EXITS)
86 mark_single_exit_loops (loops);
87
88 /* Dump loops. */
89 flow_loops_dump (loops, dump_file, NULL, 1);
90
91#ifdef ENABLE_CHECKING
92 verify_dominators (CDI_DOMINATORS);
93 verify_loop_structure (loops);
94#endif
95
96 return loops;
97}
98
99/* Finalize loop optimizer. */
100void
101loop_optimizer_finalize (struct loops *loops)
102{
103 unsigned i;
104
105 if (!loops)
106 return;
107
108 for (i = 1; i < loops->num; i++)
109 if (loops->parray[i])
110 free_simple_loop_desc (loops->parray[i]);
111
112 /* Clean up. */
113 flow_loops_free (loops);
114 free (loops);
115
116 /* Checking. */
117#ifdef ENABLE_CHECKING
118 verify_flow_info ();
119#endif
120}
121
122
123/* Gate for the RTL loop superpass. The actual passes are subpasses.
124 See passes.c for more on that. */
125
126static bool
127gate_handle_loop2 (void)
128{
129 return (optimize > 0
130 && (flag_move_loop_invariants
131 || flag_unswitch_loops
132 || flag_peel_loops
133 || flag_unroll_loops
134#ifdef HAVE_doloop_end
135 || (flag_branch_on_count_reg && HAVE_doloop_end)
136#endif
137 ));
138}
139
140struct tree_opt_pass pass_loop2 =
141{
142 "loop2", /* name */
143 gate_handle_loop2, /* gate */
144 NULL, /* execute */
145 NULL, /* sub */
146 NULL, /* next */
147 0, /* static_pass_number */
148 TV_LOOP, /* tv_id */
149 0, /* properties_required */
150 0, /* properties_provided */
151 0, /* properties_destroyed */
152 0, /* todo_flags_start */
153 TODO_dump_func |
154 TODO_ggc_collect, /* todo_flags_finish */
155 'L' /* letter */
156};
157
158
159/* Initialization of the RTL loop passes. */
160static unsigned int
161rtl_loop_init (void)
162{
163 if (dump_file)
164 dump_flow_info (dump_file, dump_flags);
165
166 /* Initialize structures for layout changes. */
167 cfg_layout_initialize (0);
168
169 current_loops = loop_optimizer_init (LOOPS_NORMAL);
170 return 0;
171}
172
173struct tree_opt_pass pass_rtl_loop_init =
174{
175 "loop2_init", /* name */
176 NULL, /* gate */
177 rtl_loop_init, /* execute */
178 NULL, /* sub */
179 NULL, /* next */
180 0, /* static_pass_number */
181 TV_LOOP, /* tv_id */
182 0, /* properties_required */
183 0, /* properties_provided */
184 0, /* properties_destroyed */
185 0, /* todo_flags_start */
186 TODO_dump_func, /* todo_flags_finish */
187 'L' /* letter */
188};
189
190
191/* Finalization of the RTL loop passes. */
192static unsigned int
193rtl_loop_done (void)
194{
195 basic_block bb;
196
197 if (current_loops)
198 loop_optimizer_finalize (current_loops);
199
200 free_dominance_info (CDI_DOMINATORS);
201
202 /* Finalize layout changes. */
203 FOR_EACH_BB (bb)
204 if (bb->next_bb != EXIT_BLOCK_PTR)
205 bb->aux = bb->next_bb;
206 cfg_layout_finalize ();
207
208 cleanup_cfg (CLEANUP_EXPENSIVE);
209 delete_trivially_dead_insns (get_insns (), max_reg_num ());
210 reg_scan (get_insns (), max_reg_num ());
211 if (dump_file)
212 dump_flow_info (dump_file, dump_flags);
213
214 current_loops = NULL;
215 return 0;
216}
217
218struct tree_opt_pass pass_rtl_loop_done =
219{
220 "loop2_done", /* name */
221 NULL, /* gate */
222 rtl_loop_done, /* execute */
223 NULL, /* sub */
224 NULL, /* next */
225 0, /* static_pass_number */
226 TV_LOOP, /* tv_id */
227 0, /* properties_required */
228 0, /* properties_provided */
229 0, /* properties_destroyed */
230 0, /* todo_flags_start */
231 TODO_dump_func, /* todo_flags_finish */
232 'L' /* letter */
233};
234
235
236/* Loop invariant code motion. */
237static bool
238gate_rtl_move_loop_invariants (void)
239{
240 return flag_move_loop_invariants;
241}
242
243static unsigned int
244rtl_move_loop_invariants (void)
245{
246 if (current_loops)
247 move_loop_invariants (current_loops);
248 return 0;
249}
250
251struct tree_opt_pass pass_rtl_move_loop_invariants =
252{
253 "loop2_invariant", /* name */
254 gate_rtl_move_loop_invariants, /* gate */
255 rtl_move_loop_invariants, /* execute */
256 NULL, /* sub */
257 NULL, /* next */
258 0, /* static_pass_number */
259 TV_LOOP, /* tv_id */
260 0, /* properties_required */
261 0, /* properties_provided */
262 0, /* properties_destroyed */
263 0, /* todo_flags_start */
264 TODO_dump_func, /* todo_flags_finish */
265 'L' /* letter */
266};
267
268
269/* Loop unswitching for RTL. */
270static bool
271gate_rtl_unswitch (void)
272{
273 return flag_unswitch_loops;
274}
275
276static unsigned int
277rtl_unswitch (void)
278{
279 if (current_loops)
280 unswitch_loops (current_loops);
281 return 0;
282}
283
284struct tree_opt_pass pass_rtl_unswitch =
285{
286 "loop2_unswitch", /* name */
287 gate_rtl_unswitch, /* gate */
288 rtl_unswitch, /* execute */
289 NULL, /* sub */
290 NULL, /* next */
291 0, /* static_pass_number */
292 TV_LOOP, /* tv_id */
293 0, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 TODO_dump_func, /* todo_flags_finish */
298 'L' /* letter */
299};
300
301
302/* Loop unswitching for RTL. */
303static bool
304gate_rtl_unroll_and_peel_loops (void)
305{
306 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
307}
308
309static unsigned int
310rtl_unroll_and_peel_loops (void)
311{
312 if (current_loops)
313 {
314 int flags = 0;
315
316 if (flag_peel_loops)
317 flags |= UAP_PEEL;
318 if (flag_unroll_loops)
319 flags |= UAP_UNROLL;
320 if (flag_unroll_all_loops)
321 flags |= UAP_UNROLL_ALL;
322
323 unroll_and_peel_loops (current_loops, flags);
324 }
325 return 0;
326}
327
328struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
329{
330 "loop2_unroll", /* name */
331 gate_rtl_unroll_and_peel_loops, /* gate */
332 rtl_unroll_and_peel_loops, /* execute */
333 NULL, /* sub */
334 NULL, /* next */
335 0, /* static_pass_number */
336 TV_LOOP, /* tv_id */
337 0, /* properties_required */
338 0, /* properties_provided */
339 0, /* properties_destroyed */
340 0, /* todo_flags_start */
341 TODO_dump_func, /* todo_flags_finish */
342 'L' /* letter */
343};
344
345
346/* The doloop optimization. */
347static bool
348gate_rtl_doloop (void)
349{
350#ifdef HAVE_doloop_end
351 return (flag_branch_on_count_reg && HAVE_doloop_end);
352#else
353 return 0;
354#endif
355}
356
357static unsigned int
358rtl_doloop (void)
359{
360#ifdef HAVE_doloop_end
361 if (current_loops)
362 doloop_optimize_loops (current_loops);
363#endif
364 return 0;
365}
366
367struct tree_opt_pass pass_rtl_doloop =
368{
369 "loop2_doloop", /* name */
370 gate_rtl_doloop, /* gate */
371 rtl_doloop, /* execute */
372 NULL, /* sub */
373 NULL, /* next */
374 0, /* static_pass_number */
375 TV_LOOP, /* tv_id */
376 0, /* properties_required */
377 0, /* properties_provided */
378 0, /* properties_destroyed */
379 0, /* todo_flags_start */
380 TODO_dump_func, /* todo_flags_finish */
381 'L' /* letter */
382};
383
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "rtl.h"
26#include "hard-reg-set.h"
27#include "obstack.h"
28#include "basic-block.h"
29#include "cfgloop.h"
30#include "cfglayout.h"
31#include "tree-pass.h"
32#include "timevar.h"
33#include "flags.h"
34
35
36/* Initialize loop optimizer. This is used by the tree and RTL loop
37 optimizers. FLAGS specify what properties to compute and/or ensure for
38 loops. */
39
40struct loops *
41loop_optimizer_init (unsigned flags)
42{
43 struct loops *loops = XCNEW (struct loops);
44 edge e;
45 edge_iterator ei;
46
47 /* Avoid annoying special cases of edges going to exit
48 block. */
49
50 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
51 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
52 split_edge (e);
53 else
54 ei_next (&ei);
55
56 /* Find the loops. */
57
58 if (flow_loops_find (loops) <= 1)
59 {
60 /* No loops. */
61 flow_loops_free (loops);
62 free (loops);
63
64 return NULL;
65 }
66
67 /* Not going to update these. */
68 free (loops->cfg.rc_order);
69 loops->cfg.rc_order = NULL;
70 free (loops->cfg.dfs_order);
71 loops->cfg.dfs_order = NULL;
72
73 /* Create pre-headers. */
74 if (flags & LOOPS_HAVE_PREHEADERS)
75 create_preheaders (loops, CP_SIMPLE_PREHEADERS);
76
77 /* Force all latches to have only single successor. */
78 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
79 force_single_succ_latches (loops);
80
81 /* Mark irreducible loops. */
82 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
83 mark_irreducible_loops (loops);
84
85 if (flags & LOOPS_HAVE_MARKED_SINGLE_EXITS)
86 mark_single_exit_loops (loops);
87
88 /* Dump loops. */
89 flow_loops_dump (loops, dump_file, NULL, 1);
90
91#ifdef ENABLE_CHECKING
92 verify_dominators (CDI_DOMINATORS);
93 verify_loop_structure (loops);
94#endif
95
96 return loops;
97}
98
99/* Finalize loop optimizer. */
100void
101loop_optimizer_finalize (struct loops *loops)
102{
103 unsigned i;
104
105 if (!loops)
106 return;
107
108 for (i = 1; i < loops->num; i++)
109 if (loops->parray[i])
110 free_simple_loop_desc (loops->parray[i]);
111
112 /* Clean up. */
113 flow_loops_free (loops);
114 free (loops);
115
116 /* Checking. */
117#ifdef ENABLE_CHECKING
118 verify_flow_info ();
119#endif
120}
121
122
123/* Gate for the RTL loop superpass. The actual passes are subpasses.
124 See passes.c for more on that. */
125
126static bool
127gate_handle_loop2 (void)
128{
129 return (optimize > 0
130 && (flag_move_loop_invariants
131 || flag_unswitch_loops
132 || flag_peel_loops
133 || flag_unroll_loops
134#ifdef HAVE_doloop_end
135 || (flag_branch_on_count_reg && HAVE_doloop_end)
136#endif
137 ));
138}
139
140struct tree_opt_pass pass_loop2 =
141{
142 "loop2", /* name */
143 gate_handle_loop2, /* gate */
144 NULL, /* execute */
145 NULL, /* sub */
146 NULL, /* next */
147 0, /* static_pass_number */
148 TV_LOOP, /* tv_id */
149 0, /* properties_required */
150 0, /* properties_provided */
151 0, /* properties_destroyed */
152 0, /* todo_flags_start */
153 TODO_dump_func |
154 TODO_ggc_collect, /* todo_flags_finish */
155 'L' /* letter */
156};
157
158
159/* Initialization of the RTL loop passes. */
160static unsigned int
161rtl_loop_init (void)
162{
163 if (dump_file)
164 dump_flow_info (dump_file, dump_flags);
165
166 /* Initialize structures for layout changes. */
167 cfg_layout_initialize (0);
168
169 current_loops = loop_optimizer_init (LOOPS_NORMAL);
170 return 0;
171}
172
173struct tree_opt_pass pass_rtl_loop_init =
174{
175 "loop2_init", /* name */
176 NULL, /* gate */
177 rtl_loop_init, /* execute */
178 NULL, /* sub */
179 NULL, /* next */
180 0, /* static_pass_number */
181 TV_LOOP, /* tv_id */
182 0, /* properties_required */
183 0, /* properties_provided */
184 0, /* properties_destroyed */
185 0, /* todo_flags_start */
186 TODO_dump_func, /* todo_flags_finish */
187 'L' /* letter */
188};
189
190
191/* Finalization of the RTL loop passes. */
192static unsigned int
193rtl_loop_done (void)
194{
195 basic_block bb;
196
197 if (current_loops)
198 loop_optimizer_finalize (current_loops);
199
200 free_dominance_info (CDI_DOMINATORS);
201
202 /* Finalize layout changes. */
203 FOR_EACH_BB (bb)
204 if (bb->next_bb != EXIT_BLOCK_PTR)
205 bb->aux = bb->next_bb;
206 cfg_layout_finalize ();
207
208 cleanup_cfg (CLEANUP_EXPENSIVE);
209 delete_trivially_dead_insns (get_insns (), max_reg_num ());
210 reg_scan (get_insns (), max_reg_num ());
211 if (dump_file)
212 dump_flow_info (dump_file, dump_flags);
213
214 current_loops = NULL;
215 return 0;
216}
217
218struct tree_opt_pass pass_rtl_loop_done =
219{
220 "loop2_done", /* name */
221 NULL, /* gate */
222 rtl_loop_done, /* execute */
223 NULL, /* sub */
224 NULL, /* next */
225 0, /* static_pass_number */
226 TV_LOOP, /* tv_id */
227 0, /* properties_required */
228 0, /* properties_provided */
229 0, /* properties_destroyed */
230 0, /* todo_flags_start */
231 TODO_dump_func, /* todo_flags_finish */
232 'L' /* letter */
233};
234
235
236/* Loop invariant code motion. */
237static bool
238gate_rtl_move_loop_invariants (void)
239{
240 return flag_move_loop_invariants;
241}
242
243static unsigned int
244rtl_move_loop_invariants (void)
245{
246 if (current_loops)
247 move_loop_invariants (current_loops);
248 return 0;
249}
250
251struct tree_opt_pass pass_rtl_move_loop_invariants =
252{
253 "loop2_invariant", /* name */
254 gate_rtl_move_loop_invariants, /* gate */
255 rtl_move_loop_invariants, /* execute */
256 NULL, /* sub */
257 NULL, /* next */
258 0, /* static_pass_number */
259 TV_LOOP, /* tv_id */
260 0, /* properties_required */
261 0, /* properties_provided */
262 0, /* properties_destroyed */
263 0, /* todo_flags_start */
264 TODO_dump_func, /* todo_flags_finish */
265 'L' /* letter */
266};
267
268
269/* Loop unswitching for RTL. */
270static bool
271gate_rtl_unswitch (void)
272{
273 return flag_unswitch_loops;
274}
275
276static unsigned int
277rtl_unswitch (void)
278{
279 if (current_loops)
280 unswitch_loops (current_loops);
281 return 0;
282}
283
284struct tree_opt_pass pass_rtl_unswitch =
285{
286 "loop2_unswitch", /* name */
287 gate_rtl_unswitch, /* gate */
288 rtl_unswitch, /* execute */
289 NULL, /* sub */
290 NULL, /* next */
291 0, /* static_pass_number */
292 TV_LOOP, /* tv_id */
293 0, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 TODO_dump_func, /* todo_flags_finish */
298 'L' /* letter */
299};
300
301
302/* Loop unswitching for RTL. */
303static bool
304gate_rtl_unroll_and_peel_loops (void)
305{
306 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
307}
308
309static unsigned int
310rtl_unroll_and_peel_loops (void)
311{
312 if (current_loops)
313 {
314 int flags = 0;
315
316 if (flag_peel_loops)
317 flags |= UAP_PEEL;
318 if (flag_unroll_loops)
319 flags |= UAP_UNROLL;
320 if (flag_unroll_all_loops)
321 flags |= UAP_UNROLL_ALL;
322
323 unroll_and_peel_loops (current_loops, flags);
324 }
325 return 0;
326}
327
328struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
329{
330 "loop2_unroll", /* name */
331 gate_rtl_unroll_and_peel_loops, /* gate */
332 rtl_unroll_and_peel_loops, /* execute */
333 NULL, /* sub */
334 NULL, /* next */
335 0, /* static_pass_number */
336 TV_LOOP, /* tv_id */
337 0, /* properties_required */
338 0, /* properties_provided */
339 0, /* properties_destroyed */
340 0, /* todo_flags_start */
341 TODO_dump_func, /* todo_flags_finish */
342 'L' /* letter */
343};
344
345
346/* The doloop optimization. */
347static bool
348gate_rtl_doloop (void)
349{
350#ifdef HAVE_doloop_end
351 return (flag_branch_on_count_reg && HAVE_doloop_end);
352#else
353 return 0;
354#endif
355}
356
357static unsigned int
358rtl_doloop (void)
359{
360#ifdef HAVE_doloop_end
361 if (current_loops)
362 doloop_optimize_loops (current_loops);
363#endif
364 return 0;
365}
366
367struct tree_opt_pass pass_rtl_doloop =
368{
369 "loop2_doloop", /* name */
370 gate_rtl_doloop, /* gate */
371 rtl_doloop, /* execute */
372 NULL, /* sub */
373 NULL, /* next */
374 0, /* static_pass_number */
375 TV_LOOP, /* tv_id */
376 0, /* properties_required */
377 0, /* properties_provided */
378 0, /* properties_destroyed */
379 0, /* todo_flags_start */
380 TODO_dump_func, /* todo_flags_finish */
381 'L' /* letter */
382};
383