Clean up some minor white space issues in trans-decl.c and trans-expr.c
[official-gcc.git] / gcc / tree-ssa-loop.c
blobde09aa989f29767ad1fe8439cce834064f1e9bd0
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2016 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 "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "tree-pass.h"
27 #include "tm_p.h"
28 #include "fold-const.h"
29 #include "gimple-iterator.h"
30 #include "tree-ssa-loop-ivopts.h"
31 #include "tree-ssa-loop-manip.h"
32 #include "tree-ssa-loop-niter.h"
33 #include "tree-ssa-loop.h"
34 #include "cfgloop.h"
35 #include "tree-inline.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-vectorizer.h"
38 #include "omp-low.h"
39 #include "diagnostic-core.h"
42 /* A pass making sure loops are fixed up. */
44 namespace {
46 const pass_data pass_data_fix_loops =
48 GIMPLE_PASS, /* type */
49 "fix_loops", /* name */
50 OPTGROUP_LOOP, /* optinfo_flags */
51 TV_TREE_LOOP, /* tv_id */
52 PROP_cfg, /* properties_required */
53 0, /* properties_provided */
54 0, /* properties_destroyed */
55 0, /* todo_flags_start */
56 0, /* todo_flags_finish */
59 class pass_fix_loops : public gimple_opt_pass
61 public:
62 pass_fix_loops (gcc::context *ctxt)
63 : gimple_opt_pass (pass_data_fix_loops, ctxt)
66 /* opt_pass methods: */
67 virtual bool gate (function *) { return flag_tree_loop_optimize; }
69 virtual unsigned int execute (function *fn);
70 }; // class pass_fix_loops
72 unsigned int
73 pass_fix_loops::execute (function *)
75 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
77 calculate_dominance_info (CDI_DOMINATORS);
78 fix_loop_structure (NULL);
80 return 0;
83 } // anon namespace
85 gimple_opt_pass *
86 make_pass_fix_loops (gcc::context *ctxt)
88 return new pass_fix_loops (ctxt);
92 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
93 but we also avoid running it when the IL doesn't contain any loop. */
95 static bool
96 gate_loop (function *fn)
98 if (!flag_tree_loop_optimize)
99 return false;
101 /* For -fdump-passes which runs before loop discovery print the
102 state of -ftree-loop-optimize. */
103 if (!loops_for_fn (fn))
104 return true;
106 return number_of_loops (fn) > 1;
109 /* The loop superpass. */
111 namespace {
113 const pass_data pass_data_tree_loop =
115 GIMPLE_PASS, /* type */
116 "loop", /* name */
117 OPTGROUP_LOOP, /* optinfo_flags */
118 TV_TREE_LOOP, /* tv_id */
119 PROP_cfg, /* properties_required */
120 0, /* properties_provided */
121 0, /* properties_destroyed */
122 0, /* todo_flags_start */
123 0, /* todo_flags_finish */
126 class pass_tree_loop : public gimple_opt_pass
128 public:
129 pass_tree_loop (gcc::context *ctxt)
130 : gimple_opt_pass (pass_data_tree_loop, ctxt)
133 /* opt_pass methods: */
134 virtual bool gate (function *fn) { return gate_loop (fn); }
136 }; // class pass_tree_loop
138 } // anon namespace
140 gimple_opt_pass *
141 make_pass_tree_loop (gcc::context *ctxt)
143 return new pass_tree_loop (ctxt);
146 /* Gate for oacc kernels pass group. */
148 static bool
149 gate_oacc_kernels (function *fn)
151 if (flag_tree_parallelize_loops <= 1)
152 return false;
154 tree oacc_function_attr = get_oacc_fn_attrib (fn->decl);
155 if (oacc_function_attr == NULL_TREE)
156 return false;
158 tree val = TREE_VALUE (oacc_function_attr);
159 while (val != NULL_TREE && TREE_VALUE (val) == NULL_TREE)
160 val = TREE_CHAIN (val);
162 if (val != NULL_TREE)
163 return false;
165 struct loop *loop;
166 FOR_EACH_LOOP (loop, 0)
167 if (loop->in_oacc_kernels_region)
168 return true;
170 return false;
173 /* The oacc kernels superpass. */
175 namespace {
177 const pass_data pass_data_oacc_kernels =
179 GIMPLE_PASS, /* type */
180 "oacc_kernels", /* name */
181 OPTGROUP_LOOP, /* optinfo_flags */
182 TV_TREE_LOOP, /* tv_id */
183 PROP_cfg, /* properties_required */
184 0, /* properties_provided */
185 0, /* properties_destroyed */
186 0, /* todo_flags_start */
187 0, /* todo_flags_finish */
190 class pass_oacc_kernels : public gimple_opt_pass
192 public:
193 pass_oacc_kernels (gcc::context *ctxt)
194 : gimple_opt_pass (pass_data_oacc_kernels, ctxt)
197 /* opt_pass methods: */
198 virtual bool gate (function *fn) { return gate_oacc_kernels (fn); }
200 }; // class pass_oacc_kernels
202 } // anon namespace
204 gimple_opt_pass *
205 make_pass_oacc_kernels (gcc::context *ctxt)
207 return new pass_oacc_kernels (ctxt);
210 /* The ipa oacc superpass. */
212 namespace {
214 const pass_data pass_data_ipa_oacc =
216 SIMPLE_IPA_PASS, /* type */
217 "ipa_oacc", /* name */
218 OPTGROUP_LOOP, /* optinfo_flags */
219 TV_TREE_LOOP, /* tv_id */
220 PROP_cfg, /* properties_required */
221 0, /* properties_provided */
222 0, /* properties_destroyed */
223 0, /* todo_flags_start */
224 0, /* todo_flags_finish */
227 class pass_ipa_oacc : public simple_ipa_opt_pass
229 public:
230 pass_ipa_oacc (gcc::context *ctxt)
231 : simple_ipa_opt_pass (pass_data_ipa_oacc, ctxt)
234 /* opt_pass methods: */
235 virtual bool gate (function *)
237 return (optimize
238 /* Don't bother doing anything if the program has errors. */
239 && !seen_error ()
240 && flag_openacc
241 && flag_tree_parallelize_loops > 1);
244 }; // class pass_ipa_oacc
246 } // anon namespace
248 simple_ipa_opt_pass *
249 make_pass_ipa_oacc (gcc::context *ctxt)
251 return new pass_ipa_oacc (ctxt);
254 /* The ipa oacc kernels pass. */
256 namespace {
258 const pass_data pass_data_ipa_oacc_kernels =
260 SIMPLE_IPA_PASS, /* type */
261 "ipa_oacc_kernels", /* name */
262 OPTGROUP_LOOP, /* optinfo_flags */
263 TV_TREE_LOOP, /* tv_id */
264 PROP_cfg, /* properties_required */
265 0, /* properties_provided */
266 0, /* properties_destroyed */
267 0, /* todo_flags_start */
268 0, /* todo_flags_finish */
271 class pass_ipa_oacc_kernels : public simple_ipa_opt_pass
273 public:
274 pass_ipa_oacc_kernels (gcc::context *ctxt)
275 : simple_ipa_opt_pass (pass_data_ipa_oacc_kernels, ctxt)
278 }; // class pass_ipa_oacc_kernels
280 } // anon namespace
282 simple_ipa_opt_pass *
283 make_pass_ipa_oacc_kernels (gcc::context *ctxt)
285 return new pass_ipa_oacc_kernels (ctxt);
288 /* The no-loop superpass. */
290 namespace {
292 const pass_data pass_data_tree_no_loop =
294 GIMPLE_PASS, /* type */
295 "no_loop", /* name */
296 OPTGROUP_NONE, /* optinfo_flags */
297 TV_TREE_NOLOOP, /* tv_id */
298 PROP_cfg, /* properties_required */
299 0, /* properties_provided */
300 0, /* properties_destroyed */
301 0, /* todo_flags_start */
302 0, /* todo_flags_finish */
305 class pass_tree_no_loop : public gimple_opt_pass
307 public:
308 pass_tree_no_loop (gcc::context *ctxt)
309 : gimple_opt_pass (pass_data_tree_no_loop, ctxt)
312 /* opt_pass methods: */
313 virtual bool gate (function *fn) { return !gate_loop (fn); }
315 }; // class pass_tree_no_loop
317 } // anon namespace
319 gimple_opt_pass *
320 make_pass_tree_no_loop (gcc::context *ctxt)
322 return new pass_tree_no_loop (ctxt);
326 /* Loop optimizer initialization. */
328 namespace {
330 const pass_data pass_data_tree_loop_init =
332 GIMPLE_PASS, /* type */
333 "loopinit", /* name */
334 OPTGROUP_LOOP, /* optinfo_flags */
335 TV_NONE, /* tv_id */
336 PROP_cfg, /* properties_required */
337 0, /* properties_provided */
338 0, /* properties_destroyed */
339 0, /* todo_flags_start */
340 0, /* todo_flags_finish */
343 class pass_tree_loop_init : public gimple_opt_pass
345 public:
346 pass_tree_loop_init (gcc::context *ctxt)
347 : gimple_opt_pass (pass_data_tree_loop_init, ctxt)
350 /* opt_pass methods: */
351 virtual unsigned int execute (function *);
353 }; // class pass_tree_loop_init
355 unsigned int
356 pass_tree_loop_init::execute (function *fun ATTRIBUTE_UNUSED)
358 /* When processing a loop in the loop pipeline, we should be able to assert
359 that:
360 (loops_state_satisfies_p (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS
361 | LOOP_CLOSED_SSA)
362 && scev_initialized_p ())
364 loop_optimizer_init (LOOPS_NORMAL
365 | LOOPS_HAVE_RECORDED_EXITS);
366 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
367 scev_initialize ();
369 return 0;
372 } // anon namespace
374 gimple_opt_pass *
375 make_pass_tree_loop_init (gcc::context *ctxt)
377 return new pass_tree_loop_init (ctxt);
380 /* Loop autovectorization. */
382 namespace {
384 const pass_data pass_data_vectorize =
386 GIMPLE_PASS, /* type */
387 "vect", /* name */
388 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
389 TV_TREE_VECTORIZATION, /* tv_id */
390 ( PROP_cfg | PROP_ssa ), /* properties_required */
391 0, /* properties_provided */
392 0, /* properties_destroyed */
393 0, /* todo_flags_start */
394 0, /* todo_flags_finish */
397 class pass_vectorize : public gimple_opt_pass
399 public:
400 pass_vectorize (gcc::context *ctxt)
401 : gimple_opt_pass (pass_data_vectorize, ctxt)
404 /* opt_pass methods: */
405 virtual bool gate (function *fun)
407 return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
410 virtual unsigned int execute (function *);
412 }; // class pass_vectorize
414 unsigned int
415 pass_vectorize::execute (function *fun)
417 if (number_of_loops (fun) <= 1)
418 return 0;
420 return vectorize_loops ();
423 } // anon namespace
425 gimple_opt_pass *
426 make_pass_vectorize (gcc::context *ctxt)
428 return new pass_vectorize (ctxt);
431 /* Propagation of constants using scev. */
433 namespace {
435 const pass_data pass_data_scev_cprop =
437 GIMPLE_PASS, /* type */
438 "sccp", /* name */
439 OPTGROUP_LOOP, /* optinfo_flags */
440 TV_SCEV_CONST, /* tv_id */
441 ( PROP_cfg | PROP_ssa ), /* properties_required */
442 0, /* properties_provided */
443 0, /* properties_destroyed */
444 0, /* todo_flags_start */
445 ( TODO_cleanup_cfg
446 | TODO_update_ssa_only_virtuals ), /* todo_flags_finish */
449 class pass_scev_cprop : public gimple_opt_pass
451 public:
452 pass_scev_cprop (gcc::context *ctxt)
453 : gimple_opt_pass (pass_data_scev_cprop, ctxt)
456 /* opt_pass methods: */
457 virtual bool gate (function *) { return flag_tree_scev_cprop; }
458 virtual unsigned int execute (function *) { return scev_const_prop (); }
460 }; // class pass_scev_cprop
462 } // anon namespace
464 gimple_opt_pass *
465 make_pass_scev_cprop (gcc::context *ctxt)
467 return new pass_scev_cprop (ctxt);
470 /* Record bounds on numbers of iterations of loops. */
472 namespace {
474 const pass_data pass_data_record_bounds =
476 GIMPLE_PASS, /* type */
477 "*record_bounds", /* name */
478 OPTGROUP_NONE, /* optinfo_flags */
479 TV_TREE_LOOP_BOUNDS, /* tv_id */
480 ( PROP_cfg | PROP_ssa ), /* properties_required */
481 0, /* properties_provided */
482 0, /* properties_destroyed */
483 0, /* todo_flags_start */
484 0, /* todo_flags_finish */
487 class pass_record_bounds : public gimple_opt_pass
489 public:
490 pass_record_bounds (gcc::context *ctxt)
491 : gimple_opt_pass (pass_data_record_bounds, ctxt)
494 /* opt_pass methods: */
495 virtual unsigned int execute (function *);
497 }; // class pass_record_bounds
499 unsigned int
500 pass_record_bounds::execute (function *fun)
502 if (number_of_loops (fun) <= 1)
503 return 0;
505 estimate_numbers_of_iterations ();
506 scev_reset ();
507 return 0;
510 } // anon namespace
512 gimple_opt_pass *
513 make_pass_record_bounds (gcc::context *ctxt)
515 return new pass_record_bounds (ctxt);
518 /* Induction variable optimizations. */
520 namespace {
522 const pass_data pass_data_iv_optimize =
524 GIMPLE_PASS, /* type */
525 "ivopts", /* name */
526 OPTGROUP_LOOP, /* optinfo_flags */
527 TV_TREE_LOOP_IVOPTS, /* tv_id */
528 ( PROP_cfg | PROP_ssa ), /* properties_required */
529 0, /* properties_provided */
530 0, /* properties_destroyed */
531 0, /* todo_flags_start */
532 TODO_update_ssa, /* todo_flags_finish */
535 class pass_iv_optimize : public gimple_opt_pass
537 public:
538 pass_iv_optimize (gcc::context *ctxt)
539 : gimple_opt_pass (pass_data_iv_optimize, ctxt)
542 /* opt_pass methods: */
543 virtual bool gate (function *) { return flag_ivopts != 0; }
544 virtual unsigned int execute (function *);
546 }; // class pass_iv_optimize
548 unsigned int
549 pass_iv_optimize::execute (function *fun)
551 if (number_of_loops (fun) <= 1)
552 return 0;
554 tree_ssa_iv_optimize ();
555 return 0;
558 } // anon namespace
560 gimple_opt_pass *
561 make_pass_iv_optimize (gcc::context *ctxt)
563 return new pass_iv_optimize (ctxt);
566 /* Loop optimizer finalization. */
568 static unsigned int
569 tree_ssa_loop_done (void)
571 free_numbers_of_iterations_estimates (cfun);
572 scev_finalize ();
573 loop_optimizer_finalize ();
574 return 0;
577 namespace {
579 const pass_data pass_data_tree_loop_done =
581 GIMPLE_PASS, /* type */
582 "loopdone", /* name */
583 OPTGROUP_LOOP, /* optinfo_flags */
584 TV_NONE, /* tv_id */
585 PROP_cfg, /* properties_required */
586 0, /* properties_provided */
587 0, /* properties_destroyed */
588 0, /* todo_flags_start */
589 TODO_cleanup_cfg, /* todo_flags_finish */
592 class pass_tree_loop_done : public gimple_opt_pass
594 public:
595 pass_tree_loop_done (gcc::context *ctxt)
596 : gimple_opt_pass (pass_data_tree_loop_done, ctxt)
599 /* opt_pass methods: */
600 virtual unsigned int execute (function *) { return tree_ssa_loop_done (); }
602 }; // class pass_tree_loop_done
604 } // anon namespace
606 gimple_opt_pass *
607 make_pass_tree_loop_done (gcc::context *ctxt)
609 return new pass_tree_loop_done (ctxt);
612 /* Calls CBCK for each index in memory reference ADDR_P. There are two
613 kinds situations handled; in each of these cases, the memory reference
614 and DATA are passed to the callback:
616 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
617 pass the pointer to the index to the callback.
619 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
620 pointer to addr to the callback.
622 If the callback returns false, the whole search stops and false is returned.
623 Otherwise the function returns true after traversing through the whole
624 reference *ADDR_P. */
626 bool
627 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
629 tree *nxt, *idx;
631 for (; ; addr_p = nxt)
633 switch (TREE_CODE (*addr_p))
635 case SSA_NAME:
636 return cbck (*addr_p, addr_p, data);
638 case MEM_REF:
639 nxt = &TREE_OPERAND (*addr_p, 0);
640 return cbck (*addr_p, nxt, data);
642 case BIT_FIELD_REF:
643 case VIEW_CONVERT_EXPR:
644 case REALPART_EXPR:
645 case IMAGPART_EXPR:
646 nxt = &TREE_OPERAND (*addr_p, 0);
647 break;
649 case COMPONENT_REF:
650 /* If the component has varying offset, it behaves like index
651 as well. */
652 idx = &TREE_OPERAND (*addr_p, 2);
653 if (*idx
654 && !cbck (*addr_p, idx, data))
655 return false;
657 nxt = &TREE_OPERAND (*addr_p, 0);
658 break;
660 case ARRAY_REF:
661 case ARRAY_RANGE_REF:
662 nxt = &TREE_OPERAND (*addr_p, 0);
663 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
664 return false;
665 break;
667 case VAR_DECL:
668 case PARM_DECL:
669 case CONST_DECL:
670 case STRING_CST:
671 case RESULT_DECL:
672 case VECTOR_CST:
673 case COMPLEX_CST:
674 case INTEGER_CST:
675 case REAL_CST:
676 case FIXED_CST:
677 case CONSTRUCTOR:
678 return true;
680 case ADDR_EXPR:
681 gcc_assert (is_gimple_min_invariant (*addr_p));
682 return true;
684 case TARGET_MEM_REF:
685 idx = &TMR_BASE (*addr_p);
686 if (*idx
687 && !cbck (*addr_p, idx, data))
688 return false;
689 idx = &TMR_INDEX (*addr_p);
690 if (*idx
691 && !cbck (*addr_p, idx, data))
692 return false;
693 idx = &TMR_INDEX2 (*addr_p);
694 if (*idx
695 && !cbck (*addr_p, idx, data))
696 return false;
697 return true;
699 default:
700 gcc_unreachable ();
706 /* The name and the length of the currently generated variable
707 for lsm. */
708 #define MAX_LSM_NAME_LENGTH 40
709 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
710 static int lsm_tmp_name_length;
712 /* Adds S to lsm_tmp_name. */
714 static void
715 lsm_tmp_name_add (const char *s)
717 int l = strlen (s) + lsm_tmp_name_length;
718 if (l > MAX_LSM_NAME_LENGTH)
719 return;
721 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
722 lsm_tmp_name_length = l;
725 /* Stores the name for temporary variable that replaces REF to
726 lsm_tmp_name. */
728 static void
729 gen_lsm_tmp_name (tree ref)
731 const char *name;
733 switch (TREE_CODE (ref))
735 case MEM_REF:
736 case TARGET_MEM_REF:
737 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
738 lsm_tmp_name_add ("_");
739 break;
741 case ADDR_EXPR:
742 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
743 break;
745 case BIT_FIELD_REF:
746 case VIEW_CONVERT_EXPR:
747 case ARRAY_RANGE_REF:
748 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
749 break;
751 case REALPART_EXPR:
752 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
753 lsm_tmp_name_add ("_RE");
754 break;
756 case IMAGPART_EXPR:
757 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
758 lsm_tmp_name_add ("_IM");
759 break;
761 case COMPONENT_REF:
762 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
763 lsm_tmp_name_add ("_");
764 name = get_name (TREE_OPERAND (ref, 1));
765 if (!name)
766 name = "F";
767 lsm_tmp_name_add (name);
768 break;
770 case ARRAY_REF:
771 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
772 lsm_tmp_name_add ("_I");
773 break;
775 case SSA_NAME:
776 case VAR_DECL:
777 case PARM_DECL:
778 name = get_name (ref);
779 if (!name)
780 name = "D";
781 lsm_tmp_name_add (name);
782 break;
784 case STRING_CST:
785 lsm_tmp_name_add ("S");
786 break;
788 case RESULT_DECL:
789 lsm_tmp_name_add ("R");
790 break;
792 case INTEGER_CST:
793 /* Nothing. */
794 break;
796 default:
797 gcc_unreachable ();
801 /* Determines name for temporary variable that replaces REF.
802 The name is accumulated into the lsm_tmp_name variable.
803 N is added to the name of the temporary. */
805 char *
806 get_lsm_tmp_name (tree ref, unsigned n, const char *suffix)
808 char ns[2];
810 lsm_tmp_name_length = 0;
811 gen_lsm_tmp_name (ref);
812 lsm_tmp_name_add ("_lsm");
813 if (n < 10)
815 ns[0] = '0' + n;
816 ns[1] = 0;
817 lsm_tmp_name_add (ns);
819 return lsm_tmp_name;
820 if (suffix != NULL)
821 lsm_tmp_name_add (suffix);
824 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
826 unsigned
827 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
829 basic_block *body = get_loop_body (loop);
830 gimple_stmt_iterator gsi;
831 unsigned size = 0, i;
833 for (i = 0; i < loop->num_nodes; i++)
834 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
835 size += estimate_num_insns (gsi_stmt (gsi), weights);
836 free (body);
838 return size;