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
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"
26 #include "basic-block.h"
27 #include "tree-flow.h"
28 #include "tree-pass.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. */
41 return flag_tree_loop_optimize
!= 0;
44 struct gimple_opt_pass pass_tree_loop
=
49 OPTGROUP_LOOP
, /* optinfo_flags */
50 gate_tree_loop
, /* gate */
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. */
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. */
77 if (number_of_loops (cfun
) <= 1)
83 struct gimple_opt_pass pass_tree_loop_init
=
87 "loopinit", /* name */
88 OPTGROUP_LOOP
, /* optinfo_flags */
90 tree_ssa_loop_init
, /* execute */
93 0, /* static_pass_number */
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. */
106 tree_ssa_loop_im (void)
108 if (number_of_loops (cfun
) <= 1)
111 return tree_ssa_lim ();
115 gate_tree_ssa_loop_im (void)
117 return flag_tree_loop_im
!= 0;
120 struct gimple_opt_pass pass_lim
=
125 OPTGROUP_LOOP
, /* optinfo_flags */
126 gate_tree_ssa_loop_im
, /* gate */
127 tree_ssa_loop_im
, /* execute */
130 0, /* static_pass_number */
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. */
143 tree_ssa_loop_unswitch (void)
145 if (number_of_loops (cfun
) <= 1)
148 return tree_ssa_unswitch_loops ();
152 gate_tree_ssa_loop_unswitch (void)
154 return flag_unswitch_loops
!= 0;
157 struct gimple_opt_pass pass_tree_unswitch
=
161 "unswitch", /* name */
162 OPTGROUP_LOOP
, /* optinfo_flags */
163 gate_tree_ssa_loop_unswitch
, /* gate */
164 tree_ssa_loop_unswitch
, /* execute */
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. */
180 run_tree_predictive_commoning (void)
185 return tree_predictive_commoning ();
189 gate_tree_predictive_commoning (void)
191 return flag_predictive_commoning
!= 0;
194 struct gimple_opt_pass pass_predcom
=
199 OPTGROUP_LOOP
, /* optinfo_flags */
200 gate_tree_predictive_commoning
, /* gate */
201 run_tree_predictive_commoning
, /* execute */
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. */
217 tree_vectorize (void)
219 if (number_of_loops (cfun
) <= 1)
222 return vectorize_loops ();
226 gate_tree_vectorize (void)
228 return flag_tree_vectorize
;
231 struct gimple_opt_pass pass_vectorize
=
237 | OPTGROUP_VEC
, /* optinfo_flags */
238 gate_tree_vectorize
, /* gate */
239 tree_vectorize
, /* execute */
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. */
255 graphite_transforms (void)
260 graphite_transform_loops ();
266 gate_graphite_transforms (void)
268 /* Enable -fgraphite pass if any one of the graphite optimization flags
271 || flag_loop_interchange
272 || flag_loop_strip_mine
273 || flag_graphite_identity
274 || flag_loop_parallelize_all
275 || flag_loop_optimize_isl
)
278 return flag_graphite
!= 0;
281 struct gimple_opt_pass pass_graphite
=
285 "graphite0", /* name */
286 OPTGROUP_LOOP
, /* optinfo_flags */
287 gate_graphite_transforms
, /* gate */
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
=
305 "graphite", /* name */
306 OPTGROUP_LOOP
, /* optinfo_flags */
307 gate_graphite_transforms
, /* gate */
308 graphite_transforms
, /* execute */
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. */
324 check_data_deps (void)
326 if (number_of_loops (cfun
) <= 1)
329 tree_check_data_deps ();
334 gate_check_data_deps (void)
336 return flag_check_data_deps
!= 0;
339 struct gimple_opt_pass pass_check_data_deps
=
344 OPTGROUP_LOOP
, /* optinfo_flags */
345 gate_check_data_deps
, /* gate */
346 check_data_deps
, /* execute */
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. */
362 tree_ssa_loop_ivcanon (void)
364 if (number_of_loops (cfun
) <= 1)
367 return canonicalize_induction_variables ();
371 gate_tree_ssa_loop_ivcanon (void)
373 return flag_tree_loop_ivcanon
!= 0;
376 struct gimple_opt_pass pass_iv_canon
=
380 "ivcanon", /* name */
381 OPTGROUP_LOOP
, /* optinfo_flags */
382 gate_tree_ssa_loop_ivcanon
, /* gate */
383 tree_ssa_loop_ivcanon
, /* execute */
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. */
399 gate_scev_const_prop (void)
401 return flag_tree_scev_cprop
;
404 struct gimple_opt_pass pass_scev_cprop
=
409 OPTGROUP_LOOP
, /* optinfo_flags */
410 gate_scev_const_prop
, /* gate */
411 scev_const_prop
, /* execute */
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 */
421 | TODO_update_ssa_only_virtuals
422 /* todo_flags_finish */
426 /* Record bounds on numbers of iterations of loops. */
429 tree_ssa_loop_bounds (void)
431 if (number_of_loops (cfun
) <= 1)
434 estimate_numbers_of_iterations ();
439 struct gimple_opt_pass pass_record_bounds
=
443 "*record_bounds", /* name */
444 OPTGROUP_NONE
, /* optinfo_flags */
446 tree_ssa_loop_bounds
, /* execute */
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. */
462 tree_complete_unroll (void)
464 if (number_of_loops (cfun
) <= 1)
467 return tree_unroll_loops_completely (flag_unroll_loops
469 || optimize
>= 3, true);
473 gate_tree_complete_unroll (void)
478 struct gimple_opt_pass pass_complete_unroll
=
482 "cunroll", /* name */
483 OPTGROUP_LOOP
, /* optinfo_flags */
484 gate_tree_complete_unroll
, /* gate */
485 tree_complete_unroll
, /* execute */
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. */
501 tree_complete_unroll_inner (void)
505 loop_optimizer_init (LOOPS_NORMAL
506 | LOOPS_HAVE_RECORDED_EXITS
);
507 if (number_of_loops (cfun
) > 1)
510 ret
= tree_unroll_loops_completely (optimize
>= 3, false);
511 free_numbers_of_iterations_estimates ();
514 loop_optimizer_finalize ();
520 gate_tree_complete_unroll_inner (void)
522 return optimize
>= 2;
525 struct gimple_opt_pass pass_complete_unrolli
=
529 "cunrolli", /* name */
530 OPTGROUP_LOOP
, /* optinfo_flags */
531 gate_tree_complete_unroll_inner
, /* gate */
532 tree_complete_unroll_inner
, /* execute */
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. */
548 gate_tree_parallelize_loops (void)
550 return flag_tree_parallelize_loops
> 1;
554 tree_parallelize_loops (void)
556 if (number_of_loops (cfun
) <= 1)
559 if (parallelize_loops ())
560 return TODO_cleanup_cfg
| TODO_rebuild_alias
;
564 struct gimple_opt_pass pass_parallelize_loops
=
568 "parloops", /* name */
569 OPTGROUP_LOOP
, /* optinfo_flags */
570 gate_tree_parallelize_loops
, /* gate */
571 tree_parallelize_loops
, /* execute */
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 */
587 tree_ssa_loop_prefetch (void)
589 if (number_of_loops (cfun
) <= 1)
592 return tree_ssa_prefetch_arrays ();
596 gate_tree_ssa_loop_prefetch (void)
598 return flag_prefetch_loop_arrays
> 0;
601 struct gimple_opt_pass pass_loop_prefetch
=
605 "aprefetch", /* name */
606 OPTGROUP_LOOP
, /* optinfo_flags */
607 gate_tree_ssa_loop_prefetch
, /* gate */
608 tree_ssa_loop_prefetch
, /* execute */
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. */
624 tree_ssa_loop_ivopts (void)
626 if (number_of_loops (cfun
) <= 1)
629 tree_ssa_iv_optimize ();
634 gate_tree_ssa_loop_ivopts (void)
636 return flag_ivopts
!= 0;
639 struct gimple_opt_pass pass_iv_optimize
=
644 OPTGROUP_LOOP
, /* optinfo_flags */
645 gate_tree_ssa_loop_ivopts
, /* gate */
646 tree_ssa_loop_ivopts
, /* execute */
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. */
662 tree_ssa_loop_done (void)
664 free_numbers_of_iterations_estimates ();
666 loop_optimizer_finalize ();
670 struct gimple_opt_pass pass_tree_loop_done
=
674 "loopdone", /* name */
675 OPTGROUP_LOOP
, /* optinfo_flags */
677 tree_ssa_loop_done
, /* execute */
680 0, /* static_pass_number */
682 PROP_cfg
, /* properties_required */
683 0, /* properties_provided */
684 0, /* properties_destroyed */
685 0, /* todo_flags_start */
687 | TODO_verify_flow
/* todo_flags_finish */