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