* check-init.c, decl.c, expr.c, gcj.texi, java-tree.h,
[official-gcc.git] / gcc / tree-ssa-loop.c
blob692f9b10e63b53669ea6fa284ab8ca38a60dc036
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
35 #include "timevar.h"
36 #include "cfgloop.h"
37 #include "flags.h"
38 #include "tree-inline.h"
39 #include "tree-scalar-evolution.h"
41 /* The loop tree currently optimized. */
43 struct loops *current_loops = NULL;
45 /* Initializes the loop structures. DUMP is the file to that the details
46 about the analysis should be dumped. */
48 static struct loops *
49 tree_loop_optimizer_init (FILE *dump)
51 struct loops *loops = loop_optimizer_init (dump);
53 if (!loops)
54 return NULL;
56 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
58 return loops;
61 /* The loop superpass. */
63 static bool
64 gate_tree_loop (void)
66 return flag_tree_loop_optimize != 0;
69 struct tree_opt_pass pass_tree_loop =
71 "loop", /* name */
72 gate_tree_loop, /* gate */
73 NULL, /* execute */
74 NULL, /* sub */
75 NULL, /* next */
76 0, /* static_pass_number */
77 TV_TREE_LOOP, /* tv_id */
78 PROP_cfg, /* properties_required */
79 0, /* properties_provided */
80 0, /* properties_destroyed */
81 TODO_ggc_collect, /* todo_flags_start */
82 TODO_dump_func | TODO_verify_ssa | TODO_ggc_collect, /* todo_flags_finish */
83 0 /* letter */
86 /* Loop optimizer initialization. */
88 static void
89 tree_ssa_loop_init (void)
91 current_loops = tree_loop_optimizer_init (dump_file);
92 if (!current_loops)
93 return;
95 /* Find the loops that are exited just through a single edge. */
96 mark_single_exit_loops (current_loops);
98 scev_initialize (current_loops);
101 struct tree_opt_pass pass_tree_loop_init =
103 "loopinit", /* name */
104 NULL, /* gate */
105 tree_ssa_loop_init, /* execute */
106 NULL, /* sub */
107 NULL, /* next */
108 0, /* static_pass_number */
109 TV_TREE_LOOP_INIT, /* tv_id */
110 PROP_cfg, /* properties_required */
111 0, /* properties_provided */
112 0, /* properties_destroyed */
113 0, /* todo_flags_start */
114 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
115 0 /* letter */
118 /* Loop invariant motion pass. */
120 static void
121 tree_ssa_loop_im (void)
123 if (!current_loops)
124 return;
126 tree_ssa_lim (current_loops);
129 static bool
130 gate_tree_ssa_loop_im (void)
132 return flag_tree_loop_im != 0;
135 struct tree_opt_pass pass_lim =
137 "lim", /* name */
138 gate_tree_ssa_loop_im, /* gate */
139 tree_ssa_loop_im, /* execute */
140 NULL, /* sub */
141 NULL, /* next */
142 0, /* static_pass_number */
143 TV_LIM, /* tv_id */
144 PROP_cfg, /* properties_required */
145 0, /* properties_provided */
146 0, /* properties_destroyed */
147 0, /* todo_flags_start */
148 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
149 0 /* letter */
152 /* Loop unswitching pass. */
154 static void
155 tree_ssa_loop_unswitch (void)
157 if (!current_loops)
158 return;
160 tree_ssa_unswitch_loops (current_loops);
163 static bool
164 gate_tree_ssa_loop_unswitch (void)
166 return flag_unswitch_loops != 0;
169 struct tree_opt_pass pass_tree_unswitch =
171 "unswitch", /* name */
172 gate_tree_ssa_loop_unswitch, /* gate */
173 tree_ssa_loop_unswitch, /* execute */
174 NULL, /* sub */
175 NULL, /* next */
176 0, /* static_pass_number */
177 TV_TREE_LOOP_UNSWITCH, /* tv_id */
178 PROP_cfg, /* properties_required */
179 0, /* properties_provided */
180 0, /* properties_destroyed */
181 0, /* todo_flags_start */
182 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
183 0 /* letter */
186 /* Loop autovectorization. */
188 static void
189 tree_vectorize (void)
191 if (!current_loops)
192 return;
194 vectorize_loops (current_loops);
197 static bool
198 gate_tree_vectorize (void)
200 return flag_tree_vectorize != 0;
203 struct tree_opt_pass pass_vectorize =
205 "vect", /* name */
206 gate_tree_vectorize, /* gate */
207 tree_vectorize, /* execute */
208 NULL, /* sub */
209 NULL, /* next */
210 0, /* static_pass_number */
211 TV_TREE_VECTORIZATION, /* tv_id */
212 PROP_cfg | PROP_ssa, /* properties_required */
213 0, /* properties_provided */
214 0, /* properties_destroyed */
215 TODO_verify_loops, /* todo_flags_start */
216 TODO_dump_func | TODO_update_ssa, /* todo_flags_finish */
217 0 /* letter */
221 /* Loop nest optimizations. */
223 static void
224 tree_linear_transform (void)
226 if (!current_loops)
227 return;
229 linear_transform_loops (current_loops);
232 static bool
233 gate_tree_linear_transform (void)
235 return flag_tree_loop_linear != 0;
238 struct tree_opt_pass pass_linear_transform =
240 "ltrans", /* name */
241 gate_tree_linear_transform, /* gate */
242 tree_linear_transform, /* execute */
243 NULL, /* sub */
244 NULL, /* next */
245 0, /* static_pass_number */
246 TV_TREE_LINEAR_TRANSFORM, /* tv_id */
247 PROP_cfg | PROP_ssa, /* properties_required */
248 0, /* properties_provided */
249 0, /* properties_destroyed */
250 0, /* todo_flags_start */
251 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
252 0 /* letter */
255 /* Canonical induction variable creation pass. */
257 static void
258 tree_ssa_loop_ivcanon (void)
260 if (!current_loops)
261 return;
263 canonicalize_induction_variables (current_loops);
266 static bool
267 gate_tree_ssa_loop_ivcanon (void)
269 return flag_tree_loop_ivcanon != 0;
272 struct tree_opt_pass pass_iv_canon =
274 "ivcanon", /* name */
275 gate_tree_ssa_loop_ivcanon, /* gate */
276 tree_ssa_loop_ivcanon, /* execute */
277 NULL, /* sub */
278 NULL, /* next */
279 0, /* static_pass_number */
280 TV_TREE_LOOP_IVCANON, /* tv_id */
281 PROP_cfg | PROP_ssa, /* properties_required */
282 0, /* properties_provided */
283 0, /* properties_destroyed */
284 0, /* todo_flags_start */
285 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
286 0 /* letter */
289 /* Propagation of constants using scev. */
291 static bool
292 gate_scev_const_prop (void)
294 return true;
297 struct tree_opt_pass pass_scev_cprop =
299 "sccp", /* name */
300 gate_scev_const_prop, /* gate */
301 scev_const_prop, /* execute */
302 NULL, /* sub */
303 NULL, /* next */
304 0, /* static_pass_number */
305 TV_SCEV_CONST, /* tv_id */
306 PROP_cfg | PROP_ssa, /* properties_required */
307 0, /* properties_provided */
308 0, /* properties_destroyed */
309 0, /* todo_flags_start */
310 TODO_dump_func | TODO_update_ssa_only_virtuals,
311 /* todo_flags_finish */
312 0 /* letter */
315 /* Remove empty loops. */
317 static void
318 tree_ssa_empty_loop (void)
320 if (!current_loops)
321 return;
323 remove_empty_loops (current_loops);
326 struct tree_opt_pass pass_empty_loop =
328 "empty", /* name */
329 NULL, /* gate */
330 tree_ssa_empty_loop, /* execute */
331 NULL, /* sub */
332 NULL, /* next */
333 0, /* static_pass_number */
334 TV_COMPLETE_UNROLL, /* tv_id */
335 PROP_cfg | PROP_ssa, /* properties_required */
336 0, /* properties_provided */
337 0, /* properties_destroyed */
338 0, /* todo_flags_start */
339 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
340 0 /* letter */
343 /* Record bounds on numbers of iterations of loops. */
345 static void
346 tree_ssa_loop_bounds (void)
348 if (!current_loops)
349 return;
351 estimate_numbers_of_iterations (current_loops);
352 scev_reset ();
355 struct tree_opt_pass pass_record_bounds =
357 NULL, /* name */
358 NULL, /* gate */
359 tree_ssa_loop_bounds, /* execute */
360 NULL, /* sub */
361 NULL, /* next */
362 0, /* static_pass_number */
363 TV_TREE_LOOP_BOUNDS, /* tv_id */
364 PROP_cfg | PROP_ssa, /* properties_required */
365 0, /* properties_provided */
366 0, /* properties_destroyed */
367 0, /* todo_flags_start */
368 0, /* todo_flags_finish */
369 0 /* letter */
372 /* Complete unrolling of loops. */
374 static void
375 tree_complete_unroll (void)
377 if (!current_loops)
378 return;
380 tree_unroll_loops_completely (current_loops,
381 flag_unroll_loops
382 || flag_peel_loops
383 || optimize >= 3);
386 static bool
387 gate_tree_complete_unroll (void)
389 return true;
392 struct tree_opt_pass pass_complete_unroll =
394 "cunroll", /* name */
395 gate_tree_complete_unroll, /* gate */
396 tree_complete_unroll, /* execute */
397 NULL, /* sub */
398 NULL, /* next */
399 0, /* static_pass_number */
400 TV_COMPLETE_UNROLL, /* tv_id */
401 PROP_cfg | PROP_ssa, /* properties_required */
402 0, /* properties_provided */
403 0, /* properties_destroyed */
404 0, /* todo_flags_start */
405 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
406 0 /* letter */
409 /* Induction variable optimizations. */
411 static void
412 tree_ssa_loop_ivopts (void)
414 if (!current_loops)
415 return;
417 tree_ssa_iv_optimize (current_loops);
420 static bool
421 gate_tree_ssa_loop_ivopts (void)
423 return flag_ivopts != 0;
426 struct tree_opt_pass pass_iv_optimize =
428 "ivopts", /* name */
429 gate_tree_ssa_loop_ivopts, /* gate */
430 tree_ssa_loop_ivopts, /* execute */
431 NULL, /* sub */
432 NULL, /* next */
433 0, /* static_pass_number */
434 TV_TREE_LOOP_IVOPTS, /* tv_id */
435 PROP_cfg | PROP_ssa, /* properties_required */
436 0, /* properties_provided */
437 0, /* properties_destroyed */
438 0, /* todo_flags_start */
439 TODO_dump_func | TODO_verify_loops, /* todo_flags_finish */
440 0 /* letter */
443 /* Loop optimizer finalization. */
445 static void
446 tree_ssa_loop_done (void)
448 if (!current_loops)
449 return;
451 free_numbers_of_iterations_estimates (current_loops);
452 scev_finalize ();
453 loop_optimizer_finalize (current_loops,
454 (dump_flags & TDF_DETAILS ? dump_file : NULL));
455 current_loops = NULL;
458 struct tree_opt_pass pass_tree_loop_done =
460 "loopdone", /* name */
461 NULL, /* gate */
462 tree_ssa_loop_done, /* execute */
463 NULL, /* sub */
464 NULL, /* next */
465 0, /* static_pass_number */
466 TV_TREE_LOOP_FINI, /* tv_id */
467 PROP_cfg, /* properties_required */
468 0, /* properties_provided */
469 0, /* properties_destroyed */
470 0, /* todo_flags_start */
471 TODO_cleanup_cfg | TODO_dump_func, /* todo_flags_finish */
472 0 /* letter */