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.
22 #include "irgraph_t.h"
27 #define set_bit(n) (returns[(n) >> 3] |= 1 << ((n) & 7))
28 #define get_bit(n) (returns[(n) >> 3] & (1 << ((n) & 7)))
31 #define IMAX(a, b) ((a) > (b) ? (a) : (b))
34 * Normalize the Returns of a graph by creating a new End block
35 * with One Return(Phi).
36 * This is the preferred input for the if-conversion.
38 * In pseudocode, it means:
53 void normalize_one_return(ir_graph *irg)
55 ir_node *endbl = get_irg_end_block(irg);
56 int i, j, k, n, last_idx, n_rets, n_ret_vals = -1;
57 unsigned char *returns;
58 ir_node **in, **retvals;
60 ir_node *block, *new_ret;
62 /* look, if we have more than one return */
63 n = get_Block_n_cfgpreds(endbl);
65 returns = alloca((n + 7) >> 3);
66 memset(returns, 0, (n + 7) >> 3);
68 for (n_rets = i = 0; i < n; ++i) {
69 ir_node *node = get_Block_cfgpred(endbl, i);
71 if (get_irn_op(node) == op_Return) {
77 n_ret_vals = get_irn_arity(node);
81 /* there should be at least one Return node in Firm */
85 in = alloca(sizeof(*in) * IMAX(n_rets, n_ret_vals));
86 retvals = alloca(sizeof(*in) * n_rets * n_ret_vals);
88 for (j = i = 0; i < n; ++i) {
90 ir_node *ret = get_Block_cfgpred(endbl, i);
91 ir_node *block = get_nodes_block(ret);
93 /* create a new Jmp for every Ret and place the in in */
94 in[j] = new_r_Jmp(irg, block);
96 /* save the return values and shuffle them */
97 for (k = 0; k < n_ret_vals; ++k)
98 retvals[j + k*n_rets] = get_irn_n(ret, k);
100 set_Block_cfgpred(endbl, i, new_r_Bad(irg));
107 /* ok, create a new block with all created in's */
108 block = new_r_Block(irg, n_rets, in);
110 /* now create the Phi nodes */
111 for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
112 /* the return values are already shuffled */
113 in[i] = new_r_Phi(irg, block, n_rets, &retvals[j], get_irn_mode(retvals[j]));
116 new_ret = new_r_Return(irg, block, in[0], n_ret_vals-1, &in[1]);
118 set_Block_cfgpred(endbl, last_idx, new_ret);
120 /* invalidate analysis information:
121 * a new Block was added, so dominator, outs and loop are inconsistent,
122 * trouts and callee-state should be still valid
124 set_irg_doms_inconsistent(irg);
125 set_irg_outs_inconsistent(irg);
126 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
130 * check, whether a Ret can be moved on block upwards.
132 * In a block with a Return, all live nodes must be linked
133 * with the Return, otherwise they are dead (because the Return leaves
134 * the graph, so no more users of the other nodes can exists.
136 * We can move a Return, if it's predecessors are Phi nodes or
137 * comes from another block. In the later case, it is always possible
138 * to move the Return one block up, because the predecessor block must
139 * dominate the Return block (SSA) and then it dominates the predecessor
140 * block of the Return block as well.
142 * All predecessors of the Return block must be Jmp's of course, or we
143 * cannot move it up, so we check this either.
145 static int can_move_ret(ir_node *ret)
147 ir_node *retbl = get_nodes_block(ret);
148 int i, n = get_irn_arity(ret);
150 for (i = 0; i < n; ++i) {
151 ir_node *pred = get_irn_n(ret, i);
153 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
154 /* first condition failed, found a non-Phi predecessor
155 * then is in the Return block */
160 /* check, that predecessors are Jmps */
161 n = get_Block_n_cfgpreds(retbl);
162 for (i = 0; i < n; ++i)
163 if (get_irn_op(get_Block_cfgpred(retbl, i)) != op_Jmp)
166 /* if we have 0 control flow predecessors, we cannot move :-) */
171 * Normalize the Returns of a graph by moving
172 * the Returns upwards as much as possible.
173 * This might be preferred for code generation.
175 * In pseudocode, it means:
183 * is transformed into
190 void normalize_n_returns(ir_graph *irg)
192 int i, j, n, n_rets, n_finals, n_ret_vals;
193 ir_node *list = NULL;
194 ir_node *final = NULL;
196 ir_node *endbl = get_irg_end_block(irg);
200 * First, link all returns:
201 * These must be predecessors of the endblock.
202 * Place Returns that can be moved on list, all others
205 n = get_Block_n_cfgpreds(endbl);
206 for (n_finals = n_rets = i = 0; i < n; ++i) {
207 ir_node *ret = get_Block_cfgpred(endbl, i);
209 if (get_irn_op(ret) == op_Return && can_move_ret(ret)) {
211 * Ok, all conditions met, we can move this Return, put it
214 set_irn_link(ret, list);
219 /* Put all nodes that are not changed on the final list. */
220 set_irn_link(ret, final);
230 * Now move the Returns upwards. We move always one block up (and create n
231 * new Returns), than we check if a newly created Return can be moved even further.
232 * If yes, we simply add it to our work list, else to the final list.
234 end = get_irg_end(irg);
235 n_ret_vals = get_irn_arity(list);
236 in = alloca(sizeof(*in) * n_ret_vals);
239 ir_node *block = get_nodes_block(ret);
242 list = get_irn_link(ret);
245 n = get_Block_n_cfgpreds(block);
246 for (i = 0; i < n; ++i) {
247 ir_node *jmp = get_Block_cfgpred(block, i);
248 ir_node *new_bl, *new_ret;
250 if (get_irn_op(jmp) != op_Jmp)
253 new_bl = get_nodes_block(jmp);
255 /* create the in-array for the new Ret */
256 for (j = 0; j < n_ret_vals; ++j) {
257 ir_node *pred = get_irn_n(ret, j);
259 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
262 new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
264 if (! is_Bad(new_ret)) {
266 * The newly created node might be bad, if we
267 * create it in a block with only Bad predecessors.
268 * In that case ignore this block.
270 * We could even kill the jmp then ...
272 if (can_move_ret(new_ret)) {
273 set_irn_link(new_ret, list);
278 set_irn_link(new_ret, final);
284 /* remove the Jmp, we have placed a Return here */
285 exchange(jmp, new_r_Bad(irg));
289 * if the memory of the old Return is a PhiM, remove it
290 * from the keep-alives, or it will keep the block which
291 * will crash the dominator algorithm.
293 phiM = get_Return_mem(ret);
295 n = get_End_n_keepalives(end);
296 for (i = 0; i < n; ++i) {
297 if (get_End_keepalive(end, i) == phiM) {
298 set_End_keepalive(end, i, new_r_Bad(irg));
306 * Last step: Create a new endblock, with all nodes on the final
307 * list as predecessors.
309 in = alloca(sizeof(*in) * n_finals);
311 for (i = 0; final; ++i, final = get_irn_link(final))
314 exchange(endbl, new_r_Block(irg, n_finals, in));
316 /* the end block is not automatically skipped, so do it here */
317 set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
319 /* Invalidate analysis information:
320 * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
321 * trouts and callee-state should be still valid
323 set_irg_doms_inconsistent(irg);
324 set_irg_outs_inconsistent(irg);
325 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);