PR target/38900
[official-gcc.git] / gcc / tree-ssa-loop.c
blob0aab6d3a2d3d69da0954dc621c9ac31767890c14
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
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 "rtl.h"
26 #include "tm_p.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "output.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "tree-pass.h"
34 #include "timevar.h"
35 #include "cfgloop.h"
36 #include "flags.h"
37 #include "tree-inline.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-vectorizer.h"
41 /* The loop superpass. */
43 static bool
44 gate_tree_loop (void)
46 return flag_tree_loop_optimize != 0;
49 struct gimple_opt_pass pass_tree_loop =
52 GIMPLE_PASS,
53 "loop", /* name */
54 gate_tree_loop, /* gate */
55 NULL, /* execute */
56 NULL, /* sub */
57 NULL, /* next */
58 0, /* static_pass_number */
59 TV_TREE_LOOP, /* tv_id */
60 PROP_cfg, /* properties_required */
61 0, /* properties_provided */
62 0, /* properties_destroyed */
63 TODO_ggc_collect, /* todo_flags_start */
64 TODO_dump_func | TODO_verify_ssa | TODO_ggc_collect /* todo_flags_finish */
68 /* Loop optimizer initialization. */
70 static unsigned int
71 tree_ssa_loop_init (void)
73 loop_optimizer_init (LOOPS_NORMAL
74 | LOOPS_HAVE_RECORDED_EXITS);
75 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
77 if (number_of_loops () <= 1)
78 return 0;
80 scev_initialize ();
81 return 0;
84 struct gimple_opt_pass pass_tree_loop_init =
87 GIMPLE_PASS,
88 "loopinit", /* name */
89 NULL, /* gate */
90 tree_ssa_loop_init, /* execute */
91 NULL, /* sub */
92 NULL, /* next */
93 0, /* static_pass_number */
94 TV_TREE_LOOP_INIT, /* tv_id */
95 PROP_cfg, /* properties_required */
96 0, /* properties_provided */
97 0, /* properties_destroyed */
98 0, /* todo_flags_start */
99 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
103 /* Loop invariant motion pass. */
105 static unsigned int
106 tree_ssa_loop_im (void)
108 if (number_of_loops () <= 1)
109 return 0;
111 tree_ssa_lim ();
112 return 0;
115 static bool
116 gate_tree_ssa_loop_im (void)
118 return flag_tree_loop_im != 0;
121 struct gimple_opt_pass pass_lim =
124 GIMPLE_PASS,
125 "lim", /* name */
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 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
140 /* Loop unswitching pass. */
142 static unsigned int
143 tree_ssa_loop_unswitch (void)
145 if (number_of_loops () <= 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 gate_tree_ssa_loop_unswitch, /* gate */
163 tree_ssa_loop_unswitch, /* execute */
164 NULL, /* sub */
165 NULL, /* next */
166 0, /* static_pass_number */
167 TV_TREE_LOOP_UNSWITCH, /* tv_id */
168 PROP_cfg, /* properties_required */
169 0, /* properties_provided */
170 0, /* properties_destroyed */
171 0, /* todo_flags_start */
172 TODO_ggc_collect | TODO_dump_func
173 | TODO_verify_loops /* todo_flags_finish */
177 /* Predictive commoning. */
179 static unsigned
180 run_tree_predictive_commoning (void)
182 if (!current_loops)
183 return 0;
185 tree_predictive_commoning ();
186 return 0;
189 static bool
190 gate_tree_predictive_commoning (void)
192 return flag_predictive_commoning != 0;
195 struct gimple_opt_pass pass_predcom =
198 GIMPLE_PASS,
199 "pcom", /* name */
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_dump_func | TODO_verify_loops
211 | TODO_update_ssa_only_virtuals /* todo_flags_finish */
215 /* Loop autovectorization. */
217 static unsigned int
218 tree_vectorize (void)
220 if (number_of_loops () <= 1)
221 return 0;
223 return vectorize_loops ();
226 static bool
227 gate_tree_vectorize (void)
229 return flag_tree_vectorize;
232 struct gimple_opt_pass pass_vectorize =
235 GIMPLE_PASS,
236 "vect", /* name */
237 gate_tree_vectorize, /* gate */
238 tree_vectorize, /* execute */
239 NULL, /* sub */
240 NULL, /* next */
241 0, /* static_pass_number */
242 TV_TREE_VECTORIZATION, /* tv_id */
243 PROP_cfg | PROP_ssa, /* properties_required */
244 0, /* properties_provided */
245 0, /* properties_destroyed */
246 TODO_verify_loops, /* todo_flags_start */
247 TODO_dump_func | TODO_update_ssa
248 | TODO_ggc_collect /* todo_flags_finish */
252 /* Loop nest optimizations. */
254 static unsigned int
255 tree_linear_transform (void)
257 if (number_of_loops () <= 1)
258 return 0;
260 linear_transform_loops ();
261 return 0;
264 static bool
265 gate_tree_linear_transform (void)
267 return flag_tree_loop_linear != 0;
270 struct gimple_opt_pass pass_linear_transform =
273 GIMPLE_PASS,
274 "ltrans", /* name */
275 gate_tree_linear_transform, /* gate */
276 tree_linear_transform, /* execute */
277 NULL, /* sub */
278 NULL, /* next */
279 0, /* static_pass_number */
280 TV_TREE_LINEAR_TRANSFORM, /* 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
286 | TODO_update_ssa_only_virtuals
287 | TODO_ggc_collect /* todo_flags_finish */
291 /* GRAPHITE optimizations. */
293 static unsigned int
294 graphite_transforms (void)
296 if (!current_loops)
297 return 0;
299 graphite_transform_loops ();
301 return 0;
304 static bool
305 gate_graphite_transforms (void)
307 /* Enable -fgraphite pass if any one of the graphite optimization flags
308 is turned on. */
309 if (flag_loop_block || flag_loop_interchange || flag_loop_strip_mine
310 || flag_graphite_identity)
311 flag_graphite = 1;
313 return flag_graphite != 0;
316 struct gimple_opt_pass pass_graphite_transforms =
319 GIMPLE_PASS,
320 "graphite", /* name */
321 gate_graphite_transforms, /* gate */
322 graphite_transforms, /* execute */
323 NULL, /* sub */
324 NULL, /* next */
325 0, /* static_pass_number */
326 TV_GRAPHITE_TRANSFORMS, /* tv_id */
327 PROP_cfg | PROP_ssa, /* properties_required */
328 0, /* properties_provided */
329 0, /* properties_destroyed */
330 0, /* todo_flags_start */
331 TODO_verify_loops /* todo_flags_finish */
335 /* Check the correctness of the data dependence analyzers. */
337 static unsigned int
338 check_data_deps (void)
340 if (number_of_loops () <= 1)
341 return 0;
343 tree_check_data_deps ();
344 return 0;
347 static bool
348 gate_check_data_deps (void)
350 return flag_check_data_deps != 0;
353 struct gimple_opt_pass pass_check_data_deps =
356 GIMPLE_PASS,
357 "ckdd", /* name */
358 gate_check_data_deps, /* gate */
359 check_data_deps, /* execute */
360 NULL, /* sub */
361 NULL, /* next */
362 0, /* static_pass_number */
363 TV_CHECK_DATA_DEPS, /* tv_id */
364 PROP_cfg | PROP_ssa, /* properties_required */
365 0, /* properties_provided */
366 0, /* properties_destroyed */
367 0, /* todo_flags_start */
368 TODO_dump_func /* todo_flags_finish */
372 /* Canonical induction variable creation pass. */
374 static unsigned int
375 tree_ssa_loop_ivcanon (void)
377 if (number_of_loops () <= 1)
378 return 0;
380 return canonicalize_induction_variables ();
383 static bool
384 gate_tree_ssa_loop_ivcanon (void)
386 return flag_tree_loop_ivcanon != 0;
389 struct gimple_opt_pass pass_iv_canon =
392 GIMPLE_PASS,
393 "ivcanon", /* name */
394 gate_tree_ssa_loop_ivcanon, /* gate */
395 tree_ssa_loop_ivcanon, /* execute */
396 NULL, /* sub */
397 NULL, /* next */
398 0, /* static_pass_number */
399 TV_TREE_LOOP_IVCANON, /* tv_id */
400 PROP_cfg | PROP_ssa, /* properties_required */
401 0, /* properties_provided */
402 0, /* properties_destroyed */
403 0, /* todo_flags_start */
404 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
408 /* Propagation of constants using scev. */
410 static bool
411 gate_scev_const_prop (void)
413 return flag_tree_scev_cprop;
416 struct gimple_opt_pass pass_scev_cprop =
419 GIMPLE_PASS,
420 "sccp", /* name */
421 gate_scev_const_prop, /* gate */
422 scev_const_prop, /* execute */
423 NULL, /* sub */
424 NULL, /* next */
425 0, /* static_pass_number */
426 TV_SCEV_CONST, /* tv_id */
427 PROP_cfg | PROP_ssa, /* properties_required */
428 0, /* properties_provided */
429 0, /* properties_destroyed */
430 0, /* todo_flags_start */
431 TODO_dump_func | TODO_cleanup_cfg
432 | TODO_update_ssa_only_virtuals
433 /* todo_flags_finish */
437 /* Record bounds on numbers of iterations of loops. */
439 static unsigned int
440 tree_ssa_loop_bounds (void)
442 if (number_of_loops () <= 1)
443 return 0;
445 estimate_numbers_of_iterations ();
446 scev_reset ();
447 return 0;
450 struct gimple_opt_pass pass_record_bounds =
453 GIMPLE_PASS,
454 NULL, /* name */
455 NULL, /* gate */
456 tree_ssa_loop_bounds, /* execute */
457 NULL, /* sub */
458 NULL, /* next */
459 0, /* static_pass_number */
460 TV_TREE_LOOP_BOUNDS, /* tv_id */
461 PROP_cfg | PROP_ssa, /* properties_required */
462 0, /* properties_provided */
463 0, /* properties_destroyed */
464 0, /* todo_flags_start */
465 0 /* todo_flags_finish */
469 /* Complete unrolling of loops. */
471 static unsigned int
472 tree_complete_unroll (void)
474 if (number_of_loops () <= 1)
475 return 0;
477 return tree_unroll_loops_completely (flag_unroll_loops
478 || flag_peel_loops
479 || optimize >= 3, true);
482 static bool
483 gate_tree_complete_unroll (void)
485 return true;
488 struct gimple_opt_pass pass_complete_unroll =
491 GIMPLE_PASS,
492 "cunroll", /* name */
493 gate_tree_complete_unroll, /* gate */
494 tree_complete_unroll, /* execute */
495 NULL, /* sub */
496 NULL, /* next */
497 0, /* static_pass_number */
498 TV_COMPLETE_UNROLL, /* tv_id */
499 PROP_cfg | PROP_ssa, /* properties_required */
500 0, /* properties_provided */
501 0, /* properties_destroyed */
502 0, /* todo_flags_start */
503 TODO_dump_func | TODO_verify_loops
504 | TODO_ggc_collect /* todo_flags_finish */
508 /* Complete unrolling of inner loops. */
510 static unsigned int
511 tree_complete_unroll_inner (void)
513 unsigned ret = 0;
515 loop_optimizer_init (LOOPS_NORMAL
516 | LOOPS_HAVE_RECORDED_EXITS);
517 if (number_of_loops () > 1)
519 scev_initialize ();
520 ret = tree_unroll_loops_completely (optimize >= 3, false);
521 free_numbers_of_iterations_estimates ();
522 scev_finalize ();
524 loop_optimizer_finalize ();
526 return ret;
529 static bool
530 gate_tree_complete_unroll_inner (void)
532 return optimize >= 2;
535 struct gimple_opt_pass pass_complete_unrolli =
538 GIMPLE_PASS,
539 "cunrolli", /* name */
540 gate_tree_complete_unroll_inner, /* gate */
541 tree_complete_unroll_inner, /* execute */
542 NULL, /* sub */
543 NULL, /* next */
544 0, /* static_pass_number */
545 TV_COMPLETE_UNROLL, /* tv_id */
546 PROP_cfg | PROP_ssa, /* properties_required */
547 0, /* properties_provided */
548 0, /* properties_destroyed */
549 0, /* todo_flags_start */
550 TODO_dump_func | TODO_verify_loops
551 | TODO_ggc_collect /* todo_flags_finish */
555 /* Parallelization. */
557 static bool
558 gate_tree_parallelize_loops (void)
560 return flag_tree_parallelize_loops > 1;
563 static unsigned
564 tree_parallelize_loops (void)
566 if (number_of_loops () <= 1)
567 return 0;
569 if (parallelize_loops ())
570 return TODO_cleanup_cfg | TODO_rebuild_alias;
571 return 0;
574 struct gimple_opt_pass pass_parallelize_loops =
577 GIMPLE_PASS,
578 "parloops", /* name */
579 gate_tree_parallelize_loops, /* gate */
580 tree_parallelize_loops, /* execute */
581 NULL, /* sub */
582 NULL, /* next */
583 0, /* static_pass_number */
584 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
585 PROP_cfg | PROP_ssa, /* properties_required */
586 0, /* properties_provided */
587 0, /* properties_destroyed */
588 0, /* todo_flags_start */
589 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
593 /* Prefetching. */
595 static unsigned int
596 tree_ssa_loop_prefetch (void)
598 if (number_of_loops () <= 1)
599 return 0;
601 return tree_ssa_prefetch_arrays ();
604 static bool
605 gate_tree_ssa_loop_prefetch (void)
607 return flag_prefetch_loop_arrays != 0;
610 struct gimple_opt_pass pass_loop_prefetch =
613 GIMPLE_PASS,
614 "aprefetch", /* name */
615 gate_tree_ssa_loop_prefetch, /* gate */
616 tree_ssa_loop_prefetch, /* execute */
617 NULL, /* sub */
618 NULL, /* next */
619 0, /* static_pass_number */
620 TV_TREE_PREFETCH, /* tv_id */
621 PROP_cfg | PROP_ssa, /* properties_required */
622 0, /* properties_provided */
623 0, /* properties_destroyed */
624 0, /* todo_flags_start */
625 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
629 /* Induction variable optimizations. */
631 static unsigned int
632 tree_ssa_loop_ivopts (void)
634 if (number_of_loops () <= 1)
635 return 0;
637 tree_ssa_iv_optimize ();
638 return 0;
641 static bool
642 gate_tree_ssa_loop_ivopts (void)
644 return flag_ivopts != 0;
647 struct gimple_opt_pass pass_iv_optimize =
650 GIMPLE_PASS,
651 "ivopts", /* name */
652 gate_tree_ssa_loop_ivopts, /* gate */
653 tree_ssa_loop_ivopts, /* execute */
654 NULL, /* sub */
655 NULL, /* next */
656 0, /* static_pass_number */
657 TV_TREE_LOOP_IVOPTS, /* tv_id */
658 PROP_cfg | PROP_ssa, /* properties_required */
659 0, /* properties_provided */
660 0, /* properties_destroyed */
661 0, /* todo_flags_start */
662 TODO_dump_func | TODO_verify_loops
663 | TODO_update_ssa | TODO_ggc_collect /* todo_flags_finish */
667 /* Loop optimizer finalization. */
669 static unsigned int
670 tree_ssa_loop_done (void)
672 free_numbers_of_iterations_estimates ();
673 scev_finalize ();
674 loop_optimizer_finalize ();
675 return 0;
678 struct gimple_opt_pass pass_tree_loop_done =
681 GIMPLE_PASS,
682 "loopdone", /* name */
683 NULL, /* gate */
684 tree_ssa_loop_done, /* execute */
685 NULL, /* sub */
686 NULL, /* next */
687 0, /* static_pass_number */
688 TV_TREE_LOOP_FINI, /* tv_id */
689 PROP_cfg, /* properties_required */
690 0, /* properties_provided */
691 0, /* properties_destroyed */
692 0, /* todo_flags_start */
693 TODO_cleanup_cfg | TODO_dump_func /* todo_flags_finish */