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"
40 /* A pass making sure loops are fixed up. */
44 const pass_data pass_data_fix_loops
=
46 GIMPLE_PASS
, /* type */
47 "fix_loops", /* name */
48 OPTGROUP_LOOP
, /* optinfo_flags */
49 TV_TREE_LOOP
, /* tv_id */
50 PROP_cfg
, /* properties_required */
51 0, /* properties_provided */
52 0, /* properties_destroyed */
53 0, /* todo_flags_start */
54 0, /* todo_flags_finish */
57 class pass_fix_loops
: public gimple_opt_pass
60 pass_fix_loops (gcc::context
*ctxt
)
61 : gimple_opt_pass (pass_data_fix_loops
, ctxt
)
64 /* opt_pass methods: */
65 virtual bool gate (function
*) { return flag_tree_loop_optimize
; }
67 virtual unsigned int execute (function
*fn
);
68 }; // class pass_fix_loops
71 pass_fix_loops::execute (function
*)
73 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP
))
75 calculate_dominance_info (CDI_DOMINATORS
);
76 fix_loop_structure (NULL
);
84 make_pass_fix_loops (gcc::context
*ctxt
)
86 return new pass_fix_loops (ctxt
);
90 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
91 but we also avoid running it when the IL doesn't contain any loop. */
94 gate_loop (function
*fn
)
96 if (!flag_tree_loop_optimize
)
99 /* For -fdump-passes which runs before loop discovery print the
100 state of -ftree-loop-optimize. */
101 if (!loops_for_fn (fn
))
104 return number_of_loops (fn
) > 1;
107 /* The loop superpass. */
111 const pass_data pass_data_tree_loop
=
113 GIMPLE_PASS
, /* type */
115 OPTGROUP_LOOP
, /* optinfo_flags */
116 TV_TREE_LOOP
, /* tv_id */
117 PROP_cfg
, /* properties_required */
118 0, /* properties_provided */
119 0, /* properties_destroyed */
120 0, /* todo_flags_start */
121 0, /* todo_flags_finish */
124 class pass_tree_loop
: public gimple_opt_pass
127 pass_tree_loop (gcc::context
*ctxt
)
128 : gimple_opt_pass (pass_data_tree_loop
, ctxt
)
131 /* opt_pass methods: */
132 virtual bool gate (function
*fn
) { return gate_loop (fn
); }
134 }; // class pass_tree_loop
139 make_pass_tree_loop (gcc::context
*ctxt
)
141 return new pass_tree_loop (ctxt
);
144 /* The no-loop superpass. */
148 const pass_data pass_data_tree_no_loop
=
150 GIMPLE_PASS
, /* type */
151 "no_loop", /* name */
152 OPTGROUP_NONE
, /* optinfo_flags */
153 TV_TREE_NOLOOP
, /* tv_id */
154 PROP_cfg
, /* properties_required */
155 0, /* properties_provided */
156 0, /* properties_destroyed */
157 0, /* todo_flags_start */
158 0, /* todo_flags_finish */
161 class pass_tree_no_loop
: public gimple_opt_pass
164 pass_tree_no_loop (gcc::context
*ctxt
)
165 : gimple_opt_pass (pass_data_tree_no_loop
, ctxt
)
168 /* opt_pass methods: */
169 virtual bool gate (function
*fn
) { return !gate_loop (fn
); }
171 }; // class pass_tree_no_loop
176 make_pass_tree_no_loop (gcc::context
*ctxt
)
178 return new pass_tree_no_loop (ctxt
);
182 /* Loop optimizer initialization. */
186 const pass_data pass_data_tree_loop_init
=
188 GIMPLE_PASS
, /* type */
189 "loopinit", /* name */
190 OPTGROUP_LOOP
, /* optinfo_flags */
192 PROP_cfg
, /* properties_required */
193 0, /* properties_provided */
194 0, /* properties_destroyed */
195 0, /* todo_flags_start */
196 0, /* todo_flags_finish */
199 class pass_tree_loop_init
: public gimple_opt_pass
202 pass_tree_loop_init (gcc::context
*ctxt
)
203 : gimple_opt_pass (pass_data_tree_loop_init
, ctxt
)
206 /* opt_pass methods: */
207 virtual unsigned int execute (function
*);
209 }; // class pass_tree_loop_init
212 pass_tree_loop_init::execute (function
*fun ATTRIBUTE_UNUSED
)
214 /* When processing a loop in the loop pipeline, we should be able to assert
216 (loops_state_satisfies_p (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS
218 && scev_initialized_p ())
220 loop_optimizer_init (LOOPS_NORMAL
221 | LOOPS_HAVE_RECORDED_EXITS
);
222 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
231 make_pass_tree_loop_init (gcc::context
*ctxt
)
233 return new pass_tree_loop_init (ctxt
);
236 /* Loop autovectorization. */
240 const pass_data pass_data_vectorize
=
242 GIMPLE_PASS
, /* type */
244 OPTGROUP_LOOP
| OPTGROUP_VEC
, /* optinfo_flags */
245 TV_TREE_VECTORIZATION
, /* tv_id */
246 ( PROP_cfg
| PROP_ssa
), /* properties_required */
247 0, /* properties_provided */
248 0, /* properties_destroyed */
249 0, /* todo_flags_start */
250 0, /* todo_flags_finish */
253 class pass_vectorize
: public gimple_opt_pass
256 pass_vectorize (gcc::context
*ctxt
)
257 : gimple_opt_pass (pass_data_vectorize
, ctxt
)
260 /* opt_pass methods: */
261 virtual bool gate (function
*fun
)
263 return flag_tree_loop_vectorize
|| fun
->has_force_vectorize_loops
;
266 virtual unsigned int execute (function
*);
268 }; // class pass_vectorize
271 pass_vectorize::execute (function
*fun
)
273 if (number_of_loops (fun
) <= 1)
276 return vectorize_loops ();
282 make_pass_vectorize (gcc::context
*ctxt
)
284 return new pass_vectorize (ctxt
);
287 /* Propagation of constants using scev. */
291 const pass_data pass_data_scev_cprop
=
293 GIMPLE_PASS
, /* type */
295 OPTGROUP_LOOP
, /* optinfo_flags */
296 TV_SCEV_CONST
, /* tv_id */
297 ( PROP_cfg
| PROP_ssa
), /* properties_required */
298 0, /* properties_provided */
299 0, /* properties_destroyed */
300 0, /* todo_flags_start */
302 | TODO_update_ssa_only_virtuals
), /* todo_flags_finish */
305 class pass_scev_cprop
: public gimple_opt_pass
308 pass_scev_cprop (gcc::context
*ctxt
)
309 : gimple_opt_pass (pass_data_scev_cprop
, ctxt
)
312 /* opt_pass methods: */
313 virtual bool gate (function
*) { return flag_tree_scev_cprop
; }
314 virtual unsigned int execute (function
*) { return scev_const_prop (); }
316 }; // class pass_scev_cprop
321 make_pass_scev_cprop (gcc::context
*ctxt
)
323 return new pass_scev_cprop (ctxt
);
326 /* Record bounds on numbers of iterations of loops. */
330 const pass_data pass_data_record_bounds
=
332 GIMPLE_PASS
, /* type */
333 "*record_bounds", /* name */
334 OPTGROUP_NONE
, /* optinfo_flags */
335 TV_TREE_LOOP_BOUNDS
, /* tv_id */
336 ( PROP_cfg
| PROP_ssa
), /* properties_required */
337 0, /* properties_provided */
338 0, /* properties_destroyed */
339 0, /* todo_flags_start */
340 0, /* todo_flags_finish */
343 class pass_record_bounds
: public gimple_opt_pass
346 pass_record_bounds (gcc::context
*ctxt
)
347 : gimple_opt_pass (pass_data_record_bounds
, ctxt
)
350 /* opt_pass methods: */
351 virtual unsigned int execute (function
*);
353 }; // class pass_record_bounds
356 pass_record_bounds::execute (function
*fun
)
358 if (number_of_loops (fun
) <= 1)
361 estimate_numbers_of_iterations ();
369 make_pass_record_bounds (gcc::context
*ctxt
)
371 return new pass_record_bounds (ctxt
);
374 /* Induction variable optimizations. */
378 const pass_data pass_data_iv_optimize
=
380 GIMPLE_PASS
, /* type */
382 OPTGROUP_LOOP
, /* optinfo_flags */
383 TV_TREE_LOOP_IVOPTS
, /* tv_id */
384 ( PROP_cfg
| PROP_ssa
), /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 TODO_update_ssa
, /* todo_flags_finish */
391 class pass_iv_optimize
: public gimple_opt_pass
394 pass_iv_optimize (gcc::context
*ctxt
)
395 : gimple_opt_pass (pass_data_iv_optimize
, ctxt
)
398 /* opt_pass methods: */
399 virtual bool gate (function
*) { return flag_ivopts
!= 0; }
400 virtual unsigned int execute (function
*);
402 }; // class pass_iv_optimize
405 pass_iv_optimize::execute (function
*fun
)
407 if (number_of_loops (fun
) <= 1)
410 tree_ssa_iv_optimize ();
417 make_pass_iv_optimize (gcc::context
*ctxt
)
419 return new pass_iv_optimize (ctxt
);
422 /* Loop optimizer finalization. */
425 tree_ssa_loop_done (void)
427 free_numbers_of_iterations_estimates (cfun
);
429 loop_optimizer_finalize ();
435 const pass_data pass_data_tree_loop_done
=
437 GIMPLE_PASS
, /* type */
438 "loopdone", /* name */
439 OPTGROUP_LOOP
, /* optinfo_flags */
441 PROP_cfg
, /* properties_required */
442 0, /* properties_provided */
443 0, /* properties_destroyed */
444 0, /* todo_flags_start */
445 TODO_cleanup_cfg
, /* todo_flags_finish */
448 class pass_tree_loop_done
: public gimple_opt_pass
451 pass_tree_loop_done (gcc::context
*ctxt
)
452 : gimple_opt_pass (pass_data_tree_loop_done
, ctxt
)
455 /* opt_pass methods: */
456 virtual unsigned int execute (function
*) { return tree_ssa_loop_done (); }
458 }; // class pass_tree_loop_done
463 make_pass_tree_loop_done (gcc::context
*ctxt
)
465 return new pass_tree_loop_done (ctxt
);
468 /* Calls CBCK for each index in memory reference ADDR_P. There are two
469 kinds situations handled; in each of these cases, the memory reference
470 and DATA are passed to the callback:
472 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
473 pass the pointer to the index to the callback.
475 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
476 pointer to addr to the callback.
478 If the callback returns false, the whole search stops and false is returned.
479 Otherwise the function returns true after traversing through the whole
480 reference *ADDR_P. */
483 for_each_index (tree
*addr_p
, bool (*cbck
) (tree
, tree
*, void *), void *data
)
487 for (; ; addr_p
= nxt
)
489 switch (TREE_CODE (*addr_p
))
492 return cbck (*addr_p
, addr_p
, data
);
495 nxt
= &TREE_OPERAND (*addr_p
, 0);
496 return cbck (*addr_p
, nxt
, data
);
499 case VIEW_CONVERT_EXPR
:
502 nxt
= &TREE_OPERAND (*addr_p
, 0);
506 /* If the component has varying offset, it behaves like index
508 idx
= &TREE_OPERAND (*addr_p
, 2);
510 && !cbck (*addr_p
, idx
, data
))
513 nxt
= &TREE_OPERAND (*addr_p
, 0);
517 case ARRAY_RANGE_REF
:
518 nxt
= &TREE_OPERAND (*addr_p
, 0);
519 if (!cbck (*addr_p
, &TREE_OPERAND (*addr_p
, 1), data
))
537 gcc_assert (is_gimple_min_invariant (*addr_p
));
541 idx
= &TMR_BASE (*addr_p
);
543 && !cbck (*addr_p
, idx
, data
))
545 idx
= &TMR_INDEX (*addr_p
);
547 && !cbck (*addr_p
, idx
, data
))
549 idx
= &TMR_INDEX2 (*addr_p
);
551 && !cbck (*addr_p
, idx
, data
))
562 /* The name and the length of the currently generated variable
564 #define MAX_LSM_NAME_LENGTH 40
565 static char lsm_tmp_name
[MAX_LSM_NAME_LENGTH
+ 1];
566 static int lsm_tmp_name_length
;
568 /* Adds S to lsm_tmp_name. */
571 lsm_tmp_name_add (const char *s
)
573 int l
= strlen (s
) + lsm_tmp_name_length
;
574 if (l
> MAX_LSM_NAME_LENGTH
)
577 strcpy (lsm_tmp_name
+ lsm_tmp_name_length
, s
);
578 lsm_tmp_name_length
= l
;
581 /* Stores the name for temporary variable that replaces REF to
585 gen_lsm_tmp_name (tree ref
)
589 switch (TREE_CODE (ref
))
593 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
594 lsm_tmp_name_add ("_");
598 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
602 case VIEW_CONVERT_EXPR
:
603 case ARRAY_RANGE_REF
:
604 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
608 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
609 lsm_tmp_name_add ("_RE");
613 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
614 lsm_tmp_name_add ("_IM");
618 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
619 lsm_tmp_name_add ("_");
620 name
= get_name (TREE_OPERAND (ref
, 1));
623 lsm_tmp_name_add (name
);
627 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
628 lsm_tmp_name_add ("_I");
634 name
= get_name (ref
);
637 lsm_tmp_name_add (name
);
641 lsm_tmp_name_add ("S");
645 lsm_tmp_name_add ("R");
657 /* Determines name for temporary variable that replaces REF.
658 The name is accumulated into the lsm_tmp_name variable.
659 N is added to the name of the temporary. */
662 get_lsm_tmp_name (tree ref
, unsigned n
, const char *suffix
)
666 lsm_tmp_name_length
= 0;
667 gen_lsm_tmp_name (ref
);
668 lsm_tmp_name_add ("_lsm");
673 lsm_tmp_name_add (ns
);
677 lsm_tmp_name_add (suffix
);
680 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
683 tree_num_loop_insns (struct loop
*loop
, eni_weights
*weights
)
685 basic_block
*body
= get_loop_body (loop
);
686 gimple_stmt_iterator gsi
;
687 unsigned size
= 0, i
;
689 for (i
= 0; i
< loop
->num_nodes
; i
++)
690 for (gsi
= gsi_start_bb (body
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
691 size
+= estimate_num_insns (gsi_stmt (gsi
), weights
);