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, 59 Temple Place - Suite 330, 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 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
59 is the exit edge whose condition is replaced. */
62 create_canonical_iv (struct loop
*loop
, edge exit
, tree niter
)
66 block_stmt_iterator incr_at
;
69 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
71 fprintf (dump_file
, "Added canonical iv to loop %d, ", loop
->num
);
72 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
73 fprintf (dump_file
, " iterations.\n");
76 cond
= last_stmt (exit
->src
);
77 in
= EDGE_SUCC (exit
->src
, 0);
79 in
= EDGE_SUCC (exit
->src
, 1);
81 /* Note that we do not need to worry about overflows, since
82 type of niter is always unsigned and all comparisons are
83 just for equality/nonequality -- i.e. everything works
84 with a modulo arithmetics. */
86 type
= TREE_TYPE (niter
);
87 niter
= fold (build2 (PLUS_EXPR
, type
,
89 build_int_cst (type
, 1)));
90 incr_at
= bsi_last (in
->src
);
92 fold_convert (type
, integer_minus_one_node
),
94 &incr_at
, false, NULL
, &var
);
96 cmp
= (exit
->flags
& EDGE_TRUE_VALUE
) ? EQ_EXPR
: NE_EXPR
;
97 COND_EXPR_COND (cond
) = build2 (cmp
, boolean_type_node
,
99 build_int_cst (type
, 0));
103 /* Computes an estimated number of insns in LOOP. */
106 tree_num_loop_insns (struct loop
*loop
)
108 basic_block
*body
= get_loop_body (loop
);
109 block_stmt_iterator bsi
;
110 unsigned size
= 1, i
;
112 for (i
= 0; i
< loop
->num_nodes
; i
++)
113 for (bsi
= bsi_start (body
[i
]); !bsi_end_p (bsi
); bsi_next (&bsi
))
114 size
+= estimate_num_insns (bsi_stmt (bsi
));
120 /* Tries to unroll LOOP completely, i.e. NITER times. LOOPS is the
121 loop tree. COMPLETELY_UNROLL is true if we should unroll the loop
122 even if it may cause code growth. EXIT is the exit of the loop
123 that should be eliminated. */
126 try_unroll_loop_completely (struct loops
*loops ATTRIBUTE_UNUSED
,
128 edge exit
, tree niter
,
129 bool completely_unroll
)
131 unsigned HOST_WIDE_INT n_unroll
, ninsns
, max_unroll
;
132 tree old_cond
, cond
, dont_exit
, do_exit
;
137 if (!host_integerp (niter
, 1))
139 n_unroll
= tree_low_cst (niter
, 1);
141 max_unroll
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
142 if (n_unroll
> max_unroll
)
147 if (!completely_unroll
)
150 ninsns
= tree_num_loop_insns (loop
);
152 if (n_unroll
* ninsns
153 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
))
157 if (exit
->flags
& EDGE_TRUE_VALUE
)
159 dont_exit
= boolean_false_node
;
160 do_exit
= boolean_true_node
;
164 dont_exit
= boolean_true_node
;
165 do_exit
= boolean_false_node
;
167 cond
= last_stmt (exit
->src
);
171 old_cond
= COND_EXPR_COND (cond
);
172 COND_EXPR_COND (cond
) = dont_exit
;
175 if (!tree_duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
176 loops
, n_unroll
, NULL
,
177 NULL
, NULL
, NULL
, 0))
179 COND_EXPR_COND (cond
) = old_cond
;
185 COND_EXPR_COND (cond
) = do_exit
;
188 update_ssa (TODO_update_ssa
);
190 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
191 fprintf (dump_file
, "Unrolled loop %d completely.\n", loop
->num
);
196 /* Adds a canonical induction variable to LOOP if suitable. LOOPS is the loops
197 tree. CREATE_IV is true if we may create a new iv. COMPLETELY_UNROLL is
198 true if we should do complete unrolling even if it may cause the code
199 growth. If TRY_EVAL is true, we try to determine the number of iterations
200 of a loop by direct evaluation. Returns true if cfg is changed. */
203 canonicalize_loop_induction_variables (struct loops
*loops
, struct loop
*loop
,
204 bool create_iv
, bool completely_unroll
,
210 niter
= number_of_iterations_in_loop (loop
);
211 if (TREE_CODE (niter
) == INTEGER_CST
)
213 exit
= loop
->single_exit
;
214 if (!just_once_each_iteration_p (loop
, exit
->src
))
217 /* The result of number_of_iterations_in_loop is by one higher than
218 we expect (i.e. it returns number of executions of the exit
219 condition, not of the loop latch edge). */
220 niter
= fold (build2 (MINUS_EXPR
, TREE_TYPE (niter
), niter
,
221 build_int_cst (TREE_TYPE (niter
), 1)));
225 /* If the loop has more than one exit, try checking all of them
226 for # of iterations determinable through scev. */
227 if (!loop
->single_exit
)
228 niter
= find_loop_niter (loop
, &exit
);
230 /* Finally if everything else fails, try brute force evaluation. */
232 && (chrec_contains_undetermined (niter
)
233 || TREE_CODE (niter
) != INTEGER_CST
))
234 niter
= find_loop_niter_by_eval (loop
, &exit
);
236 if (chrec_contains_undetermined (niter
)
237 || TREE_CODE (niter
) != INTEGER_CST
)
241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
243 fprintf (dump_file
, "Loop %d iterates ", loop
->num
);
244 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
245 fprintf (dump_file
, " times.\n");
248 if (try_unroll_loop_completely (loops
, loop
, exit
, niter
, completely_unroll
))
252 create_canonical_iv (loop
, exit
, niter
);
257 /* The main entry point of the pass. Adds canonical induction variables
258 to the suitable LOOPS. */
261 canonicalize_induction_variables (struct loops
*loops
)
265 bool changed
= false;
267 for (i
= 1; i
< loops
->num
; i
++)
269 loop
= loops
->parray
[i
];
272 changed
|= canonicalize_loop_induction_variables (loops
, loop
,
276 /* Clean up the information about numbers of iterations, since brute force
277 evaluation could reveal new information. */
281 cleanup_tree_cfg_loop ();
284 /* Unroll LOOPS completely if they iterate just few times. */
287 tree_unroll_loops_completely (struct loops
*loops
)
291 bool changed
= false;
293 for (i
= 1; i
< loops
->num
; i
++)
295 loop
= loops
->parray
[i
];
300 changed
|= canonicalize_loop_induction_variables (loops
, loop
,
302 !flag_tree_loop_ivcanon
);
305 /* Clean up the information about numbers of iterations, since complete
306 unrolling might have invalidated it. */
310 cleanup_tree_cfg_loop ();