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
30 #include "iroptimize.h"
31 #include "irgraph_t.h"
37 #define set_bit(n) (returns[(n) >> 3] |= 1 << ((n) & 7))
38 #define get_bit(n) (returns[(n) >> 3] & (1 << ((n) & 7)))
41 #define IMAX(a, b) ((a) > (b) ? (a) : (b))
44 * Normalize the Returns of a graph by creating a new End block
45 * with One Return(Phi).
46 * This is the preferred input for the if-conversion.
48 * In pseudocode, it means:
63 void normalize_one_return(ir_graph *irg) {
64 ir_node *endbl = get_irg_end_block(irg);
65 int i, j, k, n, last_idx, n_rets, n_ret_vals = -1;
66 unsigned char *returns;
67 ir_node **in, **retvals, **endbl_in;
71 /* look, if we have more than one return */
72 n = get_Block_n_cfgpreds(endbl);
74 /* The end block has no predecessors, we have an endless
75 loop. In that case, no returns exists. */
79 returns = alloca((n + 7) >> 3);
80 memset(returns, 0, (n + 7) >> 3);
82 for (n_rets = i = 0; i < n; ++i) {
83 ir_node *node = get_Block_cfgpred(endbl, i);
85 if (is_Return(node)) {
91 n_ret_vals = get_irn_arity(node);
95 /* there should be at least one Return node in Firm */
99 in = alloca(sizeof(*in) * IMAX(n_rets, n_ret_vals));
100 retvals = alloca(sizeof(*retvals) * n_rets * n_ret_vals);
101 endbl_in = alloca(sizeof(*endbl_in) * n);
104 for (j = i = 0; i < n; ++i) {
105 ir_node *ret = get_Block_cfgpred(endbl, i);
108 ir_node *block = get_nodes_block(ret);
110 /* create a new Jmp for every Ret and place the in in */
111 in[j] = new_r_Jmp(irg, block);
113 /* save the return values and shuffle them */
114 for (k = 0; k < n_ret_vals; ++k)
115 retvals[j + k*n_rets] = get_irn_n(ret, k);
119 endbl_in[last_idx++] = ret;
122 /* ok, create a new block with all created in's */
123 block = new_r_Block(irg, n_rets, in);
125 /* now create the Phi nodes */
126 for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
129 /* the return values are already shuffled */
131 /* Beware: normally the Phi constructor automatically replaces a Phi(a,...a) into a
132 but NOT, if a is Unknown. Here, we known that this case can be optimize also,
134 first = retvals[j + 0];
135 for (k = 1; k < n_rets; ++k) {
136 if (retvals[j + k] != first) {
144 in[i] = new_r_Phi(irg, block, n_rets, &retvals[j], get_irn_mode(retvals[j]));
147 endbl_in[last_idx++] = new_r_Return(irg, block, in[0], n_ret_vals-1, &in[1]);
149 set_irn_in(endbl, last_idx, endbl_in);
151 /* invalidate analysis information:
152 * a new Block was added, so dominator, outs and loop are inconsistent,
153 * trouts and callee-state should be still valid
155 set_irg_doms_inconsistent(irg);
156 set_irg_outs_inconsistent(irg);
157 set_irg_extblk_inconsistent(irg);
158 set_irg_loopinfo_inconsistent(irg);
162 * Check, whether a Return can be moved on block upwards.
164 * In a block with a Return, all live nodes must be linked
165 * with the Return, otherwise they are dead (because the Return leaves
166 * the graph, so no more users of the other nodes can exists.
168 * We can move a Return, if it's predecessors are Phi nodes or
169 * comes from another block. In the later case, it is always possible
170 * to move the Return one block up, because the predecessor block must
171 * dominate the Return block (SSA) and then it dominates the predecessor
172 * block of the Return block as well.
174 * All predecessors of the Return block must be Jmp's of course, or we
175 * cannot move it up, so we add blocks if needed.
177 static int can_move_ret(ir_node *ret) {
178 ir_node *retbl = get_nodes_block(ret);
179 int i, n = get_irn_arity(ret);
181 for (i = 0; i < n; ++i) {
182 ir_node *pred = get_irn_n(ret, i);
184 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
185 /* first condition failed, found a non-Phi predecessor
186 * then is in the Return block */
191 /* check, that predecessors are Jmps */
192 n = get_Block_n_cfgpreds(retbl);
193 for (i = 0; i < n; ++i) {
194 ir_node *pred = get_Block_cfgpred(retbl, i);
196 pred = skip_Tuple(pred);
197 if (! is_Jmp(pred) && !is_Bad(pred)) {
198 /* simply place a new block here */
199 ir_graph *irg = get_irn_irg(retbl);
200 ir_node *block = new_r_Block(irg, 1, &pred);
201 ir_node *jmp = new_r_Jmp(irg, block);
202 set_Block_cfgpred(retbl, i, jmp);
206 /* if we have 0 control flow predecessors, we cannot move :-) */
211 * Normalize the Returns of a graph by moving
212 * the Returns upwards as much as possible.
213 * This might be preferred for code generation.
215 * In pseudocode, it means:
223 * is transformed into
230 void normalize_n_returns(ir_graph *irg) {
231 int i, j, n, n_rets, n_finals, n_ret_vals;
232 ir_node *list = NULL;
233 ir_node *final = NULL;
235 ir_node *endbl = get_irg_end_block(irg);
239 * First, link all returns:
240 * These must be predecessors of the endblock.
241 * Place Returns that can be moved on list, all others
244 n = get_Block_n_cfgpreds(endbl);
245 for (n_finals = n_rets = i = 0; i < n; ++i) {
246 ir_node *ret = get_Block_cfgpred(endbl, i);
248 if (is_Return(ret) && can_move_ret(ret)) {
250 * Ok, all conditions met, we can move this Return, put it
253 set_irn_link(ret, list);
257 /* Put all nodes that are not changed on the final list. */
258 set_irn_link(ret, final);
268 * Now move the Returns upwards. We move always one block up (and create n
269 * new Returns), than we check if a newly created Return can be moved even further.
270 * If yes, we simply add it to our work list, else to the final list.
272 end = get_irg_end(irg);
273 n_ret_vals = get_irn_arity(list);
274 in = alloca(sizeof(*in) * n_ret_vals);
277 ir_node *block = get_nodes_block(ret);
280 list = get_irn_link(ret);
283 n = get_Block_n_cfgpreds(block);
284 for (i = 0; i < n; ++i) {
285 ir_node *jmp = get_Block_cfgpred(block, i);
286 ir_node *new_bl, *new_ret;
292 new_bl = get_nodes_block(jmp);
294 /* create the in-array for the new Return */
295 for (j = 0; j < n_ret_vals; ++j) {
296 ir_node *pred = get_irn_n(ret, j);
298 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
301 new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
303 if (! is_Bad(new_ret)) {
305 * The newly created node might be bad, if we
306 * create it in a block with only Bad predecessors.
307 * In that case ignore this block.
309 * We could even kill the jmp then ...
311 if (can_move_ret(new_ret)) {
312 set_irn_link(new_ret, list);
316 set_irn_link(new_ret, final);
322 /* remove the Jmp, we have placed a Return here */
323 exchange(jmp, new_r_Bad(irg));
327 * if the memory of the old Return is a PhiM, remove it
328 * from the keep-alives, or it will keep the block which
329 * will crash the dominator algorithm.
331 phiM = get_Return_mem(ret);
333 n = get_End_n_keepalives(end);
334 for (i = 0; i < n; ++i) {
335 if (get_End_keepalive(end, i) == phiM) {
336 set_End_keepalive(end, i, new_r_Bad(irg));
344 * Last step: Create a new endblock, with all nodes on the final
345 * list as predecessors.
347 in = alloca(sizeof(*in) * n_finals);
349 for (i = 0; final; ++i, final = get_irn_link(final))
352 exchange(endbl, new_r_Block(irg, n_finals, in));
354 /* the end block is not automatically skipped, so do it here */
355 set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
357 /* Invalidate analysis information:
358 * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
359 * trouts and callee-state should be still valid
361 set_irg_doms_inconsistent(irg);
362 set_irg_extblk_inconsistent(irg); /* may not be needed */
363 set_irg_outs_inconsistent(irg);
364 set_irg_loopinfo_inconsistent(current_ir_graph);