1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003, 2005, 2006, 2007, 2008 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
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
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"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "tree-pass.h"
37 #include "tree-inline.h"
38 #include "tree-scalar-evolution.h"
40 /* The loop superpass. */
45 return flag_tree_loop_optimize
!= 0;
48 struct gimple_opt_pass pass_tree_loop
=
53 gate_tree_loop
, /* gate */
57 0, /* static_pass_number */
58 TV_TREE_LOOP
, /* tv_id */
59 PROP_cfg
, /* properties_required */
60 0, /* properties_provided */
61 0, /* properties_destroyed */
62 TODO_ggc_collect
, /* todo_flags_start */
63 TODO_dump_func
| TODO_verify_ssa
| TODO_ggc_collect
/* todo_flags_finish */
67 /* Loop optimizer initialization. */
70 tree_ssa_loop_init (void)
72 loop_optimizer_init (LOOPS_NORMAL
73 | LOOPS_HAVE_RECORDED_EXITS
);
74 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
76 if (number_of_loops () <= 1)
83 struct gimple_opt_pass pass_tree_loop_init
=
87 "loopinit", /* name */
89 tree_ssa_loop_init
, /* execute */
92 0, /* static_pass_number */
93 TV_TREE_LOOP_INIT
, /* tv_id */
94 PROP_cfg
, /* properties_required */
95 0, /* properties_provided */
96 0, /* properties_destroyed */
97 0, /* todo_flags_start */
98 TODO_dump_func
| TODO_verify_loops
/* todo_flags_finish */
102 /* Loop invariant motion pass. */
105 tree_ssa_loop_im (void)
107 if (number_of_loops () <= 1)
115 gate_tree_ssa_loop_im (void)
117 return flag_tree_loop_im
!= 0;
120 struct gimple_opt_pass pass_lim
=
125 gate_tree_ssa_loop_im
, /* gate */
126 tree_ssa_loop_im
, /* execute */
129 0, /* static_pass_number */
131 PROP_cfg
, /* properties_required */
132 0, /* properties_provided */
133 0, /* properties_destroyed */
134 0, /* todo_flags_start */
135 TODO_dump_func
| TODO_verify_loops
/* todo_flags_finish */
139 /* Loop unswitching pass. */
142 tree_ssa_loop_unswitch (void)
144 if (number_of_loops () <= 1)
147 return tree_ssa_unswitch_loops ();
151 gate_tree_ssa_loop_unswitch (void)
153 return flag_unswitch_loops
!= 0;
156 struct gimple_opt_pass pass_tree_unswitch
=
160 "unswitch", /* name */
161 gate_tree_ssa_loop_unswitch
, /* gate */
162 tree_ssa_loop_unswitch
, /* execute */
165 0, /* static_pass_number */
166 TV_TREE_LOOP_UNSWITCH
, /* tv_id */
167 PROP_cfg
, /* properties_required */
168 0, /* properties_provided */
169 0, /* properties_destroyed */
170 0, /* todo_flags_start */
171 TODO_ggc_collect
| TODO_dump_func
172 | TODO_verify_loops
/* todo_flags_finish */
176 /* Predictive commoning. */
179 run_tree_predictive_commoning (void)
184 tree_predictive_commoning ();
189 gate_tree_predictive_commoning (void)
191 return flag_predictive_commoning
!= 0;
194 struct gimple_opt_pass pass_predcom
=
199 gate_tree_predictive_commoning
, /* gate */
200 run_tree_predictive_commoning
, /* execute */
203 0, /* static_pass_number */
204 TV_PREDCOM
, /* tv_id */
205 PROP_cfg
, /* properties_required */
206 0, /* properties_provided */
207 0, /* properties_destroyed */
208 0, /* todo_flags_start */
209 TODO_dump_func
| TODO_verify_loops
210 | TODO_update_ssa_only_virtuals
/* todo_flags_finish */
214 /* Loop autovectorization. */
217 tree_vectorize (void)
219 if (number_of_loops () <= 1)
222 return vectorize_loops ();
226 gate_tree_vectorize (void)
228 return flag_tree_vectorize
;
231 struct gimple_opt_pass pass_vectorize
=
236 gate_tree_vectorize
, /* gate */
237 tree_vectorize
, /* execute */
240 0, /* static_pass_number */
241 TV_TREE_VECTORIZATION
, /* tv_id */
242 PROP_cfg
| PROP_ssa
, /* properties_required */
243 0, /* properties_provided */
244 0, /* properties_destroyed */
245 TODO_verify_loops
, /* todo_flags_start */
246 TODO_dump_func
| TODO_update_ssa
247 | TODO_ggc_collect
/* todo_flags_finish */
251 /* Loop nest optimizations. */
254 tree_linear_transform (void)
256 if (number_of_loops () <= 1)
259 linear_transform_loops ();
264 gate_tree_linear_transform (void)
266 return flag_tree_loop_linear
!= 0;
269 struct gimple_opt_pass pass_linear_transform
=
274 gate_tree_linear_transform
, /* gate */
275 tree_linear_transform
, /* execute */
278 0, /* static_pass_number */
279 TV_TREE_LINEAR_TRANSFORM
, /* tv_id */
280 PROP_cfg
| PROP_ssa
, /* properties_required */
281 0, /* properties_provided */
282 0, /* properties_destroyed */
283 0, /* todo_flags_start */
284 TODO_dump_func
| TODO_verify_loops
285 | TODO_update_ssa_only_virtuals
286 | TODO_ggc_collect
/* todo_flags_finish */
290 /* GRAPHITE optimizations. */
293 graphite_transforms (void)
298 graphite_transform_loops ();
304 gate_graphite_transforms (void)
306 /* Enable -fgraphite pass if any one of the graphite optimization flags
308 if (flag_loop_block
|| flag_loop_interchange
|| flag_loop_strip_mine
309 || flag_graphite_identity
)
312 return flag_graphite
!= 0;
315 struct gimple_opt_pass pass_graphite_transforms
=
319 "graphite", /* name */
320 gate_graphite_transforms
, /* gate */
321 graphite_transforms
, /* execute */
324 0, /* static_pass_number */
325 TV_GRAPHITE_TRANSFORMS
, /* tv_id */
326 PROP_cfg
| PROP_ssa
, /* properties_required */
327 0, /* properties_provided */
328 0, /* properties_destroyed */
329 0, /* todo_flags_start */
330 TODO_verify_loops
/* todo_flags_finish */
334 /* Check the correctness of the data dependence analyzers. */
337 check_data_deps (void)
339 if (number_of_loops () <= 1)
342 tree_check_data_deps ();
347 gate_check_data_deps (void)
349 return flag_check_data_deps
!= 0;
352 struct gimple_opt_pass pass_check_data_deps
=
357 gate_check_data_deps
, /* gate */
358 check_data_deps
, /* execute */
361 0, /* static_pass_number */
362 TV_CHECK_DATA_DEPS
, /* tv_id */
363 PROP_cfg
| PROP_ssa
, /* properties_required */
364 0, /* properties_provided */
365 0, /* properties_destroyed */
366 0, /* todo_flags_start */
367 TODO_dump_func
/* todo_flags_finish */
371 /* Canonical induction variable creation pass. */
374 tree_ssa_loop_ivcanon (void)
376 if (number_of_loops () <= 1)
379 return canonicalize_induction_variables ();
383 gate_tree_ssa_loop_ivcanon (void)
385 return flag_tree_loop_ivcanon
!= 0;
388 struct gimple_opt_pass pass_iv_canon
=
392 "ivcanon", /* name */
393 gate_tree_ssa_loop_ivcanon
, /* gate */
394 tree_ssa_loop_ivcanon
, /* execute */
397 0, /* static_pass_number */
398 TV_TREE_LOOP_IVCANON
, /* tv_id */
399 PROP_cfg
| PROP_ssa
, /* properties_required */
400 0, /* properties_provided */
401 0, /* properties_destroyed */
402 0, /* todo_flags_start */
403 TODO_dump_func
| TODO_verify_loops
/* todo_flags_finish */
407 /* Propagation of constants using scev. */
410 gate_scev_const_prop (void)
412 return flag_tree_scev_cprop
;
415 struct gimple_opt_pass pass_scev_cprop
=
420 gate_scev_const_prop
, /* gate */
421 scev_const_prop
, /* execute */
424 0, /* static_pass_number */
425 TV_SCEV_CONST
, /* tv_id */
426 PROP_cfg
| PROP_ssa
, /* properties_required */
427 0, /* properties_provided */
428 0, /* properties_destroyed */
429 0, /* todo_flags_start */
430 TODO_dump_func
| TODO_cleanup_cfg
431 | TODO_update_ssa_only_virtuals
432 /* todo_flags_finish */
436 /* Remove empty loops. */
439 tree_ssa_empty_loop (void)
441 if (number_of_loops () <= 1)
444 return remove_empty_loops ();
447 struct gimple_opt_pass pass_empty_loop
=
453 tree_ssa_empty_loop
, /* execute */
456 0, /* static_pass_number */
457 TV_COMPLETE_UNROLL
, /* tv_id */
458 PROP_cfg
| PROP_ssa
, /* properties_required */
459 0, /* properties_provided */
460 0, /* properties_destroyed */
461 0, /* todo_flags_start */
462 TODO_dump_func
| TODO_verify_loops
463 | TODO_ggc_collect
/* todo_flags_finish */
467 /* Record bounds on numbers of iterations of loops. */
470 tree_ssa_loop_bounds (void)
472 if (number_of_loops () <= 1)
475 estimate_numbers_of_iterations ();
480 struct gimple_opt_pass pass_record_bounds
=
486 tree_ssa_loop_bounds
, /* execute */
489 0, /* static_pass_number */
490 TV_TREE_LOOP_BOUNDS
, /* tv_id */
491 PROP_cfg
| PROP_ssa
, /* properties_required */
492 0, /* properties_provided */
493 0, /* properties_destroyed */
494 0, /* todo_flags_start */
495 0 /* todo_flags_finish */
499 /* Complete unrolling of loops. */
502 tree_complete_unroll (void)
504 if (number_of_loops () <= 1)
507 return tree_unroll_loops_completely (flag_unroll_loops
509 || optimize
>= 3, true);
513 gate_tree_complete_unroll (void)
518 struct gimple_opt_pass pass_complete_unroll
=
522 "cunroll", /* name */
523 gate_tree_complete_unroll
, /* gate */
524 tree_complete_unroll
, /* execute */
527 0, /* static_pass_number */
528 TV_COMPLETE_UNROLL
, /* tv_id */
529 PROP_cfg
| PROP_ssa
, /* properties_required */
530 0, /* properties_provided */
531 0, /* properties_destroyed */
532 0, /* todo_flags_start */
533 TODO_dump_func
| TODO_verify_loops
534 | TODO_ggc_collect
/* todo_flags_finish */
538 /* Complete unrolling of inner loops. */
541 tree_complete_unroll_inner (void)
545 loop_optimizer_init (LOOPS_NORMAL
546 | LOOPS_HAVE_RECORDED_EXITS
);
547 if (number_of_loops () > 1)
550 ret
= tree_unroll_loops_completely (optimize
>= 3, false);
551 free_numbers_of_iterations_estimates ();
554 loop_optimizer_finalize ();
560 gate_tree_complete_unroll_inner (void)
562 return optimize
>= 2;
565 struct gimple_opt_pass pass_complete_unrolli
=
569 "cunrolli", /* name */
570 gate_tree_complete_unroll_inner
, /* gate */
571 tree_complete_unroll_inner
, /* execute */
574 0, /* static_pass_number */
575 TV_COMPLETE_UNROLL
, /* tv_id */
576 PROP_cfg
| PROP_ssa
, /* properties_required */
577 0, /* properties_provided */
578 0, /* properties_destroyed */
579 0, /* todo_flags_start */
580 TODO_dump_func
| TODO_verify_loops
581 | TODO_ggc_collect
/* todo_flags_finish */
585 /* Parallelization. */
588 gate_tree_parallelize_loops (void)
590 return flag_tree_parallelize_loops
> 1;
594 tree_parallelize_loops (void)
596 if (number_of_loops () <= 1)
599 if (parallelize_loops ())
600 return TODO_cleanup_cfg
| TODO_rebuild_alias
;
604 struct gimple_opt_pass pass_parallelize_loops
=
608 "parloops", /* name */
609 gate_tree_parallelize_loops
, /* gate */
610 tree_parallelize_loops
, /* execute */
613 0, /* static_pass_number */
614 TV_TREE_PARALLELIZE_LOOPS
, /* tv_id */
615 PROP_cfg
| PROP_ssa
, /* properties_required */
616 0, /* properties_provided */
617 0, /* properties_destroyed */
618 0, /* todo_flags_start */
619 TODO_dump_func
| TODO_verify_loops
/* todo_flags_finish */
626 tree_ssa_loop_prefetch (void)
628 if (number_of_loops () <= 1)
631 return tree_ssa_prefetch_arrays ();
635 gate_tree_ssa_loop_prefetch (void)
637 return flag_prefetch_loop_arrays
!= 0;
640 struct gimple_opt_pass pass_loop_prefetch
=
644 "aprefetch", /* name */
645 gate_tree_ssa_loop_prefetch
, /* gate */
646 tree_ssa_loop_prefetch
, /* execute */
649 0, /* static_pass_number */
650 TV_TREE_PREFETCH
, /* tv_id */
651 PROP_cfg
| PROP_ssa
, /* properties_required */
652 0, /* properties_provided */
653 0, /* properties_destroyed */
654 0, /* todo_flags_start */
655 TODO_dump_func
| TODO_verify_loops
/* todo_flags_finish */
659 /* Induction variable optimizations. */
662 tree_ssa_loop_ivopts (void)
664 if (number_of_loops () <= 1)
667 tree_ssa_iv_optimize ();
672 gate_tree_ssa_loop_ivopts (void)
674 return flag_ivopts
!= 0;
677 struct gimple_opt_pass pass_iv_optimize
=
682 gate_tree_ssa_loop_ivopts
, /* gate */
683 tree_ssa_loop_ivopts
, /* execute */
686 0, /* static_pass_number */
687 TV_TREE_LOOP_IVOPTS
, /* tv_id */
688 PROP_cfg
| PROP_ssa
, /* properties_required */
689 0, /* properties_provided */
690 0, /* properties_destroyed */
691 0, /* todo_flags_start */
692 TODO_dump_func
| TODO_verify_loops
693 | TODO_update_ssa
| TODO_ggc_collect
/* todo_flags_finish */
697 /* Loop optimizer finalization. */
700 tree_ssa_loop_done (void)
702 free_numbers_of_iterations_estimates ();
704 loop_optimizer_finalize ();
708 struct gimple_opt_pass pass_tree_loop_done
=
712 "loopdone", /* name */
714 tree_ssa_loop_done
, /* execute */
717 0, /* static_pass_number */
718 TV_TREE_LOOP_FINI
, /* tv_id */
719 PROP_cfg
, /* properties_required */
720 0, /* properties_provided */
721 0, /* properties_destroyed */
722 0, /* todo_flags_start */
723 TODO_cleanup_cfg
| TODO_dump_func
/* todo_flags_finish */