3 * File name: ir/opt/return.c
4 * Purpose: normalize returns
8 * Copyright: (c) 1998-2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 #include "irgraph_t.h"
21 #define set_bit(n) (returns[(n) >> 3] |= 1 << ((n) & 7))
22 #define get_bit(n) (returns[(n) >> 3] & (1 << ((n) & 7)))
25 #define IMAX(a, b) ((a) > (b) ? (a) : (b))
28 * Normalize the Returns of a graph by creating a new End block
29 * with One Return(Phi).
30 * This is the preferred input for the if-conversion.
32 * In pseudocode, it means:
47 void normalize_one_return(ir_graph *irg)
49 ir_node *endbl = get_irg_end_block(irg);
50 int i, j, k, n, last_idx, n_rets, n_ret_vals = -1;
51 unsigned char *returns;
52 ir_node **in, **retvals, **endbl_in;
56 /* look, if we have more than one return */
57 n = get_Block_n_cfgpreds(endbl);
59 /* The end block has no predecessors, we have an endless
60 loop. In that case, no returns exists. */
64 returns = alloca((n + 7) >> 3);
65 memset(returns, 0, (n + 7) >> 3);
67 for (n_rets = i = 0; i < n; ++i) {
68 ir_node *node = get_Block_cfgpred(endbl, i);
70 if (is_Return(node)) {
76 n_ret_vals = get_irn_arity(node);
80 /* there should be at least one Return node in Firm */
84 in = alloca(sizeof(*in) * IMAX(n_rets, n_ret_vals));
85 retvals = alloca(sizeof(*retvals) * n_rets * n_ret_vals);
86 endbl_in = alloca(sizeof(*endbl_in) * n);
89 for (j = i = 0; i < n; ++i) {
90 ir_node *ret = get_Block_cfgpred(endbl, i);
93 ir_node *block = get_nodes_block(ret);
95 /* create a new Jmp for every Ret and place the in in */
96 in[j] = new_r_Jmp(irg, block);
98 /* save the return values and shuffle them */
99 for (k = 0; k < n_ret_vals; ++k)
100 retvals[j + k*n_rets] = get_irn_n(ret, k);
105 endbl_in[last_idx++] = ret;
108 /* ok, create a new block with all created in's */
109 block = new_r_Block(irg, n_rets, in);
111 /* now create the Phi nodes */
112 for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
115 /* the return values are already shuffled */
117 /* Beware: normally the Phi constructor automatically replaces a Phi(a,...a) into a
118 but NOT, if a is Unknown. Here, we known that this case can be optimize also,
120 first = retvals[j + 0];
121 for (k = 1; k < n_rets; ++k) {
122 if (retvals[j + k] != first) {
130 in[i] = new_r_Phi(irg, block, n_rets, &retvals[j], get_irn_mode(retvals[j]));
133 endbl_in[last_idx++] = new_r_Return(irg, block, in[0], n_ret_vals-1, &in[1]);
135 set_irn_in(endbl, last_idx, endbl_in);
137 /* invalidate analysis information:
138 * a new Block was added, so dominator, outs and loop are inconsistent,
139 * trouts and callee-state should be still valid
141 set_irg_doms_inconsistent(irg);
142 set_irg_outs_inconsistent(irg);
143 set_irg_extblk_inconsistent(irg);
144 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
148 * check, whether a Ret can be moved on block upwards.
150 * In a block with a Return, all live nodes must be linked
151 * with the Return, otherwise they are dead (because the Return leaves
152 * the graph, so no more users of the other nodes can exists.
154 * We can move a Return, if it's predecessors are Phi nodes or
155 * comes from another block. In the later case, it is always possible
156 * to move the Return one block up, because the predecessor block must
157 * dominate the Return block (SSA) and then it dominates the predecessor
158 * block of the Return block as well.
160 * All predecessors of the Return block must be Jmp's of course, or we
161 * cannot move it up, so we check this either.
163 static int can_move_ret(ir_node *ret)
165 ir_node *retbl = get_nodes_block(ret);
166 int i, n = get_irn_arity(ret);
168 for (i = 0; i < n; ++i) {
169 ir_node *pred = get_irn_n(ret, i);
171 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
172 /* first condition failed, found a non-Phi predecessor
173 * then is in the Return block */
178 /* check, that predecessors are Jmps */
179 n = get_Block_n_cfgpreds(retbl);
180 for (i = 0; i < n; ++i)
181 if (get_irn_op(get_Block_cfgpred(retbl, i)) != op_Jmp)
184 /* if we have 0 control flow predecessors, we cannot move :-) */
189 * Normalize the Returns of a graph by moving
190 * the Returns upwards as much as possible.
191 * This might be preferred for code generation.
193 * In pseudocode, it means:
201 * is transformed into
208 void normalize_n_returns(ir_graph *irg)
210 int i, j, n, n_rets, n_finals, n_ret_vals;
211 ir_node *list = NULL;
212 ir_node *final = NULL;
214 ir_node *endbl = get_irg_end_block(irg);
218 * First, link all returns:
219 * These must be predecessors of the endblock.
220 * Place Returns that can be moved on list, all others
223 n = get_Block_n_cfgpreds(endbl);
224 for (n_finals = n_rets = i = 0; i < n; ++i) {
225 ir_node *ret = get_Block_cfgpred(endbl, i);
227 if (is_Return(ret) && can_move_ret(ret)) {
229 * Ok, all conditions met, we can move this Return, put it
232 set_irn_link(ret, list);
237 /* Put all nodes that are not changed on the final list. */
238 set_irn_link(ret, final);
248 * Now move the Returns upwards. We move always one block up (and create n
249 * new Returns), than we check if a newly created Return can be moved even further.
250 * If yes, we simply add it to our work list, else to the final list.
252 end = get_irg_end(irg);
253 n_ret_vals = get_irn_arity(list);
254 in = alloca(sizeof(*in) * n_ret_vals);
257 ir_node *block = get_nodes_block(ret);
260 list = get_irn_link(ret);
263 n = get_Block_n_cfgpreds(block);
264 for (i = 0; i < n; ++i) {
265 ir_node *jmp = get_Block_cfgpred(block, i);
266 ir_node *new_bl, *new_ret;
268 if (get_irn_op(jmp) != op_Jmp)
271 new_bl = get_nodes_block(jmp);
273 /* create the in-array for the new Ret */
274 for (j = 0; j < n_ret_vals; ++j) {
275 ir_node *pred = get_irn_n(ret, j);
277 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
280 new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
282 if (! is_Bad(new_ret)) {
284 * The newly created node might be bad, if we
285 * create it in a block with only Bad predecessors.
286 * In that case ignore this block.
288 * We could even kill the jmp then ...
290 if (can_move_ret(new_ret)) {
291 set_irn_link(new_ret, list);
296 set_irn_link(new_ret, final);
302 /* remove the Jmp, we have placed a Return here */
303 exchange(jmp, new_r_Bad(irg));
307 * if the memory of the old Return is a PhiM, remove it
308 * from the keep-alives, or it will keep the block which
309 * will crash the dominator algorithm.
311 phiM = get_Return_mem(ret);
313 n = get_End_n_keepalives(end);
314 for (i = 0; i < n; ++i) {
315 if (get_End_keepalive(end, i) == phiM) {
316 set_End_keepalive(end, i, new_r_Bad(irg));
324 * Last step: Create a new endblock, with all nodes on the final
325 * list as predecessors.
327 in = alloca(sizeof(*in) * n_finals);
329 for (i = 0; final; ++i, final = get_irn_link(final))
332 exchange(endbl, new_r_Block(irg, n_finals, in));
334 /* the end block is not automatically skipped, so do it here */
335 set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
337 /* Invalidate analysis information:
338 * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
339 * trouts and callee-state should be still valid
341 set_irg_doms_inconsistent(irg);
342 set_irg_outs_inconsistent(irg);
343 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);