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