first pubplic release
[libfirm] / ir / opt / tailrec.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/opt/tailrec.c
4  * Purpose:     tail-recursion optimization
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     08.06.2004
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2004 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15
16 #include <assert.h>
17 #include "tailrec.h"
18 #include "array.h"
19 #include "irprog.h"
20 #include "irgwalk.h"
21 #include "irgmod.h"
22 #include "irop.h"
23 #include "irnode_t.h"
24 #include "irgraph_t.h"
25 #include "ircons.h"
26 #include "irflag.h"
27 #include "tv.h"
28 #include "firmstat.h"
29
30 /**
31  * the environment for colelcting data
32  */
33 typedef struct _collect_t {
34   ir_node *proj_X;              /**< initial exec proj */
35   ir_node *block;               /**< old first block */
36   ir_node *proj_m;              /**< linked list of memory from start proj's */
37   ir_node *proj_data;           /**< linked list of all parameter access proj's */
38 } collect_t;
39
40 /**
41  * walker for collecting data
42  */
43 static void collect_data(ir_node *node, void *env)
44 {
45   collect_t *data = env;
46   ir_node *pred;
47   ir_op *op;
48
49   switch (intern_get_irn_opcode(node)) {
50   case iro_Proj:
51     pred = get_Proj_pred(node);
52
53     op = intern_get_irn_op(pred);
54     if (op == op_Proj) {
55       ir_node *start = get_Proj_pred(pred);
56
57       if (intern_get_irn_op(start) == op_Start) {
58         if (get_Proj_proj(pred) == pn_Start_T_args) {
59           /* found Proj(ProjT(Start)) */
60           set_irn_link(node, data->proj_data);
61           data->proj_data = node;
62         }
63       }
64     }
65     else if (op == op_Start) {
66       switch (get_Proj_proj(node)) {
67         case pn_Start_M:
68           /* found ProjM(Start) */
69           set_irn_link(node, data->proj_m);
70           data->proj_m = node;
71           break;
72         case pn_Start_X_initial_exec:
73           /* found ProjX(Start) */
74           data->proj_X = node;
75           break;
76         default:
77           break;
78       }
79     }
80     break;
81   case iro_Block: {
82     int i, n_pred = get_Block_n_cfgpreds(node);
83
84     /*
85      * the first block has the initial exec as cfg predecessor
86      */
87     if (node != current_ir_graph->start_block) {
88       for (i = 0; i < n_pred; ++i) {
89         if (get_Block_cfgpred(node, i) == data->proj_X) {
90           data->block = node;
91           break;
92         }
93       }
94     }
95     break;
96   }
97   default:
98     break;
99   }
100 }
101
102 /**
103  * do the graph reconstruction for tail-recursion elimination
104  *
105  * @param irg           the graph that will reconstructed
106  * @param rets          linked list of all rets
107  * @param n_tail_calls  number of tail-recursion calls
108  */
109 static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
110 {
111   ir_graph *rem_irg = current_ir_graph;
112   ir_node *end_block = irg->end_block;
113   ir_node *block, *jmp, *call, *calls;
114   ir_node **in;
115   ir_node **phis;
116   ir_node ***call_params;
117   ir_node *p;
118   int i, j, n_params;
119   collect_t data;
120   int rem = get_optimize();
121
122   assert(n_tail_calls);
123
124   set_optimize(0);
125
126   /* collect needed data */
127   data.proj_X    = NULL;
128   data.block     = NULL;
129   data.proj_m    = NULL;
130   data.proj_data = NULL;
131   irg_walk_graph(irg, NULL, collect_data, &data);
132
133   /* check number of arguments */
134   call = get_irn_link(end_block);
135   n_params = get_Call_n_params(call);
136
137   assert(data.proj_X && "Could not find initial exec from Start");
138   assert(data.block  && "Could not find first block");
139   assert(data.proj_m && "Could not find ProjM(Start)");
140   assert((data.proj_data || n_params == 0) && "Could not find Proj(ProjT(Start)) of non-void function");
141
142   current_ir_graph = irg;
143
144   /* allocate in's for phi and block construction */
145   NEW_ARR_A(ir_node *, in, n_tail_calls + 1);
146
147   in[0] = data.proj_X;
148
149   /* turn Return's into Jmp's */
150   for (i = 1, p = rets; p; p = get_irn_link(p)) {
151     ir_node *jmp;
152
153     switch_block(get_nodes_Block(p));
154     jmp = new_Jmp();
155
156     exchange(p, new_Bad());
157     in[i++] = jmp;
158
159     add_End_keepalive(get_irg_end(irg), jmp);
160   }
161
162   /* create a new block at start */
163   block = new_Block(n_tail_calls + 1, in);
164   jmp   = new_Jmp();
165
166   /* the old first block is now the second one */
167   for (i = 0; i < get_Block_n_cfgpreds(data.block); ++i) {
168     if (get_Block_cfgpred(data.block, i) == data.proj_X) {
169       set_Block_cfgpred(data.block, i, jmp);
170       break;
171     }
172   }
173
174   /* allocate phi's, position 0 contains the memory phi */
175   NEW_ARR_A(ir_node *, phis, n_params + 1);
176
177   /* build the memory phi */
178   i = 0;
179   in[i] = new_rd_Proj(NULL, irg, irg->start_block, irg->start, mode_M, pn_Start_M);
180   ++i;
181
182   for (calls = call; calls; calls = get_irn_link(calls)) {
183     in[i] = get_Call_mem(calls);
184     ++i;
185   }
186   phis[0] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode_M);
187
188   /* build the data phi's */
189   if (n_params > 0) {
190     ir_node *calls;
191
192     NEW_ARR_A(ir_node **, call_params, n_params);
193
194     /* collect all parameters */
195     for (i = 0, calls = call; calls; calls = get_irn_link(calls)) {
196       call_params[i] = get_Call_param_arr(calls);
197       ++i;
198     }
199
200     /* build new projs and Phi's */
201     for (i = 0; i < n_params; ++i) {
202       ir_mode *mode = get_irn_mode(call_params[0][i]);
203
204       in[0] = new_rd_Proj(NULL, irg, block, irg->args, mode, i);
205       for (j = 0; j < n_tail_calls; ++j)
206         in[j + 1] = call_params[j][i];
207
208       phis[i + 1] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode);
209     }
210   }
211
212   /*
213    * ok, we are here, so we have build and collected all needed Phi's
214    * now exchange all Projs into links to Phi
215    */
216   for (p = data.proj_m; p; p = get_irn_link(p)) {
217     exchange(p, phis[0]);
218   }
219   for (p = data.proj_data; p; p = get_irn_link(p)) {
220     long proj = get_Proj_proj(p);
221
222     assert(0 <= proj && proj < n_params);
223     exchange(p, phis[proj + 1]);
224   }
225
226   current_ir_graph = rem_irg;
227   set_optimize(rem);
228 }
229
230 /*
231  * convert simple tail-calls into loops
232  */
233 void opt_tail_rec_irg(ir_graph *irg)
234 {
235   ir_node *end_block = irg->end_block;
236   int n_preds;
237   int i, n_tail_calls = 0;
238   ir_node *rets = NULL;
239
240   if (! get_opt_tail_recursion() || ! get_opt_optimize())
241     return;
242
243   set_irn_link(end_block, NULL);
244
245   n_preds = get_Block_n_cfgpreds(end_block);
246   for (i = 0; i < n_preds; ++i) {
247     ir_node *ret = get_Block_cfgpred(end_block, i);
248     ir_node *proj_m, *call, *call_ptr;
249     tarval *tv;
250     entity *ent;
251     int j, n_ress;
252     ir_node **ress;
253
254     /* search all returns of a block */
255     if (intern_get_irn_op(ret) != op_Return)
256       continue;
257
258     /* check, if it's a Return self() */
259     proj_m = get_Return_mem(ret);
260
261     if (intern_get_irn_op(proj_m) != op_Proj)
262       continue;
263
264     call = get_Proj_pred(proj_m);
265     if (intern_get_irn_op(call) != op_Call)
266       continue;
267
268     /* check if it's a recursive call */
269     call_ptr = get_Call_ptr(call);
270
271     if (intern_get_irn_op(call_ptr) != op_Const)
272       continue;
273
274     tv = get_Const_tarval(call_ptr);
275     if (! tarval_is_entity(tv))
276       continue;
277
278     ent = tarval_to_entity(tv);
279     if (!ent || get_entity_irg(ent) != irg)
280       continue;
281
282     /* ok, mem is routed to a recursive call, check return args */
283     n_ress = get_Return_n_ress(ret);
284     ress = get_Return_res_arr(ret);
285
286     for (j = 0; j < n_ress; ++j) {
287       ir_node *proj = ress[j];
288       ir_node *proj_proj;
289       ir_node *irn;
290
291       if (intern_get_irn_op(proj) != op_Proj) {
292         /* not routed to a call */
293         break;
294       }
295
296       proj_proj = get_Proj_pred(proj);
297
298       if (intern_get_irn_op(proj) != op_Proj) {
299         /* not routed to a call */
300         break;
301       }
302
303       irn = get_Proj_pred(proj_proj);
304
305       if (irn != call) {
306         /* not routed to a call */
307         break;
308       }
309     }
310     if (j < n_ress)
311       continue;
312
313     /* here, we have found a call */
314     set_irn_link(call, get_irn_link(end_block));
315     set_irn_link(end_block, call);
316     ++n_tail_calls;
317
318     /* link all returns, we will need this */
319     set_irn_link(ret, rets);
320     rets = ret;
321   }
322
323   /* now, end_block->link contains the list of all tail calls */
324   if (! n_tail_calls)
325     return;
326
327   do_opt_tail_rec(irg, rets, n_tail_calls);
328 }
329
330 /*
331  * optimize tail recursion away
332  */
333 void opt_tail_recursion(void)
334 {
335   int i;
336
337   if (! get_opt_tail_recursion() || ! get_opt_optimize())
338     return;
339
340   for (i = 0; i < get_irp_n_irgs(); i++) {
341     current_ir_graph = get_irp_irg(i);
342
343     opt_tail_rec_irg(current_ir_graph);
344   }
345 }