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