1 /* Loop header copying on trees.
2 Copyright (C) 2004 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 "tree-flow.h"
33 #include "tree-dump.h"
34 #include "tree-pass.h"
37 #include "tree-inline.h"
39 #include "tree-inline.h"
41 /* Duplicates headers of loops if they are small enough, so that the statements
42 in the loop body are always executed when the loop is entered. This
43 increases effectivity of code motion optimizations, and reduces the need
44 for loop preconditioning. */
46 /* Check whether we should duplicate HEADER of LOOP. At most *LIMIT
47 instructions should be duplicated, limit is decreased by the actual
51 should_duplicate_loop_header_p (basic_block header
, struct loop
*loop
,
54 block_stmt_iterator bsi
;
57 /* Do not copy one block more than once (we do not really want to do
58 loop peeling here). */
62 gcc_assert (header
->succ
);
63 if (!header
->succ
->succ_next
)
65 if (header
->succ
->succ_next
->succ_next
)
67 if (flow_bb_inside_loop_p (loop
, header
->succ
->dest
)
68 && flow_bb_inside_loop_p (loop
, header
->succ
->succ_next
->dest
))
71 /* If this is not the original loop header, we want it to have just
72 one predecessor in order to match the && pattern. */
73 if (header
!= loop
->header
74 && header
->pred
->pred_next
)
77 last
= last_stmt (header
);
78 if (TREE_CODE (last
) != COND_EXPR
)
81 /* Approximately copy the conditions that used to be used in jump.c --
82 at most 20 insns and no calls. */
83 for (bsi
= bsi_start (header
); !bsi_end_p (bsi
); bsi_next (&bsi
))
85 last
= bsi_stmt (bsi
);
87 if (TREE_CODE (last
) == LABEL_EXPR
)
90 if (get_call_expr_in (last
))
93 *limit
-= estimate_num_insns (last
);
101 /* Duplicates destinations of edges in BBS_TO_DUPLICATE. */
104 duplicate_blocks (varray_type bbs_to_duplicate
)
107 edge preheader_edge
, e
, e1
;
108 basic_block header
, new_header
;
109 tree phi
, new_phi
, var
;
111 /* TODO: It should be quite easy to keep the dominance information
113 free_dominance_info (CDI_DOMINATORS
);
115 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (bbs_to_duplicate
); i
++)
117 preheader_edge
= VARRAY_GENERIC_PTR_NOGC (bbs_to_duplicate
, i
);
118 header
= preheader_edge
->dest
;
120 gcc_assert (header
->aux
);
123 new_header
= duplicate_block (header
, preheader_edge
);
125 /* Create the phi nodes on on entry to new_header. */
126 for (phi
= phi_nodes (header
), var
= PENDING_STMT (preheader_edge
);
128 phi
= TREE_CHAIN (phi
), var
= TREE_CHAIN (var
))
130 new_phi
= create_phi_node (PHI_RESULT (phi
), new_header
);
131 add_phi_arg (&new_phi
, TREE_VALUE (var
), preheader_edge
);
133 PENDING_STMT (preheader_edge
) = NULL
;
135 /* Add the phi arguments to the outgoing edges. */
136 for (e
= header
->succ
; e
; e
= e
->succ_next
)
138 for (e1
= new_header
->succ
; e1
->dest
!= e
->dest
; e1
= e1
->succ_next
)
141 for (phi
= phi_nodes (e
->dest
); phi
; phi
= TREE_CHAIN (phi
))
143 tree def
= PHI_ARG_DEF_FROM_EDGE (phi
, e
);
144 add_phi_arg (&phi
, def
, e1
);
149 calculate_dominance_info (CDI_DOMINATORS
);
151 rewrite_ssa_into_ssa ();
154 /* Checks whether LOOP is a do-while style loop. */
157 do_while_loop_p (struct loop
*loop
)
159 tree stmt
= last_stmt (loop
->latch
);
161 /* If the latch of the loop is not empty, it is not a do-while loop. */
163 && TREE_CODE (stmt
) != LABEL_EXPR
)
166 /* If the header contains just a condition, it is not a do-while loop. */
167 stmt
= last_and_only_stmt (loop
->header
);
169 && TREE_CODE (stmt
) == COND_EXPR
)
175 /* For all loops, copy the condition at the end of the loop body in front
176 of the loop. This is beneficial since it increases efficiency of
177 code motion optimizations. It also saves one jump on entry to the loop. */
180 copy_loop_headers (void)
187 varray_type bbs_to_duplicate
= NULL
;
189 loops
= loop_optimizer_init (dump_file
);
193 /* We do not try to keep the information about irreducible regions
195 loops
->state
&= ~LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS
;
197 #ifdef ENABLE_CHECKING
198 verify_loop_structure (loops
);
201 for (i
= 1; i
< loops
->num
; i
++)
203 /* Copy at most 20 insns. */
206 loop
= loops
->parray
[i
];
207 preheader_edge
= loop_preheader_edge (loop
);
208 header
= preheader_edge
->dest
;
210 /* If the loop is already a do-while style one (either because it was
211 written as such, or because jump threading transformed it into one),
212 we might be in fact peeling the first iteration of the loop. This
213 in general is not a good idea. */
214 if (do_while_loop_p (loop
))
217 /* Iterate the header copying up to limit; this takes care of the cases
218 like while (a && b) {...}, where we want to have both of the conditions
219 copied. TODO -- handle while (a || b) - like cases, by not requiring
220 the header to have just a single successor and copying up to
223 We do not really copy the blocks immediately, so that we do not have
224 to worry about updating loop structures, and also so that we do not
225 have to rewrite variables out of and into ssa form for each block.
226 Instead we just record the block into worklist and duplicate all of
228 while (should_duplicate_loop_header_p (header
, loop
, &limit
))
230 if (!bbs_to_duplicate
)
231 VARRAY_GENERIC_PTR_NOGC_INIT (bbs_to_duplicate
, 10,
233 VARRAY_PUSH_GENERIC_PTR_NOGC (bbs_to_duplicate
, preheader_edge
);
234 header
->aux
= &header
->aux
;
236 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
238 "Scheduled basic block %d for duplication.\n",
241 /* Find a successor of header that is inside a loop; i.e. the new
242 header after the condition is copied. */
243 if (flow_bb_inside_loop_p (loop
, header
->succ
->dest
))
244 preheader_edge
= header
->succ
;
246 preheader_edge
= header
->succ
->succ_next
;
247 header
= preheader_edge
->dest
;
251 loop_optimizer_finalize (loops
, NULL
);
253 if (bbs_to_duplicate
)
255 duplicate_blocks (bbs_to_duplicate
);
256 VARRAY_FREE (bbs_to_duplicate
);
259 /* Run cleanup_tree_cfg here regardless of whether we have done anything, so
260 that we cleanup the blocks created in order to get the loops into a
268 return flag_tree_ch
!= 0;
271 struct tree_opt_pass pass_ch
=
275 copy_loop_headers
, /* execute */
278 0, /* static_pass_number */
279 TV_TREE_CH
, /* tv_id */
280 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
281 0, /* properties_provided */
282 0, /* properties_destroyed */
283 0, /* todo_flags_start */
285 | TODO_verify_ssa
), /* todo_flags_finish */