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