update copyright message
[libfirm] / ir / opt / return.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Normalize returns.
23  * @author  Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "iroptimize.h"
31 #include "irgraph_t.h"
32 #include "ircons_t.h"
33 #include "irnode_t.h"
34 #include "irgmod.h"
35 #include "xmalloc.h"
36
37 #define set_bit(n)      (returns[(n) >> 3] |= 1 << ((n) & 7))
38 #define get_bit(n)      (returns[(n) >> 3] & (1 << ((n) & 7)))
39
40 #undef IMAX
41 #define IMAX(a, b)       ((a) > (b) ? (a) : (b))
42
43 /*
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.
47  *
48  * In pseudocode, it means:
49  *
50  * if (a)
51  *   return b;
52  * else
53  *   return c;
54  *
55  * is transformed into
56  *
57  * if (a)
58  *   res = b;
59  * else
60  *   res = c;
61  * return res;
62  */
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;
68
69         ir_node *block;
70
71         /* look, if we have more than one return */
72         n = get_Block_n_cfgpreds(endbl);
73         if (n <= 0) {
74                 /* The end block has no predecessors, we have an endless
75                    loop. In that case, no returns exists. */
76                 return;
77         }
78
79         returns = alloca((n + 7) >> 3);
80         memset(returns, 0, (n + 7) >> 3);
81
82         for (n_rets = i = 0; i < n; ++i) {
83                 ir_node *node = get_Block_cfgpred(endbl, i);
84
85                 if (is_Return(node)) {
86                         ++n_rets;
87
88                         set_bit(i);
89
90                         if (n_ret_vals < 0)
91                                 n_ret_vals = get_irn_arity(node);
92                 }
93         }
94
95         /* there should be at least one Return node in Firm */
96         if (n_rets <= 1)
97                 return;
98
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);
102
103         last_idx = 0;
104         for (j = i = 0; i < n; ++i) {
105                 ir_node *ret = get_Block_cfgpred(endbl, i);
106
107                 if (get_bit(i)) {
108                         ir_node *block = get_nodes_block(ret);
109
110                         /* create a new Jmp for every Ret and place the in in */
111                         in[j] = new_r_Jmp(irg, block);
112
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);
116
117                         ++j;
118                 } else
119                         endbl_in[last_idx++] = ret;
120         }
121
122         /* ok, create a new block with all created in's */
123         block = new_r_Block(irg, n_rets, in);
124
125         /* now create the Phi nodes */
126         for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
127                 int k;
128                 ir_node *first;
129                 /* the return values are already shuffled */
130
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,
133                    so do it here */
134                 first = retvals[j + 0];
135                 for (k = 1; k < n_rets; ++k) {
136                         if (retvals[j + k] != first) {
137                                 first = NULL;
138                                 break;
139                         }
140                 }
141                 if (first)
142                         in[i] = first;
143                 else
144                         in[i] = new_r_Phi(irg, block, n_rets, &retvals[j], get_irn_mode(retvals[j]));
145         }
146
147         endbl_in[last_idx++] = new_r_Return(irg, block, in[0], n_ret_vals-1, &in[1]);
148
149         set_irn_in(endbl, last_idx, endbl_in);
150
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
154          */
155         set_irg_doms_inconsistent(irg);
156         set_irg_outs_inconsistent(irg);
157         set_irg_extblk_inconsistent(irg);
158         set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
159 }
160
161 /**
162  * check, whether a Ret can be moved on block upwards.
163  *
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.
167  *
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.
173  *
174  * All predecessors of the Return block must be Jmp's of course, or we
175  * cannot move it up, so we check this either.
176  */
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);
180
181         for (i = 0; i < n; ++i) {
182                 ir_node *pred = get_irn_n(ret, i);
183
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 */
187                         return 0;
188                 }
189         }
190
191         /* check, that predecessors are Jmps */
192         n = get_Block_n_cfgpreds(retbl);
193         for (i = 0; i < n; ++i)
194                 if (get_irn_op(get_Block_cfgpred(retbl, i)) != op_Jmp)
195                         return 0;
196
197         /* if we have 0 control flow predecessors, we cannot move :-) */
198         return n > 0;
199 }
200
201 /*
202  * Normalize the Returns of a graph by moving
203  * the Returns upwards as much as possible.
204  * This might be preferred for code generation.
205  *
206  * In pseudocode, it means:
207  *
208  * if (a)
209  *   res = b;
210  * else
211  *   res = c;
212  * return res;
213  *
214  * is transformed into
215  *
216  * if (a)
217  *   return b;
218  * else
219  *   return c;
220  */
221 void normalize_n_returns(ir_graph *irg) {
222         int i, j, n, n_rets, n_finals, n_ret_vals;
223         ir_node *list  = NULL;
224         ir_node *final = NULL;
225         ir_node **in;
226         ir_node *endbl = get_irg_end_block(irg);
227         ir_node *end;
228
229         /*
230          * First, link all returns:
231          * These must be predecessors of the endblock.
232          * Place Returns that can be moved on list, all others
233          * on final.
234          */
235         n = get_Block_n_cfgpreds(endbl);
236         for (n_finals = n_rets = i = 0; i < n; ++i) {
237                 ir_node *ret = get_Block_cfgpred(endbl, i);
238
239                 if (is_Return(ret) && can_move_ret(ret)) {
240                         /*
241                         * Ok, all conditions met, we can move this Return, put it
242                         * on our work list.
243                         */
244                         set_irn_link(ret, list);
245                         list = ret;
246                         ++n_rets;
247                 } else {
248                         /* Put all nodes that are not changed on the final list. */
249                         set_irn_link(ret, final);
250                         final = ret;
251                         ++n_finals;
252                 }
253         }
254
255         if (n_rets <= 0)
256                 return;
257
258         /*
259          * Now move the Returns upwards. We move always one block up (and create n
260          * new Returns), than we check if a newly created Return can be moved even further.
261          * If yes, we simply add it to our work list, else to the final list.
262          */
263         end        = get_irg_end(irg);
264         n_ret_vals = get_irn_arity(list);
265         in         = alloca(sizeof(*in) * n_ret_vals);
266         while (list) {
267                 ir_node *ret   = list;
268                 ir_node *block = get_nodes_block(ret);
269                 ir_node *phiM;
270
271                 list = get_irn_link(ret);
272                 --n_rets;
273
274                 n = get_Block_n_cfgpreds(block);
275                 for (i = 0; i < n; ++i) {
276                         ir_node *jmp = get_Block_cfgpred(block, i);
277                         ir_node *new_bl, *new_ret;
278
279                         if (get_irn_op(jmp) != op_Jmp)
280                                 continue;
281
282                         new_bl = get_nodes_block(jmp);
283
284                         /* create the in-array for the new Ret */
285                         for (j = 0; j < n_ret_vals; ++j) {
286                                 ir_node *pred = get_irn_n(ret, j);
287
288                                 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
289                         }
290
291                         new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
292
293                         if (! is_Bad(new_ret)) {
294                                 /*
295                                  * The newly created node might be bad, if we
296                                  * create it in a block with only Bad predecessors.
297                                  * In that case ignore this block.
298                                  *
299                                  * We could even kill the jmp then ...
300                                  */
301                                 if (can_move_ret(new_ret)) {
302                                         set_irn_link(new_ret, list);
303                                         list = new_ret;
304                                         ++n_rets;
305                                 }
306                                 else {
307                                         set_irn_link(new_ret, final);
308                                         final = new_ret;
309                                         ++n_finals;
310                                 }
311                         }
312
313                         /* remove the Jmp, we have placed a Return here */
314                         exchange(jmp, new_r_Bad(irg));
315                 }
316
317                 /*
318                  * if the memory of the old Return is a PhiM, remove it
319                  * from the keep-alives, or it will keep the block which
320                  * will crash the dominator algorithm.
321                  */
322                 phiM = get_Return_mem(ret);
323                 if (is_Phi(phiM)) {
324                         n = get_End_n_keepalives(end);
325                         for (i = 0; i < n; ++i) {
326                                 if (get_End_keepalive(end, i) == phiM) {
327                                         set_End_keepalive(end, i, new_r_Bad(irg));
328                                         break;
329                                 }
330                         }
331                 }
332         }
333
334         /*
335          * Last step: Create a new endblock, with all nodes on the final
336          * list as predecessors.
337          */
338         in = alloca(sizeof(*in) * n_finals);
339
340         for (i = 0; final; ++i, final = get_irn_link(final))
341                 in[i] = final;
342
343         exchange(endbl, new_r_Block(irg, n_finals, in));
344
345         /* the end block is not automatically skipped, so do it here */
346         set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
347
348         /* Invalidate analysis information:
349          * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
350          * trouts and callee-state should be still valid
351          */
352         set_irg_doms_inconsistent(irg);
353         set_irg_extblk_inconsistent(irg);  /* may not be needed */
354         set_irg_outs_inconsistent(irg);
355         set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
356 }