4d7e7292af8efedf96c6df5b0952be1973d37b35
[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
36 #define set_bit(n)      (returns[(n) >> 3] |= 1 << ((n) & 7))
37 #define get_bit(n)      (returns[(n) >> 3] & (1 << ((n) & 7)))
38
39 #undef IMAX
40 #define IMAX(a, b)       ((a) > (b) ? (a) : (b))
41
42 /*
43  * Normalize the Returns of a graph by creating a new End block
44  * with One Return(Phi).
45  * This is the preferred input for the if-conversion.
46  *
47  * In pseudocode, it means:
48  *
49  * if (a)
50  *   return b;
51  * else
52  *   return c;
53  *
54  * is transformed into
55  *
56  * if (a)
57  *   res = b;
58  * else
59  *   res = c;
60  * return res;
61  */
62 void normalize_one_return(ir_graph *irg) {
63         ir_node *endbl = get_irg_end_block(irg);
64         int i, j, k, n, last_idx, n_rets, n_ret_vals = -1;
65         unsigned char *returns;
66         ir_node **in, **retvals, **endbl_in;
67
68         ir_node *block;
69
70         /* look, if we have more than one return */
71         n = get_Block_n_cfgpreds(endbl);
72         if (n <= 0) {
73                 /* The end block has no predecessors, we have an endless
74                    loop. In that case, no returns exists. */
75                 return;
76         }
77
78         returns = alloca((n + 7) >> 3);
79         memset(returns, 0, (n + 7) >> 3);
80
81         for (n_rets = i = 0; i < n; ++i) {
82                 ir_node *node = get_Block_cfgpred(endbl, i);
83
84                 if (is_Return(node)) {
85                         ++n_rets;
86
87                         set_bit(i);
88
89                         if (n_ret_vals < 0)
90                                 n_ret_vals = get_irn_arity(node);
91                 }
92         }
93
94         /* there should be at least one Return node in Firm */
95         if (n_rets <= 1)
96                 return;
97
98         in       = alloca(sizeof(*in)       * IMAX(n_rets, n_ret_vals));
99         retvals  = alloca(sizeof(*retvals)  * n_rets * n_ret_vals);
100         endbl_in = alloca(sizeof(*endbl_in) * n);
101
102         last_idx = 0;
103         for (j = i = 0; i < n; ++i) {
104                 ir_node *ret = get_Block_cfgpred(endbl, i);
105
106                 if (get_bit(i)) {
107                         ir_node *block = get_nodes_block(ret);
108
109                         /* create a new Jmp for every Ret and place the in in */
110                         in[j] = new_r_Jmp(irg, block);
111
112                         /* save the return values and shuffle them */
113                         for (k = 0; k < n_ret_vals; ++k)
114                                 retvals[j + k*n_rets] = get_irn_n(ret, k);
115
116                         ++j;
117                 } else
118                         endbl_in[last_idx++] = ret;
119         }
120
121         /* ok, create a new block with all created in's */
122         block = new_r_Block(irg, n_rets, in);
123
124         /* now create the Phi nodes */
125         for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
126                 int k;
127                 ir_node *first;
128                 /* the return values are already shuffled */
129
130                 /* Beware: normally the Phi constructor automatically replaces a Phi(a,...a) into a
131                    but NOT, if a is Unknown. Here, we known that this case can be optimize also,
132                    so do it here */
133                 first = retvals[j + 0];
134                 for (k = 1; k < n_rets; ++k) {
135                         if (retvals[j + k] != first) {
136                                 first = NULL;
137                                 break;
138                         }
139                 }
140                 if (first)
141                         in[i] = first;
142                 else
143                         in[i] = new_r_Phi(irg, block, n_rets, &retvals[j], get_irn_mode(retvals[j]));
144         }
145
146         endbl_in[last_idx++] = new_r_Return(irg, block, in[0], n_ret_vals-1, &in[1]);
147
148         set_irn_in(endbl, last_idx, endbl_in);
149
150         /* invalidate analysis information:
151          * a new Block was added, so dominator, outs and loop are inconsistent,
152          * trouts and callee-state should be still valid
153          */
154         set_irg_doms_inconsistent(irg);
155         set_irg_outs_inconsistent(irg);
156         set_irg_extblk_inconsistent(irg);
157         set_irg_loopinfo_inconsistent(irg);
158 }
159
160 /**
161  * Check, whether a Return can be moved on block upwards.
162  *
163  * In a block with a Return, all live nodes must be linked
164  * with the Return, otherwise they are dead (because the Return leaves
165  * the graph, so no more users of the other nodes can exists.
166  *
167  * We can move a Return, if it's predecessors are Phi nodes or
168  * comes from another block. In the later case, it is always possible
169  * to move the Return one block up, because the predecessor block must
170  * dominate the Return block (SSA) and then it dominates the predecessor
171  * block of the Return block as well.
172  *
173  * All predecessors of the Return block must be Jmp's of course, or we
174  * cannot move it up, so we add blocks if needed.
175  */
176 static int can_move_ret(ir_node *ret) {
177         ir_node *retbl = get_nodes_block(ret);
178         int i, n = get_irn_arity(ret);
179
180         for (i = 0; i < n; ++i) {
181                 ir_node *pred = get_irn_n(ret, i);
182
183                 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
184                         /* first condition failed, found a non-Phi predecessor
185                          * then is in the Return block */
186                         return 0;
187                 }
188         }
189
190         /* check, that predecessors are Jmps */
191         n = get_Block_n_cfgpreds(retbl);
192         if (n <= 1)
193                 return 0;
194         for (i = 0; i < n; ++i) {
195                 ir_node *pred = get_Block_cfgpred(retbl, i);
196
197                 pred = skip_Tuple(pred);
198                 if (! is_Jmp(pred) && !is_Bad(pred)) {
199                         /* simply place a new block here */
200                         ir_graph *irg  = get_irn_irg(retbl);
201                         ir_node *block = new_r_Block(irg, 1, &pred);
202                         ir_node *jmp   = new_r_Jmp(irg, block);
203                         set_Block_cfgpred(retbl, i, jmp);
204                 }
205         }
206         return 1;
207 }
208
209 /*
210  * Normalize the Returns of a graph by moving
211  * the Returns upwards as much as possible.
212  * This might be preferred for code generation.
213  *
214  * In pseudocode, it means:
215  *
216  * if (a)
217  *   res = b;
218  * else
219  *   res = c;
220  * return res;
221  *
222  * is transformed into
223  *
224  * if (a)
225  *   return b;
226  * else
227  *   return c;
228  */
229 void normalize_n_returns(ir_graph *irg) {
230         int i, j, n, n_rets, n_finals, n_ret_vals;
231         ir_node *list  = NULL;
232         ir_node *final = NULL;
233         ir_node **in;
234         ir_node *endbl = get_irg_end_block(irg);
235         ir_node *end;
236
237         /*
238          * First, link all returns:
239          * These must be predecessors of the endblock.
240          * Place Returns that can be moved on list, all others
241          * on final.
242          */
243         n = get_Block_n_cfgpreds(endbl);
244         for (n_finals = n_rets = i = 0; i < n; ++i) {
245                 ir_node *ret = get_Block_cfgpred(endbl, i);
246
247                 if (is_Return(ret) && can_move_ret(ret)) {
248                         /*
249                          * Ok, all conditions met, we can move this Return, put it
250                          * on our work list.
251                          */
252                         set_irn_link(ret, list);
253                         list = ret;
254                         ++n_rets;
255                 } else {
256                         /* Put all nodes that are not changed on the final list. */
257                         set_irn_link(ret, final);
258                         final = ret;
259                         ++n_finals;
260                 }
261         }
262
263         if (n_rets <= 0)
264                 return;
265
266         /*
267          * Now move the Returns upwards. We move always one block up (and create n
268          * new Returns), than we check if a newly created Return can be moved even further.
269          * If yes, we simply add it to our work list, else to the final list.
270          */
271         end        = get_irg_end(irg);
272         n_ret_vals = get_irn_arity(list);
273         in         = alloca(sizeof(*in) * n_ret_vals);
274         while (list) {
275                 ir_node *ret   = list;
276                 ir_node *block = get_nodes_block(ret);
277                 ir_node *phiM;
278
279                 list = get_irn_link(ret);
280                 --n_rets;
281
282                 n = get_Block_n_cfgpreds(block);
283                 for (i = 0; i < n; ++i) {
284                         ir_node *jmp = get_Block_cfgpred(block, i);
285                         ir_node *new_bl, *new_ret;
286
287                         if (is_Bad(jmp))
288                                 continue;
289                         assert(is_Jmp(jmp));
290
291                         new_bl = get_nodes_block(jmp);
292
293                         /* create the in-array for the new Return */
294                         for (j = 0; j < n_ret_vals; ++j) {
295                                 ir_node *pred = get_irn_n(ret, j);
296
297                                 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
298                         }
299
300                         new_ret = new_r_Return(irg, new_bl, in[0], n_ret_vals - 1, &in[1]);
301
302                         if (! is_Bad(new_ret)) {
303                                 /*
304                                  * The newly created node might be bad, if we
305                                  * create it in a block with only Bad predecessors.
306                                  * In that case ignore this block.
307                                  *
308                                  * We could even kill the jmp then ...
309                                  */
310                                 if (can_move_ret(new_ret)) {
311                                         set_irn_link(new_ret, list);
312                                         list = new_ret;
313                                         ++n_rets;
314                                 } else {
315                                         set_irn_link(new_ret, final);
316                                         final = new_ret;
317                                         ++n_finals;
318                                 }
319                         }
320
321                         /* remove the Jmp, we have placed a Return here */
322                         exchange(jmp, new_r_Bad(irg));
323                 }
324
325                 /*
326                  * if the memory of the old Return is a PhiM, remove it
327                  * from the keep-alives, or it will keep the block which
328                  * will crash the dominator algorithm.
329                  */
330                 phiM = get_Return_mem(ret);
331                 if (is_Phi(phiM)) {
332                         n = get_End_n_keepalives(end);
333                         for (i = 0; i < n; ++i) {
334                                 if (get_End_keepalive(end, i) == phiM) {
335                                         set_End_keepalive(end, i, new_r_Bad(irg));
336                                         break;
337                                 }
338                         }
339                 }
340         }
341
342         /*
343          * Last step: Create a new endblock, with all nodes on the final
344          * list as predecessors.
345          */
346         in = alloca(sizeof(*in) * n_finals);
347
348         for (i = 0; final; ++i, final = get_irn_link(final))
349                 in[i] = final;
350
351         exchange(endbl, new_r_Block(irg, n_finals, in));
352
353         /* the end block is not automatically skipped, so do it here */
354         set_irg_end_block(irg, skip_Id(get_irg_end_block(irg)));
355
356         /* Invalidate analysis information:
357          * Blocks become dead and new Returns were deleted, so dominator, outs and loop are inconsistent,
358          * trouts and callee-state should be still valid
359          */
360         set_irg_doms_inconsistent(irg);
361         set_irg_extblk_inconsistent(irg);  /* may not be needed */
362         set_irg_outs_inconsistent(irg);
363         set_irg_loopinfo_inconsistent(current_ir_graph);
364 }