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
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
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/>. */
22 #include "coretypes.h"
26 #include "tree-pass.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"
35 #include "tree-inline.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-vectorizer.h"
39 #include "diagnostic-core.h"
42 /* A pass making sure loops are fixed up. */
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
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
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
);
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. */
96 gate_loop (function
*fn
)
98 if (!flag_tree_loop_optimize
)
101 /* For -fdump-passes which runs before loop discovery print the
102 state of -ftree-loop-optimize. */
103 if (!loops_for_fn (fn
))
106 return number_of_loops (fn
) > 1;
109 /* The loop superpass. */
113 const pass_data pass_data_tree_loop
=
115 GIMPLE_PASS
, /* type */
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
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
141 make_pass_tree_loop (gcc::context
*ctxt
)
143 return new pass_tree_loop (ctxt
);
146 /* Gate for oacc kernels pass group. */
149 gate_oacc_kernels (function
*fn
)
151 if (flag_tree_parallelize_loops
<= 1)
154 tree oacc_function_attr
= get_oacc_fn_attrib (fn
->decl
);
155 if (oacc_function_attr
== NULL_TREE
)
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
)
166 FOR_EACH_LOOP (loop
, 0)
167 if (loop
->in_oacc_kernels_region
)
173 /* The oacc kernels superpass. */
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
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
205 make_pass_oacc_kernels (gcc::context
*ctxt
)
207 return new pass_oacc_kernels (ctxt
);
210 /* The ipa oacc superpass. */
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
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
*)
238 /* Don't bother doing anything if the program has errors. */
241 && flag_tree_parallelize_loops
> 1);
244 }; // class pass_ipa_oacc
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. */
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
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
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. */
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
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
320 make_pass_tree_no_loop (gcc::context
*ctxt
)
322 return new pass_tree_no_loop (ctxt
);
326 /* Loop optimizer initialization. */
330 const pass_data pass_data_tree_loop_init
=
332 GIMPLE_PASS
, /* type */
333 "loopinit", /* name */
334 OPTGROUP_LOOP
, /* optinfo_flags */
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
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
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
360 (loops_state_satisfies_p (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS
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
);
375 make_pass_tree_loop_init (gcc::context
*ctxt
)
377 return new pass_tree_loop_init (ctxt
);
380 /* Loop autovectorization. */
384 const pass_data pass_data_vectorize
=
386 GIMPLE_PASS
, /* type */
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
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
415 pass_vectorize::execute (function
*fun
)
417 if (number_of_loops (fun
) <= 1)
420 return vectorize_loops ();
426 make_pass_vectorize (gcc::context
*ctxt
)
428 return new pass_vectorize (ctxt
);
431 /* Propagation of constants using scev. */
435 const pass_data pass_data_scev_cprop
=
437 GIMPLE_PASS
, /* type */
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 */
446 | TODO_update_ssa_only_virtuals
), /* todo_flags_finish */
449 class pass_scev_cprop
: public gimple_opt_pass
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
465 make_pass_scev_cprop (gcc::context
*ctxt
)
467 return new pass_scev_cprop (ctxt
);
470 /* Record bounds on numbers of iterations of loops. */
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
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
500 pass_record_bounds::execute (function
*fun
)
502 if (number_of_loops (fun
) <= 1)
505 estimate_numbers_of_iterations ();
513 make_pass_record_bounds (gcc::context
*ctxt
)
515 return new pass_record_bounds (ctxt
);
518 /* Induction variable optimizations. */
522 const pass_data pass_data_iv_optimize
=
524 GIMPLE_PASS
, /* type */
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
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
549 pass_iv_optimize::execute (function
*fun
)
551 if (number_of_loops (fun
) <= 1)
554 tree_ssa_iv_optimize ();
561 make_pass_iv_optimize (gcc::context
*ctxt
)
563 return new pass_iv_optimize (ctxt
);
566 /* Loop optimizer finalization. */
569 tree_ssa_loop_done (void)
571 free_numbers_of_iterations_estimates (cfun
);
573 loop_optimizer_finalize ();
579 const pass_data pass_data_tree_loop_done
=
581 GIMPLE_PASS
, /* type */
582 "loopdone", /* name */
583 OPTGROUP_LOOP
, /* optinfo_flags */
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
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
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. */
627 for_each_index (tree
*addr_p
, bool (*cbck
) (tree
, tree
*, void *), void *data
)
631 for (; ; addr_p
= nxt
)
633 switch (TREE_CODE (*addr_p
))
636 return cbck (*addr_p
, addr_p
, data
);
639 nxt
= &TREE_OPERAND (*addr_p
, 0);
640 return cbck (*addr_p
, nxt
, data
);
643 case VIEW_CONVERT_EXPR
:
646 nxt
= &TREE_OPERAND (*addr_p
, 0);
650 /* If the component has varying offset, it behaves like index
652 idx
= &TREE_OPERAND (*addr_p
, 2);
654 && !cbck (*addr_p
, idx
, data
))
657 nxt
= &TREE_OPERAND (*addr_p
, 0);
661 case ARRAY_RANGE_REF
:
662 nxt
= &TREE_OPERAND (*addr_p
, 0);
663 if (!cbck (*addr_p
, &TREE_OPERAND (*addr_p
, 1), data
))
681 gcc_assert (is_gimple_min_invariant (*addr_p
));
685 idx
= &TMR_BASE (*addr_p
);
687 && !cbck (*addr_p
, idx
, data
))
689 idx
= &TMR_INDEX (*addr_p
);
691 && !cbck (*addr_p
, idx
, data
))
693 idx
= &TMR_INDEX2 (*addr_p
);
695 && !cbck (*addr_p
, idx
, data
))
706 /* The name and the length of the currently generated variable
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. */
715 lsm_tmp_name_add (const char *s
)
717 int l
= strlen (s
) + lsm_tmp_name_length
;
718 if (l
> MAX_LSM_NAME_LENGTH
)
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
729 gen_lsm_tmp_name (tree ref
)
733 switch (TREE_CODE (ref
))
737 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
738 lsm_tmp_name_add ("_");
742 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
746 case VIEW_CONVERT_EXPR
:
747 case ARRAY_RANGE_REF
:
748 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
752 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
753 lsm_tmp_name_add ("_RE");
757 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
758 lsm_tmp_name_add ("_IM");
762 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
763 lsm_tmp_name_add ("_");
764 name
= get_name (TREE_OPERAND (ref
, 1));
767 lsm_tmp_name_add (name
);
771 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
772 lsm_tmp_name_add ("_I");
778 name
= get_name (ref
);
781 lsm_tmp_name_add (name
);
785 lsm_tmp_name_add ("S");
789 lsm_tmp_name_add ("R");
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. */
806 get_lsm_tmp_name (tree ref
, unsigned n
, const char *suffix
)
810 lsm_tmp_name_length
= 0;
811 gen_lsm_tmp_name (ref
);
812 lsm_tmp_name_add ("_lsm");
817 lsm_tmp_name_add (ns
);
821 lsm_tmp_name_add (suffix
);
824 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
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
);