1 /* Induction variable canonicalization.
2 Copyright (C) 2004, 2005, 2007 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/>. */
20 /* This pass detects the loops that iterate a constant number of times,
21 adds a canonical induction variable (step -1, tested against 0)
22 and replaces the exit test. This enables the less powerful rtl
23 level analysis to use this information.
25 This might spoil the code in some cases (by increasing register pressure).
26 Note that in the case the new variable is not needed, ivopts will get rid
27 of it, so it might only be a problem when there are no other linear induction
28 variables. In that case the created optimization possibilities are likely
31 Additionally in case we detect that it is beneficial to unroll the
32 loop completely, we do it right here to expose the optimization
33 possibilities to the following passes. */
37 #include "coretypes.h"
42 #include "hard-reg-set.h"
43 #include "basic-block.h"
45 #include "diagnostic.h"
46 #include "tree-flow.h"
47 #include "tree-dump.h"
49 #include "tree-pass.h"
51 #include "tree-chrec.h"
52 #include "tree-scalar-evolution.h"
55 #include "tree-inline.h"
57 /* Specifies types of loops that may be unrolled. */
61 UL_SINGLE_ITER
, /* Only loops that exit immediately in the first
63 UL_NO_GROWTH
, /* Only loops whose unrolling will not cause increase
65 UL_ALL
/* All suitable loops. */
68 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
69 is the exit edge whose condition is replaced. */
72 create_canonical_iv (struct loop
*loop
, edge exit
, tree niter
)
76 block_stmt_iterator incr_at
;
79 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
81 fprintf (dump_file
, "Added canonical iv to loop %d, ", loop
->num
);
82 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
83 fprintf (dump_file
, " iterations.\n");
86 cond
= last_stmt (exit
->src
);
87 in
= EDGE_SUCC (exit
->src
, 0);
89 in
= EDGE_SUCC (exit
->src
, 1);
91 /* Note that we do not need to worry about overflows, since
92 type of niter is always unsigned and all comparisons are
93 just for equality/nonequality -- i.e. everything works
94 with a modulo arithmetics. */
96 type
= TREE_TYPE (niter
);
97 niter
= fold_build2 (PLUS_EXPR
, type
,
99 build_int_cst (type
, 1));
100 incr_at
= bsi_last (in
->src
);
102 build_int_cst (type
, -1),
104 &incr_at
, false, NULL
, &var
);
106 cmp
= (exit
->flags
& EDGE_TRUE_VALUE
) ? EQ_EXPR
: NE_EXPR
;
107 COND_EXPR_COND (cond
) = build2 (cmp
, boolean_type_node
,
109 build_int_cst (type
, 0));
113 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
116 tree_num_loop_insns (struct loop
*loop
, eni_weights
*weights
)
118 basic_block
*body
= get_loop_body (loop
);
119 block_stmt_iterator bsi
;
120 unsigned size
= 1, i
;
122 for (i
= 0; i
< loop
->num_nodes
; i
++)
123 for (bsi
= bsi_start (body
[i
]); !bsi_end_p (bsi
); bsi_next (&bsi
))
124 size
+= estimate_num_insns (bsi_stmt (bsi
), weights
);
130 /* Estimate number of insns of completely unrolled loop. We assume
131 that the size of the unrolled loop is decreased in the
132 following way (the numbers of insns are based on what
133 estimate_num_insns returns for appropriate statements):
135 1) exit condition gets removed (2 insns)
136 2) increment of the control variable gets removed (2 insns)
137 3) All remaining statements are likely to get simplified
138 due to constant propagation. Hard to estimate; just
139 as a heuristics we decrease the rest by 1/3.
141 NINSNS is the number of insns in the loop before unrolling.
142 NUNROLL is the number of times the loop is unrolled. */
144 static unsigned HOST_WIDE_INT
145 estimated_unrolled_size (unsigned HOST_WIDE_INT ninsns
,
146 unsigned HOST_WIDE_INT nunroll
)
148 HOST_WIDE_INT unr_insns
= 2 * ((HOST_WIDE_INT
) ninsns
- 4) / 3;
151 unr_insns
*= (nunroll
+ 1);
156 /* Tries to unroll LOOP completely, i.e. NITER times.
157 UL determines which loops we are allowed to unroll.
158 EXIT is the exit of the loop that should be eliminated. */
161 try_unroll_loop_completely (struct loop
*loop
,
162 edge exit
, tree niter
,
163 enum unroll_level ul
)
165 unsigned HOST_WIDE_INT n_unroll
, ninsns
, max_unroll
, unr_insns
;
171 if (!host_integerp (niter
, 1))
173 n_unroll
= tree_low_cst (niter
, 1);
175 max_unroll
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
176 if (n_unroll
> max_unroll
)
181 if (ul
== UL_SINGLE_ITER
)
184 ninsns
= tree_num_loop_insns (loop
, &eni_size_weights
);
186 if (n_unroll
* ninsns
187 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
))
190 if (ul
== UL_NO_GROWTH
)
192 unr_insns
= estimated_unrolled_size (ninsns
, n_unroll
);
194 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
196 fprintf (dump_file
, " Loop size: %d\n", (int) ninsns
);
197 fprintf (dump_file
, " Estimated size after unrolling: %d\n",
201 if (unr_insns
> ninsns
)
203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
204 fprintf (dump_file
, "Not unrolling loop %d:\n", loop
->num
);
215 VEC (edge
, heap
) *to_remove
= NULL
;
217 initialize_original_copy_tables ();
218 wont_exit
= sbitmap_alloc (n_unroll
+ 1);
219 sbitmap_ones (wont_exit
);
220 RESET_BIT (wont_exit
, 0);
222 if (!tree_duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
225 DLTHE_FLAG_UPDATE_FREQ
226 | DLTHE_FLAG_COMPLETTE_PEEL
))
228 free_original_copy_tables ();
233 for (i
= 0; VEC_iterate (edge
, to_remove
, i
, e
); i
++)
235 bool ok
= remove_path (e
);
239 VEC_free (edge
, heap
, to_remove
);
241 free_original_copy_tables ();
244 cond
= last_stmt (exit
->src
);
245 COND_EXPR_COND (cond
) = (exit
->flags
& EDGE_TRUE_VALUE
) ? boolean_true_node
246 : boolean_false_node
;
248 update_ssa (TODO_update_ssa
);
250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
251 fprintf (dump_file
, "Unrolled loop %d completely.\n", loop
->num
);
256 /* Adds a canonical induction variable to LOOP if suitable.
257 CREATE_IV is true if we may create a new iv. UL determines
258 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
259 to determine the number of iterations of a loop by direct evaluation.
260 Returns true if cfg is changed. */
263 canonicalize_loop_induction_variables (struct loop
*loop
,
264 bool create_iv
, enum unroll_level ul
,
270 niter
= number_of_latch_executions (loop
);
271 if (TREE_CODE (niter
) == INTEGER_CST
)
273 exit
= single_exit (loop
);
274 if (!just_once_each_iteration_p (loop
, exit
->src
))
279 /* If the loop has more than one exit, try checking all of them
280 for # of iterations determinable through scev. */
281 if (!single_exit (loop
))
282 niter
= find_loop_niter (loop
, &exit
);
284 /* Finally if everything else fails, try brute force evaluation. */
286 && (chrec_contains_undetermined (niter
)
287 || TREE_CODE (niter
) != INTEGER_CST
))
288 niter
= find_loop_niter_by_eval (loop
, &exit
);
290 if (chrec_contains_undetermined (niter
)
291 || TREE_CODE (niter
) != INTEGER_CST
)
295 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
297 fprintf (dump_file
, "Loop %d iterates ", loop
->num
);
298 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
299 fprintf (dump_file
, " times.\n");
302 if (try_unroll_loop_completely (loop
, exit
, niter
, ul
))
306 create_canonical_iv (loop
, exit
, niter
);
311 /* The main entry point of the pass. Adds canonical induction variables
312 to the suitable loops. */
315 canonicalize_induction_variables (void)
319 bool changed
= false;
321 FOR_EACH_LOOP (li
, loop
, 0)
323 changed
|= canonicalize_loop_induction_variables (loop
,
324 true, UL_SINGLE_ITER
,
328 /* Clean up the information about numbers of iterations, since brute force
329 evaluation could reveal new information. */
333 return TODO_cleanup_cfg
;
337 /* Unroll LOOPS completely if they iterate just few times. Unless
338 MAY_INCREASE_SIZE is true, perform the unrolling only if the
339 size of the code does not increase. */
342 tree_unroll_loops_completely (bool may_increase_size
)
346 bool changed
= false;
347 enum unroll_level ul
;
349 FOR_EACH_LOOP (li
, loop
, 0)
351 if (may_increase_size
&& maybe_hot_bb_p (loop
->header
))
355 changed
|= canonicalize_loop_induction_variables (loop
,
357 !flag_tree_loop_ivcanon
);
360 /* Clean up the information about numbers of iterations, since complete
361 unrolling might have invalidated it. */
365 return TODO_cleanup_cfg
;
369 /* Checks whether LOOP is empty. */
372 empty_loop_p (struct loop
*loop
)
375 struct tree_niter_desc niter
;
378 block_stmt_iterator bsi
;
382 /* If the loop has multiple exits, it is too hard for us to handle.
383 Similarly, if the exit is not dominating, we cannot determine
384 whether the loop is not infinite. */
385 exit
= single_dom_exit (loop
);
389 /* The loop must be finite. */
390 if (!number_of_iterations_exit (loop
, exit
, &niter
, false))
393 /* Values of all loop exit phi nodes must be invariants. */
394 for (phi
= phi_nodes (exit
->dest
); phi
; phi
= PHI_CHAIN (phi
))
396 if (!is_gimple_reg (PHI_RESULT (phi
)))
399 def
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
401 if (!expr_invariant_in_loop_p (loop
, def
))
405 /* And there should be no memory modifying or from other reasons
406 unremovable statements. */
407 body
= get_loop_body (loop
);
408 for (i
= 0; i
< loop
->num_nodes
; i
++)
410 /* Irreducible region might be infinite. */
411 if (body
[i
]->flags
& BB_IRREDUCIBLE_LOOP
)
417 for (bsi
= bsi_start (body
[i
]); !bsi_end_p (bsi
); bsi_next (&bsi
))
419 stmt
= bsi_stmt (bsi
);
420 if (!ZERO_SSA_OPERANDS (stmt
, SSA_OP_VIRTUAL_DEFS
)
421 || stmt_ann (stmt
)->has_volatile_ops
)
427 /* Also, asm statements and calls may have side effects and we
428 cannot change the number of times they are executed. */
429 switch (TREE_CODE (stmt
))
432 case GIMPLE_MODIFY_STMT
:
433 stmt
= get_call_expr_in (stmt
);
438 if (TREE_SIDE_EFFECTS (stmt
))
446 /* We cannot remove volatile assembler. */
447 if (ASM_VOLATILE_P (stmt
))
464 /* Remove LOOP by making it exit in the first iteration. */
467 remove_empty_loop (struct loop
*loop
)
469 edge exit
= single_dom_exit (loop
), non_exit
;
470 tree cond_stmt
= last_stmt (exit
->src
);
473 unsigned n_before
, freq_in
, freq_h
;
474 gcov_type exit_count
= exit
->count
;
477 fprintf (dump_file
, "Removing empty loop %d\n", loop
->num
);
479 non_exit
= EDGE_SUCC (exit
->src
, 0);
480 if (non_exit
== exit
)
481 non_exit
= EDGE_SUCC (exit
->src
, 1);
483 if (exit
->flags
& EDGE_TRUE_VALUE
)
484 do_exit
= boolean_true_node
;
486 do_exit
= boolean_false_node
;
488 COND_EXPR_COND (cond_stmt
) = do_exit
;
489 update_stmt (cond_stmt
);
491 /* Let us set the probabilities of the edges coming from the exit block. */
492 exit
->probability
= REG_BR_PROB_BASE
;
493 non_exit
->probability
= 0;
496 /* Update frequencies and counts. Everything before
497 the exit needs to be scaled FREQ_IN/FREQ_H times,
498 where FREQ_IN is the frequency of the entry edge
499 and FREQ_H is the frequency of the loop header.
500 Everything after the exit has zero frequency. */
501 freq_h
= loop
->header
->frequency
;
502 freq_in
= EDGE_FREQUENCY (loop_preheader_edge (loop
));
505 body
= get_loop_body_in_dom_order (loop
);
506 for (n_before
= 1; n_before
<= loop
->num_nodes
; n_before
++)
507 if (body
[n_before
- 1] == exit
->src
)
509 scale_bbs_frequencies_int (body
, n_before
, freq_in
, freq_h
);
510 scale_bbs_frequencies_int (body
+ n_before
, loop
->num_nodes
- n_before
,
515 /* Number of executions of exit is not changed, thus we need to restore
516 the original value. */
517 exit
->count
= exit_count
;
520 /* Removes LOOP if it is empty. Returns true if LOOP is removed. CHANGED
521 is set to true if LOOP or any of its subloops is removed. */
524 try_remove_empty_loop (struct loop
*loop
, bool *changed
)
526 bool nonempty_subloop
= false;
529 /* First, all subloops must be removed. */
530 for (sub
= loop
->inner
; sub
; sub
= sub
->next
)
531 nonempty_subloop
|= !try_remove_empty_loop (sub
, changed
);
533 if (nonempty_subloop
|| !empty_loop_p (loop
))
536 remove_empty_loop (loop
);
541 /* Remove the empty loops. */
544 remove_empty_loops (void)
546 bool changed
= false;
549 for (loop
= current_loops
->tree_root
->inner
; loop
; loop
= loop
->next
)
550 try_remove_empty_loop (loop
, &changed
);
555 return TODO_cleanup_cfg
;