1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003-2023 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"
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"
36 #include "tree-inline.h"
37 #include "tree-scalar-evolution.h"
38 #include "omp-general.h"
39 #include "diagnostic-core.h"
40 #include "stringpool.h"
44 /* A pass making sure loops are fixed up. */
48 const pass_data pass_data_fix_loops
=
50 GIMPLE_PASS
, /* type */
51 "fix_loops", /* name */
52 OPTGROUP_LOOP
, /* optinfo_flags */
53 TV_TREE_LOOP
, /* tv_id */
54 PROP_cfg
, /* properties_required */
55 0, /* properties_provided */
56 0, /* properties_destroyed */
57 0, /* todo_flags_start */
58 0, /* todo_flags_finish */
61 class pass_fix_loops
: public gimple_opt_pass
64 pass_fix_loops (gcc::context
*ctxt
)
65 : gimple_opt_pass (pass_data_fix_loops
, ctxt
)
68 /* opt_pass methods: */
69 bool gate (function
*) final override
{ return flag_tree_loop_optimize
; }
71 unsigned int execute (function
*fn
) final override
;
72 }; // class pass_fix_loops
75 pass_fix_loops::execute (function
*)
77 if (loops_state_satisfies_p (LOOPS_NEED_FIXUP
))
79 calculate_dominance_info (CDI_DOMINATORS
);
80 fix_loop_structure (NULL
);
88 make_pass_fix_loops (gcc::context
*ctxt
)
90 return new pass_fix_loops (ctxt
);
94 /* Gate for loop pass group. The group is controlled by -ftree-loop-optimize
95 but we also avoid running it when the IL doesn't contain any loop. */
98 gate_loop (function
*fn
)
100 if (!flag_tree_loop_optimize
)
103 /* For -fdump-passes which runs before loop discovery print the
104 state of -ftree-loop-optimize. */
105 if (!loops_for_fn (fn
))
108 return number_of_loops (fn
) > 1;
111 /* The loop superpass. */
115 const pass_data pass_data_tree_loop
=
117 GIMPLE_PASS
, /* type */
119 OPTGROUP_LOOP
, /* optinfo_flags */
120 TV_TREE_LOOP
, /* tv_id */
121 PROP_cfg
, /* properties_required */
122 0, /* properties_provided */
123 0, /* properties_destroyed */
124 0, /* todo_flags_start */
125 0, /* todo_flags_finish */
128 class pass_tree_loop
: public gimple_opt_pass
131 pass_tree_loop (gcc::context
*ctxt
)
132 : gimple_opt_pass (pass_data_tree_loop
, ctxt
)
135 /* opt_pass methods: */
136 bool gate (function
*fn
) final override
{ return gate_loop (fn
); }
138 }; // class pass_tree_loop
143 make_pass_tree_loop (gcc::context
*ctxt
)
145 return new pass_tree_loop (ctxt
);
148 /* Gate for oacc kernels pass group. */
151 gate_oacc_kernels (function
*fn
)
156 if (!lookup_attribute ("oacc kernels", DECL_ATTRIBUTES (fn
->decl
)))
159 for (auto loop
: loops_list (cfun
, 0))
160 if (loop
->in_oacc_kernels_region
)
166 /* The oacc kernels superpass. */
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
186 pass_oacc_kernels (gcc::context
*ctxt
)
187 : gimple_opt_pass (pass_data_oacc_kernels
, ctxt
)
190 /* opt_pass methods: */
191 bool gate (function
*fn
) final override
{ return gate_oacc_kernels (fn
); }
193 }; // class pass_oacc_kernels
198 make_pass_oacc_kernels (gcc::context
*ctxt
)
200 return new pass_oacc_kernels (ctxt
);
203 /* The ipa oacc superpass. */
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
223 pass_ipa_oacc (gcc::context
*ctxt
)
224 : simple_ipa_opt_pass (pass_data_ipa_oacc
, ctxt
)
227 /* opt_pass methods: */
228 bool gate (function
*) final override
232 /* Don't bother doing anything if the program has errors. */
236 }; // class pass_ipa_oacc
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. */
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
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
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. */
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
300 pass_tree_no_loop (gcc::context
*ctxt
)
301 : gimple_opt_pass (pass_data_tree_no_loop
, ctxt
)
304 /* opt_pass methods: */
305 bool gate (function
*fn
) final override
{ return !gate_loop (fn
); }
307 }; // class pass_tree_no_loop
312 make_pass_tree_no_loop (gcc::context
*ctxt
)
314 return new pass_tree_no_loop (ctxt
);
318 /* Loop optimizer initialization. */
322 const pass_data pass_data_tree_loop_init
=
324 GIMPLE_PASS
, /* type */
325 "loopinit", /* name */
326 OPTGROUP_LOOP
, /* optinfo_flags */
328 PROP_cfg
, /* properties_required */
329 0, /* properties_provided */
330 0, /* properties_destroyed */
331 TODO_update_address_taken
, /* todo_flags_start */
332 0, /* todo_flags_finish */
335 class pass_tree_loop_init
: public gimple_opt_pass
338 pass_tree_loop_init (gcc::context
*ctxt
)
339 : gimple_opt_pass (pass_data_tree_loop_init
, ctxt
)
342 /* opt_pass methods: */
343 unsigned int execute (function
*) final override
;
345 }; // class pass_tree_loop_init
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
352 (loops_state_satisfies_p (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS
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
);
367 make_pass_tree_loop_init (gcc::context
*ctxt
)
369 return new pass_tree_loop_init (ctxt
);
372 /* Propagation of constants using scev. */
376 const pass_data pass_data_scev_cprop
=
378 GIMPLE_PASS
, /* type */
380 OPTGROUP_LOOP
, /* optinfo_flags */
381 TV_SCEV_CONST
, /* 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_scev_cprop
: public gimple_opt_pass
392 pass_scev_cprop (gcc::context
*ctxt
)
393 : gimple_opt_pass (pass_data_scev_cprop
, ctxt
)
396 /* opt_pass methods: */
397 bool gate (function
*) final override
{ return flag_tree_scev_cprop
; }
398 unsigned int execute (function
*) final override
;
400 }; // class pass_scev_cprop
403 pass_scev_cprop::execute (function
*)
407 /* Perform final value replacement in loops, in case the replacement
408 expressions are cheap. */
409 for (auto loop
: loops_list (cfun
, LI_FROM_INNERMOST
))
410 any
|= final_value_replacement_loop (loop
);
412 return any
? TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
: 0;
418 make_pass_scev_cprop (gcc::context
*ctxt
)
420 return new pass_scev_cprop (ctxt
);
423 /* Induction variable optimizations. */
427 const pass_data pass_data_iv_optimize
=
429 GIMPLE_PASS
, /* type */
431 OPTGROUP_LOOP
, /* optinfo_flags */
432 TV_TREE_LOOP_IVOPTS
, /* tv_id */
433 ( PROP_cfg
| PROP_ssa
), /* properties_required */
434 0, /* properties_provided */
435 0, /* properties_destroyed */
436 0, /* todo_flags_start */
437 TODO_update_ssa
, /* todo_flags_finish */
440 class pass_iv_optimize
: public gimple_opt_pass
443 pass_iv_optimize (gcc::context
*ctxt
)
444 : gimple_opt_pass (pass_data_iv_optimize
, ctxt
)
447 /* opt_pass methods: */
448 bool gate (function
*) final override
{ return flag_ivopts
!= 0; }
449 unsigned int execute (function
*) final override
;
451 }; // class pass_iv_optimize
454 pass_iv_optimize::execute (function
*fun
)
456 if (number_of_loops (fun
) <= 1)
459 tree_ssa_iv_optimize ();
466 make_pass_iv_optimize (gcc::context
*ctxt
)
468 return new pass_iv_optimize (ctxt
);
471 /* Loop optimizer finalization. */
474 tree_ssa_loop_done (void)
476 free_numbers_of_iterations_estimates (cfun
);
478 loop_optimizer_finalize (cfun
, true);
484 const pass_data pass_data_tree_loop_done
=
486 GIMPLE_PASS
, /* type */
487 "loopdone", /* name */
488 OPTGROUP_LOOP
, /* optinfo_flags */
490 PROP_cfg
, /* properties_required */
491 PROP_loop_opts_done
, /* properties_provided */
492 0, /* properties_destroyed */
493 0, /* todo_flags_start */
494 TODO_cleanup_cfg
, /* todo_flags_finish */
497 class pass_tree_loop_done
: public gimple_opt_pass
500 pass_tree_loop_done (gcc::context
*ctxt
)
501 : gimple_opt_pass (pass_data_tree_loop_done
, ctxt
)
504 /* opt_pass methods: */
505 unsigned int execute (function
*) final override
507 return tree_ssa_loop_done ();
510 }; // class pass_tree_loop_done
515 make_pass_tree_loop_done (gcc::context
*ctxt
)
517 return new pass_tree_loop_done (ctxt
);
520 /* Calls CBCK for each index in memory reference ADDR_P. There are two
521 kinds situations handled; in each of these cases, the memory reference
522 and DATA are passed to the callback:
524 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
525 pass the pointer to the index to the callback.
527 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
528 pointer to addr to the callback.
530 If the callback returns false, the whole search stops and false is returned.
531 Otherwise the function returns true after traversing through the whole
532 reference *ADDR_P. */
535 for_each_index (tree
*addr_p
, bool (*cbck
) (tree
, tree
*, void *), void *data
)
539 for (; ; addr_p
= nxt
)
541 switch (TREE_CODE (*addr_p
))
544 return cbck (*addr_p
, addr_p
, data
);
547 nxt
= &TREE_OPERAND (*addr_p
, 0);
548 return cbck (*addr_p
, nxt
, data
);
551 case VIEW_CONVERT_EXPR
:
554 nxt
= &TREE_OPERAND (*addr_p
, 0);
558 /* If the component has varying offset, it behaves like index
560 idx
= &TREE_OPERAND (*addr_p
, 2);
562 && !cbck (*addr_p
, idx
, data
))
565 nxt
= &TREE_OPERAND (*addr_p
, 0);
569 case ARRAY_RANGE_REF
:
570 nxt
= &TREE_OPERAND (*addr_p
, 0);
571 if (!cbck (*addr_p
, &TREE_OPERAND (*addr_p
, 1), data
))
579 gcc_assert (is_gimple_min_invariant (*addr_p
));
583 idx
= &TMR_BASE (*addr_p
);
585 && !cbck (*addr_p
, idx
, data
))
587 idx
= &TMR_INDEX (*addr_p
);
589 && !cbck (*addr_p
, idx
, data
))
591 idx
= &TMR_INDEX2 (*addr_p
);
593 && !cbck (*addr_p
, idx
, data
))
599 || CONSTANT_CLASS_P (*addr_p
))
607 /* The name and the length of the currently generated variable
609 #define MAX_LSM_NAME_LENGTH 40
610 static char lsm_tmp_name
[MAX_LSM_NAME_LENGTH
+ 1];
611 static int lsm_tmp_name_length
;
613 /* Adds S to lsm_tmp_name. */
616 lsm_tmp_name_add (const char *s
)
618 int l
= strlen (s
) + lsm_tmp_name_length
;
619 if (l
> MAX_LSM_NAME_LENGTH
)
622 strcpy (lsm_tmp_name
+ lsm_tmp_name_length
, s
);
623 lsm_tmp_name_length
= l
;
626 /* Stores the name for temporary variable that replaces REF to
630 gen_lsm_tmp_name (tree ref
)
634 switch (TREE_CODE (ref
))
638 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
639 lsm_tmp_name_add ("_");
643 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
647 case VIEW_CONVERT_EXPR
:
648 case ARRAY_RANGE_REF
:
649 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
653 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
654 lsm_tmp_name_add ("_RE");
658 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
659 lsm_tmp_name_add ("_IM");
663 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
664 lsm_tmp_name_add ("_");
665 name
= get_name (TREE_OPERAND (ref
, 1));
668 lsm_tmp_name_add (name
);
672 gen_lsm_tmp_name (TREE_OPERAND (ref
, 0));
673 lsm_tmp_name_add ("_I");
681 name
= get_name (ref
);
684 lsm_tmp_name_add (name
);
688 lsm_tmp_name_add ("S");
692 lsm_tmp_name_add ("R");
702 /* Determines name for temporary variable that replaces REF.
703 The name is accumulated into the lsm_tmp_name variable.
704 N is added to the name of the temporary. */
707 get_lsm_tmp_name (tree ref
, unsigned n
, const char *suffix
)
711 lsm_tmp_name_length
= 0;
712 gen_lsm_tmp_name (ref
);
713 lsm_tmp_name_add ("_lsm");
718 lsm_tmp_name_add (ns
);
721 lsm_tmp_name_add (suffix
);
725 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
728 tree_num_loop_insns (class loop
*loop
, eni_weights
*weights
)
730 basic_block
*body
= get_loop_body (loop
);
731 gimple_stmt_iterator gsi
;
732 unsigned size
= 0, i
;
734 for (i
= 0; i
< loop
->num_nodes
; i
++)
735 for (gsi
= gsi_start_bb (body
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
736 size
+= estimate_num_insns (gsi_stmt (gsi
), weights
);