Fixed warning: Made global variable static.
[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  */
25 #include "config.h"
26
27 #include <stdbool.h>
28
29 #include "iroptimize.h"
30 #include "irgraph_t.h"
31 #include "ircons_t.h"
32 #include "irnode_t.h"
33 #include "irgmod.h"
34 #include "irpass.h"
35 #include "util.h"
36 #include "raw_bitset.h"
37
38 /*
39  * Normalize the Returns of a graph by creating a new End block
40  * with One Return(Phi).
41  * This is the preferred input for the if-conversion.
42  *
43  * In pseudocode, it means:
44  *
45  * if (a)
46  *   return b;
47  * else
48  *   return c;
49  *
50  * is transformed into
51  *
52  * if (a)
53  *   res = b;
54  * else
55  *   res = c;
56  * return res;
57  */
58 void normalize_one_return(ir_graph *irg)
59 {
60         ir_node   *endbl         = get_irg_end_block(irg);
61         ir_entity *entity        = get_irg_entity(irg);
62         ir_type   *type          = get_entity_type(entity);
63         int        n_ret_vals    = get_method_n_ress(type) + 1;
64         int        n_rets        = 0;
65         bool       filter_dbgi   = false;
66         dbg_info  *combined_dbgi = NULL;
67         unsigned  *returns;
68         int i, j, k, n, last_idx;
69         ir_node **in, **retvals, **endbl_in;
70         ir_node *block;
71
72         /* look, if we have more than one return */
73         n = get_Block_n_cfgpreds(endbl);
74         if (n <= 0) {
75                 /* The end block has no predecessors, we have an endless
76                    loop. In that case, no returns exists. */
77                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
78                 add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
79                 return;
80         }
81
82         rbitset_alloca(returns, n);
83
84         for (i = 0; i < n; ++i) {
85                 ir_node *node = get_Block_cfgpred(endbl, i);
86
87                 if (is_Return(node)) {
88                         dbg_info *dbgi = get_irn_dbg_info(node);
89
90                         if (dbgi != NULL && dbgi != combined_dbgi) {
91                                 if (filter_dbgi) {
92                                         combined_dbgi = NULL;
93                                 } else {
94                                         combined_dbgi = dbgi;
95                                         filter_dbgi   = true;
96                                 }
97                         }
98
99                         ++n_rets;
100                         rbitset_set(returns, i);
101                 }
102         }
103
104         if (n_rets <= 1) {
105                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
106                 add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
107                 return;
108         }
109
110         in       = ALLOCAN(ir_node*, MAX(n_rets, n_ret_vals));
111         retvals  = ALLOCAN(ir_node*, n_rets * n_ret_vals);
112         endbl_in = ALLOCAN(ir_node*, n);
113
114         last_idx = 0;
115         for (j = i = 0; i < n; ++i) {
116                 ir_node *ret = get_Block_cfgpred(endbl, i);
117
118                 if (rbitset_is_set(returns, i)) {
119                         ir_node *block = get_nodes_block(ret);
120
121                         /* create a new Jmp for every Ret and place the in in */
122                         in[j] = new_r_Jmp(block);
123
124                         /* save the return values and shuffle them */
125                         for (k = 0; k < n_ret_vals; ++k)
126                                 retvals[j + k*n_rets] = get_irn_n(ret, k);
127
128                         ++j;
129                 } else {
130                         endbl_in[last_idx++] = ret;
131                 }
132         }
133
134         /* ok, create a new block with all created in's */
135         block = new_r_Block(irg, n_rets, in);
136
137         /* now create the Phi nodes */
138         for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
139                 ir_mode *mode = get_irn_mode(retvals[j]);
140                 in[i] = new_r_Phi(block, n_rets, &retvals[j], mode);
141         }
142
143         endbl_in[last_idx++] = new_rd_Return(combined_dbgi, block, in[0], n_ret_vals-1, &in[1]);
144
145         set_irn_in(endbl, last_idx, endbl_in);
146
147         /* invalidate analysis information:
148          * a new Block was added, so dominator, outs and loop are inconsistent,
149          * trouts and callee-state should be still valid */
150         confirm_irg_properties(irg,
151                 IR_GRAPH_PROPERTY_NO_BADS
152                 | IR_GRAPH_PROPERTY_NO_TUPLES
153                 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
154                 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
155                 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
156         add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
157 }
158
159 /* Create a graph pass. */
160 ir_graph_pass_t *normalize_one_return_pass(const char *name)
161 {
162         return def_graph_pass(name ? name : "one_ret", normalize_one_return);
163 }
164
165 /**
166  * Check, whether a Return can be moved on block upwards.
167  *
168  * In a block with a Return, all live nodes must be linked
169  * with the Return, otherwise they are dead (because the Return leaves
170  * the graph, so no more users of the other nodes can exists.
171  *
172  * We can move a Return, if its predecessors are Phi nodes or
173  * comes from another block. In the later case, it is always possible
174  * to move the Return one block up, because the predecessor block must
175  * dominate the Return block (SSA) and then it dominates the predecessor
176  * block of the Return block as well.
177  *
178  * All predecessors of the Return block must be Jmp's of course, or we
179  * cannot move it up, so we add blocks if needed.
180  */
181 static bool can_move_ret(ir_node *ret)
182 {
183         ir_node *retbl = get_nodes_block(ret);
184         int i, n = get_irn_arity(ret);
185
186         for (i = 0; i < n; ++i) {
187                 ir_node *pred = get_irn_n(ret, i);
188
189                 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
190                         /* first condition failed, found a non-Phi predecessor
191                          * then is in the Return block */
192                         return false;
193                 }
194         }
195
196         /* check, that predecessors are Jmps */
197         n = get_Block_n_cfgpreds(retbl);
198         /* we cannot move above a labeled block, as this might kill the block */
199         if (n <= 1 || get_Block_entity(retbl) != NULL)
200                 return false;
201         for (i = 0; i < n; ++i) {
202                 ir_node *pred = get_Block_cfgpred(retbl, i);
203
204                 pred = skip_Tuple(pred);
205                 if (! is_Jmp(pred) && !is_Bad(pred)) {
206                         /* simply place a new block here */
207                         ir_graph *irg  = get_irn_irg(retbl);
208                         ir_node *block = new_r_Block(irg, 1, &pred);
209                         ir_node *jmp   = new_r_Jmp(block);
210                         set_Block_cfgpred(retbl, i, jmp);
211                 }
212         }
213         return true;
214 }
215
216 /*
217  * Normalize the Returns of a graph by moving
218  * the Returns upwards as much as possible.
219  * This might be preferred for code generation.
220  *
221  * In pseudocode, it means:
222  *
223  * if (a)
224  *   res = b;
225  * else
226  *   res = c;
227  * return res;
228  *
229  * is transformed into
230  *
231  * if (a)
232  *   return b;
233  * else
234  *   return c;
235  */
236 void normalize_n_returns(ir_graph *irg)
237 {
238         int i, j, n;
239         ir_node  *list     = NULL;
240         ir_node  *final    = NULL;
241         unsigned  n_rets   = 0;
242         unsigned  n_finals = 0;
243         ir_node  *endbl    = get_irg_end_block(irg);
244         int       n_ret_vals;
245         ir_node **in;
246         ir_node  *end;
247
248         /*
249          * First, link all returns:
250          * These must be predecessors of the endblock.
251          * Place Returns that can be moved on list, all others
252          * on final.
253          */
254         n = get_Block_n_cfgpreds(endbl);
255         for (i = 0; i < n; ++i) {
256                 ir_node *ret = get_Block_cfgpred(endbl, i);
257
258                 if (is_Bad(ret)) {
259                         continue;
260                 } else if (is_Return(ret) && can_move_ret(ret)) {
261                         /*
262                          * Ok, all conditions met, we can move this Return, put it
263                          * on our work list.
264                          */
265                         set_irn_link(ret, list);
266                         list = ret;
267                         ++n_rets;
268                 } else {
269                         /* Put all nodes that are not changed on the final list. */
270                         set_irn_link(ret, final);
271                         final = ret;
272                         ++n_finals;
273                 }
274         }
275
276         if (n_rets == 0) {
277                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
278                 add_irg_properties(irg, IR_GRAPH_PROPERTY_MANY_RETURNS);
279                 return;
280         }
281
282         /*
283          * Now move the Returns upwards. We move always one block up (and create n
284          * new Returns), than we check if a newly created Return can be moved even
285          * further. If yes, we simply add it to our work list, else to the final
286          * 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 != NULL) {
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 = (ir_node*)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, mode_X));
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, mode_M));
354                                         break;
355                                 }
356                         }
357                 }
358         }
359
360         /*
361          * Last step: Create a new endblock, with all nodes on the final list as
362          * predecessors.
363          */
364         in = ALLOCAN(ir_node*, n_finals);
365
366         for (i = 0; final != NULL; ++i, final = (ir_node*)get_irn_link(final)) {
367                 in[i] = final;
368         }
369
370         exchange(endbl, new_r_Block(irg, n_finals, in));
371
372         /* Invalidate analysis information:
373          * Blocks become dead and new Returns were deleted, so dominator, outs and
374          * loop are inconsistent, trouts and callee-state should be still valid */
375         confirm_irg_properties(irg,
376                 IR_GRAPH_PROPERTY_NO_TUPLES
377                 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
378                 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
379         add_irg_properties(irg, IR_GRAPH_PROPERTY_MANY_RETURNS);
380 }
381
382 /* Create a graph pass. */
383 ir_graph_pass_t *normalize_n_returns_pass(const char *name)
384 {
385         return def_graph_pass(name ? name : "n_rets", normalize_n_returns);
386 }