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