ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / tree-ssa-loop.c
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2014 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 "tm_p.h"
26 #include "predict.h"
27 #include "vec.h"
28 #include "hashtab.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "hard-reg-set.h"
32 #include "input.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "is-a.h"
41 #include "gimple.h"
42 #include "gimple-iterator.h"
43 #include "tree-ssa-loop-ivopts.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "tree-ssa-loop-niter.h"
46 #include "tree-ssa-loop.h"
47 #include "tree-pass.h"
48 #include "cfgloop.h"
49 #include "flags.h"
50 #include "tree-inline.h"
51 #include "tree-scalar-evolution.h"
52 #include "diagnostic-core.h"
53 #include "tree-vectorizer.h"
54
55
56 /* A pass making sure loops are fixed up. */
57
58 namespace {
59
60 const pass_data pass_data_fix_loops =
61 {
62 GIMPLE_PASS, /* type */
63 "fix_loops", /* name */
64 OPTGROUP_LOOP, /* optinfo_flags */
65 TV_TREE_LOOP, /* tv_id */
66 PROP_cfg, /* properties_required */
67 0, /* properties_provided */
68 0, /* properties_destroyed */
69 0, /* todo_flags_start */
70 0, /* todo_flags_finish */
71 };
72
73 class pass_fix_loops : public gimple_opt_pass
74 {
75 public:
76 pass_fix_loops (gcc::context *ctxt)
77 : gimple_opt_pass (pass_data_fix_loops, ctxt)
78 {}
79
80 /* opt_pass methods: */
81 virtual bool gate (function *) { return flag_tree_loop_optimize; }
82
83 virtual unsigned int execute (function *fn);
84 }; // class pass_fix_loops
85
86 unsigned int
87 pass_fix_loops::execute (function *)
88 {
89 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
90 {
91 calculate_dominance_info (CDI_DOMINATORS);
92 fix_loop_structure (NULL);
93 }
94 return 0;
95 }
96
97 } // anon namespace
98
99 gimple_opt_pass *
100 make_pass_fix_loops (gcc::context *ctxt)
101 {
102 return new pass_fix_loops (ctxt);
103 }
104
105
106 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
107 but we also avoid running it when the IL doesn't contain any loop. */
108
109 static bool
110 gate_loop (function *fn)
111 {
112 if (!flag_tree_loop_optimize)
113 return false;
114
115 /* For -fdump-passes which runs before loop discovery print the
116 state of -ftree-loop-optimize. */
117 if (!loops_for_fn (fn))
118 return true;
119
120 return number_of_loops (fn) > 1;
121 }
122
123 /* The loop superpass. */
124
125 namespace {
126
127 const pass_data pass_data_tree_loop =
128 {
129 GIMPLE_PASS, /* type */
130 "loop", /* name */
131 OPTGROUP_LOOP, /* optinfo_flags */
132 TV_TREE_LOOP, /* tv_id */
133 PROP_cfg, /* properties_required */
134 0, /* properties_provided */
135 0, /* properties_destroyed */
136 0, /* todo_flags_start */
137 0, /* todo_flags_finish */
138 };
139
140 class pass_tree_loop : public gimple_opt_pass
141 {
142 public:
143 pass_tree_loop (gcc::context *ctxt)
144 : gimple_opt_pass (pass_data_tree_loop, ctxt)
145 {}
146
147 /* opt_pass methods: */
148 virtual bool gate (function *fn) { return gate_loop (fn); }
149
150 }; // class pass_tree_loop
151
152 } // anon namespace
153
154 gimple_opt_pass *
155 make_pass_tree_loop (gcc::context *ctxt)
156 {
157 return new pass_tree_loop (ctxt);
158 }
159
160 /* The no-loop superpass. */
161
162 namespace {
163
164 const pass_data pass_data_tree_no_loop =
165 {
166 GIMPLE_PASS, /* type */
167 "no_loop", /* name */
168 OPTGROUP_NONE, /* optinfo_flags */
169 TV_TREE_NOLOOP, /* tv_id */
170 PROP_cfg, /* properties_required */
171 0, /* properties_provided */
172 0, /* properties_destroyed */
173 0, /* todo_flags_start */
174 0, /* todo_flags_finish */
175 };
176
177 class pass_tree_no_loop : public gimple_opt_pass
178 {
179 public:
180 pass_tree_no_loop (gcc::context *ctxt)
181 : gimple_opt_pass (pass_data_tree_no_loop, ctxt)
182 {}
183
184 /* opt_pass methods: */
185 virtual bool gate (function *fn) { return !gate_loop (fn); }
186
187 }; // class pass_tree_no_loop
188
189 } // anon namespace
190
191 gimple_opt_pass *
192 make_pass_tree_no_loop (gcc::context *ctxt)
193 {
194 return new pass_tree_no_loop (ctxt);
195 }
196
197
198 /* Loop optimizer initialization. */
199
200 namespace {
201
202 const pass_data pass_data_tree_loop_init =
203 {
204 GIMPLE_PASS, /* type */
205 "loopinit", /* name */
206 OPTGROUP_LOOP, /* optinfo_flags */
207 TV_NONE, /* tv_id */
208 PROP_cfg, /* properties_required */
209 0, /* properties_provided */
210 0, /* properties_destroyed */
211 0, /* todo_flags_start */
212 0, /* todo_flags_finish */
213 };
214
215 class pass_tree_loop_init : public gimple_opt_pass
216 {
217 public:
218 pass_tree_loop_init (gcc::context *ctxt)
219 : gimple_opt_pass (pass_data_tree_loop_init, ctxt)
220 {}
221
222 /* opt_pass methods: */
223 virtual unsigned int execute (function *);
224
225 }; // class pass_tree_loop_init
226
227 unsigned int
228 pass_tree_loop_init::execute (function *fun ATTRIBUTE_UNUSED)
229 {
230 loop_optimizer_init (LOOPS_NORMAL
231 | LOOPS_HAVE_RECORDED_EXITS);
232 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
233
234 /* We might discover new loops, e.g. when turning irreducible
235 regions into reducible. */
236 scev_initialize ();
237
238 return 0;
239 }
240
241 } // anon namespace
242
243 gimple_opt_pass *
244 make_pass_tree_loop_init (gcc::context *ctxt)
245 {
246 return new pass_tree_loop_init (ctxt);
247 }
248
249 /* Loop autovectorization. */
250
251 namespace {
252
253 const pass_data pass_data_vectorize =
254 {
255 GIMPLE_PASS, /* type */
256 "vect", /* name */
257 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
258 TV_TREE_VECTORIZATION, /* tv_id */
259 ( PROP_cfg | PROP_ssa ), /* properties_required */
260 0, /* properties_provided */
261 0, /* properties_destroyed */
262 0, /* todo_flags_start */
263 0, /* todo_flags_finish */
264 };
265
266 class pass_vectorize : public gimple_opt_pass
267 {
268 public:
269 pass_vectorize (gcc::context *ctxt)
270 : gimple_opt_pass (pass_data_vectorize, ctxt)
271 {}
272
273 /* opt_pass methods: */
274 virtual bool gate (function *fun)
275 {
276 return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
277 }
278
279 virtual unsigned int execute (function *);
280
281 }; // class pass_vectorize
282
283 unsigned int
284 pass_vectorize::execute (function *fun)
285 {
286 if (number_of_loops (fun) <= 1)
287 return 0;
288
289 return vectorize_loops ();
290 }
291
292 } // anon namespace
293
294 gimple_opt_pass *
295 make_pass_vectorize (gcc::context *ctxt)
296 {
297 return new pass_vectorize (ctxt);
298 }
299
300 /* Check the correctness of the data dependence analyzers. */
301
302 namespace {
303
304 const pass_data pass_data_check_data_deps =
305 {
306 GIMPLE_PASS, /* type */
307 "ckdd", /* name */
308 OPTGROUP_LOOP, /* optinfo_flags */
309 TV_CHECK_DATA_DEPS, /* tv_id */
310 ( PROP_cfg | PROP_ssa ), /* properties_required */
311 0, /* properties_provided */
312 0, /* properties_destroyed */
313 0, /* todo_flags_start */
314 0, /* todo_flags_finish */
315 };
316
317 class pass_check_data_deps : public gimple_opt_pass
318 {
319 public:
320 pass_check_data_deps (gcc::context *ctxt)
321 : gimple_opt_pass (pass_data_check_data_deps, ctxt)
322 {}
323
324 /* opt_pass methods: */
325 virtual bool gate (function *) { return flag_check_data_deps != 0; }
326 virtual unsigned int execute (function *);
327
328 }; // class pass_check_data_deps
329
330 unsigned int
331 pass_check_data_deps::execute (function *fun)
332 {
333 if (number_of_loops (fun) <= 1)
334 return 0;
335
336 tree_check_data_deps ();
337 return 0;
338 }
339
340 } // anon namespace
341
342 gimple_opt_pass *
343 make_pass_check_data_deps (gcc::context *ctxt)
344 {
345 return new pass_check_data_deps (ctxt);
346 }
347
348 /* Propagation of constants using scev. */
349
350 namespace {
351
352 const pass_data pass_data_scev_cprop =
353 {
354 GIMPLE_PASS, /* type */
355 "sccp", /* name */
356 OPTGROUP_LOOP, /* optinfo_flags */
357 TV_SCEV_CONST, /* tv_id */
358 ( PROP_cfg | PROP_ssa ), /* properties_required */
359 0, /* properties_provided */
360 0, /* properties_destroyed */
361 0, /* todo_flags_start */
362 ( TODO_cleanup_cfg
363 | TODO_update_ssa_only_virtuals ), /* todo_flags_finish */
364 };
365
366 class pass_scev_cprop : public gimple_opt_pass
367 {
368 public:
369 pass_scev_cprop (gcc::context *ctxt)
370 : gimple_opt_pass (pass_data_scev_cprop, ctxt)
371 {}
372
373 /* opt_pass methods: */
374 virtual bool gate (function *) { return flag_tree_scev_cprop; }
375 virtual unsigned int execute (function *) { return scev_const_prop (); }
376
377 }; // class pass_scev_cprop
378
379 } // anon namespace
380
381 gimple_opt_pass *
382 make_pass_scev_cprop (gcc::context *ctxt)
383 {
384 return new pass_scev_cprop (ctxt);
385 }
386
387 /* Record bounds on numbers of iterations of loops. */
388
389 namespace {
390
391 const pass_data pass_data_record_bounds =
392 {
393 GIMPLE_PASS, /* type */
394 "*record_bounds", /* name */
395 OPTGROUP_NONE, /* optinfo_flags */
396 TV_TREE_LOOP_BOUNDS, /* tv_id */
397 ( PROP_cfg | PROP_ssa ), /* properties_required */
398 0, /* properties_provided */
399 0, /* properties_destroyed */
400 0, /* todo_flags_start */
401 0, /* todo_flags_finish */
402 };
403
404 class pass_record_bounds : public gimple_opt_pass
405 {
406 public:
407 pass_record_bounds (gcc::context *ctxt)
408 : gimple_opt_pass (pass_data_record_bounds, ctxt)
409 {}
410
411 /* opt_pass methods: */
412 virtual unsigned int execute (function *);
413
414 }; // class pass_record_bounds
415
416 unsigned int
417 pass_record_bounds::execute (function *fun)
418 {
419 if (number_of_loops (fun) <= 1)
420 return 0;
421
422 estimate_numbers_of_iterations ();
423 scev_reset ();
424 return 0;
425 }
426
427 } // anon namespace
428
429 gimple_opt_pass *
430 make_pass_record_bounds (gcc::context *ctxt)
431 {
432 return new pass_record_bounds (ctxt);
433 }
434
435 /* Induction variable optimizations. */
436
437 namespace {
438
439 const pass_data pass_data_iv_optimize =
440 {
441 GIMPLE_PASS, /* type */
442 "ivopts", /* name */
443 OPTGROUP_LOOP, /* optinfo_flags */
444 TV_TREE_LOOP_IVOPTS, /* tv_id */
445 ( PROP_cfg | PROP_ssa ), /* properties_required */
446 0, /* properties_provided */
447 0, /* properties_destroyed */
448 0, /* todo_flags_start */
449 TODO_update_ssa, /* todo_flags_finish */
450 };
451
452 class pass_iv_optimize : public gimple_opt_pass
453 {
454 public:
455 pass_iv_optimize (gcc::context *ctxt)
456 : gimple_opt_pass (pass_data_iv_optimize, ctxt)
457 {}
458
459 /* opt_pass methods: */
460 virtual bool gate (function *) { return flag_ivopts != 0; }
461 virtual unsigned int execute (function *);
462
463 }; // class pass_iv_optimize
464
465 unsigned int
466 pass_iv_optimize::execute (function *fun)
467 {
468 if (number_of_loops (fun) <= 1)
469 return 0;
470
471 tree_ssa_iv_optimize ();
472 return 0;
473 }
474
475 } // anon namespace
476
477 gimple_opt_pass *
478 make_pass_iv_optimize (gcc::context *ctxt)
479 {
480 return new pass_iv_optimize (ctxt);
481 }
482
483 /* Loop optimizer finalization. */
484
485 static unsigned int
486 tree_ssa_loop_done (void)
487 {
488 free_numbers_of_iterations_estimates ();
489 scev_finalize ();
490 loop_optimizer_finalize ();
491 return 0;
492 }
493
494 namespace {
495
496 const pass_data pass_data_tree_loop_done =
497 {
498 GIMPLE_PASS, /* type */
499 "loopdone", /* name */
500 OPTGROUP_LOOP, /* optinfo_flags */
501 TV_NONE, /* tv_id */
502 PROP_cfg, /* properties_required */
503 0, /* properties_provided */
504 0, /* properties_destroyed */
505 0, /* todo_flags_start */
506 TODO_cleanup_cfg, /* todo_flags_finish */
507 };
508
509 class pass_tree_loop_done : public gimple_opt_pass
510 {
511 public:
512 pass_tree_loop_done (gcc::context *ctxt)
513 : gimple_opt_pass (pass_data_tree_loop_done, ctxt)
514 {}
515
516 /* opt_pass methods: */
517 virtual unsigned int execute (function *) { return tree_ssa_loop_done (); }
518
519 }; // class pass_tree_loop_done
520
521 } // anon namespace
522
523 gimple_opt_pass *
524 make_pass_tree_loop_done (gcc::context *ctxt)
525 {
526 return new pass_tree_loop_done (ctxt);
527 }
528
529 /* Calls CBCK for each index in memory reference ADDR_P. There are two
530 kinds situations handled; in each of these cases, the memory reference
531 and DATA are passed to the callback:
532
533 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
534 pass the pointer to the index to the callback.
535
536 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
537 pointer to addr to the callback.
538
539 If the callback returns false, the whole search stops and false is returned.
540 Otherwise the function returns true after traversing through the whole
541 reference *ADDR_P. */
542
543 bool
544 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
545 {
546 tree *nxt, *idx;
547
548 for (; ; addr_p = nxt)
549 {
550 switch (TREE_CODE (*addr_p))
551 {
552 case SSA_NAME:
553 return cbck (*addr_p, addr_p, data);
554
555 case MEM_REF:
556 nxt = &TREE_OPERAND (*addr_p, 0);
557 return cbck (*addr_p, nxt, data);
558
559 case BIT_FIELD_REF:
560 case VIEW_CONVERT_EXPR:
561 case REALPART_EXPR:
562 case IMAGPART_EXPR:
563 nxt = &TREE_OPERAND (*addr_p, 0);
564 break;
565
566 case COMPONENT_REF:
567 /* If the component has varying offset, it behaves like index
568 as well. */
569 idx = &TREE_OPERAND (*addr_p, 2);
570 if (*idx
571 && !cbck (*addr_p, idx, data))
572 return false;
573
574 nxt = &TREE_OPERAND (*addr_p, 0);
575 break;
576
577 case ARRAY_REF:
578 case ARRAY_RANGE_REF:
579 nxt = &TREE_OPERAND (*addr_p, 0);
580 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
581 return false;
582 break;
583
584 case VAR_DECL:
585 case PARM_DECL:
586 case CONST_DECL:
587 case STRING_CST:
588 case RESULT_DECL:
589 case VECTOR_CST:
590 case COMPLEX_CST:
591 case INTEGER_CST:
592 case REAL_CST:
593 case FIXED_CST:
594 case CONSTRUCTOR:
595 return true;
596
597 case ADDR_EXPR:
598 gcc_assert (is_gimple_min_invariant (*addr_p));
599 return true;
600
601 case TARGET_MEM_REF:
602 idx = &TMR_BASE (*addr_p);
603 if (*idx
604 && !cbck (*addr_p, idx, data))
605 return false;
606 idx = &TMR_INDEX (*addr_p);
607 if (*idx
608 && !cbck (*addr_p, idx, data))
609 return false;
610 idx = &TMR_INDEX2 (*addr_p);
611 if (*idx
612 && !cbck (*addr_p, idx, data))
613 return false;
614 return true;
615
616 default:
617 gcc_unreachable ();
618 }
619 }
620 }
621
622
623 /* The name and the length of the currently generated variable
624 for lsm. */
625 #define MAX_LSM_NAME_LENGTH 40
626 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
627 static int lsm_tmp_name_length;
628
629 /* Adds S to lsm_tmp_name. */
630
631 static void
632 lsm_tmp_name_add (const char *s)
633 {
634 int l = strlen (s) + lsm_tmp_name_length;
635 if (l > MAX_LSM_NAME_LENGTH)
636 return;
637
638 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
639 lsm_tmp_name_length = l;
640 }
641
642 /* Stores the name for temporary variable that replaces REF to
643 lsm_tmp_name. */
644
645 static void
646 gen_lsm_tmp_name (tree ref)
647 {
648 const char *name;
649
650 switch (TREE_CODE (ref))
651 {
652 case MEM_REF:
653 case TARGET_MEM_REF:
654 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
655 lsm_tmp_name_add ("_");
656 break;
657
658 case ADDR_EXPR:
659 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
660 break;
661
662 case BIT_FIELD_REF:
663 case VIEW_CONVERT_EXPR:
664 case ARRAY_RANGE_REF:
665 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
666 break;
667
668 case REALPART_EXPR:
669 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
670 lsm_tmp_name_add ("_RE");
671 break;
672
673 case IMAGPART_EXPR:
674 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
675 lsm_tmp_name_add ("_IM");
676 break;
677
678 case COMPONENT_REF:
679 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
680 lsm_tmp_name_add ("_");
681 name = get_name (TREE_OPERAND (ref, 1));
682 if (!name)
683 name = "F";
684 lsm_tmp_name_add (name);
685 break;
686
687 case ARRAY_REF:
688 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
689 lsm_tmp_name_add ("_I");
690 break;
691
692 case SSA_NAME:
693 case VAR_DECL:
694 case PARM_DECL:
695 name = get_name (ref);
696 if (!name)
697 name = "D";
698 lsm_tmp_name_add (name);
699 break;
700
701 case STRING_CST:
702 lsm_tmp_name_add ("S");
703 break;
704
705 case RESULT_DECL:
706 lsm_tmp_name_add ("R");
707 break;
708
709 case INTEGER_CST:
710 /* Nothing. */
711 break;
712
713 default:
714 gcc_unreachable ();
715 }
716 }
717
718 /* Determines name for temporary variable that replaces REF.
719 The name is accumulated into the lsm_tmp_name variable.
720 N is added to the name of the temporary. */
721
722 char *
723 get_lsm_tmp_name (tree ref, unsigned n, const char *suffix)
724 {
725 char ns[2];
726
727 lsm_tmp_name_length = 0;
728 gen_lsm_tmp_name (ref);
729 lsm_tmp_name_add ("_lsm");
730 if (n < 10)
731 {
732 ns[0] = '0' + n;
733 ns[1] = 0;
734 lsm_tmp_name_add (ns);
735 }
736 return lsm_tmp_name;
737 if (suffix != NULL)
738 lsm_tmp_name_add (suffix);
739 }
740
741 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
742
743 unsigned
744 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
745 {
746 basic_block *body = get_loop_body (loop);
747 gimple_stmt_iterator gsi;
748 unsigned size = 0, i;
749
750 for (i = 0; i < loop->num_nodes; i++)
751 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
752 size += estimate_num_insns (gsi_stmt (gsi), weights);
753 free (body);
754
755 return size;
756 }
757
758
759