verify: Clarify assertion message.
[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         int i, j, k, n, last_idx;
68         ir_node **in, **retvals, **endbl_in;
69         ir_node *block;
70
71         /* look, if we have more than one return */
72         n = get_Block_n_cfgpreds(endbl);
73         if (n <= 0) {
74                 /* The end block has no predecessors, we have an endless
75                    loop. In that case, no returns exists. */
76                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
77                 add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
78                 return;
79         }
80
81         unsigned *const returns = rbitset_alloca(n);
82         for (i = 0; i < n; ++i) {
83                 ir_node *node = get_Block_cfgpred(endbl, i);
84
85                 if (is_Return(node)) {
86                         dbg_info *dbgi = get_irn_dbg_info(node);
87
88                         if (dbgi != NULL && dbgi != combined_dbgi) {
89                                 if (filter_dbgi) {
90                                         combined_dbgi = NULL;
91                                 } else {
92                                         combined_dbgi = dbgi;
93                                         filter_dbgi   = true;
94                                 }
95                         }
96
97                         ++n_rets;
98                         rbitset_set(returns, i);
99                 }
100         }
101
102         if (n_rets <= 1) {
103                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
104                 add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
105                 return;
106         }
107
108         in       = ALLOCAN(ir_node*, MAX(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 (rbitset_is_set(returns, 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
132         /* ok, create a new block with all created in's */
133         block = new_r_Block(irg, n_rets, in);
134
135         /* now create the Phi nodes */
136         for (j = i = 0; i < n_ret_vals; ++i, j += n_rets) {
137                 ir_mode *mode = get_irn_mode(retvals[j]);
138                 in[i] = new_r_Phi(block, n_rets, &retvals[j], mode);
139         }
140
141         endbl_in[last_idx++] = new_rd_Return(combined_dbgi, block, in[0], n_ret_vals-1, &in[1]);
142
143         set_irn_in(endbl, last_idx, endbl_in);
144
145         /* invalidate analysis information:
146          * a new Block was added, so dominator, outs and loop are inconsistent,
147          * trouts and callee-state should be still valid */
148         confirm_irg_properties(irg,
149                 IR_GRAPH_PROPERTY_NO_BADS
150                 | IR_GRAPH_PROPERTY_NO_TUPLES
151                 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
152                 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
153                 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
154         add_irg_properties(irg, IR_GRAPH_PROPERTY_ONE_RETURN);
155 }
156
157 /* Create a graph pass. */
158 ir_graph_pass_t *normalize_one_return_pass(const char *name)
159 {
160         return def_graph_pass(name ? name : "one_ret", normalize_one_return);
161 }
162
163 /**
164  * Check, whether a Return can be moved on block upwards.
165  *
166  * In a block with a Return, all live nodes must be linked
167  * with the Return, otherwise they are dead (because the Return leaves
168  * the graph, so no more users of the other nodes can exists.
169  *
170  * We can move a Return, if its predecessors are Phi nodes or
171  * comes from another block. In the later case, it is always possible
172  * to move the Return one block up, because the predecessor block must
173  * dominate the Return block (SSA) and then it dominates the predecessor
174  * block of the Return block as well.
175  *
176  * All predecessors of the Return block must be Jmp's of course, or we
177  * cannot move it up, so we add blocks if needed.
178  */
179 static bool can_move_ret(ir_node *ret)
180 {
181         ir_node *retbl = get_nodes_block(ret);
182         int i, n = get_irn_arity(ret);
183
184         for (i = 0; i < n; ++i) {
185                 ir_node *pred = get_irn_n(ret, i);
186
187                 if (! is_Phi(pred) && retbl == get_nodes_block(pred)) {
188                         /* first condition failed, found a non-Phi predecessor
189                          * then is in the Return block */
190                         return false;
191                 }
192         }
193
194         /* check, that predecessors are Jmps */
195         n = get_Block_n_cfgpreds(retbl);
196         /* we cannot move above a labeled block, as this might kill the block */
197         if (n <= 1 || get_Block_entity(retbl) != NULL)
198                 return false;
199         for (i = 0; i < n; ++i) {
200                 ir_node *pred = get_Block_cfgpred(retbl, i);
201
202                 pred = skip_Tuple(pred);
203                 if (! is_Jmp(pred) && !is_Bad(pred)) {
204                         /* simply place a new block here */
205                         ir_graph *irg  = get_irn_irg(retbl);
206                         ir_node *block = new_r_Block(irg, 1, &pred);
207                         ir_node *jmp   = new_r_Jmp(block);
208                         set_Block_cfgpred(retbl, i, jmp);
209                 }
210         }
211         return true;
212 }
213
214 /*
215  * Normalize the Returns of a graph by moving
216  * the Returns upwards as much as possible.
217  * This might be preferred for code generation.
218  *
219  * In pseudocode, it means:
220  *
221  * if (a)
222  *   res = b;
223  * else
224  *   res = c;
225  * return res;
226  *
227  * is transformed into
228  *
229  * if (a)
230  *   return b;
231  * else
232  *   return c;
233  */
234 void normalize_n_returns(ir_graph *irg)
235 {
236         int i, j, n;
237         ir_node  *list     = NULL;
238         ir_node  *final    = NULL;
239         unsigned  n_rets   = 0;
240         unsigned  n_finals = 0;
241         ir_node  *endbl    = get_irg_end_block(irg);
242         int       n_ret_vals;
243         ir_node **in;
244         ir_node  *end;
245
246         /*
247          * First, link all returns:
248          * These must be predecessors of the endblock.
249          * Place Returns that can be moved on list, all others
250          * on final.
251          */
252         n = get_Block_n_cfgpreds(endbl);
253         for (i = 0; i < n; ++i) {
254                 ir_node *ret = get_Block_cfgpred(endbl, i);
255
256                 if (is_Bad(ret)) {
257                         continue;
258                 } else if (is_Return(ret) && can_move_ret(ret)) {
259                         /*
260                          * Ok, all conditions met, we can move this Return, put it
261                          * on our work list.
262                          */
263                         set_irn_link(ret, list);
264                         list = ret;
265                         ++n_rets;
266                 } else {
267                         /* Put all nodes that are not changed on the final list. */
268                         set_irn_link(ret, final);
269                         final = ret;
270                         ++n_finals;
271                 }
272         }
273
274         if (n_rets == 0) {
275                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
276                 add_irg_properties(irg, IR_GRAPH_PROPERTY_MANY_RETURNS);
277                 return;
278         }
279
280         /*
281          * Now move the Returns upwards. We move always one block up (and create n
282          * new Returns), than we check if a newly created Return can be moved even
283          * further. If yes, we simply add it to our work list, else to the final
284          * list.
285          */
286         end        = get_irg_end(irg);
287         n_ret_vals = get_irn_arity(list);
288         in         = ALLOCAN(ir_node*, n_ret_vals);
289         while (list != NULL) {
290                 ir_node  *ret   = list;
291                 ir_node  *block = get_nodes_block(ret);
292                 dbg_info *dbgi  = get_irn_dbg_info(ret);
293                 ir_node  *phiM;
294
295                 list = (ir_node*)get_irn_link(ret);
296                 --n_rets;
297
298                 n = get_Block_n_cfgpreds(block);
299                 for (i = 0; i < n; ++i) {
300                         ir_node *jmp = get_Block_cfgpred(block, i);
301                         ir_node *new_bl, *new_ret;
302
303                         if (is_Bad(jmp))
304                                 continue;
305                         assert(is_Jmp(jmp));
306
307                         new_bl = get_nodes_block(jmp);
308
309                         /* create the in-array for the new Return */
310                         for (j = 0; j < n_ret_vals; ++j) {
311                                 ir_node *pred = get_irn_n(ret, j);
312
313                                 in[j] = (is_Phi(pred) && get_nodes_block(pred) == block) ? get_Phi_pred(pred, i) : pred;
314                         }
315
316                         new_ret = new_rd_Return(dbgi, new_bl, in[0], n_ret_vals-1, &in[1]);
317
318                         if (! is_Bad(new_ret)) {
319                                 /*
320                                  * The newly created node might be bad, if we
321                                  * create it in a block with only Bad predecessors.
322                                  * In that case ignore this block.
323                                  *
324                                  * We could even kill the jmp then ...
325                                  */
326                                 if (can_move_ret(new_ret)) {
327                                         set_irn_link(new_ret, list);
328                                         list = new_ret;
329                                         ++n_rets;
330                                 } else {
331                                         set_irn_link(new_ret, final);
332                                         final = new_ret;
333                                         ++n_finals;
334                                 }
335                         }
336
337                         /* remove the Jmp, we have placed a Return here */
338                         exchange(jmp, new_r_Bad(irg, mode_X));
339                 }
340
341                 /*
342                  * if the memory of the old Return is a PhiM, remove it
343                  * from the keep-alives, or it will keep the block which
344                  * will crash the dominator algorithm.
345                  */
346                 phiM = get_Return_mem(ret);
347                 if (is_Phi(phiM)) {
348                         n = get_End_n_keepalives(end);
349                         for (i = 0; i < n; ++i) {
350                                 if (get_End_keepalive(end, i) == phiM) {
351                                         set_End_keepalive(end, i, new_r_Bad(irg, mode_M));
352                                         break;
353                                 }
354                         }
355                 }
356         }
357
358         /*
359          * Last step: Create a new endblock, with all nodes on the final list as
360          * predecessors.
361          */
362         in = ALLOCAN(ir_node*, n_finals);
363
364         for (i = 0; final != NULL; ++i, final = (ir_node*)get_irn_link(final)) {
365                 in[i] = final;
366         }
367
368         exchange(endbl, new_r_Block(irg, n_finals, in));
369
370         /* Invalidate analysis information:
371          * Blocks become dead and new Returns were deleted, so dominator, outs and
372          * loop are inconsistent, trouts and callee-state should be still valid */
373         confirm_irg_properties(irg,
374                 IR_GRAPH_PROPERTY_NO_TUPLES
375                 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
376                 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
377         add_irg_properties(irg, IR_GRAPH_PROPERTY_MANY_RETURNS);
378 }
379
380 /* Create a graph pass. */
381 ir_graph_pass_t *normalize_n_returns_pass(const char *name)
382 {
383         return def_graph_pass(name ? name : "n_rets", normalize_n_returns);
384 }