1 /* Loop optimizations over tree-ssa.
2 Copyright (C) 2003 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
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "basic-block.h"
33 #include "tree-flow.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
39 #include "tree-inline.h"
42 /* Check whether we should duplicate HEADER of LOOP. At most *LIMIT
43 instructions should be duplicated, limit is decreased by the actual
47 should_duplicate_loop_header_p (basic_block header
, struct loop
*loop
,
50 block_stmt_iterator bsi
;
53 /* Do not copy one block more than once (we do not really want to do
54 loop peeling here). */
60 if (!header
->succ
->succ_next
)
62 if (header
->succ
->succ_next
->succ_next
)
64 if (flow_bb_inside_loop_p (loop
, header
->succ
->dest
)
65 && flow_bb_inside_loop_p (loop
, header
->succ
->succ_next
->dest
))
68 /* If this is not the original loop header, we want it to have just
69 one predecessor in order to match the && pattern. */
70 if (header
!= loop
->header
71 && header
->pred
->pred_next
)
74 last
= last_stmt (header
);
75 if (TREE_CODE (last
) != COND_EXPR
)
78 /* Approximately copy the conditions that used to be used in jump.c --
79 at most 20 insns and no calls. */
80 for (bsi
= bsi_start (header
); !bsi_end_p (bsi
); bsi_next (&bsi
))
82 last
= bsi_stmt (bsi
);
84 if (TREE_CODE (last
) == LABEL_EXPR
)
87 if (get_call_expr_in (last
))
90 *limit
-= estimate_num_insns (last
);
98 /* Marks variables defined in basic block BB for rewriting. */
101 mark_defs_for_rewrite (basic_block bb
)
104 block_stmt_iterator bsi
;
111 for (stmt
= phi_nodes (bb
); stmt
; stmt
= TREE_CHAIN (stmt
))
113 var
= SSA_NAME_VAR (PHI_RESULT (stmt
));
114 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
116 /* If we have a type_mem_tag, add it as well. Due to rewriting the
117 variable out of ssa, we lose its name tag, so we use type_mem_tag
119 var
= var_ann (var
)->type_mem_tag
;
121 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
124 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
126 stmt
= bsi_stmt (bsi
);
127 get_stmt_operands (stmt
);
128 ann
= stmt_ann (stmt
);
130 defs
= DEF_OPS (ann
);
131 for (i
= 0; i
< NUM_DEFS (defs
); i
++)
133 var
= SSA_NAME_VAR (DEF_OP (defs
, i
));
134 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
136 /* If we have a type_mem_tag, add it as well. Due to rewriting the
137 variable out of ssa, we lose its name tag, so we use type_mem_tag
139 var
= var_ann (var
)->type_mem_tag
;
141 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
144 vdefs
= VDEF_OPS (ann
);
145 for (i
= 0; i
< NUM_VDEFS (vdefs
); i
++)
147 var
= SSA_NAME_VAR (VDEF_RESULT (vdefs
, i
));
148 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
151 /* We also need to rewrite vuses, since we will copy the statements
152 and the ssa versions could not be recovered in the copy. We do
153 not have to do this for operands of VDEFS explicitly, since
154 they have the same underlying variable as the results. */
155 vuses
= VUSE_OPS (ann
);
156 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
158 var
= SSA_NAME_VAR (VUSE_OP (vuses
, i
));
159 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
164 /* Duplicates destinations of edges in BBS_TO_DUPLICATE. */
167 duplicate_blocks (varray_type bbs_to_duplicate
)
170 edge preheader_edge
, e
, e1
;
171 basic_block header
, new_header
;
173 size_t old_num_referenced_vars
= num_referenced_vars
;
175 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (bbs_to_duplicate
); i
++)
177 preheader_edge
= VARRAY_GENERIC_PTR_NOGC (bbs_to_duplicate
, i
);
178 header
= preheader_edge
->dest
;
180 /* It is sufficient to rewrite the definitions, since the uses of
181 the operands defined outside of the duplicated basic block are
182 still valid (every basic block that dominates the original block
183 also dominates the duplicate). */
184 mark_defs_for_rewrite (header
);
187 rewrite_vars_out_of_ssa (vars_to_rename
);
189 for (i
= old_num_referenced_vars
; i
< num_referenced_vars
; i
++)
191 bitmap_set_bit (vars_to_rename
, i
);
192 var_ann (referenced_var (i
))->out_of_ssa_tag
= 0;
195 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (bbs_to_duplicate
); i
++)
197 preheader_edge
= VARRAY_GENERIC_PTR_NOGC (bbs_to_duplicate
, i
);
198 header
= preheader_edge
->dest
;
200 /* We might have split the edge into the loop header when we have
201 eliminated the phi nodes, so find the edge to that we want to
205 preheader_edge
= header
->succ
;
206 header
= preheader_edge
->dest
;
210 new_header
= duplicate_block (header
, preheader_edge
);
212 /* Add the phi arguments to the outgoing edges. */
213 for (e
= header
->succ
; e
; e
= e
->succ_next
)
215 for (e1
= new_header
->succ
; e1
->dest
!= e
->dest
; e1
= e1
->succ_next
)
218 for (phi
= phi_nodes (e
->dest
); phi
; phi
= TREE_CHAIN (phi
))
220 tree def
= phi_element_for_edge (phi
, e
)->def
;
221 add_phi_arg (&phi
, def
, e1
);
227 /* Checks whether LOOP is a do-while style loop. */
230 do_while_loop_p (struct loop
*loop
)
232 tree stmt
= last_stmt (loop
->latch
);
234 /* If the latch of the loop is not empty, it is not a do-while loop. */
236 && TREE_CODE (stmt
) != LABEL_EXPR
)
239 /* If the header contains just a condition, it is not a do-while loop. */
240 stmt
= last_and_only_stmt (loop
->header
);
242 && TREE_CODE (stmt
) == COND_EXPR
)
248 /* For all loops, copy the condition at the end of the loop body in front
249 of the loop. This is beneficial since it increases effectivity of
250 code motion optimizations. It also saves one jump on entry to the loop. */
253 copy_loop_headers (void)
260 varray_type bbs_to_duplicate
= NULL
;
262 loops
= loop_optimizer_init (dump_file
);
266 /* We are not going to need or update dominators. */
267 free_dominance_info (CDI_DOMINATORS
);
269 create_preheaders (loops
, CP_SIMPLE_PREHEADERS
);
271 /* We do not try to keep the information about irreductible regions
273 loops
->state
&= ~LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
;
275 #ifdef ENABLE_CHECKING
276 verify_loop_structure (loops
);
279 for (i
= 1; i
< loops
->num
; i
++)
281 /* Copy at most 20 insns. */
284 loop
= loops
->parray
[i
];
285 preheader_edge
= loop_preheader_edge (loop
);
286 header
= preheader_edge
->dest
;
288 /* If the loop is already a do-while style one (either because it was
289 written as such, or because jump threading transformed it into one),
290 we might be in fact peeling the first iteration of the loop. This
291 in general is not a good idea. */
292 if (do_while_loop_p (loop
))
295 /* Iterate the header copying up to limit; this takes care of the cases
296 like while (a && b) {...}, where we want to have both of the conditions
297 copied. TODO -- handle while (a || b) - like cases, by not requiring
298 the header to have just a single successor and copying up to
301 We do not really copy the blocks immediately, so that we do not have
302 to worry about updating loop structures, and also so that we do not
303 have to rewrite variables out of and into ssa form for each block.
304 Instead we just record the block into worklist and duplicate all of
306 while (should_duplicate_loop_header_p (header
, loop
, &limit
))
308 if (!bbs_to_duplicate
)
309 VARRAY_GENERIC_PTR_NOGC_INIT (bbs_to_duplicate
, 10,
311 VARRAY_PUSH_GENERIC_PTR_NOGC (bbs_to_duplicate
, preheader_edge
);
312 header
->aux
= &header
->aux
;
314 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
316 "Scheduled basic block %d for duplication.\n",
319 /* Find a successor of header that is inside a loop; i.e. the new
320 header after the condition is copied. */
321 if (flow_bb_inside_loop_p (loop
, header
->succ
->dest
))
322 preheader_edge
= header
->succ
;
324 preheader_edge
= header
->succ
->succ_next
;
325 header
= preheader_edge
->dest
;
329 loop_optimizer_finalize (loops
, NULL
);
331 if (bbs_to_duplicate
)
333 duplicate_blocks (bbs_to_duplicate
);
334 VARRAY_FREE (bbs_to_duplicate
);
337 /* Run cleanup_tree_cfg here regardless of whether we have done anything, so
338 that we cleanup the blocks created in order to get the loops into a
346 return flag_tree_ch
!= 0;
349 struct tree_opt_pass pass_ch
=
353 copy_loop_headers
, /* execute */
356 0, /* static_pass_number */
357 TV_TREE_CH
, /* tv_id */
358 PROP_cfg
| PROP_ssa
, /* properties_required */
359 0, /* properties_provided */
360 0, /* properties_destroyed */
361 0, /* todo_flags_start */
364 | TODO_verify_ssa
) /* todo_flags_finish */