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.
34 #include "irgraph_t.h"
39 #include "scalar_replace.h"
44 * the environment for collecting data
46 typedef struct _collect_t {
47 ir_node *proj_X; /**< initial exec proj */
48 ir_node *block; /**< old first block */
49 int blk_idx; /**< cfgpred index of the initial exec in block */
50 ir_node *proj_m; /**< linked list of memory from start proj's */
51 ir_node *proj_data; /**< linked list of all parameter access proj's */
55 * walker for collecting data, fills a collect_t environment
57 static void collect_data(ir_node *node, void *env)
59 collect_t *data = env;
63 switch (get_irn_opcode(node)) {
65 pred = get_Proj_pred(node);
67 op = get_irn_op(pred);
69 ir_node *start = get_Proj_pred(pred);
71 if (get_irn_op(start) == op_Start) {
72 if (get_Proj_proj(pred) == pn_Start_T_args) {
73 /* found Proj(ProjT(Start)) */
74 set_irn_link(node, data->proj_data);
75 data->proj_data = node;
79 else if (op == op_Start) {
80 switch (get_Proj_proj(node)) {
82 /* found ProjM(Start) */
83 set_irn_link(node, data->proj_m);
86 case pn_Start_X_initial_exec:
87 /* found ProjX(Start) */
96 int i, n_pred = get_Block_n_cfgpreds(node);
99 * the first block has the initial exec as cfg predecessor
101 if (node != get_irg_start_block(current_ir_graph)) {
102 for (i = 0; i < n_pred; ++i) {
103 if (get_Block_cfgpred(node, i) == data->proj_X) {
118 * do the graph reconstruction for tail-recursion elimination
120 * @param irg the graph that will reconstructed
121 * @param rets linked list of all rets
122 * @param n_tail_calls number of tail-recursion calls
124 static void do_opt_tail_rec(ir_graph *irg, ir_node *rets, int n_tail_calls)
126 ir_node *end_block = get_irg_end_block(irg);
127 ir_node *block, *jmp, *call, *calls;
130 ir_node ***call_params;
134 int rem = get_optimize();
135 entity *ent = get_irg_entity(irg);
136 ir_type *method_tp = get_entity_type(ent);
138 assert(n_tail_calls);
140 /* we add new nodes, so the outs are inconsistent */
141 set_irg_outs_inconsistent(irg);
143 /* we add new blocks and change the control flow */
144 set_irg_doms_inconsistent(irg);
145 set_irg_extblk_inconsistent(irg);
147 /* we add a new loop */
148 set_irg_loopinfo_inconsistent(irg);
150 /* calls are removed */
151 set_trouts_inconsistent();
153 /* we must build some new nodes WITHOUT CSE */
156 /* collect needed data */
161 data.proj_data = NULL;
162 irg_walk_graph(irg, NULL, collect_data, &data);
164 /* check number of arguments */
165 call = get_irn_link(end_block);
166 n_params = get_Call_n_params(call);
168 assert(data.proj_X && "Could not find initial exec from Start");
169 assert(data.block && "Could not find first block");
170 assert(data.proj_m && "Could not find ProjM(Start)");
171 assert((data.proj_data || n_params == 0) && "Could not find Proj(ProjT(Start)) of non-void function");
173 /* allocate in's for phi and block construction */
174 NEW_ARR_A(ir_node *, in, n_tail_calls + 1);
178 /* turn Return's into Jmp's */
179 for (i = 1, p = rets; p; p = get_irn_link(p)) {
181 ir_node *block = get_nodes_block(p);
183 jmp = new_r_Jmp(irg, block);
185 exchange(p, new_r_Bad(irg));
188 /* we might generate an endless loop, so add
189 * the block to the keep-alive list */
190 add_End_keepalive(get_irg_end(irg), block);
193 /* create a new block at start */
194 block = new_r_Block(irg, n_tail_calls + 1, in);
195 jmp = new_r_Jmp(irg, block);
197 /* the old first block is now the second one */
198 set_Block_cfgpred(data.block, data.blk_idx, jmp);
200 /* allocate phi's, position 0 contains the memory phi */
201 NEW_ARR_A(ir_node *, phis, n_params + 1);
203 /* build the memory phi */
205 in[i] = new_r_Proj(irg, get_irg_start_block(irg), get_irg_start(irg), mode_M, pn_Start_M);
208 for (calls = call; calls; calls = get_irn_link(calls)) {
209 in[i] = get_Call_mem(calls);
212 assert(i == n_tail_calls + 1);
214 phis[0] = new_r_Phi(irg, block, n_tail_calls + 1, in, mode_M);
216 /* build the data Phi's */
221 NEW_ARR_A(ir_node **, call_params, n_tail_calls);
223 /* collect all parameters */
224 for (i = 0, calls = call; calls; calls = get_irn_link(calls)) {
225 call_params[i] = get_Call_param_arr(calls);
229 /* build new Proj's and Phi's */
230 args = get_irg_args(irg);
231 for (i = 0; i < n_params; ++i) {
232 ir_mode *mode = get_type_mode(get_method_param_type(method_tp, i));
234 in[0] = new_r_Proj(irg, block, args, mode, i);
235 for (j = 0; j < n_tail_calls; ++j)
236 in[j + 1] = call_params[j][i];
238 phis[i + 1] = new_r_Phi(irg, block, n_tail_calls + 1, in, mode);
243 * ok, we are here, so we have build and collected all needed Phi's
244 * now exchange all Projs into links to Phi
246 for (p = data.proj_m; p; p = get_irn_link(p)) {
247 exchange(p, phis[0]);
249 for (p = data.proj_data; p; p = get_irn_link(p)) {
250 long proj = get_Proj_proj(p);
252 assert(0 <= proj && proj < n_params);
253 exchange(p, phis[proj + 1]);
256 /* tail recursion was done, all info is invalid */
257 set_irg_doms_inconsistent(irg);
258 set_irg_outs_inconsistent(irg);
259 set_irg_extblk_inconsistent(irg);
260 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
261 set_trouts_inconsistent();
262 set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
268 * Check the lifetime of locals in the given graph.
269 * Tail recursion can only be done, if we can prove that
270 * the lifetime of locals end with the recursive call.
271 * We do this by checking that no address of a local variable is
272 * stored or transmitted as an argument to a call.
274 * @return non-zero if it's ok to do tail recursion
276 static int check_lifetime_of_locals(ir_graph *irg)
278 ir_node *irg_frame = get_irg_frame(irg);
281 if (get_irg_outs_state(irg) != outs_consistent)
282 compute_irg_outs(irg);
284 for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
285 ir_node *succ = get_irn_out(irg_frame, i);
287 if (is_Sel(succ) && is_address_taken(succ))
294 * convert simple tail-calls into loops
296 int opt_tail_rec_irg(ir_graph *irg)
299 int i, n_tail_calls = 0;
300 ir_node *rets = NULL;
301 ir_type *mtd_type, *call_type;
303 if (! get_opt_tail_recursion() || ! get_opt_optimize())
306 if (! check_lifetime_of_locals(irg))
310 * This tail recursion optimization works best
311 * if the Returns are normalized.
313 normalize_n_returns(irg);
315 end_block = get_irg_end_block(irg);
316 set_irn_link(end_block, NULL);
318 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
319 ir_node *ret = get_Block_cfgpred(end_block, i);
320 ir_node *call, *call_ptr;
325 /* search all Returns of a block */
326 if (! is_Return(ret))
329 /* check, if it's a Return self() */
330 call = skip_Proj(get_Return_mem(ret));
334 /* check if it's a recursive call */
335 call_ptr = get_Call_ptr(call);
337 if (get_irn_op(call_ptr) != op_SymConst)
340 if (get_SymConst_kind(call_ptr) != symconst_addr_ent)
343 ent = get_SymConst_entity(call_ptr);
344 if (!ent || get_entity_irg(ent) != irg)
347 /* ok, mem is routed to a recursive call, check return args */
348 ress = get_Return_res_arr(ret);
349 for (j = get_Return_n_ress(ret) - 1; j >= 0; --j) {
350 ir_node *irn = skip_Proj(skip_Proj(ress[j]));
353 /* not routed to a call */
361 * Check, that the types match. At least in C
364 mtd_type = get_entity_type(ent);
365 call_type = get_Call_type(call);
367 if (mtd_type != call_type) {
369 * Hmm, the types did not match, bad.
370 * This can happen in C when no prototype is given
371 * or K&R style is used.
374 printf("Warning: Tail recursion fails because of different method and call types:\n");
376 dump_type(call_type);
381 /* here, we have found a call */
382 set_irn_link(call, get_irn_link(end_block));
383 set_irn_link(end_block, call);
386 /* link all returns, we will need this */
387 set_irn_link(ret, rets);
391 /* now, end_block->link contains the list of all tail calls */
395 if (get_opt_tail_recursion_verbose() && get_firm_verbosity() > 1)
396 printf(" Performing tail recursion for graph %s and %d Calls\n",
397 get_entity_ld_name(get_irg_entity(irg)), n_tail_calls);
399 hook_tail_rec(irg, n_tail_calls);
400 do_opt_tail_rec(irg, rets, n_tail_calls);
406 * optimize tail recursion away
408 void opt_tail_recursion(void)
411 int n_opt_applications = 0;
414 if (! get_opt_tail_recursion() || ! get_opt_optimize())
417 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
418 irg = get_irp_irg(i);
420 if (opt_tail_rec_irg(irg))
421 ++n_opt_applications;
424 if (get_opt_tail_recursion_verbose())
425 printf("Performed tail recursion for %d of %d graphs\n", n_opt_applications, get_irp_n_irgs());