a9bb3691b33de7ae9e0331343b3b3bc53cc926a7
[libfirm] / ir / opt / tailrec.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Tail-recursion call optimization.
23  * @date    08.06.2004
24  * @author  Michael Beck
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <assert.h>
30
31 #include "debug.h"
32 #include "iroptimize.h"
33 #include "scalar_replace.h"
34 #include "array_t.h"
35 #include "irprog_t.h"
36 #include "irgwalk.h"
37 #include "irgmod.h"
38 #include "irop.h"
39 #include "irnode_t.h"
40 #include "irgraph_t.h"
41 #include "ircons.h"
42 #include "irflag.h"
43 #include "trouts.h"
44 #include "irouts.h"
45 #include "irhooks.h"
46 #include "ircons_t.h"
47 #include "irpass.h"
48
49 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
50
51 /**
52  * the environment for collecting data
53  */
54 typedef struct collect_t {
55         ir_node *proj_X;      /**< initial exec proj */
56         ir_node *block;       /**< old first block */
57         int     blk_idx;      /**< cfgpred index of the initial exec in block */
58         ir_node *proj_m;      /**< memory from start proj's */
59         ir_node *proj_data;   /**< linked list of all parameter access proj's */
60 } collect_t;
61
62 /**
63  * walker for collecting data, fills a collect_t environment
64  */
65 static void collect_data(ir_node *node, void *env)
66 {
67         collect_t *data = (collect_t*)env;
68         ir_node *pred;
69         ir_opcode opcode;
70
71         switch (get_irn_opcode(node)) {
72         case iro_Proj:
73                 pred = get_Proj_pred(node);
74
75                 opcode = (ir_opcode)get_irn_opcode(pred);
76                 if (opcode == iro_Proj) {
77                         ir_node *start = get_Proj_pred(pred);
78
79                         if (is_Start(start)) {
80                                 if (get_Proj_proj(pred) == pn_Start_T_args) {
81                                         /* found Proj(ProjT(Start)) */
82                                         set_irn_link(node, data->proj_data);
83                                         data->proj_data = node;
84                                 }
85                         }
86                 } else if (opcode == iro_Start) {
87                         if (get_Proj_proj(node) == pn_Start_X_initial_exec) {
88                                 /* found ProjX(Start) */
89                                 data->proj_X = node;
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 != get_irg_start_block(get_irn_irg(node))) {
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 typedef enum tail_rec_variants {
116         TR_DIRECT,  /**< direct return value, i.e. return func(). */
117         TR_ADD,     /**< additive return value, i.e. return x +/- func() */
118         TR_MUL,     /**< multiplicative return value, i.e. return x * func() or return -func() */
119         TR_BAD,     /**< any other transformation */
120         TR_UNKNOWN  /**< during construction */
121 } tail_rec_variants;
122
123 typedef struct tr_env {
124         int               n_tail_calls;  /**< number of tail calls found */
125         int               n_ress;        /**< number of return values */
126         tail_rec_variants *variants;     /**< return value variants */
127         ir_node           *rets;         /**< list of returns that can be transformed */
128 } tr_env;
129
130
131 /**
132  * do the graph reconstruction for tail-recursion elimination
133  *
134  * @param irg  the graph that will reconstructed
135  * @param env  tail recursion environment
136  */
137 static void do_opt_tail_rec(ir_graph *irg, tr_env *env)
138 {
139         ir_node *end_block = get_irg_end_block(irg);
140         ir_node *block, *jmp, *call, *calls;
141         ir_node **in;
142         ir_node **phis;
143         ir_node ***call_params;
144         ir_node *p, *n;
145         int i, j, n_params, n_locs;
146         collect_t data;
147         int rem            = get_optimize();
148         ir_entity *ent     = get_irg_entity(irg);
149         ir_type *method_tp = get_entity_type(ent);
150
151         assert(env->n_tail_calls > 0);
152
153         /* we add new blocks and change the control flow */
154         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
155
156         /* we must build some new nodes WITHOUT CSE */
157         set_optimize(0);
158
159         /* collect needed data */
160         data.proj_X    = NULL;
161         data.block     = NULL;
162         data.blk_idx   = -1;
163         data.proj_m    = get_irg_initial_mem(irg);
164         data.proj_data = NULL;
165         irg_walk_graph(irg, NULL, collect_data, &data);
166
167         /* check number of arguments */
168         call     = (ir_node*)get_irn_link(end_block);
169         n_params = get_Call_n_params(call);
170
171         assert(data.proj_X && "Could not find initial exec from Start");
172         assert(data.block  && "Could not find first block");
173         assert(data.proj_m && "Could not find initial memory");
174         assert((data.proj_data || n_params == 0) && "Could not find Proj(ProjT(Start)) of non-void function");
175
176         /* allocate in's for phi and block construction */
177         NEW_ARR_A(ir_node *, in, env->n_tail_calls + 1);
178
179         /* build a new header block for the loop we create */
180         i = 0;
181         in[i++] = data.proj_X;
182
183         /* turn Return's into Jmp's */
184         for (p = env->rets; p; p = n) {
185                 ir_node *block = get_nodes_block(p);
186
187                 n = (ir_node*)get_irn_link(p);
188                 in[i++] = new_r_Jmp(block);
189
190                 // exchange(p, new_r_Bad(irg));
191
192                 /* we might generate an endless loop, so add
193                  * the block to the keep-alive list */
194                 add_End_keepalive(get_irg_end(irg), block);
195         }
196         assert(i == env->n_tail_calls + 1);
197
198         /* now create it */
199         block = new_r_Block(irg, i, in);
200         jmp   = new_r_Jmp(block);
201
202         /* the old first block is now the second one */
203         set_Block_cfgpred(data.block, data.blk_idx, jmp);
204
205         /* allocate phi's, position 0 contains the memory phi */
206         NEW_ARR_A(ir_node *, phis, n_params + 1);
207
208         /* build the memory phi */
209         i = 0;
210         in[i] = new_r_Proj(get_irg_start(irg), mode_M, pn_Start_M);
211         set_irg_initial_mem(irg, in[i]);
212         ++i;
213
214         for (calls = call; calls != NULL; calls = (ir_node*)get_irn_link(calls)) {
215                 in[i] = get_Call_mem(calls);
216                 ++i;
217         }
218         assert(i == env->n_tail_calls + 1);
219
220         phis[0] = new_r_Phi(block, env->n_tail_calls + 1, in, mode_M);
221
222         /* build the data Phi's */
223         if (n_params > 0) {
224                 ir_node *calls;
225                 ir_node *args;
226
227                 NEW_ARR_A(ir_node **, call_params, env->n_tail_calls);
228
229                 /* collect all parameters */
230                 for (i = 0, calls = call; calls != NULL;
231                      calls = (ir_node*)get_irn_link(calls)) {
232                         call_params[i] = get_Call_param_arr(calls);
233                         ++i;
234                 }
235
236                 /* build new Proj's and Phi's */
237                 args    = get_irg_args(irg);
238                 for (i = 0; i < n_params; ++i) {
239                         ir_mode *mode = get_type_mode(get_method_param_type(method_tp, i));
240
241                         in[0] = new_r_Proj(args, mode, i);
242                         for (j = 0; j < env->n_tail_calls; ++j)
243                                 in[j + 1] = call_params[j][i];
244
245                         phis[i + 1] = new_r_Phi(block, env->n_tail_calls + 1, in, mode);
246                 }
247         }
248
249         /*
250          * ok, we are here, so we have build and collected all needed Phi's
251          * now exchange all Projs into links to Phi
252          */
253         exchange(data.proj_m, phis[0]);
254         for (p = data.proj_data; p; p = n) {
255                 long proj = get_Proj_proj(p);
256
257                 assert(0 <= proj && proj < n_params);
258                 n = (ir_node*)get_irn_link(p);
259                 exchange(p, phis[proj + 1]);
260         }
261
262         /* tail recursion was done, all info is invalid */
263         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
264                            | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
265         set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
266
267         set_optimize(rem);
268
269         /* check if we need new values */
270         n_locs = 0;
271         for (i = 0; i < env->n_ress; ++i) {
272                 if (env->variants[i] != TR_DIRECT) {
273                         ++n_locs;
274                         break;
275                 }
276         }
277
278         if (n_locs > 0) {
279                 ir_node *start_block;
280                 ir_node **in;
281                 ir_mode **modes;
282
283                 NEW_ARR_A(ir_node *, in, env->n_ress);
284                 NEW_ARR_A(ir_mode *, modes, env->n_ress);
285                 ssa_cons_start(irg, env->n_ress);
286
287                 start_block = get_irg_start_block(irg);
288                 set_r_cur_block(irg, start_block);
289
290                 /* set the neutral elements for the iteration start */
291                 for (i = 0; i < env->n_ress; ++i) {
292                         ir_type *tp = get_method_res_type(method_tp, i);
293                         ir_mode *mode = get_type_mode(tp);
294
295                         modes[i] = mode;
296                         if (env->variants[i] == TR_ADD) {
297                                 set_r_value(irg, i, new_r_Const(irg, get_mode_null(mode)));
298                         } else if (env->variants[i] == TR_MUL) {
299                                 set_r_value(irg, i, new_r_Const(irg, get_mode_one(mode)));
300                         }
301                 }
302                 mature_immBlock(start_block);
303
304                 /* no: we can kill all returns */
305                 for (p = env->rets; p; p = n) {
306                         ir_node *block = get_nodes_block(p);
307                         ir_node *call, *mem, *jmp, *tuple;
308
309                         set_r_cur_block(irg, block);
310                         n = (ir_node*)get_irn_link(p);
311
312                         call = skip_Proj(get_Return_mem(p));
313                         assert(is_Call(call));
314
315                         mem = get_Call_mem(call);
316
317                         /* create a new jump, free of CSE */
318                         set_optimize(0);
319                         jmp = new_r_Jmp(block);
320                         set_optimize(rem);
321
322                         for (i = 0; i < env->n_ress; ++i) {
323                                 ir_mode *mode = modes[i];
324                                 if (env->variants[i] != TR_DIRECT) {
325                                         in[i] = get_r_value(irg, i, mode);
326                                 } else {
327                                         in[i] = new_r_Bad(irg, mode);
328                                 }
329                         }
330                         /* create a new tuple for the return values */
331                         tuple = new_r_Tuple(block, env->n_ress, in);
332
333                         turn_into_tuple(call, pn_Call_max+1);
334                         set_Tuple_pred(call, pn_Call_M,         mem);
335                         set_Tuple_pred(call, pn_Call_X_regular, jmp);
336                         set_Tuple_pred(call, pn_Call_X_except,  new_r_Bad(irg, mode_X));
337                         set_Tuple_pred(call, pn_Call_T_result,  tuple);
338
339                         for (i = 0; i < env->n_ress; ++i) {
340                                 ir_node *res = get_Return_res(p, i);
341                                 if (env->variants[i] != TR_DIRECT) {
342                                         set_r_value(irg, i, res);
343                                 }
344                         }
345
346                         exchange(p, new_r_Bad(irg, mode_X));
347                 }
348
349                 /* finally fix all other returns */
350                 end_block = get_irg_end_block(irg);
351                 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
352                         ir_node *ret = get_Block_cfgpred(end_block, i);
353                         ir_node *block;
354
355                         /* search all Returns of a block */
356                         if (! is_Return(ret))
357                                 continue;
358
359                         block = get_nodes_block(ret);
360                         set_r_cur_block(irg, block);
361                         for (j = 0; j < env->n_ress; ++j) {
362                                 ir_node *pred = get_Return_res(ret, j);
363                                 ir_node *n;
364
365                                 switch (env->variants[j]) {
366                                 case TR_DIRECT:
367                                         continue;
368
369                                 case TR_ADD:
370                                         n = get_r_value(irg, j, modes[j]);
371                                         n = new_r_Add(block, n, pred, modes[j]);
372                                         set_Return_res(ret, j, n);
373                                         break;
374
375                                 case TR_MUL:
376                                         n = get_r_value(irg, j, modes[j]);
377                                         n = new_r_Mul(block, n, pred, modes[j]);
378                                         set_Return_res(ret, j, n);
379                                         break;
380
381                                 default:
382                                         assert(!"unexpected tail recursion variant");
383                                 }
384                         }
385                 }
386                 ssa_cons_finish(irg);
387         } else {
388                 ir_node *bad = new_r_Bad(irg, mode_X);
389
390                 /* no: we can kill all returns */
391                 for (p = env->rets; p; p = n) {
392                         n = (ir_node*)get_irn_link(p);
393                         exchange(p, bad);
394                 }
395         }
396 }
397
398 /**
399  * Check the lifetime of locals in the given graph.
400  * Tail recursion can only be done, if we can prove that
401  * the lifetime of locals end with the recursive call.
402  * We do this by checking that no address of a local variable is
403  * stored or transmitted as an argument to a call.
404  *
405  * @return non-zero if it's ok to do tail recursion
406  */
407 static int check_lifetime_of_locals(ir_graph *irg)
408 {
409         ir_node *irg_frame;
410         int i;
411         ir_type *frame_tp = get_irg_frame_type(irg);
412
413         irg_frame = get_irg_frame(irg);
414         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
415                 ir_node *succ = get_irn_out(irg_frame, i);
416
417                 if (is_Sel(succ)) {
418                         /* Check if we have compound arguments.
419                            For now, we cannot handle them, */
420                         if (get_entity_owner(get_Sel_entity(succ)) != frame_tp)
421                                 return 0;
422
423                         if (is_address_taken(succ))
424                                 return 0;
425                 }
426         }
427         return 1;
428 }
429
430 /**
431  * Examine irn and detect the recursion variant.
432  */
433 static tail_rec_variants find_variant(ir_node *irn, ir_node *call)
434 {
435         ir_node           *a, *b;
436         tail_rec_variants va, vb, res;
437
438         if (skip_Proj(skip_Proj(irn)) == call) {
439                 /* found it */
440                 return TR_DIRECT;
441         }
442         switch (get_irn_opcode(irn)) {
443         case iro_Add:
444                 /* try additive */
445                 a = get_Add_left(irn);
446                 if (get_nodes_block(a) != get_nodes_block(call)) {
447                         /* we are outside, ignore */
448                         va = TR_UNKNOWN;
449                 } else {
450                         va = find_variant(a, call);
451                         if (va == TR_BAD)
452                                 return TR_BAD;
453                 }
454                 b = get_Add_right(irn);
455                 if (get_nodes_block(b) != get_nodes_block(call)) {
456                         /* we are outside, ignore */
457                         vb = TR_UNKNOWN;
458                 } else {
459                         vb = find_variant(b, call);
460                         if (vb == TR_BAD)
461                                 return TR_BAD;
462                 }
463                 if (va == vb) {
464                         res = va;
465                 }
466                 else if (va == TR_UNKNOWN)
467                         res = vb;
468                 else if (vb == TR_UNKNOWN)
469                         res = va;
470                 else {
471                         /* they are different but none is TR_UNKNOWN -> incompatible */
472                         return TR_BAD;
473                 }
474                 if (res == TR_DIRECT || res == TR_ADD)
475                         return TR_ADD;
476                 /* not compatible */
477                 return TR_BAD;
478
479         case iro_Sub:
480                 /* try additive, but return value must be left */
481                 a = get_Sub_left(irn);
482                 if (get_nodes_block(a) != get_nodes_block(call)) {
483                         /* we are outside, ignore */
484                         va = TR_UNKNOWN;
485                 } else {
486                         va = find_variant(a, call);
487                         if (va == TR_BAD)
488                                 return TR_BAD;
489                 }
490                 b = get_Sub_right(irn);
491                 if (get_nodes_block(b) != get_nodes_block(call)) {
492                         /* we are outside, ignore */
493                         vb = TR_UNKNOWN;
494                 } else {
495                         vb = find_variant(b, call);
496                         if (vb != TR_UNKNOWN)
497                                 return TR_BAD;
498                 }
499                 res = va;
500                 if (res == TR_DIRECT || res == TR_ADD)
501                         return res;
502                 /* not compatible */
503                 return TR_BAD;
504
505         case iro_Mul:
506                 /* try multiplicative */
507                 a = get_Mul_left(irn);
508                 if (get_nodes_block(a) != get_nodes_block(call)) {
509                         /* we are outside, ignore */
510                         va = TR_UNKNOWN;
511                 } else {
512                         va = find_variant(a, call);
513                         if (va == TR_BAD)
514                                 return TR_BAD;
515                 }
516                 b = get_Mul_right(irn);
517                 if (get_nodes_block(b) != get_nodes_block(call)) {
518                         /* we are outside, ignore */
519                         vb = TR_UNKNOWN;
520                 } else {
521                         vb = find_variant(b, call);
522                         if (vb == TR_BAD)
523                                 return TR_BAD;
524                 }
525                 if (va == vb) {
526                         res = va;
527                 }
528                 else if (va == TR_UNKNOWN)
529                         res = vb;
530                 else if (vb == TR_UNKNOWN)
531                         res = va;
532                 else {
533                         /* they are different but none is TR_UNKNOWN -> incompatible */
534                         return TR_BAD;
535                 }
536                 if (res == TR_DIRECT || res == TR_MUL)
537                         return TR_MUL;
538                 /* not compatible */
539                 return TR_BAD;
540
541         case iro_Minus:
542                 /* try multiplicative */
543                 a = get_Minus_op(irn);
544                 res =  find_variant(a, call);
545                 if (res == TR_DIRECT)
546                         return TR_MUL;
547                 if (res == TR_MUL || res == TR_UNKNOWN)
548                         return res;
549                 /* not compatible */
550                 return TR_BAD;
551
552         default:
553                 return TR_UNKNOWN;
554         }
555 }
556
557
558 /*
559  * convert simple tail-calls into loops
560  */
561 void opt_tail_rec_irg(ir_graph *irg)
562 {
563         tr_env    env;
564         ir_node   *end_block;
565         int       i, n_ress, n_tail_calls = 0;
566         ir_node   *rets = NULL;
567         ir_type   *mtd_type, *call_type;
568         ir_entity *ent;
569         ir_graph  *rem;
570
571         assure_irg_properties(irg,
572                 IR_GRAPH_PROPERTY_MANY_RETURNS
573                 | IR_GRAPH_PROPERTY_NO_BADS
574                 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
575
576         FIRM_DBG_REGISTER(dbg, "firm.opt.tailrec");
577
578         if (! check_lifetime_of_locals(irg)) {
579                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
580                 return;
581         }
582
583         rem = current_ir_graph;
584         current_ir_graph = irg;
585
586         ent      = get_irg_entity(irg);
587         mtd_type = get_entity_type(ent);
588         n_ress   = get_method_n_ress(mtd_type);
589
590         env.variants = NULL;
591         env.n_ress   = n_ress;
592
593         if (n_ress > 0) {
594                 NEW_ARR_A(tail_rec_variants, env.variants, n_ress);
595
596                 for (i = 0; i < n_ress; ++i)
597                         env.variants[i] = TR_DIRECT;
598         }
599
600         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
601
602         end_block = get_irg_end_block(irg);
603         set_irn_link(end_block, NULL);
604
605         for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
606                 ir_node *ret = get_Block_cfgpred(end_block, i);
607                 ir_node *call, *call_ptr;
608                 int j;
609                 ir_node **ress;
610
611                 /* search all Returns of a block */
612                 if (! is_Return(ret))
613                         continue;
614
615                 /* check, if it's a Return self() */
616                 call = skip_Proj(get_Return_mem(ret));
617                 if (! is_Call(call))
618                         continue;
619
620                 /* the call must be in the same block as the return */
621                 if (get_nodes_block(call) != get_nodes_block(ret))
622                         continue;
623
624                 /* check if it's a recursive call */
625                 call_ptr = get_Call_ptr(call);
626
627                 if (! is_SymConst_addr_ent(call_ptr))
628                         continue;
629
630                 ent = get_SymConst_entity(call_ptr);
631                 if (!ent || get_entity_irg(ent) != irg)
632                         continue;
633
634                 /*
635                  * Check, that the types match. At least in C
636                  * this might fail.
637                  */
638                 mtd_type  = get_entity_type(ent);
639                 call_type = get_Call_type(call);
640
641                 if (mtd_type != call_type) {
642                         /*
643                          * Hmm, the types did not match, bad.
644                          * This can happen in C when no prototype is given
645                          * or K&R style is used.
646                          */
647                         DB((dbg, LEVEL_3, "  tail recursion fails because of call type mismatch: %+F != %+F\n", mtd_type, call_type));
648                         continue;
649                 }
650
651                 /* ok, mem is routed to a recursive call, check return args */
652                 ress = get_Return_res_arr(ret);
653                 for (j = get_Return_n_ress(ret) - 1; j >= 0; --j) {
654                         tail_rec_variants var = find_variant(ress[j], call);
655
656                         if (var >= TR_BAD) {
657                                 /* cannot be transformed */
658                                 break;
659                         }
660                         if (var == TR_DIRECT) {
661                                 var = env.variants[j];
662                         } else if (env.variants[j] == TR_DIRECT) {
663                                 env.variants[j] = var;
664                         }
665                         if (env.variants[j] != var) {
666                                 /* not compatible */
667                                 DB((dbg, LEVEL_3, "  tail recursion fails for %d return value of %+F\n", j, ret));
668                                 break;
669                         }
670                 }
671                 if (j >= 0)
672                         continue;
673
674                 /* here, we have found a call */
675                 set_irn_link(call, get_irn_link(end_block));
676                 set_irn_link(end_block, call);
677                 ++n_tail_calls;
678
679                 /* link all returns, we will need this */
680                 set_irn_link(ret, rets);
681                 rets = ret;
682         }
683
684         /* now, end_block->link contains the list of all tail calls */
685         if (n_tail_calls > 0) {
686                 DB((dbg, LEVEL_2, "  Performing tail recursion for graph %s and %d Calls\n",
687                     get_entity_ld_name(get_irg_entity(irg)), n_tail_calls));
688
689                 hook_tail_rec(irg, n_tail_calls);
690
691                 env.n_tail_calls = n_tail_calls;
692                 env.rets         = rets;
693                 do_opt_tail_rec(irg, &env);
694                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
695         } else {
696                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
697         }
698         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
699         current_ir_graph = rem;
700 }
701
702 ir_graph_pass_t *opt_tail_rec_irg_pass(const char *name)
703 {
704         return def_graph_pass(name ? name : "tailrec", opt_tail_rec_irg);
705 }
706
707 /*
708  * optimize tail recursion away
709  */
710 void opt_tail_recursion(void)
711 {
712         size_t i, n;
713
714         FIRM_DBG_REGISTER(dbg, "firm.opt.tailrec");
715
716         DB((dbg, LEVEL_1, "Performing tail recursion ...\n"));
717         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
718                 ir_graph *irg = get_irp_irg(i);
719                 opt_tail_rec_irg(irg);
720         }
721 }
722
723 ir_prog_pass_t *opt_tail_recursion_pass(const char *name)
724 {
725         return def_prog_pass(name ? name : "tailrec", opt_tail_recursion);
726 }