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