1 /* Induction variable canonicalization.
2 Copyright (C) 2004, 2005, 2007, 2008, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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"
42 #include "basic-block.h"
43 #include "gimple-pretty-print.h"
44 #include "tree-flow.h"
46 #include "tree-pass.h"
47 #include "tree-chrec.h"
48 #include "tree-scalar-evolution.h"
51 #include "tree-inline.h"
54 /* Specifies types of loops that may be unrolled. */
58 UL_SINGLE_ITER
, /* Only loops that exit immediately in the first
60 UL_NO_GROWTH
, /* Only loops whose unrolling will not cause increase
62 UL_ALL
/* All suitable loops. */
65 /* Adds a canonical induction variable to LOOP iterating NITER times. EXIT
66 is the exit edge whose condition is replaced. */
69 create_canonical_iv (struct loop
*loop
, edge exit
, tree niter
)
74 gimple_stmt_iterator incr_at
;
77 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
79 fprintf (dump_file
, "Added canonical iv to loop %d, ", loop
->num
);
80 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
81 fprintf (dump_file
, " iterations.\n");
84 cond
= last_stmt (exit
->src
);
85 in
= EDGE_SUCC (exit
->src
, 0);
87 in
= EDGE_SUCC (exit
->src
, 1);
89 /* Note that we do not need to worry about overflows, since
90 type of niter is always unsigned and all comparisons are
91 just for equality/nonequality -- i.e. everything works
92 with a modulo arithmetics. */
94 type
= TREE_TYPE (niter
);
95 niter
= fold_build2 (PLUS_EXPR
, type
,
97 build_int_cst (type
, 1));
98 incr_at
= gsi_last_bb (in
->src
);
100 build_int_cst (type
, -1),
102 &incr_at
, false, NULL
, &var
);
104 cmp
= (exit
->flags
& EDGE_TRUE_VALUE
) ? EQ_EXPR
: NE_EXPR
;
105 gimple_cond_set_code (cond
, cmp
);
106 gimple_cond_set_lhs (cond
, var
);
107 gimple_cond_set_rhs (cond
, build_int_cst (type
, 0));
111 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS. */
114 tree_num_loop_insns (struct loop
*loop
, eni_weights
*weights
)
116 basic_block
*body
= get_loop_body (loop
);
117 gimple_stmt_iterator gsi
;
118 unsigned size
= 0, i
;
120 for (i
= 0; i
< loop
->num_nodes
; i
++)
121 for (gsi
= gsi_start_bb (body
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
122 size
+= estimate_num_insns (gsi_stmt (gsi
), weights
);
128 /* Describe size of loop as detected by tree_estimate_loop_size. */
131 /* Number of instructions in the loop. */
134 /* Number of instructions that will be likely optimized out in
135 peeled iterations of loop (i.e. computation based on induction
136 variable where induction variable starts at known constant.) */
137 int eliminated_by_peeling
;
139 /* Same statistics for last iteration of loop: it is smaller because
140 instructions after exit are not executed. */
142 int last_iteration_eliminated_by_peeling
;
145 /* Return true if OP in STMT will be constant after peeling LOOP. */
148 constant_after_peeling (tree op
, gimple stmt
, struct loop
*loop
)
152 if (is_gimple_min_invariant (op
))
155 /* We can still fold accesses to constant arrays when index is known. */
156 if (TREE_CODE (op
) != SSA_NAME
)
160 /* First make fast look if we see constant array inside. */
161 while (handled_component_p (base
))
162 base
= TREE_OPERAND (base
, 0);
163 if ((DECL_P (base
) == VAR_DECL
164 && const_value_known_p (base
))
165 || CONSTANT_CLASS_P (base
))
167 /* If so, see if we understand all the indices. */
169 while (handled_component_p (base
))
171 if (TREE_CODE (base
) == ARRAY_REF
172 && !constant_after_peeling (TREE_OPERAND (base
, 1), stmt
, loop
))
174 base
= TREE_OPERAND (base
, 0);
181 /* Induction variables are constants. */
182 if (!simple_iv (loop
, loop_containing_stmt (stmt
), op
, &iv
, false))
184 if (!is_gimple_min_invariant (iv
.base
))
186 if (!is_gimple_min_invariant (iv
.step
))
191 /* Computes an estimated number of insns in LOOP, weighted by WEIGHTS.
192 Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT. */
195 tree_estimate_loop_size (struct loop
*loop
, edge exit
, struct loop_size
*size
)
197 basic_block
*body
= get_loop_body (loop
);
198 gimple_stmt_iterator gsi
;
203 size
->eliminated_by_peeling
= 0;
204 size
->last_iteration
= 0;
205 size
->last_iteration_eliminated_by_peeling
= 0;
207 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
208 fprintf (dump_file
, "Estimating sizes for loop %i\n", loop
->num
);
209 for (i
= 0; i
< loop
->num_nodes
; i
++)
211 if (exit
&& body
[i
] != exit
->src
212 && dominated_by_p (CDI_DOMINATORS
, body
[i
], exit
->src
))
216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
217 fprintf (dump_file
, " BB: %i, after_exit: %i\n", body
[i
]->index
, after_exit
);
219 for (gsi
= gsi_start_bb (body
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
221 gimple stmt
= gsi_stmt (gsi
);
222 int num
= estimate_num_insns (stmt
, &eni_size_weights
);
223 bool likely_eliminated
= false;
225 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
227 fprintf (dump_file
, " size: %3i ", num
);
228 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0, 0);
231 /* Look for reasons why we might optimize this stmt away. */
233 /* Exit conditional. */
234 if (body
[i
] == exit
->src
&& stmt
== last_stmt (exit
->src
))
236 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
237 fprintf (dump_file
, " Exit condition will be eliminated.\n");
238 likely_eliminated
= true;
240 /* Sets of IV variables */
241 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
242 && constant_after_peeling (gimple_assign_lhs (stmt
), stmt
, loop
))
244 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
245 fprintf (dump_file
, " Induction variable computation will"
246 " be folded away.\n");
247 likely_eliminated
= true;
249 /* Assignments of IV variables. */
250 else if (gimple_code (stmt
) == GIMPLE_ASSIGN
251 && TREE_CODE (gimple_assign_lhs (stmt
)) == SSA_NAME
252 && constant_after_peeling (gimple_assign_rhs1 (stmt
), stmt
,loop
)
253 && (gimple_assign_rhs_class (stmt
) != GIMPLE_BINARY_RHS
254 || constant_after_peeling (gimple_assign_rhs2 (stmt
),
257 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
258 fprintf (dump_file
, " Constant expression will be folded away.\n");
259 likely_eliminated
= true;
262 else if (gimple_code (stmt
) == GIMPLE_COND
263 && constant_after_peeling (gimple_cond_lhs (stmt
), stmt
, loop
)
264 && constant_after_peeling (gimple_cond_rhs (stmt
), stmt
, loop
))
266 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
267 fprintf (dump_file
, " Constant conditional.\n");
268 likely_eliminated
= true;
271 size
->overall
+= num
;
272 if (likely_eliminated
)
273 size
->eliminated_by_peeling
+= num
;
276 size
->last_iteration
+= num
;
277 if (likely_eliminated
)
278 size
->last_iteration_eliminated_by_peeling
+= num
;
282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
283 fprintf (dump_file
, "size: %i-%i, last_iteration: %i-%i\n", size
->overall
,
284 size
->eliminated_by_peeling
, size
->last_iteration
,
285 size
->last_iteration_eliminated_by_peeling
);
290 /* Estimate number of insns of completely unrolled loop.
291 It is (NUNROLL + 1) * size of loop body with taking into account
292 the fact that in last copy everything after exit conditional
293 is dead and that some instructions will be eliminated after
296 Loop body is likely going to simplify futher, this is difficult
297 to guess, we just decrease the result by 1/3. */
299 static unsigned HOST_WIDE_INT
300 estimated_unrolled_size (struct loop_size
*size
,
301 unsigned HOST_WIDE_INT nunroll
)
303 HOST_WIDE_INT unr_insns
= ((nunroll
)
304 * (HOST_WIDE_INT
) (size
->overall
305 - size
->eliminated_by_peeling
));
308 unr_insns
+= size
->last_iteration
- size
->last_iteration_eliminated_by_peeling
;
310 unr_insns
= unr_insns
* 2 / 3;
317 /* Tries to unroll LOOP completely, i.e. NITER times.
318 UL determines which loops we are allowed to unroll.
319 EXIT is the exit of the loop that should be eliminated. */
322 try_unroll_loop_completely (struct loop
*loop
,
323 edge exit
, tree niter
,
324 enum unroll_level ul
)
326 unsigned HOST_WIDE_INT n_unroll
, ninsns
, max_unroll
, unr_insns
;
328 struct loop_size size
;
333 if (!host_integerp (niter
, 1))
335 n_unroll
= tree_low_cst (niter
, 1);
337 max_unroll
= PARAM_VALUE (PARAM_MAX_COMPLETELY_PEEL_TIMES
);
338 if (n_unroll
> max_unroll
)
343 if (ul
== UL_SINGLE_ITER
)
346 tree_estimate_loop_size (loop
, exit
, &size
);
347 ninsns
= size
.overall
;
349 unr_insns
= estimated_unrolled_size (&size
, n_unroll
);
350 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
352 fprintf (dump_file
, " Loop size: %d\n", (int) ninsns
);
353 fprintf (dump_file
, " Estimated size after unrolling: %d\n",
357 if (unr_insns
> ninsns
359 > (unsigned) PARAM_VALUE (PARAM_MAX_COMPLETELY_PEELED_INSNS
)))
361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
362 fprintf (dump_file
, "Not unrolling loop %d "
363 "(--param max-completely-peeled-insns limit reached).\n",
368 if (ul
== UL_NO_GROWTH
369 && unr_insns
> ninsns
)
371 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
372 fprintf (dump_file
, "Not unrolling loop %d.\n", loop
->num
);
382 VEC (edge
, heap
) *to_remove
= NULL
;
384 initialize_original_copy_tables ();
385 wont_exit
= sbitmap_alloc (n_unroll
+ 1);
386 sbitmap_ones (wont_exit
);
387 RESET_BIT (wont_exit
, 0);
389 if (!gimple_duplicate_loop_to_header_edge (loop
, loop_preheader_edge (loop
),
392 DLTHE_FLAG_UPDATE_FREQ
393 | DLTHE_FLAG_COMPLETTE_PEEL
))
395 free_original_copy_tables ();
400 FOR_EACH_VEC_ELT (edge
, to_remove
, i
, e
)
402 bool ok
= remove_path (e
);
406 VEC_free (edge
, heap
, to_remove
);
408 free_original_copy_tables ();
411 cond
= last_stmt (exit
->src
);
412 if (exit
->flags
& EDGE_TRUE_VALUE
)
413 gimple_cond_make_true (cond
);
415 gimple_cond_make_false (cond
);
418 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
419 fprintf (dump_file
, "Unrolled loop %d completely.\n", loop
->num
);
424 /* Adds a canonical induction variable to LOOP if suitable.
425 CREATE_IV is true if we may create a new iv. UL determines
426 which loops we are allowed to completely unroll. If TRY_EVAL is true, we try
427 to determine the number of iterations of a loop by direct evaluation.
428 Returns true if cfg is changed. */
431 canonicalize_loop_induction_variables (struct loop
*loop
,
432 bool create_iv
, enum unroll_level ul
,
438 niter
= number_of_latch_executions (loop
);
439 if (TREE_CODE (niter
) == INTEGER_CST
)
441 exit
= single_exit (loop
);
442 if (!just_once_each_iteration_p (loop
, exit
->src
))
447 /* If the loop has more than one exit, try checking all of them
448 for # of iterations determinable through scev. */
449 if (!single_exit (loop
))
450 niter
= find_loop_niter (loop
, &exit
);
452 /* Finally if everything else fails, try brute force evaluation. */
454 && (chrec_contains_undetermined (niter
)
455 || TREE_CODE (niter
) != INTEGER_CST
))
456 niter
= find_loop_niter_by_eval (loop
, &exit
);
458 if (chrec_contains_undetermined (niter
)
459 || TREE_CODE (niter
) != INTEGER_CST
)
463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
465 fprintf (dump_file
, "Loop %d iterates ", loop
->num
);
466 print_generic_expr (dump_file
, niter
, TDF_SLIM
);
467 fprintf (dump_file
, " times.\n");
470 if (try_unroll_loop_completely (loop
, exit
, niter
, ul
))
474 create_canonical_iv (loop
, exit
, niter
);
479 /* The main entry point of the pass. Adds canonical induction variables
480 to the suitable loops. */
483 canonicalize_induction_variables (void)
487 bool changed
= false;
489 FOR_EACH_LOOP (li
, loop
, 0)
491 changed
|= canonicalize_loop_induction_variables (loop
,
492 true, UL_SINGLE_ITER
,
495 gcc_assert (!need_ssa_update_p (cfun
));
497 /* Clean up the information about numbers of iterations, since brute force
498 evaluation could reveal new information. */
502 return TODO_cleanup_cfg
;
506 /* Propagate VAL into all uses of SSA_NAME. */
509 propagate_into_all_uses (tree ssa_name
, tree val
)
511 imm_use_iterator iter
;
514 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, ssa_name
)
516 gimple_stmt_iterator use_stmt_gsi
= gsi_for_stmt (use_stmt
);
519 FOR_EACH_IMM_USE_ON_STMT (use
, iter
)
522 if (is_gimple_assign (use_stmt
)
523 && get_gimple_rhs_class (gimple_assign_rhs_code (use_stmt
))
524 == GIMPLE_SINGLE_RHS
)
526 tree rhs
= gimple_assign_rhs1 (use_stmt
);
528 if (TREE_CODE (rhs
) == ADDR_EXPR
)
529 recompute_tree_invariant_for_addr_expr (rhs
);
532 fold_stmt_inplace (&use_stmt_gsi
);
533 update_stmt (use_stmt
);
534 maybe_clean_or_replace_eh_stmt (use_stmt
, use_stmt
);
538 /* Propagate constant SSA_NAMEs defined in basic block BB. */
541 propagate_constants_for_unrolling (basic_block bb
)
543 gimple_stmt_iterator gsi
;
545 /* Look for degenerate PHI nodes with constant argument. */
546 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); )
548 gimple phi
= gsi_stmt (gsi
);
549 tree result
= gimple_phi_result (phi
);
550 tree arg
= gimple_phi_arg_def (phi
, 0);
552 if (gimple_phi_num_args (phi
) == 1 && TREE_CODE (arg
) == INTEGER_CST
)
554 propagate_into_all_uses (result
, arg
);
555 gsi_remove (&gsi
, true);
556 release_ssa_name (result
);
562 /* Look for assignments to SSA names with constant RHS. */
563 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
565 gimple stmt
= gsi_stmt (gsi
);
568 if (is_gimple_assign (stmt
)
569 && (lhs
= gimple_assign_lhs (stmt
), TREE_CODE (lhs
) == SSA_NAME
)
570 && gimple_assign_rhs_code (stmt
) == INTEGER_CST
)
572 propagate_into_all_uses (lhs
, gimple_assign_rhs1 (stmt
));
573 gsi_remove (&gsi
, true);
574 release_ssa_name (lhs
);
581 /* Unroll LOOPS completely if they iterate just few times. Unless
582 MAY_INCREASE_SIZE is true, perform the unrolling only if the
583 size of the code does not increase. */
586 tree_unroll_loops_completely (bool may_increase_size
, bool unroll_outer
)
588 VEC(loop_p
,stack
) *father_stack
= VEC_alloc (loop_p
, stack
, 16);
592 enum unroll_level ul
;
599 FOR_EACH_LOOP (li
, loop
, LI_ONLY_INNERMOST
)
601 struct loop
*loop_father
= loop_outer (loop
);
603 if (may_increase_size
&& optimize_loop_for_speed_p (loop
)
604 /* Unroll outermost loops only if asked to do so or they do
605 not cause code growth. */
606 && (unroll_outer
|| loop_outer (loop_father
)))
611 if (canonicalize_loop_induction_variables (loop
, false, ul
,
612 !flag_tree_loop_ivcanon
))
615 /* If we'll continue unrolling, we need to propagate constants
616 within the new basic blocks to fold away induction variable
617 computations; otherwise, the size might blow up before the
618 iteration is complete and the IR eventually cleaned up. */
619 if (loop_outer (loop_father
) && !loop_father
->aux
)
621 VEC_safe_push (loop_p
, stack
, father_stack
, loop_father
);
622 loop_father
->aux
= loop_father
;
632 update_ssa (TODO_update_ssa
);
634 /* Propagate the constants within the new basic blocks. */
635 FOR_EACH_VEC_ELT (loop_p
, father_stack
, i
, iter
)
638 basic_block
*body
= get_loop_body_in_dom_order (*iter
);
639 for (j
= 0; j
< (*iter
)->num_nodes
; j
++)
640 propagate_constants_for_unrolling (body
[j
]);
644 VEC_truncate (loop_p
, father_stack
, 0);
646 /* This will take care of removing completely unrolled loops
647 from the loop structures so we can continue unrolling now
649 if (cleanup_tree_cfg ())
650 update_ssa (TODO_update_ssa_only_virtuals
);
652 /* Clean up the information about numbers of iterations, since
653 complete unrolling might have invalidated it. */
658 && ++iteration
<= PARAM_VALUE (PARAM_MAX_UNROLL_ITERATIONS
));
660 VEC_free (loop_p
, stack
, father_stack
);