]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/loop-init.c
* tree-vrp.c (execute_vrp): Do not pass dump argument to.
[thirdparty/gcc.git] / gcc / loop-init.c
CommitLineData
0526a3ff 1/* Loop optimizer initialization routines and RTL loop optimization passes.
102973cc 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6a606e3c 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
67ce556b 18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA. */
6a606e3c 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"
42fe97ed 27#include "obstack.h"
6a606e3c 28#include "basic-block.h"
29#include "cfgloop.h"
30#include "cfglayout.h"
77fce4cd 31#include "tree-pass.h"
32#include "timevar.h"
33#include "flags.h"
6a606e3c 34
0526a3ff 35\f
36/* Initialize loop optimizer. This is used by the tree and RTL loop
c8d055f6 37 optimizers. FLAGS specify what properties to compute and/or ensure for
38 loops. */
6a606e3c 39
40struct loops *
3f5be5f4 41loop_optimizer_init (unsigned flags)
6a606e3c 42{
4c36ffe6 43 struct loops *loops = XCNEW (struct loops);
6a606e3c 44 edge e;
cd665a06 45 edge_iterator ei;
3a0ecac2 46 static bool first_time = true;
47
48 if (first_time)
49 {
50 first_time = false;
51 init_set_costs ();
52 }
6a606e3c 53
54 /* Avoid annoying special cases of edges going to exit
55 block. */
cd665a06 56
57 for (ei = ei_start (EXIT_BLOCK_PTR->preds); (e = ei_safe_edge (ei)); )
ea091dfd 58 if ((e->flags & EDGE_FALLTHRU) && !single_succ_p (e->src))
6a606e3c 59 split_edge (e);
cd665a06 60 else
61 ei_next (&ei);
6a606e3c 62
63 /* Find the loops. */
64
ffc6b5d5 65 if (flow_loops_find (loops) <= 1)
6a606e3c 66 {
67 /* No loops. */
68 flow_loops_free (loops);
69 free (loops);
0051c76a 70
6a606e3c 71 return NULL;
72 }
73
74 /* Not going to update these. */
75 free (loops->cfg.rc_order);
76 loops->cfg.rc_order = NULL;
77 free (loops->cfg.dfs_order);
78 loops->cfg.dfs_order = NULL;
79
6a606e3c 80 /* Create pre-headers. */
c8d055f6 81 if (flags & LOOPS_HAVE_PREHEADERS)
82 create_preheaders (loops, CP_SIMPLE_PREHEADERS);
6a606e3c 83
84 /* Force all latches to have only single successor. */
c8d055f6 85 if (flags & LOOPS_HAVE_SIMPLE_LATCHES)
86 force_single_succ_latches (loops);
6a606e3c 87
88 /* Mark irreducible loops. */
c8d055f6 89 if (flags & LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS)
90 mark_irreducible_loops (loops);
91
92 if (flags & LOOPS_HAVE_MARKED_SINGLE_EXITS)
93 mark_single_exit_loops (loops);
6a606e3c 94
95 /* Dump loops. */
3f5be5f4 96 flow_loops_dump (loops, dump_file, NULL, 1);
6a606e3c 97
98#ifdef ENABLE_CHECKING
0051c76a 99 verify_dominators (CDI_DOMINATORS);
6a606e3c 100 verify_loop_structure (loops);
101#endif
102
103 return loops;
104}
105
106/* Finalize loop optimizer. */
107void
3f5be5f4 108loop_optimizer_finalize (struct loops *loops)
6a606e3c 109{
f9cce2dc 110 unsigned i;
6a606e3c 111
f9cce2dc 112 if (!loops)
113 return;
114
115 for (i = 1; i < loops->num; i++)
116 if (loops->parray[i])
117 free_simple_loop_desc (loops->parray[i]);
6a606e3c 118
6a606e3c 119 /* Clean up. */
120 flow_loops_free (loops);
121 free (loops);
3ad4992f 122
6a606e3c 123 /* Checking. */
124#ifdef ENABLE_CHECKING
125 verify_flow_info ();
126#endif
127}
0526a3ff 128
77fce4cd 129\f
0526a3ff 130/* Gate for the RTL loop superpass. The actual passes are subpasses.
131 See passes.c for more on that. */
132
77fce4cd 133static bool
134gate_handle_loop2 (void)
135{
136 return (optimize > 0 && flag_loop_optimize2
137 && (flag_move_loop_invariants
138 || flag_unswitch_loops
139 || flag_peel_loops
140 || flag_unroll_loops
141 || flag_branch_on_count_reg));
142}
143
0526a3ff 144struct tree_opt_pass pass_loop2 =
77fce4cd 145{
0526a3ff 146 "loop2", /* name */
147 gate_handle_loop2, /* gate */
148 NULL, /* execute */
149 NULL, /* sub */
150 NULL, /* next */
151 0, /* static_pass_number */
152 TV_LOOP, /* tv_id */
153 0, /* properties_required */
154 0, /* properties_provided */
155 0, /* properties_destroyed */
156 0, /* todo_flags_start */
157 TODO_dump_func |
158 TODO_ggc_collect, /* todo_flags_finish */
159 'L' /* letter */
160};
77fce4cd 161
0526a3ff 162\f
163/* Initialization of the RTL loop passes. */
164static void
165rtl_loop_init (void)
166{
77fce4cd 167 if (dump_file)
168 dump_flow_info (dump_file);
169
170 /* Initialize structures for layout changes. */
171 cfg_layout_initialize (0);
172
3f5be5f4 173 current_loops = loop_optimizer_init (LOOPS_NORMAL);
0526a3ff 174}
77fce4cd 175
0526a3ff 176struct tree_opt_pass pass_rtl_loop_init =
177{
228967a9 178 "loop2_init", /* name */
0526a3ff 179 NULL, /* gate */
180 rtl_loop_init, /* execute */
181 NULL, /* sub */
182 NULL, /* next */
183 0, /* static_pass_number */
184 TV_LOOP, /* tv_id */
185 0, /* properties_required */
186 0, /* properties_provided */
187 0, /* properties_destroyed */
188 0, /* todo_flags_start */
189 TODO_dump_func, /* todo_flags_finish */
190 'L' /* letter */
191};
77fce4cd 192
0526a3ff 193\f
194/* Finalization of the RTL loop passes. */
195static void
196rtl_loop_done (void)
197{
198 basic_block bb;
77fce4cd 199
0526a3ff 200 if (current_loops)
3f5be5f4 201 loop_optimizer_finalize (current_loops);
77fce4cd 202
203 free_dominance_info (CDI_DOMINATORS);
204
205 /* Finalize layout changes. */
206 FOR_EACH_BB (bb)
207 if (bb->next_bb != EXIT_BLOCK_PTR)
208 bb->aux = bb->next_bb;
209 cfg_layout_finalize ();
210
211 cleanup_cfg (CLEANUP_EXPENSIVE);
212 delete_trivially_dead_insns (get_insns (), max_reg_num ());
213 reg_scan (get_insns (), max_reg_num ());
214 if (dump_file)
215 dump_flow_info (dump_file);
0526a3ff 216
217 current_loops = NULL;
77fce4cd 218}
219
0526a3ff 220struct tree_opt_pass pass_rtl_loop_done =
77fce4cd 221{
228967a9 222 "loop2_done", /* name */
0526a3ff 223 NULL, /* gate */
224 rtl_loop_done, /* execute */
77fce4cd 225 NULL, /* sub */
226 NULL, /* next */
227 0, /* static_pass_number */
228 TV_LOOP, /* tv_id */
229 0, /* properties_required */
230 0, /* properties_provided */
231 0, /* properties_destroyed */
232 0, /* todo_flags_start */
0526a3ff 233 TODO_dump_func, /* todo_flags_finish */
234 'L' /* letter */
235};
236
237\f
238/* Loop invariant code motion. */
228967a9 239static bool
240gate_rtl_move_loop_invariants (void)
241{
242 return flag_move_loop_invariants;
243}
244
0526a3ff 245static void
246rtl_move_loop_invariants (void)
247{
228967a9 248 if (current_loops)
0526a3ff 249 move_loop_invariants (current_loops);
250}
251
252struct tree_opt_pass pass_rtl_move_loop_invariants =
253{
228967a9 254 "loop2_invariant", /* name */
255 gate_rtl_move_loop_invariants, /* gate */
0526a3ff 256 rtl_move_loop_invariants, /* execute */
257 NULL, /* sub */
258 NULL, /* next */
259 0, /* static_pass_number */
260 TV_LOOP, /* tv_id */
261 0, /* properties_required */
262 0, /* properties_provided */
263 0, /* properties_destroyed */
264 0, /* todo_flags_start */
265 TODO_dump_func, /* todo_flags_finish */
266 'L' /* letter */
267};
268
269\f
270/* Loop unswitching for RTL. */
228967a9 271static bool
272gate_rtl_unswitch (void)
273{
274 return flag_unswitch_loops;
275}
276
0526a3ff 277static void
278rtl_unswitch (void)
279{
228967a9 280 if (current_loops)
0526a3ff 281 unswitch_loops (current_loops);
282}
283
284struct tree_opt_pass pass_rtl_unswitch =
285{
228967a9 286 "loop2_unswitch", /* name */
287 gate_rtl_unswitch, /* gate */
0526a3ff 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\f
302/* Loop unswitching for RTL. */
228967a9 303static bool
304gate_rtl_unroll_and_peel_loops (void)
305{
306 return (flag_peel_loops || flag_unroll_loops || flag_unroll_all_loops);
307}
308
0526a3ff 309static void
310rtl_unroll_and_peel_loops (void)
311{
228967a9 312 if (current_loops)
0526a3ff 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}
326
327struct tree_opt_pass pass_rtl_unroll_and_peel_loops =
328{
228967a9 329 "loop2_unroll", /* name */
330 gate_rtl_unroll_and_peel_loops, /* gate */
0526a3ff 331 rtl_unroll_and_peel_loops, /* execute */
332 NULL, /* sub */
333 NULL, /* next */
334 0, /* static_pass_number */
335 TV_LOOP, /* tv_id */
336 0, /* properties_required */
337 0, /* properties_provided */
338 0, /* properties_destroyed */
339 0, /* todo_flags_start */
340 TODO_dump_func, /* todo_flags_finish */
341 'L' /* letter */
342};
343
344\f
345/* The doloop optimization. */
228967a9 346static bool
347gate_rtl_doloop (void)
348{
349#ifdef HAVE_doloop_end
350 return (flag_branch_on_count_reg && HAVE_doloop_end);
351#else
352 return 0;
353#endif
354}
355
0526a3ff 356static void
357rtl_doloop (void)
358{
359#ifdef HAVE_doloop_end
228967a9 360 if (current_loops)
0526a3ff 361 doloop_optimize_loops (current_loops);
362#endif
363}
364
365struct tree_opt_pass pass_rtl_doloop =
366{
228967a9 367 "loop2_doloop", /* name */
368 gate_rtl_doloop, /* gate */
0526a3ff 369 rtl_doloop, /* execute */
370 NULL, /* sub */
371 NULL, /* next */
372 0, /* static_pass_number */
373 TV_LOOP, /* tv_id */
374 0, /* properties_required */
375 0, /* properties_provided */
376 0, /* properties_destroyed */
377 0, /* todo_flags_start */
378 TODO_dump_func, /* todo_flags_finish */
77fce4cd 379 'L' /* letter */
380};
381