1 /* Induction variable canonicalization.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This pass detects the loops that iterate a constant number of times,
22 adds a canonical induction variable (step -1, tested against 0)
23 and replaces the exit test. This enables the less powerful rtl
24 level analysis to use this information.
26 This might spoil the code in some cases (by increasing register pressure).
27 Note that in the case the new variable is not needed, ivopts will get rid
28 of it, so it might only be a problem when there are no other linear induction
29 variables. In that case the created optimization possibilities are likely
32 Additionally in case we detect that it is beneficial to unroll the
33 loop completely, we do it right here to expose the optimization
34 possibilities to the following passes. */
38 #include "coretypes.h"
43 #include "hard-reg-set.h"
44 #include "basic-block.h"
46 #include "diagnostic.h"
47 #include "tree-flow.h"
48 #include "tree-dump.h"
50 #include "tree-pass.h"
52 #include "tree-chrec.h"
53 #include "tree-scalar-evolution.h"
56 #include "tree-inline.h"
58 /* Specifies types of loops that may be unrolled. */
62 UL_SINGLE_ITER
, /* Only loops that exit immediately in the first
64 UL_NO_GROWTH
, /* Only loops whose unrolling will not cause increase
66 UL_ALL
/* All suitable loops. */
69 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
70 is the exit edge whose condition is replaced. */
73 create_canonical_iv (struct loop
*loop
, edge exit
, tree niter
)
77 block_stmt_iterator incr_at
;
80 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
82 fprintf (dump_file
, "Added canonical iv to loop %d, ", loop
->num
);
83 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
84 fprintf (dump_file
, " iterations.\n");
87 cond
= last_stmt (exit
->src
);
88 in
= EDGE_SUCC (exit
->src
, 0);
90 in
= EDGE_SUCC (exit
->src
, 1);
92 /* Note that we do not need to worry about overflows, since
93 type of niter is always unsigned and all comparisons are
94 just for equality/nonequality -- i.e. everything works
95 with a modulo arithmetics. */
97 type
= TREE_TYPE (niter
);
98 niter
= fold_build2 (PLUS_EXPR
, type
,
100 build_int_cst (type
, 1));
101 incr_at
= bsi_last (in
->src
);
103 fold_convert (type
, integer_minus_one_node
),
105 &incr_at
, false, NULL
, &var
);
107 cmp
= (exit
->flags
& EDGE_TRUE_VALUE
) ? EQ_EXPR
: NE_EXPR
;
108 COND_EXPR_COND (cond
) = build2 (cmp
, boolean_type_node
,
110 build_int_cst (type
, 0));
114 /* Computes an estimated number of insns in LOOP. */
117 tree_num_loop_insns (struct loop
*loop
)
119 basic_block
*body
= get_loop_body (loop
);
120 block_stmt_iterator bsi
;
121 unsigned size
= 1, i
;
123 for (i
= 0; i
< loop
->num_nodes
; i
++)
124 for (bsi
= bsi_start (body
[i
]); !bsi_end_p (bsi
); bsi_next (&bsi
))
125 size
+= estimate_num_insns (bsi_stmt (bsi
));
131 /* Estimate number of insns of completely unrolled loop. We assume
132 that the size of the unrolled loop is decreased in the
133 following way (the numbers of insns are based on what
134 estimate_num_insns returns for appropriate statements):
136 1) exit condition gets removed (2 insns)
137 2) increment of the control variable gets removed (2 insns)
138 3) All remaining statements are likely to get simplified
139 due to constant propagation. Hard to estimate; just
140 as a heuristics we decrease the rest by 1/3.
142 NINSNS is the number of insns in the loop before unrolling.
143 NUNROLL is the number of times the loop is unrolled. */
145 static unsigned HOST_WIDE_INT
146 estimated_unrolled_size (unsigned HOST_WIDE_INT ninsns
,
147 unsigned HOST_WIDE_INT nunroll
)
149 HOST_WIDE_INT unr_insns
= 2 * ((HOST_WIDE_INT
) ninsns
- 4) / 3;
152 unr_insns
*= (nunroll
+ 1);
157 /* Tries to unroll LOOP completely, i.e. NITER times. LOOPS is the
158 loop tree. UL determines which loops we are allowed to unroll.
159 EXIT is the exit of the loop that should be eliminated. */
162 try_unroll_loop_completely (struct loops
*loops ATTRIBUTE_UNUSED
,
164 edge exit
, tree niter
,
165 enum unroll_level ul
)
167 unsigned HOST_WIDE_INT n_unroll
, ninsns
, max_unroll
, unr_insns
;
168 tree old_cond
, cond
, dont_exit
, do_exit
;
173 if (!host_integerp (niter
, 1))
175 n_unroll
= tree_low_cst (niter
, 1);
177 max_unroll
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
178 if (n_unroll
> max_unroll
)
183 if (ul
== UL_SINGLE_ITER
)
186 ninsns
= tree_num_loop_insns (loop
);
188 if (n_unroll
* ninsns
189 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
))
192 if (ul
== UL_NO_GROWTH
)
194 unr_insns
= estimated_unrolled_size (ninsns
, n_unroll
);
196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
198 fprintf (dump_file
, " Loop size: %d\n", (int) ninsns
);
199 fprintf (dump_file
, " Estimated size after unrolling: %d\n",
203 if (unr_insns
> ninsns
)
205 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
206 fprintf (dump_file
, "Not unrolling loop %d:\n", loop
->num
);
212 if (exit
->flags
& EDGE_TRUE_VALUE
)
214 dont_exit
= boolean_false_node
;
215 do_exit
= boolean_true_node
;
219 dont_exit
= boolean_true_node
;
220 do_exit
= boolean_false_node
;
222 cond
= last_stmt (exit
->src
);
226 old_cond
= COND_EXPR_COND (cond
);
227 COND_EXPR_COND (cond
) = dont_exit
;
229 initialize_original_copy_tables ();
231 if (!tree_duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
232 loops
, n_unroll
, NULL
,
233 NULL
, NULL
, NULL
, 0))
235 COND_EXPR_COND (cond
) = old_cond
;
237 free_original_copy_tables ();
240 free_original_copy_tables ();
243 COND_EXPR_COND (cond
) = do_exit
;
246 update_ssa (TODO_update_ssa
);
248 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
249 fprintf (dump_file
, "Unrolled loop %d completely.\n", loop
->num
);
254 /* Adds a canonical induction variable to LOOP if suitable. LOOPS is the loops
255 tree. CREATE_IV is true if we may create a new iv. UL determines what
256 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
257 to determine the number of iterations of a loop by direct evaluation.
258 Returns true if cfg is changed. */
261 canonicalize_loop_induction_variables (struct loops
*loops
, struct loop
*loop
,
262 bool create_iv
, enum unroll_level ul
,
268 niter
= number_of_iterations_in_loop (loop
);
269 if (TREE_CODE (niter
) == INTEGER_CST
)
271 exit
= loop
->single_exit
;
272 if (!just_once_each_iteration_p (loop
, exit
->src
))
275 /* The result of number_of_iterations_in_loop is by one higher than
276 we expect (i.e. it returns number of executions of the exit
277 condition, not of the loop latch edge). */
278 niter
= fold_build2 (MINUS_EXPR
, TREE_TYPE (niter
), niter
,
279 build_int_cst (TREE_TYPE (niter
), 1));
283 /* If the loop has more than one exit, try checking all of them
284 for # of iterations determinable through scev. */
285 if (!loop
->single_exit
)
286 niter
= find_loop_niter (loop
, &exit
);
288 /* Finally if everything else fails, try brute force evaluation. */
290 && (chrec_contains_undetermined (niter
)
291 || TREE_CODE (niter
) != INTEGER_CST
))
292 niter
= find_loop_niter_by_eval (loop
, &exit
);
294 if (chrec_contains_undetermined (niter
)
295 || TREE_CODE (niter
) != INTEGER_CST
)
299 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
301 fprintf (dump_file
, "Loop %d iterates ", loop
->num
);
302 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
303 fprintf (dump_file
, " times.\n");
306 if (try_unroll_loop_completely (loops
, loop
, exit
, niter
, ul
))
310 create_canonical_iv (loop
, exit
, niter
);
315 /* The main entry point of the pass. Adds canonical induction variables
316 to the suitable LOOPS. */
319 canonicalize_induction_variables (struct loops
*loops
)
323 bool changed
= false;
325 for (i
= 1; i
< loops
->num
; i
++)
327 loop
= loops
->parray
[i
];
330 changed
|= canonicalize_loop_induction_variables (loops
, loop
,
331 true, UL_SINGLE_ITER
,
335 /* Clean up the information about numbers of iterations, since brute force
336 evaluation could reveal new information. */
340 cleanup_tree_cfg_loop ();
343 /* Unroll LOOPS completely if they iterate just few times. Unless
344 MAY_INCREASE_SIZE is true, perform the unrolling only if the
345 size of the code does not increase. */
348 tree_unroll_loops_completely (struct loops
*loops
, bool may_increase_size
)
352 bool changed
= false;
353 enum unroll_level ul
= may_increase_size
? UL_ALL
: UL_NO_GROWTH
;
355 for (i
= 1; i
< loops
->num
; i
++)
357 loop
= loops
->parray
[i
];
362 changed
|= canonicalize_loop_induction_variables (loops
, loop
,
364 !flag_tree_loop_ivcanon
);
367 /* Clean up the information about numbers of iterations, since complete
368 unrolling might have invalidated it. */
372 cleanup_tree_cfg_loop ();