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