3 * File name: ir/opt/tailrec.c
4 * Purpose: tail-recursion optimization
9 * Copyright: (c) 2002-2004 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
24 #include "irgraph_t.h"
29 static int n_opt_applications = 0;
32 * the environment for colelcting data
34 typedef struct _collect_t {
35 ir_node *proj_X; /**< initial exec proj */
36 ir_node *block; /**< old first block */
37 int blk_idx; /**< cfgpred index of the initial exec in block */
38 ir_node *proj_m; /**< linked list of memory from start proj's */
39 ir_node *proj_data; /**< linked list of all parameter access proj's */
43 * walker for collecting data
45 static void collect_data(ir_node *node, void *env)
47 collect_t *data = env;
51 switch (get_irn_opcode(node)) {
53 pred = get_Proj_pred(node);
55 op = get_irn_op(pred);
57 ir_node *start = get_Proj_pred(pred);
59 if (get_irn_op(start) == op_Start) {
60 if (get_Proj_proj(pred) == pn_Start_T_args) {
61 /* found Proj(ProjT(Start)) */
62 set_irn_link(node, data->proj_data);
63 data->proj_data = node;
67 else if (op == op_Start) {
68 switch (get_Proj_proj(node)) {
70 /* found ProjM(Start) */
71 set_irn_link(node, data->proj_m);
74 case pn_Start_X_initial_exec:
75 /* found ProjX(Start) */
84 int i, n_pred = get_Block_n_cfgpreds(node);
87 * the first block has the initial exec as cfg predecessor
89 if (node != current_ir_graph->start_block) {
90 for (i = 0; i < n_pred; ++i) {
91 if (get_Block_cfgpred(node, i) == data->proj_X) {
106 * do the graph reconstruction for tail-recursion elimination
108 * @param irg the graph that will reconstructed
109 * @param rets linked list of all rets
110 * @param n_tail_calls number of tail-recursion calls
112 static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
114 ir_graph *rem_irg = current_ir_graph;
115 ir_node *end_block = irg->end_block;
116 ir_node *block, *jmp, *call, *calls;
119 ir_node ***call_params;
123 int rem = get_optimize();
125 assert(n_tail_calls);
127 /* we add nwe nodes, so the outs are inconsistant */
128 set_irg_outs_inconsistent(irg);
130 /* we add new blocks and change the control flow */
131 set_irg_dom_inconsistent(irg);
133 /* we add a new loop */
134 set_irg_loopinfo_inconsistent(irg);
138 /* collect needed data */
143 data.proj_data = NULL;
144 irg_walk_graph(irg, NULL, collect_data, &data);
146 /* check number of arguments */
147 call = get_irn_link(end_block);
148 n_params = get_Call_n_params(call);
150 assert(data.proj_X && "Could not find initial exec from Start");
151 assert(data.block && "Could not find first block");
152 assert(data.proj_m && "Could not find ProjM(Start)");
153 assert((data.proj_data || n_params == 0) && "Could not find Proj(ProjT(Start)) of non-void function");
155 current_ir_graph = irg;
157 /* allocate in's for phi and block construction */
158 NEW_ARR_A(ir_node *, in, n_tail_calls + 1);
162 /* turn Return's into Jmp's */
163 for (i = 1, p = rets; p; p = get_irn_link(p)) {
166 set_cur_block(get_nodes_block(p));
169 exchange(p, new_Bad());
172 add_End_keepalive(get_irg_end(irg), jmp);
175 /* create a new block at start */
176 block = new_Block(n_tail_calls + 1, in);
179 /* the old first block is now the second one */
180 set_Block_cfgpred(data.block, data.blk_idx, jmp);
182 /* allocate phi's, position 0 contains the memory phi */
183 NEW_ARR_A(ir_node *, phis, n_params + 1);
185 /* build the memory phi */
187 in[i] = new_rd_Proj(NULL, irg, irg->start_block, irg->start, mode_M, pn_Start_M);
190 for (calls = call; calls; calls = get_irn_link(calls)) {
191 in[i] = get_Call_mem(calls);
194 assert(i == n_tail_calls + 1);
196 phis[0] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode_M);
198 /* build the data phi's */
202 NEW_ARR_A(ir_node **, call_params, n_params);
204 /* collect all parameters */
205 for (i = 0, calls = call; calls; calls = get_irn_link(calls)) {
206 call_params[i] = get_Call_param_arr(calls);
210 /* build new projs and Phi's */
211 for (i = 0; i < n_params; ++i) {
212 ir_mode *mode = get_irn_mode(call_params[0][i]);
214 in[0] = new_rd_Proj(NULL, irg, block, irg->args, mode, i);
215 for (j = 0; j < n_tail_calls; ++j)
216 in[j + 1] = call_params[j][i];
218 phis[i + 1] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode);
223 * ok, we are here, so we have build and collected all needed Phi's
224 * now exchange all Projs into links to Phi
226 for (p = data.proj_m; p; p = get_irn_link(p)) {
227 exchange(p, phis[0]);
229 for (p = data.proj_data; p; p = get_irn_link(p)) {
230 long proj = get_Proj_proj(p);
232 assert(0 <= proj && proj < n_params);
233 exchange(p, phis[proj + 1]);
236 current_ir_graph = rem_irg;
241 * convert simple tail-calls into loops
243 void opt_tail_rec_irg(ir_graph *irg)
245 ir_node *end_block = irg->end_block;
247 int i, n_tail_calls = 0;
248 ir_node *rets = NULL;
250 if (! get_opt_tail_recursion() || ! get_opt_optimize())
253 set_irn_link(end_block, NULL);
255 n_preds = get_Block_n_cfgpreds(end_block);
256 for (i = 0; i < n_preds; ++i) {
257 ir_node *ret = get_Block_cfgpred(end_block, i);
258 ir_node *call, *call_ptr;
263 /* search all returns of a block */
264 if (get_irn_op(ret) != op_Return)
267 /* check, if it's a Return self() */
268 call = skip_Proj(get_Return_mem(ret));
269 if (get_irn_op(call) != op_Call)
272 /* check if it's a recursive call */
273 call_ptr = get_Call_ptr(call);
275 if (get_irn_op(call_ptr) != op_SymConst)
278 if (get_SymConst_kind(call_ptr) != symconst_addr_ent)
281 ent = get_SymConst_entity(call_ptr);
282 if (!ent || get_entity_irg(ent) != irg)
285 /* ok, mem is routed to a recursive call, check return args */
286 n_ress = get_Return_n_ress(ret);
287 ress = get_Return_res_arr(ret);
289 for (j = 0; j < n_ress; ++j) {
290 ir_node *irn = skip_Proj(skip_Proj(ress[j]));
293 /* not routed to a call */
300 /* here, we have found a call */
301 set_irn_link(call, get_irn_link(end_block));
302 set_irn_link(end_block, call);
305 /* link all returns, we will need this */
306 set_irn_link(ret, rets);
310 /* now, end_block->link contains the list of all tail calls */
314 if (get_opt_tail_recursion_verbose() && get_firm_verbosity() > 1)
315 printf(" Performing tail recursion for graph %s and %d Calls\n",
316 get_entity_ld_name(get_irg_entity(irg)), n_tail_calls);
317 n_opt_applications++;
319 do_opt_tail_rec(irg, rets, n_tail_calls);
323 * optimize tail recursion away
325 void opt_tail_recursion(void)
329 if (! get_opt_tail_recursion() || ! get_opt_optimize())
332 n_opt_applications = 0;
334 for (i = 0; i < get_irp_n_irgs(); i++) {
335 current_ir_graph = get_irp_irg(i);
337 opt_tail_rec_irg(current_ir_graph);
340 if (get_opt_tail_recursion_verbose())
341 printf("Performed tail recursion for %d of %d graphs\n", n_opt_applications, get_irp_n_irgs());