2015-07-10 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / tree-ssa-loop.c
blob609aff9793b8162648fa78fab13fd615e0083fc1
1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2015 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 "hard-reg-set.h"
27 #include "alias.h"
28 #include "fold-const.h"
29 #include "tm_p.h"
30 #include "internal-fn.h"
31 #include "gimple-iterator.h"
32 #include "tree-ssa-loop-ivopts.h"
33 #include "tree-ssa-loop-manip.h"
34 #include "tree-ssa-loop-niter.h"
35 #include "tree-ssa-loop.h"
36 #include "tree-pass.h"
37 #include "cfgloop.h"
38 #include "flags.h"
39 #include "tree-inline.h"
40 #include "tree-scalar-evolution.h"
41 #include "diagnostic-core.h"
42 #include "tree-vectorizer.h"
45 /* A pass making sure loops are fixed up. */
47 namespace {
49 const pass_data pass_data_fix_loops =
51 GIMPLE_PASS, /* type */
52 "fix_loops", /* name */
53 OPTGROUP_LOOP, /* optinfo_flags */
54 TV_TREE_LOOP, /* tv_id */
55 PROP_cfg, /* properties_required */
56 0, /* properties_provided */
57 0, /* properties_destroyed */
58 0, /* todo_flags_start */
59 0, /* todo_flags_finish */
62 class pass_fix_loops : public gimple_opt_pass
64 public:
65 pass_fix_loops (gcc::context *ctxt)
66 : gimple_opt_pass (pass_data_fix_loops, ctxt)
69 /* opt_pass methods: */
70 virtual bool gate (function *) { return flag_tree_loop_optimize; }
72 virtual unsigned int execute (function *fn);
73 }; // class pass_fix_loops
75 unsigned int
76 pass_fix_loops::execute (function *)
78 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
80 calculate_dominance_info (CDI_DOMINATORS);
81 fix_loop_structure (NULL);
83 return 0;
86 } // anon namespace
88 gimple_opt_pass *
89 make_pass_fix_loops (gcc::context *ctxt)
91 return new pass_fix_loops (ctxt);
95 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
96 but we also avoid running it when the IL doesn't contain any loop. */
98 static bool
99 gate_loop (function *fn)
101 if (!flag_tree_loop_optimize)
102 return false;
104 /* For -fdump-passes which runs before loop discovery print the
105 state of -ftree-loop-optimize. */
106 if (!loops_for_fn (fn))
107 return true;
109 return number_of_loops (fn) > 1;
112 /* The loop superpass. */
114 namespace {
116 const pass_data pass_data_tree_loop =
118 GIMPLE_PASS, /* type */
119 "loop", /* name */
120 OPTGROUP_LOOP, /* optinfo_flags */
121 TV_TREE_LOOP, /* tv_id */
122 PROP_cfg, /* properties_required */
123 0, /* properties_provided */
124 0, /* properties_destroyed */
125 0, /* todo_flags_start */
126 0, /* todo_flags_finish */
129 class pass_tree_loop : public gimple_opt_pass
131 public:
132 pass_tree_loop (gcc::context *ctxt)
133 : gimple_opt_pass (pass_data_tree_loop, ctxt)
136 /* opt_pass methods: */
137 virtual bool gate (function *fn) { return gate_loop (fn); }
139 }; // class pass_tree_loop
141 } // anon namespace
143 gimple_opt_pass *
144 make_pass_tree_loop (gcc::context *ctxt)
146 return new pass_tree_loop (ctxt);
149 /* The no-loop superpass. */
151 namespace {
153 const pass_data pass_data_tree_no_loop =
155 GIMPLE_PASS, /* type */
156 "no_loop", /* name */
157 OPTGROUP_NONE, /* optinfo_flags */
158 TV_TREE_NOLOOP, /* tv_id */
159 PROP_cfg, /* properties_required */
160 0, /* properties_provided */
161 0, /* properties_destroyed */
162 0, /* todo_flags_start */
163 0, /* todo_flags_finish */
166 class pass_tree_no_loop : public gimple_opt_pass
168 public:
169 pass_tree_no_loop (gcc::context *ctxt)
170 : gimple_opt_pass (pass_data_tree_no_loop, ctxt)
173 /* opt_pass methods: */
174 virtual bool gate (function *fn) { return !gate_loop (fn); }
176 }; // class pass_tree_no_loop
178 } // anon namespace
180 gimple_opt_pass *
181 make_pass_tree_no_loop (gcc::context *ctxt)
183 return new pass_tree_no_loop (ctxt);
187 /* Loop optimizer initialization. */
189 namespace {
191 const pass_data pass_data_tree_loop_init =
193 GIMPLE_PASS, /* type */
194 "loopinit", /* name */
195 OPTGROUP_LOOP, /* optinfo_flags */
196 TV_NONE, /* tv_id */
197 PROP_cfg, /* properties_required */
198 0, /* properties_provided */
199 0, /* properties_destroyed */
200 0, /* todo_flags_start */
201 0, /* todo_flags_finish */
204 class pass_tree_loop_init : public gimple_opt_pass
206 public:
207 pass_tree_loop_init (gcc::context *ctxt)
208 : gimple_opt_pass (pass_data_tree_loop_init, ctxt)
211 /* opt_pass methods: */
212 virtual unsigned int execute (function *);
214 }; // class pass_tree_loop_init
216 unsigned int
217 pass_tree_loop_init::execute (function *fun ATTRIBUTE_UNUSED)
219 loop_optimizer_init (LOOPS_NORMAL
220 | LOOPS_HAVE_RECORDED_EXITS);
221 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
223 /* We might discover new loops, e.g. when turning irreducible
224 regions into reducible. */
225 scev_initialize ();
227 return 0;
230 } // anon namespace
232 gimple_opt_pass *
233 make_pass_tree_loop_init (gcc::context *ctxt)
235 return new pass_tree_loop_init (ctxt);
238 /* Loop autovectorization. */
240 namespace {
242 const pass_data pass_data_vectorize =
244 GIMPLE_PASS, /* type */
245 "vect", /* name */
246 OPTGROUP_LOOP | OPTGROUP_VEC, /* optinfo_flags */
247 TV_TREE_VECTORIZATION, /* tv_id */
248 ( PROP_cfg | PROP_ssa ), /* properties_required */
249 0, /* properties_provided */
250 0, /* properties_destroyed */
251 0, /* todo_flags_start */
252 0, /* todo_flags_finish */
255 class pass_vectorize : public gimple_opt_pass
257 public:
258 pass_vectorize (gcc::context *ctxt)
259 : gimple_opt_pass (pass_data_vectorize, ctxt)
262 /* opt_pass methods: */
263 virtual bool gate (function *fun)
265 return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
268 virtual unsigned int execute (function *);
270 }; // class pass_vectorize
272 unsigned int
273 pass_vectorize::execute (function *fun)
275 if (number_of_loops (fun) <= 1)
276 return 0;
278 return vectorize_loops ();
281 } // anon namespace
283 gimple_opt_pass *
284 make_pass_vectorize (gcc::context *ctxt)
286 return new pass_vectorize (ctxt);
289 /* Check the correctness of the data dependence analyzers. */
291 namespace {
293 const pass_data pass_data_check_data_deps =
295 GIMPLE_PASS, /* type */
296 "ckdd", /* name */
297 OPTGROUP_LOOP, /* optinfo_flags */
298 TV_CHECK_DATA_DEPS, /* tv_id */
299 ( PROP_cfg | PROP_ssa ), /* properties_required */
300 0, /* properties_provided */
301 0, /* properties_destroyed */
302 0, /* todo_flags_start */
303 0, /* todo_flags_finish */
306 class pass_check_data_deps : public gimple_opt_pass
308 public:
309 pass_check_data_deps (gcc::context *ctxt)
310 : gimple_opt_pass (pass_data_check_data_deps, ctxt)
313 /* opt_pass methods: */
314 virtual bool gate (function *) { return flag_check_data_deps != 0; }
315 virtual unsigned int execute (function *);
317 }; // class pass_check_data_deps
319 unsigned int
320 pass_check_data_deps::execute (function *fun)
322 if (number_of_loops (fun) <= 1)
323 return 0;
325 tree_check_data_deps ();
326 return 0;
329 } // anon namespace
331 gimple_opt_pass *
332 make_pass_check_data_deps (gcc::context *ctxt)
334 return new pass_check_data_deps (ctxt);
337 /* Propagation of constants using scev. */
339 namespace {
341 const pass_data pass_data_scev_cprop =
343 GIMPLE_PASS, /* type */
344 "sccp", /* name */
345 OPTGROUP_LOOP, /* optinfo_flags */
346 TV_SCEV_CONST, /* tv_id */
347 ( PROP_cfg | PROP_ssa ), /* properties_required */
348 0, /* properties_provided */
349 0, /* properties_destroyed */
350 0, /* todo_flags_start */
351 ( TODO_cleanup_cfg
352 | TODO_update_ssa_only_virtuals ), /* todo_flags_finish */
355 class pass_scev_cprop : public gimple_opt_pass
357 public:
358 pass_scev_cprop (gcc::context *ctxt)
359 : gimple_opt_pass (pass_data_scev_cprop, ctxt)
362 /* opt_pass methods: */
363 virtual bool gate (function *) { return flag_tree_scev_cprop; }
364 virtual unsigned int execute (function *) { return scev_const_prop (); }
366 }; // class pass_scev_cprop
368 } // anon namespace
370 gimple_opt_pass *
371 make_pass_scev_cprop (gcc::context *ctxt)
373 return new pass_scev_cprop (ctxt);
376 /* Record bounds on numbers of iterations of loops. */
378 namespace {
380 const pass_data pass_data_record_bounds =
382 GIMPLE_PASS, /* type */
383 "*record_bounds", /* name */
384 OPTGROUP_NONE, /* optinfo_flags */
385 TV_TREE_LOOP_BOUNDS, /* tv_id */
386 ( PROP_cfg | PROP_ssa ), /* properties_required */
387 0, /* properties_provided */
388 0, /* properties_destroyed */
389 0, /* todo_flags_start */
390 0, /* todo_flags_finish */
393 class pass_record_bounds : public gimple_opt_pass
395 public:
396 pass_record_bounds (gcc::context *ctxt)
397 : gimple_opt_pass (pass_data_record_bounds, ctxt)
400 /* opt_pass methods: */
401 virtual unsigned int execute (function *);
403 }; // class pass_record_bounds
405 unsigned int
406 pass_record_bounds::execute (function *fun)
408 if (number_of_loops (fun) <= 1)
409 return 0;
411 estimate_numbers_of_iterations ();
412 scev_reset ();
413 return 0;
416 } // anon namespace
418 gimple_opt_pass *
419 make_pass_record_bounds (gcc::context *ctxt)
421 return new pass_record_bounds (ctxt);
424 /* Induction variable optimizations. */
426 namespace {
428 const pass_data pass_data_iv_optimize =
430 GIMPLE_PASS, /* type */
431 "ivopts", /* name */
432 OPTGROUP_LOOP, /* optinfo_flags */
433 TV_TREE_LOOP_IVOPTS, /* tv_id */
434 ( PROP_cfg | PROP_ssa ), /* properties_required */
435 0, /* properties_provided */
436 0, /* properties_destroyed */
437 0, /* todo_flags_start */
438 TODO_update_ssa, /* todo_flags_finish */
441 class pass_iv_optimize : public gimple_opt_pass
443 public:
444 pass_iv_optimize (gcc::context *ctxt)
445 : gimple_opt_pass (pass_data_iv_optimize, ctxt)
448 /* opt_pass methods: */
449 virtual bool gate (function *) { return flag_ivopts != 0; }
450 virtual unsigned int execute (function *);
452 }; // class pass_iv_optimize
454 unsigned int
455 pass_iv_optimize::execute (function *fun)
457 if (number_of_loops (fun) <= 1)
458 return 0;
460 tree_ssa_iv_optimize ();
461 return 0;
464 } // anon namespace
466 gimple_opt_pass *
467 make_pass_iv_optimize (gcc::context *ctxt)
469 return new pass_iv_optimize (ctxt);
472 /* Loop optimizer finalization. */
474 static unsigned int
475 tree_ssa_loop_done (void)
477 free_numbers_of_iterations_estimates ();
478 scev_finalize ();
479 loop_optimizer_finalize ();
480 return 0;
483 namespace {
485 const pass_data pass_data_tree_loop_done =
487 GIMPLE_PASS, /* type */
488 "loopdone", /* name */
489 OPTGROUP_LOOP, /* optinfo_flags */
490 TV_NONE, /* tv_id */
491 PROP_cfg, /* properties_required */
492 0, /* properties_provided */
493 0, /* properties_destroyed */
494 0, /* todo_flags_start */
495 TODO_cleanup_cfg, /* todo_flags_finish */
498 class pass_tree_loop_done : public gimple_opt_pass
500 public:
501 pass_tree_loop_done (gcc::context *ctxt)
502 : gimple_opt_pass (pass_data_tree_loop_done, ctxt)
505 /* opt_pass methods: */
506 virtual unsigned int execute (function *) { return tree_ssa_loop_done (); }
508 }; // class pass_tree_loop_done
510 } // anon namespace
512 gimple_opt_pass *
513 make_pass_tree_loop_done (gcc::context *ctxt)
515 return new pass_tree_loop_done (ctxt);
518 /* Calls CBCK for each index in memory reference ADDR_P. There are two
519 kinds situations handled; in each of these cases, the memory reference
520 and DATA are passed to the callback:
522 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
523 pass the pointer to the index to the callback.
525 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
526 pointer to addr to the callback.
528 If the callback returns false, the whole search stops and false is returned.
529 Otherwise the function returns true after traversing through the whole
530 reference *ADDR_P. */
532 bool
533 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
535 tree *nxt, *idx;
537 for (; ; addr_p = nxt)
539 switch (TREE_CODE (*addr_p))
541 case SSA_NAME:
542 return cbck (*addr_p, addr_p, data);
544 case MEM_REF:
545 nxt = &TREE_OPERAND (*addr_p, 0);
546 return cbck (*addr_p, nxt, data);
548 case BIT_FIELD_REF:
549 case VIEW_CONVERT_EXPR:
550 case REALPART_EXPR:
551 case IMAGPART_EXPR:
552 nxt = &TREE_OPERAND (*addr_p, 0);
553 break;
555 case COMPONENT_REF:
556 /* If the component has varying offset, it behaves like index
557 as well. */
558 idx = &TREE_OPERAND (*addr_p, 2);
559 if (*idx
560 && !cbck (*addr_p, idx, data))
561 return false;
563 nxt = &TREE_OPERAND (*addr_p, 0);
564 break;
566 case ARRAY_REF:
567 case ARRAY_RANGE_REF:
568 nxt = &TREE_OPERAND (*addr_p, 0);
569 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
570 return false;
571 break;
573 case VAR_DECL:
574 case PARM_DECL:
575 case CONST_DECL:
576 case STRING_CST:
577 case RESULT_DECL:
578 case VECTOR_CST:
579 case COMPLEX_CST:
580 case INTEGER_CST:
581 case REAL_CST:
582 case FIXED_CST:
583 case CONSTRUCTOR:
584 return true;
586 case ADDR_EXPR:
587 gcc_assert (is_gimple_min_invariant (*addr_p));
588 return true;
590 case TARGET_MEM_REF:
591 idx = &TMR_BASE (*addr_p);
592 if (*idx
593 && !cbck (*addr_p, idx, data))
594 return false;
595 idx = &TMR_INDEX (*addr_p);
596 if (*idx
597 && !cbck (*addr_p, idx, data))
598 return false;
599 idx = &TMR_INDEX2 (*addr_p);
600 if (*idx
601 && !cbck (*addr_p, idx, data))
602 return false;
603 return true;
605 default:
606 gcc_unreachable ();
612 /* The name and the length of the currently generated variable
613 for lsm. */
614 #define MAX_LSM_NAME_LENGTH 40
615 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
616 static int lsm_tmp_name_length;
618 /* Adds S to lsm_tmp_name. */
620 static void
621 lsm_tmp_name_add (const char *s)
623 int l = strlen (s) + lsm_tmp_name_length;
624 if (l > MAX_LSM_NAME_LENGTH)
625 return;
627 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
628 lsm_tmp_name_length = l;
631 /* Stores the name for temporary variable that replaces REF to
632 lsm_tmp_name. */
634 static void
635 gen_lsm_tmp_name (tree ref)
637 const char *name;
639 switch (TREE_CODE (ref))
641 case MEM_REF:
642 case TARGET_MEM_REF:
643 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
644 lsm_tmp_name_add ("_");
645 break;
647 case ADDR_EXPR:
648 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
649 break;
651 case BIT_FIELD_REF:
652 case VIEW_CONVERT_EXPR:
653 case ARRAY_RANGE_REF:
654 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
655 break;
657 case REALPART_EXPR:
658 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
659 lsm_tmp_name_add ("_RE");
660 break;
662 case IMAGPART_EXPR:
663 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
664 lsm_tmp_name_add ("_IM");
665 break;
667 case COMPONENT_REF:
668 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
669 lsm_tmp_name_add ("_");
670 name = get_name (TREE_OPERAND (ref, 1));
671 if (!name)
672 name = "F";
673 lsm_tmp_name_add (name);
674 break;
676 case ARRAY_REF:
677 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
678 lsm_tmp_name_add ("_I");
679 break;
681 case SSA_NAME:
682 case VAR_DECL:
683 case PARM_DECL:
684 name = get_name (ref);
685 if (!name)
686 name = "D";
687 lsm_tmp_name_add (name);
688 break;
690 case STRING_CST:
691 lsm_tmp_name_add ("S");
692 break;
694 case RESULT_DECL:
695 lsm_tmp_name_add ("R");
696 break;
698 case INTEGER_CST:
699 /* Nothing. */
700 break;
702 default:
703 gcc_unreachable ();
707 /* Determines name for temporary variable that replaces REF.
708 The name is accumulated into the lsm_tmp_name variable.
709 N is added to the name of the temporary. */
711 char *
712 get_lsm_tmp_name (tree ref, unsigned n, const char *suffix)
714 char ns[2];
716 lsm_tmp_name_length = 0;
717 gen_lsm_tmp_name (ref);
718 lsm_tmp_name_add ("_lsm");
719 if (n < 10)
721 ns[0] = '0' + n;
722 ns[1] = 0;
723 lsm_tmp_name_add (ns);
725 return lsm_tmp_name;
726 if (suffix != NULL)
727 lsm_tmp_name_add (suffix);
730 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
732 unsigned
733 tree_num_loop_insns (struct loop *loop, eni_weights *weights)
735 basic_block *body = get_loop_body (loop);
736 gimple_stmt_iterator gsi;
737 unsigned size = 0, i;
739 for (i = 0; i < loop->num_nodes; i++)
740 for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
741 size += estimate_num_insns (gsi_stmt (gsi), weights);
742 free (body);
744 return size;