2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Normalize returns.
23 * @author Michael Beck
29 #include "iroptimize.h"
30 #include "irgraph_t.h"
36 #include "raw_bitset.h"
39 * Normalize the Returns of a graph by creating a new End block
40 * with One Return(Phi).
41 * This is the preferred input for the if-conversion.
43 * In pseudocode, it means:
58 void normalize_one_return(ir_graph *irg)
60 ir_node *endbl = get_irg_end_block(irg);
61 ir_entity *entity = get_irg_entity(irg);
62 ir_type *type = get_entity_type(entity);
63 int n_ret_vals = get_method_n_ress(type) + 1;
65 bool filter_dbgi = false;
66 dbg_info *combined_dbgi = NULL;
68 int i, j, k, n, last_idx;
69 ir_node **in, **retvals, **endbl_in;
72 /* look, if we have more than one return */
73 n = get_Block_n_cfgpreds(endbl);
75 /* The end block has no predecessors, we have an endless
76 loop. In that case, no returns exists. */
80 rbitset_alloca(returns, n);
82 for (i = 0; i < n; ++i) {
83 ir_node *node = get_Block_cfgpred(endbl, i);
85 if (is_Return(node)) {
86 dbg_info *dbgi = get_irn_dbg_info(node);
88 if (dbgi != NULL && dbgi != combined_dbgi) {
98 rbitset_set(returns, i);
105 in = ALLOCAN(ir_node*, MAX(n_rets, n_ret_vals));
106 retvals = ALLOCAN(ir_node*, n_rets * n_ret_vals);
107 endbl_in = ALLOCAN(ir_node*, n);
110 for (j = i = 0; i < n; ++i) {
111 ir_node *ret = get_Block_cfgpred(endbl, i);
113 if (rbitset_is_set(returns, i)) {
114 ir_node *block = get_nodes_block(ret);
116 /* create a new Jmp for every Ret and place the in in */
117 in[j] = new_r_Jmp(block);
119 /* save the return values and shuffle them */
120 for (k = 0; k < n_ret_vals; ++k)
121 retvals[j + k*n_rets] = get_irn_n(ret, k);
125 endbl_in[last_idx++] = ret;
129 /* ok, create a new block with all created in's */
130 block = new_r_Block(irg, n_rets, in);
132 /* now create the Phi nodes */
133 for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
134 ir_mode *mode = get_irn_mode(retvals[j]);
135 in[i] = new_r_Phi(block, n_rets, &retvals[j], mode);
138 endbl_in[last_idx++] = new_rd_Return(combined_dbgi, block, in[0], n_ret_vals-1, &in[1]);
140 set_irn_in(endbl, last_idx, endbl_in);
142 /* invalidate analysis information:
143 * a new Block was added, so dominator, outs and loop are inconsistent,
144 * trouts and callee-state should be still valid
146 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
147 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
150 /* Create a graph pass. */
151 ir_graph_pass_t *normalize_one_return_pass(const char *name)
153 return def_graph_pass(name ? name : "one_ret", normalize_one_return);
157 * Check, whether a Return can be moved on block upwards.
159 * In a block with a Return, all live nodes must be linked
160 * with the Return, otherwise they are dead (because the Return leaves
161 * the graph, so no more users of the other nodes can exists.
163 * We can move a Return, if its predecessors are Phi nodes or
164 * comes from another block. In the later case, it is always possible
165 * to move the Return one block up, because the predecessor block must
166 * dominate the Return block (SSA) and then it dominates the predecessor
167 * block of the Return block as well.
169 * All predecessors of the Return block must be Jmp's of course, or we
170 * cannot move it up, so we add blocks if needed.
172 static bool can_move_ret(ir_node *ret)
174 ir_node *retbl = get_nodes_block(ret);
175 int i, n = get_irn_arity(ret);
177 for (i = 0; i < n; ++i) {
178 ir_node *pred = get_irn_n(ret, i);
180 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
181 /* first condition failed, found a non-Phi predecessor
182 * then is in the Return block */
187 /* check, that predecessors are Jmps */
188 n = get_Block_n_cfgpreds(retbl);
189 /* we cannot move above a labeled block, as this might kill the block */
190 if (n <= 1 || has_Block_entity(retbl))
192 for (i = 0; i < n; ++i) {
193 ir_node *pred = get_Block_cfgpred(retbl, i);
195 pred = skip_Tuple(pred);
196 if (! is_Jmp(pred) && !is_Bad(pred)) {
197 /* simply place a new block here */
198 ir_graph *irg = get_irn_irg(retbl);
199 ir_node *block = new_r_Block(irg, 1, &pred);
200 ir_node *jmp = new_r_Jmp(block);
201 set_Block_cfgpred(retbl, i, jmp);
208 * Normalize the Returns of a graph by moving
209 * the Returns upwards as much as possible.
210 * This might be preferred for code generation.
212 * In pseudocode, it means:
220 * is transformed into
227 void normalize_n_returns(ir_graph *irg)
230 ir_node *list = NULL;
231 ir_node *final = NULL;
233 unsigned n_finals = 0;
234 ir_node *endbl = get_irg_end_block(irg);
240 * First, link all returns:
241 * These must be predecessors of the endblock.
242 * Place Returns that can be moved on list, all others
245 n = get_Block_n_cfgpreds(endbl);
246 for (i = 0; i < n; ++i) {
247 ir_node *ret = get_Block_cfgpred(endbl, i);
251 } else if (is_Return(ret) && can_move_ret(ret)) {
253 * Ok, all conditions met, we can move this Return, put it
256 set_irn_link(ret, list);
260 /* Put all nodes that are not changed on the final list. */
261 set_irn_link(ret, final);
271 * Now move the Returns upwards. We move always one block up (and create n
272 * new Returns), than we check if a newly created Return can be moved even
273 * further. If yes, we simply add it to our work list, else to the final
276 end = get_irg_end(irg);
277 n_ret_vals = get_irn_arity(list);
278 in = ALLOCAN(ir_node*, n_ret_vals);
279 while (list != NULL) {
281 ir_node *block = get_nodes_block(ret);
282 dbg_info *dbgi = get_irn_dbg_info(ret);
285 list = (ir_node*)get_irn_link(ret);
288 n = get_Block_n_cfgpreds(block);
289 for (i = 0; i < n; ++i) {
290 ir_node *jmp = get_Block_cfgpred(block, i);
291 ir_node *new_bl, *new_ret;
297 new_bl = get_nodes_block(jmp);
299 /* create the in-array for the new Return */
300 for (j = 0; j < n_ret_vals; ++j) {
301 ir_node *pred = get_irn_n(ret, j);
303 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
306 new_ret = new_rd_Return(dbgi, new_bl, in[0], n_ret_vals-1, &in[1]);
308 if (! is_Bad(new_ret)) {
310 * The newly created node might be bad, if we
311 * create it in a block with only Bad predecessors.
312 * In that case ignore this block.
314 * We could even kill the jmp then ...
316 if (can_move_ret(new_ret)) {
317 set_irn_link(new_ret, list);
321 set_irn_link(new_ret, final);
327 /* remove the Jmp, we have placed a Return here */
328 exchange(jmp, new_r_Bad(irg, mode_X));
332 * if the memory of the old Return is a PhiM, remove it
333 * from the keep-alives, or it will keep the block which
334 * will crash the dominator algorithm.
336 phiM = get_Return_mem(ret);
338 n = get_End_n_keepalives(end);
339 for (i = 0; i < n; ++i) {
340 if (get_End_keepalive(end, i) == phiM) {
341 set_End_keepalive(end, i, new_r_Bad(irg, mode_M));
349 * Last step: Create a new endblock, with all nodes on the final list as
352 in = ALLOCAN(ir_node*, n_finals);
354 for (i = 0; final != NULL; ++i, final = (ir_node*)get_irn_link(final)) {
358 exchange(endbl, new_r_Block(irg, n_finals, in));
360 /* Invalidate analysis information:
361 * Blocks become dead and new Returns were deleted, so dominator, outs and
362 * loop are inconsistent, trouts and callee-state should be still valid */
363 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
364 | IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE
365 | IR_GRAPH_STATE_ONE_RETURN
366 | IR_GRAPH_STATE_CONSISTENT_OUTS
367 | IR_GRAPH_STATE_NO_UNREACHABLE_CODE
368 | IR_GRAPH_STATE_NO_BADS
369 | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
372 /* Create a graph pass. */
373 ir_graph_pass_t *normalize_n_returns_pass(const char *name)
375 return def_graph_pass(name ? name : "n_rets", normalize_n_returns);