gcc/ChangeLog
[official-gcc.git] / gcc / tree-ssa-loop.c
blob99e27a1359a6077249fd9166b1fad01c14645319
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2013 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "tm_p.h"
26 #include "basic-block.h"
27 #include "tree-flow.h"
28 #include "tree-pass.h"
29 #include "cfgloop.h"
30 #include "flags.h"
31 #include "tree-inline.h"
32 #include "tree-scalar-evolution.h"
33 #include "diagnostic-core.h"
34 #include "tree-vectorizer.h"
36 /* The loop superpass. */
38 static bool
39 gate_tree_loop (void)
41 return flag_tree_loop_optimize != 0;
44 struct gimple_opt_pass pass_tree_loop =
47 GIMPLE_PASS,
48 "loop", /* name */
49 OPTGROUP_LOOP, /* optinfo_flags */
50 gate_tree_loop, /* gate */
51 NULL, /* execute */
52 NULL, /* sub */
53 NULL, /* next */
54 0, /* static_pass_number */
55 TV_TREE_LOOP, /* tv_id */
56 PROP_cfg, /* properties_required */
57 0, /* properties_provided */
58 0, /* properties_destroyed */
59 0, /* todo_flags_start */
60 TODO_verify_ssa /* todo_flags_finish */
64 /* Loop optimizer initialization. */
66 static unsigned int
67 tree_ssa_loop_init (void)
69 loop_optimizer_init (LOOPS_NORMAL
70 | LOOPS_HAVE_RECORDED_EXITS);
71 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
73 /* We might discover new loops, e.g. when turning irreducible
74 regions into reducible. */
75 scev_initialize ();
77 if (number_of_loops (cfun) <= 1)
78 return 0;
80 return 0;
83 struct gimple_opt_pass pass_tree_loop_init =
86 GIMPLE_PASS,
87 "loopinit", /* name */
88 OPTGROUP_LOOP, /* optinfo_flags */
89 NULL, /* gate */
90 tree_ssa_loop_init, /* execute */
91 NULL, /* sub */
92 NULL, /* next */
93 0, /* static_pass_number */
94 TV_NONE, /* tv_id */
95 PROP_cfg, /* properties_required */
96 0, /* properties_provided */
97 0, /* properties_destroyed */
98 0, /* todo_flags_start */
99 0 /* todo_flags_finish */
103 /* Loop invariant motion pass. */
105 static unsigned int
106 tree_ssa_loop_im (void)
108 if (number_of_loops (cfun) <= 1)
109 return 0;
111 return tree_ssa_lim ();
114 static bool
115 gate_tree_ssa_loop_im (void)
117 return flag_tree_loop_im != 0;
120 struct gimple_opt_pass pass_lim =
123 GIMPLE_PASS,
124 "lim", /* name */
125 OPTGROUP_LOOP, /* optinfo_flags */
126 gate_tree_ssa_loop_im, /* gate */
127 tree_ssa_loop_im, /* execute */
128 NULL, /* sub */
129 NULL, /* next */
130 0, /* static_pass_number */
131 TV_LIM, /* tv_id */
132 PROP_cfg, /* properties_required */
133 0, /* properties_provided */
134 0, /* properties_destroyed */
135 0, /* todo_flags_start */
136 0 /* todo_flags_finish */
140 /* Loop unswitching pass. */
142 static unsigned int
143 tree_ssa_loop_unswitch (void)
145 if (number_of_loops (cfun) <= 1)
146 return 0;
148 return tree_ssa_unswitch_loops ();
151 static bool
152 gate_tree_ssa_loop_unswitch (void)
154 return flag_unswitch_loops != 0;
157 struct gimple_opt_pass pass_tree_unswitch =
160 GIMPLE_PASS,
161 "unswitch", /* name */
162 OPTGROUP_LOOP, /* optinfo_flags */
163 gate_tree_ssa_loop_unswitch, /* gate */
164 tree_ssa_loop_unswitch, /* execute */
165 NULL, /* sub */
166 NULL, /* next */
167 0, /* static_pass_number */
168 TV_TREE_LOOP_UNSWITCH, /* tv_id */
169 PROP_cfg, /* properties_required */
170 0, /* properties_provided */
171 0, /* properties_destroyed */
172 0, /* todo_flags_start */
173 0 /* todo_flags_finish */
177 /* Predictive commoning. */
179 static unsigned
180 run_tree_predictive_commoning (void)
182 if (!current_loops)
183 return 0;
185 return tree_predictive_commoning ();
188 static bool
189 gate_tree_predictive_commoning (void)
191 return flag_predictive_commoning != 0;
194 struct gimple_opt_pass pass_predcom =
197 GIMPLE_PASS,
198 "pcom", /* name */
199 OPTGROUP_LOOP, /* optinfo_flags */
200 gate_tree_predictive_commoning, /* gate */
201 run_tree_predictive_commoning, /* execute */
202 NULL, /* sub */
203 NULL, /* next */
204 0, /* static_pass_number */
205 TV_PREDCOM, /* tv_id */
206 PROP_cfg, /* properties_required */
207 0, /* properties_provided */
208 0, /* properties_destroyed */
209 0, /* todo_flags_start */
210 TODO_update_ssa_only_virtuals /* todo_flags_finish */
214 /* Loop autovectorization. */
216 static unsigned int
217 tree_vectorize (void)
219 if (number_of_loops (cfun) <= 1)
220 return 0;
222 return vectorize_loops ();
225 static bool
226 gate_tree_vectorize (void)
228 return flag_tree_vectorize;
231 struct gimple_opt_pass pass_vectorize =
234 GIMPLE_PASS,
235 "vect", /* name */
236 OPTGROUP_LOOP
237 | OPTGROUP_VEC, /* optinfo_flags */
238 gate_tree_vectorize, /* gate */
239 tree_vectorize, /* execute */
240 NULL, /* sub */
241 NULL, /* next */
242 0, /* static_pass_number */
243 TV_TREE_VECTORIZATION, /* tv_id */
244 PROP_cfg | PROP_ssa, /* properties_required */
245 0, /* properties_provided */
246 0, /* properties_destroyed */
247 0, /* todo_flags_start */
248 0 /* todo_flags_finish */
252 /* GRAPHITE optimizations. */
254 static unsigned int
255 graphite_transforms (void)
257 if (!current_loops)
258 return 0;
260 graphite_transform_loops ();
262 return 0;
265 static bool
266 gate_graphite_transforms (void)
268 /* Enable -fgraphite pass if any one of the graphite optimization flags
269 is turned on. */
270 if (flag_loop_block
271 || flag_loop_interchange
272 || flag_loop_strip_mine
273 || flag_graphite_identity
274 || flag_loop_parallelize_all
275 || flag_loop_optimize_isl)
276 flag_graphite = 1;
278 return flag_graphite != 0;
281 struct gimple_opt_pass pass_graphite =
284 GIMPLE_PASS,
285 "graphite0", /* name */
286 OPTGROUP_LOOP, /* optinfo_flags */
287 gate_graphite_transforms, /* gate */
288 NULL, /* execute */
289 NULL, /* sub */
290 NULL, /* next */
291 0, /* static_pass_number */
292 TV_GRAPHITE, /* tv_id */
293 PROP_cfg | PROP_ssa, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 0 /* todo_flags_finish */
301 struct gimple_opt_pass pass_graphite_transforms =
304 GIMPLE_PASS,
305 "graphite", /* name */
306 OPTGROUP_LOOP, /* optinfo_flags */
307 gate_graphite_transforms, /* gate */
308 graphite_transforms, /* execute */
309 NULL, /* sub */
310 NULL, /* next */
311 0, /* static_pass_number */
312 TV_GRAPHITE_TRANSFORMS, /* tv_id */
313 PROP_cfg | PROP_ssa, /* properties_required */
314 0, /* properties_provided */
315 0, /* properties_destroyed */
316 0, /* todo_flags_start */
317 0 /* todo_flags_finish */
321 /* Check the correctness of the data dependence analyzers. */
323 static unsigned int
324 check_data_deps (void)
326 if (number_of_loops (cfun) <= 1)
327 return 0;
329 tree_check_data_deps ();
330 return 0;
333 static bool
334 gate_check_data_deps (void)
336 return flag_check_data_deps != 0;
339 struct gimple_opt_pass pass_check_data_deps =
342 GIMPLE_PASS,
343 "ckdd", /* name */
344 OPTGROUP_LOOP, /* optinfo_flags */
345 gate_check_data_deps, /* gate */
346 check_data_deps, /* execute */
347 NULL, /* sub */
348 NULL, /* next */
349 0, /* static_pass_number */
350 TV_CHECK_DATA_DEPS, /* tv_id */
351 PROP_cfg | PROP_ssa, /* properties_required */
352 0, /* properties_provided */
353 0, /* properties_destroyed */
354 0, /* todo_flags_start */
355 0 /* todo_flags_finish */
359 /* Canonical induction variable creation pass. */
361 static unsigned int
362 tree_ssa_loop_ivcanon (void)
364 if (number_of_loops (cfun) <= 1)
365 return 0;
367 return canonicalize_induction_variables ();
370 static bool
371 gate_tree_ssa_loop_ivcanon (void)
373 return flag_tree_loop_ivcanon != 0;
376 struct gimple_opt_pass pass_iv_canon =
379 GIMPLE_PASS,
380 "ivcanon", /* name */
381 OPTGROUP_LOOP, /* optinfo_flags */
382 gate_tree_ssa_loop_ivcanon, /* gate */
383 tree_ssa_loop_ivcanon, /* execute */
384 NULL, /* sub */
385 NULL, /* next */
386 0, /* static_pass_number */
387 TV_TREE_LOOP_IVCANON, /* tv_id */
388 PROP_cfg | PROP_ssa, /* properties_required */
389 0, /* properties_provided */
390 0, /* properties_destroyed */
391 0, /* todo_flags_start */
392 0 /* todo_flags_finish */
396 /* Propagation of constants using scev. */
398 static bool
399 gate_scev_const_prop (void)
401 return flag_tree_scev_cprop;
404 struct gimple_opt_pass pass_scev_cprop =
407 GIMPLE_PASS,
408 "sccp", /* name */
409 OPTGROUP_LOOP, /* optinfo_flags */
410 gate_scev_const_prop, /* gate */
411 scev_const_prop, /* execute */
412 NULL, /* sub */
413 NULL, /* next */
414 0, /* static_pass_number */
415 TV_SCEV_CONST, /* tv_id */
416 PROP_cfg | PROP_ssa, /* properties_required */
417 0, /* properties_provided */
418 0, /* properties_destroyed */
419 0, /* todo_flags_start */
420 TODO_cleanup_cfg
421 | TODO_update_ssa_only_virtuals
422 /* todo_flags_finish */
426 /* Record bounds on numbers of iterations of loops. */
428 static unsigned int
429 tree_ssa_loop_bounds (void)
431 if (number_of_loops (cfun) <= 1)
432 return 0;
434 estimate_numbers_of_iterations ();
435 scev_reset ();
436 return 0;
439 struct gimple_opt_pass pass_record_bounds =
442 GIMPLE_PASS,
443 "*record_bounds", /* name */
444 OPTGROUP_NONE, /* optinfo_flags */
445 NULL, /* gate */
446 tree_ssa_loop_bounds, /* execute */
447 NULL, /* sub */
448 NULL, /* next */
449 0, /* static_pass_number */
450 TV_TREE_LOOP_BOUNDS, /* tv_id */
451 PROP_cfg | PROP_ssa, /* properties_required */
452 0, /* properties_provided */
453 0, /* properties_destroyed */
454 0, /* todo_flags_start */
455 0 /* todo_flags_finish */
459 /* Complete unrolling of loops. */
461 static unsigned int
462 tree_complete_unroll (void)
464 if (number_of_loops (cfun) <= 1)
465 return 0;
467 return tree_unroll_loops_completely (flag_unroll_loops
468 || flag_peel_loops
469 || optimize >= 3, true);
472 static bool
473 gate_tree_complete_unroll (void)
475 return true;
478 struct gimple_opt_pass pass_complete_unroll =
481 GIMPLE_PASS,
482 "cunroll", /* name */
483 OPTGROUP_LOOP, /* optinfo_flags */
484 gate_tree_complete_unroll, /* gate */
485 tree_complete_unroll, /* execute */
486 NULL, /* sub */
487 NULL, /* next */
488 0, /* static_pass_number */
489 TV_COMPLETE_UNROLL, /* tv_id */
490 PROP_cfg | PROP_ssa, /* properties_required */
491 0, /* properties_provided */
492 0, /* properties_destroyed */
493 0, /* todo_flags_start */
494 0 /* todo_flags_finish */
498 /* Complete unrolling of inner loops. */
500 static unsigned int
501 tree_complete_unroll_inner (void)
503 unsigned ret = 0;
505 loop_optimizer_init (LOOPS_NORMAL
506 | LOOPS_HAVE_RECORDED_EXITS);
507 if (number_of_loops (cfun) > 1)
509 scev_initialize ();
510 ret = tree_unroll_loops_completely (optimize >= 3, false);
511 free_numbers_of_iterations_estimates ();
512 scev_finalize ();
514 loop_optimizer_finalize ();
516 return ret;
519 static bool
520 gate_tree_complete_unroll_inner (void)
522 return optimize >= 2;
525 struct gimple_opt_pass pass_complete_unrolli =
528 GIMPLE_PASS,
529 "cunrolli", /* name */
530 OPTGROUP_LOOP, /* optinfo_flags */
531 gate_tree_complete_unroll_inner, /* gate */
532 tree_complete_unroll_inner, /* execute */
533 NULL, /* sub */
534 NULL, /* next */
535 0, /* static_pass_number */
536 TV_COMPLETE_UNROLL, /* tv_id */
537 PROP_cfg | PROP_ssa, /* properties_required */
538 0, /* properties_provided */
539 0, /* properties_destroyed */
540 0, /* todo_flags_start */
541 TODO_verify_flow /* todo_flags_finish */
545 /* Parallelization. */
547 static bool
548 gate_tree_parallelize_loops (void)
550 return flag_tree_parallelize_loops > 1;
553 static unsigned
554 tree_parallelize_loops (void)
556 if (number_of_loops (cfun) <= 1)
557 return 0;
559 if (parallelize_loops ())
560 return TODO_cleanup_cfg | TODO_rebuild_alias;
561 return 0;
564 struct gimple_opt_pass pass_parallelize_loops =
567 GIMPLE_PASS,
568 "parloops", /* name */
569 OPTGROUP_LOOP, /* optinfo_flags */
570 gate_tree_parallelize_loops, /* gate */
571 tree_parallelize_loops, /* execute */
572 NULL, /* sub */
573 NULL, /* next */
574 0, /* static_pass_number */
575 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
576 PROP_cfg | PROP_ssa, /* properties_required */
577 0, /* properties_provided */
578 0, /* properties_destroyed */
579 0, /* todo_flags_start */
580 TODO_verify_flow /* todo_flags_finish */
584 /* Prefetching. */
586 static unsigned int
587 tree_ssa_loop_prefetch (void)
589 if (number_of_loops (cfun) <= 1)
590 return 0;
592 return tree_ssa_prefetch_arrays ();
595 static bool
596 gate_tree_ssa_loop_prefetch (void)
598 return flag_prefetch_loop_arrays > 0;
601 struct gimple_opt_pass pass_loop_prefetch =
604 GIMPLE_PASS,
605 "aprefetch", /* name */
606 OPTGROUP_LOOP, /* optinfo_flags */
607 gate_tree_ssa_loop_prefetch, /* gate */
608 tree_ssa_loop_prefetch, /* execute */
609 NULL, /* sub */
610 NULL, /* next */
611 0, /* static_pass_number */
612 TV_TREE_PREFETCH, /* tv_id */
613 PROP_cfg | PROP_ssa, /* properties_required */
614 0, /* properties_provided */
615 0, /* properties_destroyed */
616 0, /* todo_flags_start */
617 0 /* todo_flags_finish */
621 /* Induction variable optimizations. */
623 static unsigned int
624 tree_ssa_loop_ivopts (void)
626 if (number_of_loops (cfun) <= 1)
627 return 0;
629 tree_ssa_iv_optimize ();
630 return 0;
633 static bool
634 gate_tree_ssa_loop_ivopts (void)
636 return flag_ivopts != 0;
639 struct gimple_opt_pass pass_iv_optimize =
642 GIMPLE_PASS,
643 "ivopts", /* name */
644 OPTGROUP_LOOP, /* optinfo_flags */
645 gate_tree_ssa_loop_ivopts, /* gate */
646 tree_ssa_loop_ivopts, /* execute */
647 NULL, /* sub */
648 NULL, /* next */
649 0, /* static_pass_number */
650 TV_TREE_LOOP_IVOPTS, /* tv_id */
651 PROP_cfg | PROP_ssa, /* properties_required */
652 0, /* properties_provided */
653 0, /* properties_destroyed */
654 0, /* todo_flags_start */
655 TODO_update_ssa /* todo_flags_finish */
659 /* Loop optimizer finalization. */
661 static unsigned int
662 tree_ssa_loop_done (void)
664 free_numbers_of_iterations_estimates ();
665 scev_finalize ();
666 loop_optimizer_finalize ();
667 return 0;
670 struct gimple_opt_pass pass_tree_loop_done =
673 GIMPLE_PASS,
674 "loopdone", /* name */
675 OPTGROUP_LOOP, /* optinfo_flags */
676 NULL, /* gate */
677 tree_ssa_loop_done, /* execute */
678 NULL, /* sub */
679 NULL, /* next */
680 0, /* static_pass_number */
681 TV_NONE, /* tv_id */
682 PROP_cfg, /* properties_required */
683 0, /* properties_provided */
684 0, /* properties_destroyed */
685 0, /* todo_flags_start */
686 TODO_cleanup_cfg
687 | TODO_verify_flow /* todo_flags_finish */