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"
26 #include "basic-block.h"
28 #include "tree-flow.h"
29 #include "tree-dump.h"
30 #include "tree-pass.h"
34 #include "tree-inline.h"
35 #include "tree-scalar-evolution.h"
36 #include "diagnostic-core.h"
38 #include "tree-vectorizer.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_flags_finish */
102 /* Loop invariant motion pass. */
105 tree_ssa_loop_im (void)
107 if (number_of_loops () <= 1)
110 return tree_ssa_lim ();
114 gate_tree_ssa_loop_im (void)
116 return flag_tree_loop_im != 0;
119 struct gimple_opt_pass pass_lim =
124 gate_tree_ssa_loop_im, /* gate */
125 tree_ssa_loop_im, /* execute */
128 0, /* static_pass_number */
130 PROP_cfg, /* properties_required */
131 0, /* properties_provided */
132 0, /* properties_destroyed */
133 0, /* todo_flags_start */
134 TODO_dump_func /* todo_flags_finish */
138 /* Loop unswitching pass. */
141 tree_ssa_loop_unswitch (void)
143 if (number_of_loops () <= 1)
146 return tree_ssa_unswitch_loops ();
150 gate_tree_ssa_loop_unswitch (void)
152 return flag_unswitch_loops != 0;
155 struct gimple_opt_pass pass_tree_unswitch =
159 "unswitch", /* name */
160 gate_tree_ssa_loop_unswitch, /* gate */
161 tree_ssa_loop_unswitch, /* execute */
164 0, /* static_pass_number */
165 TV_TREE_LOOP_UNSWITCH, /* tv_id */
166 PROP_cfg, /* properties_required */
167 0, /* properties_provided */
168 0, /* properties_destroyed */
169 0, /* todo_flags_start */
170 TODO_ggc_collect | TODO_dump_func /* todo_flags_finish */
174 /* Predictive commoning. */
177 run_tree_predictive_commoning (void)
182 tree_predictive_commoning ();
187 gate_tree_predictive_commoning (void)
189 return flag_predictive_commoning != 0;
192 struct gimple_opt_pass pass_predcom =
197 gate_tree_predictive_commoning, /* gate */
198 run_tree_predictive_commoning, /* execute */
201 0, /* static_pass_number */
202 TV_PREDCOM, /* tv_id */
203 PROP_cfg, /* properties_required */
204 0, /* properties_provided */
205 0, /* properties_destroyed */
206 0, /* todo_flags_start */
208 | TODO_update_ssa_only_virtuals /* todo_flags_finish */
212 /* Loop autovectorization. */
215 tree_vectorize (void)
217 if (number_of_loops () <= 1)
220 return vectorize_loops ();
224 gate_tree_vectorize (void)
226 return flag_tree_vectorize;
229 struct gimple_opt_pass pass_vectorize =
234 gate_tree_vectorize, /* gate */
235 tree_vectorize, /* execute */
238 0, /* static_pass_number */
239 TV_TREE_VECTORIZATION, /* tv_id */
240 PROP_cfg | PROP_ssa, /* properties_required */
241 0, /* properties_provided */
242 0, /* properties_destroyed */
243 0, /* todo_flags_start */
244 TODO_dump_func | TODO_update_ssa
245 | TODO_ggc_collect /* todo_flags_finish */
249 /* Loop nest optimizations. */
252 tree_linear_transform (void)
254 if (number_of_loops () <= 1)
257 linear_transform_loops ();
262 gate_tree_linear_transform (void)
264 return flag_tree_loop_linear != 0;
267 struct gimple_opt_pass pass_linear_transform =
272 gate_tree_linear_transform, /* gate */
273 tree_linear_transform, /* execute */
276 0, /* static_pass_number */
277 TV_TREE_LINEAR_TRANSFORM, /* tv_id */
278 PROP_cfg | PROP_ssa, /* properties_required */
279 0, /* properties_provided */
280 0, /* properties_destroyed */
281 0, /* todo_flags_start */
283 | TODO_update_ssa_only_virtuals
284 | TODO_ggc_collect /* todo_flags_finish */
288 /* GRAPHITE optimizations. */
291 graphite_transforms (void)
296 graphite_transform_loops ();
302 gate_graphite_transforms (void)
304 /* Enable -fgraphite pass if any one of the graphite optimization flags
306 if (flag_loop_block || flag_loop_interchange || flag_loop_strip_mine
307 || flag_graphite_identity || flag_loop_parallelize_all)
310 return flag_graphite != 0;
313 struct gimple_opt_pass pass_graphite_transforms =
317 "graphite", /* name */
318 gate_graphite_transforms, /* gate */
319 graphite_transforms, /* execute */
322 0, /* static_pass_number */
323 TV_GRAPHITE_TRANSFORMS, /* tv_id */
324 PROP_cfg | PROP_ssa, /* properties_required */
325 0, /* properties_provided */
326 0, /* properties_destroyed */
327 0, /* todo_flags_start */
328 0 /* todo_flags_finish */
332 /* Check the correctness of the data dependence analyzers. */
335 check_data_deps (void)
337 if (number_of_loops () <= 1)
340 tree_check_data_deps ();
345 gate_check_data_deps (void)
347 return flag_check_data_deps != 0;
350 struct gimple_opt_pass pass_check_data_deps =
355 gate_check_data_deps, /* gate */
356 check_data_deps, /* execute */
359 0, /* static_pass_number */
360 TV_CHECK_DATA_DEPS, /* tv_id */
361 PROP_cfg | PROP_ssa, /* properties_required */
362 0, /* properties_provided */
363 0, /* properties_destroyed */
364 0, /* todo_flags_start */
365 TODO_dump_func /* todo_flags_finish */
369 /* Canonical induction variable creation pass. */
372 tree_ssa_loop_ivcanon (void)
374 if (number_of_loops () <= 1)
377 return canonicalize_induction_variables ();
381 gate_tree_ssa_loop_ivcanon (void)
383 return flag_tree_loop_ivcanon != 0;
386 struct gimple_opt_pass pass_iv_canon =
390 "ivcanon", /* name */
391 gate_tree_ssa_loop_ivcanon, /* gate */
392 tree_ssa_loop_ivcanon, /* execute */
395 0, /* static_pass_number */
396 TV_TREE_LOOP_IVCANON, /* tv_id */
397 PROP_cfg | PROP_ssa, /* properties_required */
398 0, /* properties_provided */
399 0, /* properties_destroyed */
400 0, /* todo_flags_start */
401 TODO_dump_func /* todo_flags_finish */
405 /* Propagation of constants using scev. */
408 gate_scev_const_prop (void)
410 return flag_tree_scev_cprop;
413 struct gimple_opt_pass pass_scev_cprop =
418 gate_scev_const_prop, /* gate */
419 scev_const_prop, /* execute */
422 0, /* static_pass_number */
423 TV_SCEV_CONST, /* tv_id */
424 PROP_cfg | PROP_ssa, /* properties_required */
425 0, /* properties_provided */
426 0, /* properties_destroyed */
427 0, /* todo_flags_start */
428 TODO_dump_func | TODO_cleanup_cfg
429 | TODO_update_ssa_only_virtuals
430 /* todo_flags_finish */
434 /* Record bounds on numbers of iterations of loops. */
437 tree_ssa_loop_bounds (void)
439 if (number_of_loops () <= 1)
442 estimate_numbers_of_iterations ();
447 struct gimple_opt_pass pass_record_bounds =
451 "*record_bounds", /* name */
453 tree_ssa_loop_bounds, /* execute */
456 0, /* static_pass_number */
457 TV_TREE_LOOP_BOUNDS, /* tv_id */
458 PROP_cfg | PROP_ssa, /* properties_required */
459 0, /* properties_provided */
460 0, /* properties_destroyed */
461 0, /* todo_flags_start */
462 0 /* todo_flags_finish */
466 /* Complete unrolling of loops. */
469 tree_complete_unroll (void)
471 if (number_of_loops () <= 1)
474 return tree_unroll_loops_completely (flag_unroll_loops
476 || optimize >= 3, true);
480 gate_tree_complete_unroll (void)
485 struct gimple_opt_pass pass_complete_unroll =
489 "cunroll", /* name */
490 gate_tree_complete_unroll, /* gate */
491 tree_complete_unroll, /* execute */
494 0, /* static_pass_number */
495 TV_COMPLETE_UNROLL, /* tv_id */
496 PROP_cfg | PROP_ssa, /* properties_required */
497 0, /* properties_provided */
498 0, /* properties_destroyed */
499 0, /* todo_flags_start */
501 | TODO_ggc_collect /* todo_flags_finish */
505 /* Complete unrolling of inner loops. */
508 tree_complete_unroll_inner (void)
512 loop_optimizer_init (LOOPS_NORMAL
513 | LOOPS_HAVE_RECORDED_EXITS);
514 if (number_of_loops () > 1)
517 ret = tree_unroll_loops_completely (optimize >= 3, false);
518 free_numbers_of_iterations_estimates ();
521 loop_optimizer_finalize ();
527 gate_tree_complete_unroll_inner (void)
529 return optimize >= 2;
532 struct gimple_opt_pass pass_complete_unrolli =
536 "cunrolli", /* name */
537 gate_tree_complete_unroll_inner, /* gate */
538 tree_complete_unroll_inner, /* execute */
541 0, /* static_pass_number */
542 TV_COMPLETE_UNROLL, /* tv_id */
543 PROP_cfg | PROP_ssa, /* properties_required */
544 0, /* properties_provided */
545 0, /* properties_destroyed */
546 0, /* todo_flags_start */
548 | TODO_ggc_collect /* todo_flags_finish */
552 /* Parallelization. */
555 gate_tree_parallelize_loops (void)
557 return flag_tree_parallelize_loops > 1;
561 tree_parallelize_loops (void)
563 if (number_of_loops () <= 1)
566 if (parallelize_loops ())
567 return TODO_cleanup_cfg | TODO_rebuild_alias;
571 struct gimple_opt_pass pass_parallelize_loops =
575 "parloops", /* name */
576 gate_tree_parallelize_loops, /* gate */
577 tree_parallelize_loops, /* execute */
580 0, /* static_pass_number */
581 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
582 PROP_cfg | PROP_ssa, /* properties_required */
583 0, /* properties_provided */
584 0, /* properties_destroyed */
585 0, /* todo_flags_start */
586 TODO_dump_func /* todo_flags_finish */
593 tree_ssa_loop_prefetch (void)
595 if (number_of_loops () <= 1)
598 return tree_ssa_prefetch_arrays ();
602 gate_tree_ssa_loop_prefetch (void)
604 return flag_prefetch_loop_arrays > 0;
607 struct gimple_opt_pass pass_loop_prefetch =
611 "aprefetch", /* name */
612 gate_tree_ssa_loop_prefetch, /* gate */
613 tree_ssa_loop_prefetch, /* execute */
616 0, /* static_pass_number */
617 TV_TREE_PREFETCH, /* tv_id */
618 PROP_cfg | PROP_ssa, /* properties_required */
619 0, /* properties_provided */
620 0, /* properties_destroyed */
621 0, /* todo_flags_start */
622 TODO_dump_func /* todo_flags_finish */
626 /* Induction variable optimizations. */
629 tree_ssa_loop_ivopts (void)
631 if (number_of_loops () <= 1)
634 tree_ssa_iv_optimize ();
639 gate_tree_ssa_loop_ivopts (void)
641 return flag_ivopts != 0;
644 struct gimple_opt_pass pass_iv_optimize =
649 gate_tree_ssa_loop_ivopts, /* gate */
650 tree_ssa_loop_ivopts, /* execute */
653 0, /* static_pass_number */
654 TV_TREE_LOOP_IVOPTS, /* tv_id */
655 PROP_cfg | PROP_ssa, /* properties_required */
656 0, /* properties_provided */
657 0, /* properties_destroyed */
658 0, /* todo_flags_start */
659 TODO_dump_func | TODO_update_ssa | TODO_ggc_collect /* todo_flags_finish */
663 /* Loop optimizer finalization. */
666 tree_ssa_loop_done (void)
668 free_numbers_of_iterations_estimates ();
670 loop_optimizer_finalize ();
674 struct gimple_opt_pass pass_tree_loop_done =
678 "loopdone", /* name */
680 tree_ssa_loop_done, /* execute */
683 0, /* static_pass_number */
684 TV_TREE_LOOP_FINI, /* tv_id */
685 PROP_cfg, /* properties_required */
686 0, /* properties_provided */
687 0, /* properties_destroyed */
688 0, /* todo_flags_start */
689 TODO_cleanup_cfg | TODO_dump_func /* todo_flags_finish */