Small ChangeLog tweak.
[official-gcc.git] / gcc / tree-ssa-loop.c
blob10c43f32ba38764dc905cb53ecaea6c49bbeeb43
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2017 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 "memmodel.h"
28 #include "tm_p.h"
29 #include "fold-const.h"
30 #include "gimple-iterator.h"
31 #include "tree-ssa-loop-ivopts.h"
32 #include "tree-ssa-loop-manip.h"
33 #include "tree-ssa-loop-niter.h"
34 #include "tree-ssa-loop.h"
35 #include "cfgloop.h"
36 #include "tree-inline.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-vectorizer.h"
39 #include "omp-general.h"
40 #include "diagnostic-core.h"
43 /* A pass making sure loops are fixed up. */
45 namespace {
47 const pass_data pass_data_fix_loops =
49 GIMPLE_PASS, /* type */
50 "fix_loops", /* name */
51 OPTGROUP_LOOP, /* optinfo_flags */
52 TV_TREE_LOOP, /* tv_id */
53 PROP_cfg, /* properties_required */
54 0, /* properties_provided */
55 0, /* properties_destroyed */
56 0, /* todo_flags_start */
57 0, /* todo_flags_finish */
60 class pass_fix_loops : public gimple_opt_pass
62 public:
63 pass_fix_loops (gcc::context *ctxt)
64 : gimple_opt_pass (pass_data_fix_loops, ctxt)
67 /* opt_pass methods: */
68 virtual bool gate (function *) { return flag_tree_loop_optimize; }
70 virtual unsigned int execute (function *fn);
71 }; // class pass_fix_loops
73 unsigned int
74 pass_fix_loops::execute (function *)
76 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
78 calculate_dominance_info (CDI_DOMINATORS);
79 fix_loop_structure (NULL);
81 return 0;
84 } // anon namespace
86 gimple_opt_pass *
87 make_pass_fix_loops (gcc::context *ctxt)
89 return new pass_fix_loops (ctxt);
93 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
94 but we also avoid running it when the IL doesn't contain any loop. */
96 static bool
97 gate_loop (function *fn)
99 if (!flag_tree_loop_optimize)
100 return false;
102 /* For -fdump-passes which runs before loop discovery print the
103 state of -ftree-loop-optimize. */
104 if (!loops_for_fn (fn))
105 return true;
107 return number_of_loops (fn) > 1;
110 /* The loop superpass. */
112 namespace {
114 const pass_data pass_data_tree_loop =
116 GIMPLE_PASS, /* type */
117 "loop", /* name */
118 OPTGROUP_LOOP, /* optinfo_flags */
119 TV_TREE_LOOP, /* tv_id */
120 PROP_cfg, /* properties_required */
121 0, /* properties_provided */
122 0, /* properties_destroyed */
123 0, /* todo_flags_start */
124 0, /* todo_flags_finish */
127 class pass_tree_loop : public gimple_opt_pass
129 public:
130 pass_tree_loop (gcc::context *ctxt)
131 : gimple_opt_pass (pass_data_tree_loop, ctxt)
134 /* opt_pass methods: */
135 virtual bool gate (function *fn) { return gate_loop (fn); }
137 }; // class pass_tree_loop
139 } // anon namespace
141 gimple_opt_pass *
142 make_pass_tree_loop (gcc::context *ctxt)
144 return new pass_tree_loop (ctxt);
147 /* Gate for oacc kernels pass group. */
149 static bool
150 gate_oacc_kernels (function *fn)
152 if (!flag_openacc)
153 return false;
155 if (!lookup_attribute ("oacc kernels", DECL_ATTRIBUTES (fn->decl)))
156 return false;
158 struct loop *loop;
159 FOR_EACH_LOOP (loop, 0)
160 if (loop->in_oacc_kernels_region)
161 return true;
163 return false;
166 /* The oacc kernels superpass. */
168 namespace {
170 const pass_data pass_data_oacc_kernels =
172 GIMPLE_PASS, /* type */
173 "oacc_kernels", /* name */
174 OPTGROUP_LOOP, /* optinfo_flags */
175 TV_TREE_LOOP, /* tv_id */
176 PROP_cfg, /* properties_required */
177 0, /* properties_provided */
178 0, /* properties_destroyed */
179 0, /* todo_flags_start */
180 0, /* todo_flags_finish */
183 class pass_oacc_kernels : public gimple_opt_pass
185 public:
186 pass_oacc_kernels (gcc::context *ctxt)
187 : gimple_opt_pass (pass_data_oacc_kernels, ctxt)
190 /* opt_pass methods: */
191 virtual bool gate (function *fn) { return gate_oacc_kernels (fn); }
193 }; // class pass_oacc_kernels
195 } // anon namespace
197 gimple_opt_pass *
198 make_pass_oacc_kernels (gcc::context *ctxt)
200 return new pass_oacc_kernels (ctxt);
203 /* The ipa oacc superpass. */
205 namespace {
207 const pass_data pass_data_ipa_oacc =
209 SIMPLE_IPA_PASS, /* type */
210 "ipa_oacc", /* name */
211 OPTGROUP_LOOP, /* optinfo_flags */
212 TV_TREE_LOOP, /* tv_id */
213 PROP_cfg, /* properties_required */
214 0, /* properties_provided */
215 0, /* properties_destroyed */
216 0, /* todo_flags_start */
217 0, /* todo_flags_finish */
220 class pass_ipa_oacc : public simple_ipa_opt_pass
222 public:
223 pass_ipa_oacc (gcc::context *ctxt)
224 : simple_ipa_opt_pass (pass_data_ipa_oacc, ctxt)
227 /* opt_pass methods: */
228 virtual bool gate (function *)
230 return (optimize
231 && flag_openacc
232 /* Don't bother doing anything if the program has errors. */
233 && !seen_error ());
236 }; // class pass_ipa_oacc
238 } // anon namespace
240 simple_ipa_opt_pass *
241 make_pass_ipa_oacc (gcc::context *ctxt)
243 return new pass_ipa_oacc (ctxt);
246 /* The ipa oacc kernels pass. */
248 namespace {
250 const pass_data pass_data_ipa_oacc_kernels =
252 SIMPLE_IPA_PASS, /* type */
253 "ipa_oacc_kernels", /* name */
254 OPTGROUP_LOOP, /* optinfo_flags */
255 TV_TREE_LOOP, /* tv_id */
256 PROP_cfg, /* properties_required */
257 0, /* properties_provided */
258 0, /* properties_destroyed */
259 0, /* todo_flags_start */
260 0, /* todo_flags_finish */
263 class pass_ipa_oacc_kernels : public simple_ipa_opt_pass
265 public:
266 pass_ipa_oacc_kernels (gcc::context *ctxt)
267 : simple_ipa_opt_pass (pass_data_ipa_oacc_kernels, ctxt)
270 }; // class pass_ipa_oacc_kernels
272 } // anon namespace
274 simple_ipa_opt_pass *
275 make_pass_ipa_oacc_kernels (gcc::context *ctxt)
277 return new pass_ipa_oacc_kernels (ctxt);
280 /* The no-loop superpass. */
282 namespace {
284 const pass_data pass_data_tree_no_loop =
286 GIMPLE_PASS, /* type */
287 "no_loop", /* name */
288 OPTGROUP_NONE, /* optinfo_flags */
289 TV_TREE_NOLOOP, /* tv_id */
290 PROP_cfg, /* properties_required */
291 0, /* properties_provided */
292 0, /* properties_destroyed */
293 0, /* todo_flags_start */
294 0, /* todo_flags_finish */
297 class pass_tree_no_loop : public gimple_opt_pass
299 public:
300 pass_tree_no_loop (gcc::context *ctxt)
301 : gimple_opt_pass (pass_data_tree_no_loop, ctxt)
304 /* opt_pass methods: */
305 virtual bool gate (function *fn) { return !gate_loop (fn); }
307 }; // class pass_tree_no_loop
309 } // anon namespace
311 gimple_opt_pass *
312 make_pass_tree_no_loop (gcc::context *ctxt)
314 return new pass_tree_no_loop (ctxt);
318 /* Loop optimizer initialization. */
320 namespace {
322 const pass_data pass_data_tree_loop_init =
324 GIMPLE_PASS, /* type */
325 "loopinit", /* name */
326 OPTGROUP_LOOP, /* optinfo_flags */
327 TV_NONE, /* tv_id */
328 PROP_cfg, /* properties_required */
329 0, /* properties_provided */
330 0, /* properties_destroyed */
331 0, /* todo_flags_start */
332 0, /* todo_flags_finish */
335 class pass_tree_loop_init : public gimple_opt_pass
337 public:
338 pass_tree_loop_init (gcc::context *ctxt)
339 : gimple_opt_pass (pass_data_tree_loop_init, ctxt)
342 /* opt_pass methods: */
343 virtual unsigned int execute (function *);
345 }; // class pass_tree_loop_init
347 unsigned int
348 pass_tree_loop_init::execute (function *fun ATTRIBUTE_UNUSED)
350 /* When processing a loop in the loop pipeline, we should be able to assert
351 that:
352 (loops_state_satisfies_p (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS
353 | LOOP_CLOSED_SSA)
354 && scev_initialized_p ())
356 loop_optimizer_init (LOOPS_NORMAL
357 | LOOPS_HAVE_RECORDED_EXITS);
358 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
359 scev_initialize ();
361 return 0;
364 } // anon namespace
366 gimple_opt_pass *
367 make_pass_tree_loop_init (gcc::context *ctxt)
369 return new pass_tree_loop_init (ctxt);
372 /* Loop autovectorization. */
374 namespace {
376 const pass_data pass_data_vectorize =
378 GIMPLE_PASS, /* type */
379 "vect", /* name */
380 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
381 TV_TREE_VECTORIZATION, /* tv_id */
382 ( PROP_cfg | PROP_ssa ), /* properties_required */
383 0, /* properties_provided */
384 0, /* properties_destroyed */
385 0, /* todo_flags_start */
386 0, /* todo_flags_finish */
389 class pass_vectorize : public gimple_opt_pass
391 public:
392 pass_vectorize (gcc::context *ctxt)
393 : gimple_opt_pass (pass_data_vectorize, ctxt)
396 /* opt_pass methods: */
397 virtual bool gate (function *fun)
399 return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
402 virtual unsigned int execute (function *);
404 }; // class pass_vectorize
406 unsigned int
407 pass_vectorize::execute (function *fun)
409 if (number_of_loops (fun) <= 1)
410 return 0;
412 return vectorize_loops ();
415 } // anon namespace
417 gimple_opt_pass *
418 make_pass_vectorize (gcc::context *ctxt)
420 return new pass_vectorize (ctxt);
423 /* Propagation of constants using scev. */
425 namespace {
427 const pass_data pass_data_scev_cprop =
429 GIMPLE_PASS, /* type */
430 "sccp", /* name */
431 OPTGROUP_LOOP, /* optinfo_flags */
432 TV_SCEV_CONST, /* tv_id */
433 ( PROP_cfg | PROP_ssa ), /* properties_required */
434 0, /* properties_provided */
435 0, /* properties_destroyed */
436 0, /* todo_flags_start */
437 ( TODO_cleanup_cfg
438 | TODO_update_ssa_only_virtuals ), /* todo_flags_finish */
441 class pass_scev_cprop : public gimple_opt_pass
443 public:
444 pass_scev_cprop (gcc::context *ctxt)
445 : gimple_opt_pass (pass_data_scev_cprop, ctxt)
448 /* opt_pass methods: */
449 virtual bool gate (function *) { return flag_tree_scev_cprop; }
450 virtual unsigned int execute (function *) { return scev_const_prop (); }
452 }; // class pass_scev_cprop
454 } // anon namespace
456 gimple_opt_pass *
457 make_pass_scev_cprop (gcc::context *ctxt)
459 return new pass_scev_cprop (ctxt);
462 /* Record bounds on numbers of iterations of loops. */
464 namespace {
466 const pass_data pass_data_record_bounds =
468 GIMPLE_PASS, /* type */
469 "*record_bounds", /* name */
470 OPTGROUP_NONE, /* optinfo_flags */
471 TV_TREE_LOOP_BOUNDS, /* tv_id */
472 ( PROP_cfg | PROP_ssa ), /* properties_required */
473 0, /* properties_provided */
474 0, /* properties_destroyed */
475 0, /* todo_flags_start */
476 0, /* todo_flags_finish */
479 class pass_record_bounds : public gimple_opt_pass
481 public:
482 pass_record_bounds (gcc::context *ctxt)
483 : gimple_opt_pass (pass_data_record_bounds, ctxt)
486 /* opt_pass methods: */
487 virtual unsigned int execute (function *);
489 }; // class pass_record_bounds
491 unsigned int
492 pass_record_bounds::execute (function *fun)
494 if (number_of_loops (fun) <= 1)
495 return 0;
497 estimate_numbers_of_iterations ();
498 scev_reset ();
499 return 0;
502 } // anon namespace
504 gimple_opt_pass *
505 make_pass_record_bounds (gcc::context *ctxt)
507 return new pass_record_bounds (ctxt);
510 /* Induction variable optimizations. */
512 namespace {
514 const pass_data pass_data_iv_optimize =
516 GIMPLE_PASS, /* type */
517 "ivopts", /* name */
518 OPTGROUP_LOOP, /* optinfo_flags */
519 TV_TREE_LOOP_IVOPTS, /* tv_id */
520 ( PROP_cfg | PROP_ssa ), /* properties_required */
521 0, /* properties_provided */
522 0, /* properties_destroyed */
523 0, /* todo_flags_start */
524 TODO_update_ssa, /* todo_flags_finish */
527 class pass_iv_optimize : public gimple_opt_pass
529 public:
530 pass_iv_optimize (gcc::context *ctxt)
531 : gimple_opt_pass (pass_data_iv_optimize, ctxt)
534 /* opt_pass methods: */
535 virtual bool gate (function *) { return flag_ivopts != 0; }
536 virtual unsigned int execute (function *);
538 }; // class pass_iv_optimize
540 unsigned int
541 pass_iv_optimize::execute (function *fun)
543 if (number_of_loops (fun) <= 1)
544 return 0;
546 tree_ssa_iv_optimize ();
547 return 0;
550 } // anon namespace
552 gimple_opt_pass *
553 make_pass_iv_optimize (gcc::context *ctxt)
555 return new pass_iv_optimize (ctxt);
558 /* Loop optimizer finalization. */
560 static unsigned int
561 tree_ssa_loop_done (void)
563 free_numbers_of_iterations_estimates (cfun);
564 scev_finalize ();
565 loop_optimizer_finalize ();
566 return 0;
569 namespace {
571 const pass_data pass_data_tree_loop_done =
573 GIMPLE_PASS, /* type */
574 "loopdone", /* name */
575 OPTGROUP_LOOP, /* optinfo_flags */
576 TV_NONE, /* tv_id */
577 PROP_cfg, /* properties_required */
578 0, /* properties_provided */
579 0, /* properties_destroyed */
580 0, /* todo_flags_start */
581 TODO_cleanup_cfg, /* todo_flags_finish */
584 class pass_tree_loop_done : public gimple_opt_pass
586 public:
587 pass_tree_loop_done (gcc::context *ctxt)
588 : gimple_opt_pass (pass_data_tree_loop_done, ctxt)
591 /* opt_pass methods: */
592 virtual unsigned int execute (function *) { return tree_ssa_loop_done (); }
594 }; // class pass_tree_loop_done
596 } // anon namespace
598 gimple_opt_pass *
599 make_pass_tree_loop_done (gcc::context *ctxt)
601 return new pass_tree_loop_done (ctxt);
604 /* Calls CBCK for each index in memory reference ADDR_P. There are two
605 kinds situations handled; in each of these cases, the memory reference
606 and DATA are passed to the callback:
608 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
609 pass the pointer to the index to the callback.
611 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
612 pointer to addr to the callback.
614 If the callback returns false, the whole search stops and false is returned.
615 Otherwise the function returns true after traversing through the whole
616 reference *ADDR_P. */
618 bool
619 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
621 tree *nxt, *idx;
623 for (; ; addr_p = nxt)
625 switch (TREE_CODE (*addr_p))
627 case SSA_NAME:
628 return cbck (*addr_p, addr_p, data);
630 case MEM_REF:
631 nxt = &TREE_OPERAND (*addr_p, 0);
632 return cbck (*addr_p, nxt, data);
634 case BIT_FIELD_REF:
635 case VIEW_CONVERT_EXPR:
636 case REALPART_EXPR:
637 case IMAGPART_EXPR:
638 nxt = &TREE_OPERAND (*addr_p, 0);
639 break;
641 case COMPONENT_REF:
642 /* If the component has varying offset, it behaves like index
643 as well. */
644 idx = &TREE_OPERAND (*addr_p, 2);
645 if (*idx
646 && !cbck (*addr_p, idx, data))
647 return false;
649 nxt = &TREE_OPERAND (*addr_p, 0);
650 break;
652 case ARRAY_REF:
653 case ARRAY_RANGE_REF:
654 nxt = &TREE_OPERAND (*addr_p, 0);
655 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
656 return false;
657 break;
659 case VAR_DECL:
660 case PARM_DECL:
661 case CONST_DECL:
662 case STRING_CST:
663 case RESULT_DECL:
664 case VECTOR_CST:
665 case COMPLEX_CST:
666 case INTEGER_CST:
667 case REAL_CST:
668 case FIXED_CST:
669 case CONSTRUCTOR:
670 return true;
672 case ADDR_EXPR:
673 gcc_assert (is_gimple_min_invariant (*addr_p));
674 return true;
676 case TARGET_MEM_REF:
677 idx = &TMR_BASE (*addr_p);
678 if (*idx
679 && !cbck (*addr_p, idx, data))
680 return false;
681 idx = &TMR_INDEX (*addr_p);
682 if (*idx
683 && !cbck (*addr_p, idx, data))
684 return false;
685 idx = &TMR_INDEX2 (*addr_p);
686 if (*idx
687 && !cbck (*addr_p, idx, data))
688 return false;
689 return true;
691 default:
692 gcc_unreachable ();
698 /* The name and the length of the currently generated variable
699 for lsm. */
700 #define MAX_LSM_NAME_LENGTH 40
701 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
702 static int lsm_tmp_name_length;
704 /* Adds S to lsm_tmp_name. */
706 static void
707 lsm_tmp_name_add (const char *s)
709 int l = strlen (s) + lsm_tmp_name_length;
710 if (l > MAX_LSM_NAME_LENGTH)
711 return;
713 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
714 lsm_tmp_name_length = l;
717 /* Stores the name for temporary variable that replaces REF to
718 lsm_tmp_name. */
720 static void
721 gen_lsm_tmp_name (tree ref)
723 const char *name;
725 switch (TREE_CODE (ref))
727 case MEM_REF:
728 case TARGET_MEM_REF:
729 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
730 lsm_tmp_name_add ("_");
731 break;
733 case ADDR_EXPR:
734 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
735 break;
737 case BIT_FIELD_REF:
738 case VIEW_CONVERT_EXPR:
739 case ARRAY_RANGE_REF:
740 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
741 break;
743 case REALPART_EXPR:
744 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
745 lsm_tmp_name_add ("_RE");
746 break;
748 case IMAGPART_EXPR:
749 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
750 lsm_tmp_name_add ("_IM");
751 break;
753 case COMPONENT_REF:
754 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
755 lsm_tmp_name_add ("_");
756 name = get_name (TREE_OPERAND (ref, 1));
757 if (!name)
758 name = "F";
759 lsm_tmp_name_add (name);
760 break;
762 case ARRAY_REF:
763 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
764 lsm_tmp_name_add ("_I");
765 break;
767 case SSA_NAME:
768 case VAR_DECL:
769 case PARM_DECL:
770 case FUNCTION_DECL:
771 case LABEL_DECL:
772 name = get_name (ref);
773 if (!name)
774 name = "D";
775 lsm_tmp_name_add (name);
776 break;
778 case STRING_CST:
779 lsm_tmp_name_add ("S");
780 break;
782 case RESULT_DECL:
783 lsm_tmp_name_add ("R");
784 break;
786 case INTEGER_CST:
787 default:
788 /* Nothing. */
789 break;
793 /* Determines name for temporary variable that replaces REF.
794 The name is accumulated into the lsm_tmp_name variable.
795 N is added to the name of the temporary. */
797 char *
798 get_lsm_tmp_name (tree ref, unsigned n, const char *suffix)
800 char ns[2];
802 lsm_tmp_name_length = 0;
803 gen_lsm_tmp_name (ref);
804 lsm_tmp_name_add ("_lsm");
805 if (n < 10)
807 ns[0] = '0' + n;
808 ns[1] = 0;
809 lsm_tmp_name_add (ns);
811 return lsm_tmp_name;
812 if (suffix != NULL)
813 lsm_tmp_name_add (suffix);
816 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
818 unsigned
819 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
821 basic_block *body = get_loop_body (loop);
822 gimple_stmt_iterator gsi;
823 unsigned size = 0, i;
825 for (i = 0; i < loop->num_nodes; i++)
826 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
827 size += estimate_num_insns (gsi_stmt (gsi), weights);
828 free (body);
830 return size;