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