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"
31 * the environment for colelcting data
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 */
41 * walker for collecting data
43 static void collect_data(ir_node *node, void *env)
45 collect_t *data = env;
49 switch (intern_get_irn_opcode(node)) {
51 pred = get_Proj_pred(node);
53 op = intern_get_irn_op(pred);
55 ir_node *start = get_Proj_pred(pred);
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;
65 else if (op == op_Start) {
66 switch (get_Proj_proj(node)) {
68 /* found ProjM(Start) */
69 set_irn_link(node, data->proj_m);
72 case pn_Start_X_initial_exec:
73 /* found ProjX(Start) */
82 int i, n_pred = get_Block_n_cfgpreds(node);
85 * the first block has the initial exec as cfg predecessor
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) {
103 * do the graph reconstruction for tail-recursion elimination
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
109 static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
111 ir_graph *rem_irg = current_ir_graph;
112 ir_node *end_block = irg->end_block;
113 ir_node *block, *jmp, *call, *calls;
116 ir_node ***call_params;
120 int rem = get_optimize();
122 assert(n_tail_calls);
124 /* we add nwe nodes, so the outs are inconsistant */
125 set_irg_outs_inconsistent(irg);
127 /* we add new blocks and change the control flow */
128 set_irg_dom_inconsistent(irg);
130 /* we add a new loop */
131 set_irg_loopinfo_inconsistent(irg);
135 /* collect needed data */
139 data.proj_data = NULL;
140 irg_walk_graph(irg, NULL, collect_data, &data);
142 /* check number of arguments */
143 call = get_irn_link(end_block);
144 n_params = get_Call_n_params(call);
146 assert(data.proj_X && "Could not find initial exec from Start");
147 assert(data.block && "Could not find first block");
148 assert(data.proj_m && "Could not find ProjM(Start)");
149 assert((data.proj_data || n_params == 0) && "Could not find Proj(ProjT(Start)) of non-void function");
151 current_ir_graph = irg;
153 /* allocate in's for phi and block construction */
154 NEW_ARR_A(ir_node *, in, n_tail_calls + 1);
158 /* turn Return's into Jmp's */
159 for (i = 1, p = rets; p; p = get_irn_link(p)) {
162 switch_block(get_nodes_Block(p));
165 exchange(p, new_Bad());
168 add_End_keepalive(get_irg_end(irg), jmp);
171 /* create a new block at start */
172 block = new_Block(n_tail_calls + 1, in);
175 /* the old first block is now the second one */
176 for (i = 0; i < get_Block_n_cfgpreds(data.block); ++i) {
177 if (get_Block_cfgpred(data.block, i) == data.proj_X) {
178 set_Block_cfgpred(data.block, i, jmp);
183 /* allocate phi's, position 0 contains the memory phi */
184 NEW_ARR_A(ir_node *, phis, n_params + 1);
186 /* build the memory phi */
188 in[i] = new_rd_Proj(NULL, irg, irg->start_block, irg->start, mode_M, pn_Start_M);
191 for (calls = call; calls; calls = get_irn_link(calls)) {
192 in[i] = get_Call_mem(calls);
195 phis[0] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode_M);
197 /* build the data phi's */
201 NEW_ARR_A(ir_node **, call_params, n_params);
203 /* collect all parameters */
204 for (i = 0, calls = call; calls; calls = get_irn_link(calls)) {
205 call_params[i] = get_Call_param_arr(calls);
209 /* build new projs and Phi's */
210 for (i = 0; i < n_params; ++i) {
211 ir_mode *mode = get_irn_mode(call_params[0][i]);
213 in[0] = new_rd_Proj(NULL, irg, block, irg->args, mode, i);
214 for (j = 0; j < n_tail_calls; ++j)
215 in[j + 1] = call_params[j][i];
217 phis[i + 1] = new_rd_Phi(NULL, irg, block, n_tail_calls + 1, in, mode);
222 * ok, we are here, so we have build and collected all needed Phi's
223 * now exchange all Projs into links to Phi
225 for (p = data.proj_m; p; p = get_irn_link(p)) {
226 exchange(p, phis[0]);
228 for (p = data.proj_data; p; p = get_irn_link(p)) {
229 long proj = get_Proj_proj(p);
231 assert(0 <= proj && proj < n_params);
232 exchange(p, phis[proj + 1]);
235 current_ir_graph = rem_irg;
240 * convert simple tail-calls into loops
242 void opt_tail_rec_irg(ir_graph *irg)
244 ir_node *end_block = irg->end_block;
246 int i, n_tail_calls = 0;
247 ir_node *rets = NULL;
249 if (! get_opt_tail_recursion() || ! get_opt_optimize())
252 set_irn_link(end_block, NULL);
254 n_preds = get_Block_n_cfgpreds(end_block);
255 for (i = 0; i < n_preds; ++i) {
256 ir_node *ret = get_Block_cfgpred(end_block, i);
257 ir_node *proj_m, *call, *call_ptr;
263 /* search all returns of a block */
264 if (intern_get_irn_op(ret) != op_Return)
267 /* check, if it's a Return self() */
268 proj_m = get_Return_mem(ret);
270 if (intern_get_irn_op(proj_m) != op_Proj)
273 call = get_Proj_pred(proj_m);
274 if (intern_get_irn_op(call) != op_Call)
277 /* check if it's a recursive call */
278 call_ptr = get_Call_ptr(call);
280 if (intern_get_irn_op(call_ptr) != op_Const)
283 tv = get_Const_tarval(call_ptr);
284 if (! tarval_is_entity(tv))
287 ent = tarval_to_entity(tv);
288 if (!ent || get_entity_irg(ent) != irg)
291 /* ok, mem is routed to a recursive call, check return args */
292 n_ress = get_Return_n_ress(ret);
293 ress = get_Return_res_arr(ret);
295 for (j = 0; j < n_ress; ++j) {
296 ir_node *proj = ress[j];
300 if (intern_get_irn_op(proj) != op_Proj) {
301 /* not routed to a call */
305 proj_proj = get_Proj_pred(proj);
307 if (intern_get_irn_op(proj) != op_Proj) {
308 /* not routed to a call */
312 irn = get_Proj_pred(proj_proj);
315 /* not routed to a call */
322 /* here, we have found a call */
323 set_irn_link(call, get_irn_link(end_block));
324 set_irn_link(end_block, call);
327 /* link all returns, we will need this */
328 set_irn_link(ret, rets);
332 /* now, end_block->link contains the list of all tail calls */
336 do_opt_tail_rec(irg, rets, n_tail_calls);
340 * optimize tail recursion away
342 void opt_tail_recursion(void)
346 if (! get_opt_tail_recursion() || ! get_opt_optimize())
349 for (i = 0; i < get_irp_n_irgs(); i++) {
350 current_ir_graph = get_irp_irg(i);
352 opt_tail_rec_irg(current_ir_graph);