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