inclhack.def (hpux_imaginary_i): Remove spaces.
[gcc.git] / gcc / tree-ssa-loop.c
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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 "toplev.h"
40 #include "tree-vectorizer.h"
41
42 /* The loop superpass. */
43
44 static bool
45 gate_tree_loop (void)
46 {
47 return flag_tree_loop_optimize != 0;
48 }
49
50 struct gimple_opt_pass pass_tree_loop =
51 {
52 {
53 GIMPLE_PASS,
54 "loop", /* name */
55 gate_tree_loop, /* gate */
56 NULL, /* execute */
57 NULL, /* sub */
58 NULL, /* next */
59 0, /* static_pass_number */
60 TV_TREE_LOOP, /* tv_id */
61 PROP_cfg, /* properties_required */
62 0, /* properties_provided */
63 0, /* properties_destroyed */
64 TODO_ggc_collect, /* todo_flags_start */
65 TODO_dump_func | TODO_verify_ssa | TODO_ggc_collect /* todo_flags_finish */
66 }
67 };
68
69 /* Loop optimizer initialization. */
70
71 static unsigned int
72 tree_ssa_loop_init (void)
73 {
74 loop_optimizer_init (LOOPS_NORMAL
75 | LOOPS_HAVE_RECORDED_EXITS);
76 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
77
78 if (number_of_loops () <= 1)
79 return 0;
80
81 scev_initialize ();
82 return 0;
83 }
84
85 struct gimple_opt_pass pass_tree_loop_init =
86 {
87 {
88 GIMPLE_PASS,
89 "loopinit", /* name */
90 NULL, /* gate */
91 tree_ssa_loop_init, /* execute */
92 NULL, /* sub */
93 NULL, /* next */
94 0, /* static_pass_number */
95 TV_TREE_LOOP_INIT, /* tv_id */
96 PROP_cfg, /* properties_required */
97 0, /* properties_provided */
98 0, /* properties_destroyed */
99 0, /* todo_flags_start */
100 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
101 }
102 };
103
104 /* Loop invariant motion pass. */
105
106 static unsigned int
107 tree_ssa_loop_im (void)
108 {
109 if (number_of_loops () <= 1)
110 return 0;
111
112 tree_ssa_lim ();
113 return 0;
114 }
115
116 static bool
117 gate_tree_ssa_loop_im (void)
118 {
119 return flag_tree_loop_im != 0;
120 }
121
122 struct gimple_opt_pass pass_lim =
123 {
124 {
125 GIMPLE_PASS,
126 "lim", /* name */
127 gate_tree_ssa_loop_im, /* gate */
128 tree_ssa_loop_im, /* execute */
129 NULL, /* sub */
130 NULL, /* next */
131 0, /* static_pass_number */
132 TV_LIM, /* tv_id */
133 PROP_cfg, /* properties_required */
134 0, /* properties_provided */
135 0, /* properties_destroyed */
136 0, /* todo_flags_start */
137 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
138 }
139 };
140
141 /* Loop unswitching pass. */
142
143 static unsigned int
144 tree_ssa_loop_unswitch (void)
145 {
146 if (number_of_loops () <= 1)
147 return 0;
148
149 return tree_ssa_unswitch_loops ();
150 }
151
152 static bool
153 gate_tree_ssa_loop_unswitch (void)
154 {
155 return flag_unswitch_loops != 0;
156 }
157
158 struct gimple_opt_pass pass_tree_unswitch =
159 {
160 {
161 GIMPLE_PASS,
162 "unswitch", /* name */
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 TODO_ggc_collect | TODO_dump_func
174 | TODO_verify_loops /* todo_flags_finish */
175 }
176 };
177
178 /* Predictive commoning. */
179
180 static unsigned
181 run_tree_predictive_commoning (void)
182 {
183 if (!current_loops)
184 return 0;
185
186 tree_predictive_commoning ();
187 return 0;
188 }
189
190 static bool
191 gate_tree_predictive_commoning (void)
192 {
193 return flag_predictive_commoning != 0;
194 }
195
196 struct gimple_opt_pass pass_predcom =
197 {
198 {
199 GIMPLE_PASS,
200 "pcom", /* name */
201 gate_tree_predictive_commoning, /* gate */
202 run_tree_predictive_commoning, /* execute */
203 NULL, /* sub */
204 NULL, /* next */
205 0, /* static_pass_number */
206 TV_PREDCOM, /* tv_id */
207 PROP_cfg, /* properties_required */
208 0, /* properties_provided */
209 0, /* properties_destroyed */
210 0, /* todo_flags_start */
211 TODO_dump_func | TODO_verify_loops
212 | TODO_update_ssa_only_virtuals /* todo_flags_finish */
213 }
214 };
215
216 /* Loop autovectorization. */
217
218 static unsigned int
219 tree_vectorize (void)
220 {
221 if (number_of_loops () <= 1)
222 return 0;
223
224 return vectorize_loops ();
225 }
226
227 static bool
228 gate_tree_vectorize (void)
229 {
230 return flag_tree_vectorize;
231 }
232
233 struct gimple_opt_pass pass_vectorize =
234 {
235 {
236 GIMPLE_PASS,
237 "vect", /* name */
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 TODO_verify_loops, /* todo_flags_start */
248 TODO_dump_func | TODO_update_ssa
249 | TODO_ggc_collect /* todo_flags_finish */
250 }
251 };
252
253 /* Loop nest optimizations. */
254
255 static unsigned int
256 tree_linear_transform (void)
257 {
258 if (number_of_loops () <= 1)
259 return 0;
260
261 linear_transform_loops ();
262 return 0;
263 }
264
265 static bool
266 gate_tree_linear_transform (void)
267 {
268 return flag_tree_loop_linear != 0;
269 }
270
271 struct gimple_opt_pass pass_linear_transform =
272 {
273 {
274 GIMPLE_PASS,
275 "ltrans", /* name */
276 gate_tree_linear_transform, /* gate */
277 tree_linear_transform, /* execute */
278 NULL, /* sub */
279 NULL, /* next */
280 0, /* static_pass_number */
281 TV_TREE_LINEAR_TRANSFORM, /* tv_id */
282 PROP_cfg | PROP_ssa, /* properties_required */
283 0, /* properties_provided */
284 0, /* properties_destroyed */
285 0, /* todo_flags_start */
286 TODO_dump_func | TODO_verify_loops
287 | TODO_update_ssa_only_virtuals
288 | TODO_ggc_collect /* todo_flags_finish */
289 }
290 };
291
292 /* GRAPHITE optimizations. */
293
294 static unsigned int
295 graphite_transforms (void)
296 {
297 if (!current_loops)
298 return 0;
299
300 graphite_transform_loops ();
301
302 return 0;
303 }
304
305 static bool
306 gate_graphite_transforms (void)
307 {
308 /* Enable -fgraphite pass if any one of the graphite optimization flags
309 is turned on. */
310 if (flag_loop_block || flag_loop_interchange || flag_loop_strip_mine
311 || flag_graphite_identity || flag_loop_parallelize_all)
312 flag_graphite = 1;
313
314 if (flag_loop_block)
315 sorry ("loop blocking not implemented");
316
317 return flag_graphite != 0;
318 }
319
320 struct gimple_opt_pass pass_graphite_transforms =
321 {
322 {
323 GIMPLE_PASS,
324 "graphite", /* name */
325 gate_graphite_transforms, /* gate */
326 graphite_transforms, /* execute */
327 NULL, /* sub */
328 NULL, /* next */
329 0, /* static_pass_number */
330 TV_GRAPHITE_TRANSFORMS, /* tv_id */
331 PROP_cfg | PROP_ssa, /* properties_required */
332 0, /* properties_provided */
333 0, /* properties_destroyed */
334 0, /* todo_flags_start */
335 TODO_verify_loops /* todo_flags_finish */
336 }
337 };
338
339 /* Check the correctness of the data dependence analyzers. */
340
341 static unsigned int
342 check_data_deps (void)
343 {
344 if (number_of_loops () <= 1)
345 return 0;
346
347 tree_check_data_deps ();
348 return 0;
349 }
350
351 static bool
352 gate_check_data_deps (void)
353 {
354 return flag_check_data_deps != 0;
355 }
356
357 struct gimple_opt_pass pass_check_data_deps =
358 {
359 {
360 GIMPLE_PASS,
361 "ckdd", /* name */
362 gate_check_data_deps, /* gate */
363 check_data_deps, /* execute */
364 NULL, /* sub */
365 NULL, /* next */
366 0, /* static_pass_number */
367 TV_CHECK_DATA_DEPS, /* tv_id */
368 PROP_cfg | PROP_ssa, /* properties_required */
369 0, /* properties_provided */
370 0, /* properties_destroyed */
371 0, /* todo_flags_start */
372 TODO_dump_func /* todo_flags_finish */
373 }
374 };
375
376 /* Canonical induction variable creation pass. */
377
378 static unsigned int
379 tree_ssa_loop_ivcanon (void)
380 {
381 if (number_of_loops () <= 1)
382 return 0;
383
384 return canonicalize_induction_variables ();
385 }
386
387 static bool
388 gate_tree_ssa_loop_ivcanon (void)
389 {
390 return flag_tree_loop_ivcanon != 0;
391 }
392
393 struct gimple_opt_pass pass_iv_canon =
394 {
395 {
396 GIMPLE_PASS,
397 "ivcanon", /* name */
398 gate_tree_ssa_loop_ivcanon, /* gate */
399 tree_ssa_loop_ivcanon, /* execute */
400 NULL, /* sub */
401 NULL, /* next */
402 0, /* static_pass_number */
403 TV_TREE_LOOP_IVCANON, /* tv_id */
404 PROP_cfg | PROP_ssa, /* properties_required */
405 0, /* properties_provided */
406 0, /* properties_destroyed */
407 0, /* todo_flags_start */
408 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
409 }
410 };
411
412 /* Propagation of constants using scev. */
413
414 static bool
415 gate_scev_const_prop (void)
416 {
417 return flag_tree_scev_cprop;
418 }
419
420 struct gimple_opt_pass pass_scev_cprop =
421 {
422 {
423 GIMPLE_PASS,
424 "sccp", /* name */
425 gate_scev_const_prop, /* gate */
426 scev_const_prop, /* execute */
427 NULL, /* sub */
428 NULL, /* next */
429 0, /* static_pass_number */
430 TV_SCEV_CONST, /* tv_id */
431 PROP_cfg | PROP_ssa, /* properties_required */
432 0, /* properties_provided */
433 0, /* properties_destroyed */
434 0, /* todo_flags_start */
435 TODO_dump_func | TODO_cleanup_cfg
436 | TODO_update_ssa_only_virtuals
437 /* todo_flags_finish */
438 }
439 };
440
441 /* Record bounds on numbers of iterations of loops. */
442
443 static unsigned int
444 tree_ssa_loop_bounds (void)
445 {
446 if (number_of_loops () <= 1)
447 return 0;
448
449 estimate_numbers_of_iterations ();
450 scev_reset ();
451 return 0;
452 }
453
454 struct gimple_opt_pass pass_record_bounds =
455 {
456 {
457 GIMPLE_PASS,
458 NULL, /* name */
459 NULL, /* gate */
460 tree_ssa_loop_bounds, /* execute */
461 NULL, /* sub */
462 NULL, /* next */
463 0, /* static_pass_number */
464 TV_TREE_LOOP_BOUNDS, /* tv_id */
465 PROP_cfg | PROP_ssa, /* properties_required */
466 0, /* properties_provided */
467 0, /* properties_destroyed */
468 0, /* todo_flags_start */
469 0 /* todo_flags_finish */
470 }
471 };
472
473 /* Complete unrolling of loops. */
474
475 static unsigned int
476 tree_complete_unroll (void)
477 {
478 if (number_of_loops () <= 1)
479 return 0;
480
481 return tree_unroll_loops_completely (flag_unroll_loops
482 || flag_peel_loops
483 || optimize >= 3, true);
484 }
485
486 static bool
487 gate_tree_complete_unroll (void)
488 {
489 return true;
490 }
491
492 struct gimple_opt_pass pass_complete_unroll =
493 {
494 {
495 GIMPLE_PASS,
496 "cunroll", /* name */
497 gate_tree_complete_unroll, /* gate */
498 tree_complete_unroll, /* execute */
499 NULL, /* sub */
500 NULL, /* next */
501 0, /* static_pass_number */
502 TV_COMPLETE_UNROLL, /* tv_id */
503 PROP_cfg | PROP_ssa, /* properties_required */
504 0, /* properties_provided */
505 0, /* properties_destroyed */
506 0, /* todo_flags_start */
507 TODO_dump_func | TODO_verify_loops
508 | TODO_ggc_collect /* todo_flags_finish */
509 }
510 };
511
512 /* Complete unrolling of inner loops. */
513
514 static unsigned int
515 tree_complete_unroll_inner (void)
516 {
517 unsigned ret = 0;
518
519 loop_optimizer_init (LOOPS_NORMAL
520 | LOOPS_HAVE_RECORDED_EXITS);
521 if (number_of_loops () > 1)
522 {
523 scev_initialize ();
524 ret = tree_unroll_loops_completely (optimize >= 3, false);
525 free_numbers_of_iterations_estimates ();
526 scev_finalize ();
527 }
528 loop_optimizer_finalize ();
529
530 return ret;
531 }
532
533 static bool
534 gate_tree_complete_unroll_inner (void)
535 {
536 return optimize >= 2;
537 }
538
539 struct gimple_opt_pass pass_complete_unrolli =
540 {
541 {
542 GIMPLE_PASS,
543 "cunrolli", /* name */
544 gate_tree_complete_unroll_inner, /* gate */
545 tree_complete_unroll_inner, /* execute */
546 NULL, /* sub */
547 NULL, /* next */
548 0, /* static_pass_number */
549 TV_COMPLETE_UNROLL, /* tv_id */
550 PROP_cfg | PROP_ssa, /* properties_required */
551 0, /* properties_provided */
552 0, /* properties_destroyed */
553 0, /* todo_flags_start */
554 TODO_dump_func | TODO_verify_loops
555 | TODO_ggc_collect /* todo_flags_finish */
556 }
557 };
558
559 /* Parallelization. */
560
561 static bool
562 gate_tree_parallelize_loops (void)
563 {
564 return flag_tree_parallelize_loops > 1;
565 }
566
567 static unsigned
568 tree_parallelize_loops (void)
569 {
570 if (number_of_loops () <= 1)
571 return 0;
572
573 if (parallelize_loops ())
574 return TODO_cleanup_cfg | TODO_rebuild_alias;
575 return 0;
576 }
577
578 struct gimple_opt_pass pass_parallelize_loops =
579 {
580 {
581 GIMPLE_PASS,
582 "parloops", /* name */
583 gate_tree_parallelize_loops, /* gate */
584 tree_parallelize_loops, /* execute */
585 NULL, /* sub */
586 NULL, /* next */
587 0, /* static_pass_number */
588 TV_TREE_PARALLELIZE_LOOPS, /* tv_id */
589 PROP_cfg | PROP_ssa, /* properties_required */
590 0, /* properties_provided */
591 0, /* properties_destroyed */
592 0, /* todo_flags_start */
593 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
594 }
595 };
596
597 /* Prefetching. */
598
599 static unsigned int
600 tree_ssa_loop_prefetch (void)
601 {
602 if (number_of_loops () <= 1)
603 return 0;
604
605 return tree_ssa_prefetch_arrays ();
606 }
607
608 static bool
609 gate_tree_ssa_loop_prefetch (void)
610 {
611 return flag_prefetch_loop_arrays != 0;
612 }
613
614 struct gimple_opt_pass pass_loop_prefetch =
615 {
616 {
617 GIMPLE_PASS,
618 "aprefetch", /* name */
619 gate_tree_ssa_loop_prefetch, /* gate */
620 tree_ssa_loop_prefetch, /* execute */
621 NULL, /* sub */
622 NULL, /* next */
623 0, /* static_pass_number */
624 TV_TREE_PREFETCH, /* tv_id */
625 PROP_cfg | PROP_ssa, /* properties_required */
626 0, /* properties_provided */
627 0, /* properties_destroyed */
628 0, /* todo_flags_start */
629 TODO_dump_func | TODO_verify_loops /* todo_flags_finish */
630 }
631 };
632
633 /* Induction variable optimizations. */
634
635 static unsigned int
636 tree_ssa_loop_ivopts (void)
637 {
638 if (number_of_loops () <= 1)
639 return 0;
640
641 tree_ssa_iv_optimize ();
642 return 0;
643 }
644
645 static bool
646 gate_tree_ssa_loop_ivopts (void)
647 {
648 return flag_ivopts != 0;
649 }
650
651 struct gimple_opt_pass pass_iv_optimize =
652 {
653 {
654 GIMPLE_PASS,
655 "ivopts", /* name */
656 gate_tree_ssa_loop_ivopts, /* gate */
657 tree_ssa_loop_ivopts, /* execute */
658 NULL, /* sub */
659 NULL, /* next */
660 0, /* static_pass_number */
661 TV_TREE_LOOP_IVOPTS, /* tv_id */
662 PROP_cfg | PROP_ssa, /* properties_required */
663 0, /* properties_provided */
664 0, /* properties_destroyed */
665 0, /* todo_flags_start */
666 TODO_dump_func | TODO_verify_loops
667 | TODO_update_ssa | TODO_ggc_collect /* todo_flags_finish */
668 }
669 };
670
671 /* Loop optimizer finalization. */
672
673 static unsigned int
674 tree_ssa_loop_done (void)
675 {
676 free_numbers_of_iterations_estimates ();
677 scev_finalize ();
678 loop_optimizer_finalize ();
679 return 0;
680 }
681
682 struct gimple_opt_pass pass_tree_loop_done =
683 {
684 {
685 GIMPLE_PASS,
686 "loopdone", /* name */
687 NULL, /* gate */
688 tree_ssa_loop_done, /* execute */
689 NULL, /* sub */
690 NULL, /* next */
691 0, /* static_pass_number */
692 TV_TREE_LOOP_FINI, /* tv_id */
693 PROP_cfg, /* properties_required */
694 0, /* properties_provided */
695 0, /* properties_destroyed */
696 0, /* todo_flags_start */
697 TODO_cleanup_cfg | TODO_dump_func /* todo_flags_finish */
698 }
699 };