cleanup: Remove pointless assert(is_${NODE}(x)) just before get_${NODE}_${FOO}(x...
[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 *jmp, *tuple;
308
309                         set_r_cur_block(irg, block);
310                         n = (ir_node*)get_irn_link(p);
311
312                         ir_node *const call = skip_Proj(get_Return_mem(p));
313                         ir_node *const mem  = get_Call_mem(call);
314
315                         /* create a new jump, free of CSE */
316                         set_optimize(0);
317                         jmp = new_r_Jmp(block);
318                         set_optimize(rem);
319
320                         for (i = 0; i < env->n_ress; ++i) {
321                                 ir_mode *mode = modes[i];
322                                 if (env->variants[i] != TR_DIRECT) {
323                                         in[i] = get_r_value(irg, i, mode);
324                                 } else {
325                                         in[i] = new_r_Bad(irg, mode);
326                                 }
327                         }
328                         /* create a new tuple for the return values */
329                         tuple = new_r_Tuple(block, env->n_ress, in);
330
331                         turn_into_tuple(call, pn_Call_max+1);
332                         set_Tuple_pred(call, pn_Call_M,         mem);
333                         set_Tuple_pred(call, pn_Call_X_regular, jmp);
334                         set_Tuple_pred(call, pn_Call_X_except,  new_r_Bad(irg, mode_X));
335                         set_Tuple_pred(call, pn_Call_T_result,  tuple);
336
337                         for (i = 0; i < env->n_ress; ++i) {
338                                 ir_node *res = get_Return_res(p, i);
339                                 if (env->variants[i] != TR_DIRECT) {
340                                         set_r_value(irg, i, res);
341                                 }
342                         }
343
344                         exchange(p, new_r_Bad(irg, mode_X));
345                 }
346
347                 /* finally fix all other returns */
348                 end_block = get_irg_end_block(irg);
349                 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
350                         ir_node *ret = get_Block_cfgpred(end_block, i);
351                         ir_node *block;
352
353                         /* search all Returns of a block */
354                         if (! is_Return(ret))
355                                 continue;
356
357                         block = get_nodes_block(ret);
358                         set_r_cur_block(irg, block);
359                         for (j = 0; j < env->n_ress; ++j) {
360                                 ir_node *pred = get_Return_res(ret, j);
361                                 ir_node *n;
362
363                                 switch (env->variants[j]) {
364                                 case TR_DIRECT:
365                                         continue;
366
367                                 case TR_ADD:
368                                         n = get_r_value(irg, j, modes[j]);
369                                         n = new_r_Add(block, n, pred, modes[j]);
370                                         set_Return_res(ret, j, n);
371                                         break;
372
373                                 case TR_MUL:
374                                         n = get_r_value(irg, j, modes[j]);
375                                         n = new_r_Mul(block, n, pred, modes[j]);
376                                         set_Return_res(ret, j, n);
377                                         break;
378
379                                 default:
380                                         assert(!"unexpected tail recursion variant");
381                                 }
382                         }
383                 }
384                 ssa_cons_finish(irg);
385         } else {
386                 ir_node *bad = new_r_Bad(irg, mode_X);
387
388                 /* no: we can kill all returns */
389                 for (p = env->rets; p; p = n) {
390                         n = (ir_node*)get_irn_link(p);
391                         exchange(p, bad);
392                 }
393         }
394 }
395
396 /**
397  * Check the lifetime of locals in the given graph.
398  * Tail recursion can only be done, if we can prove that
399  * the lifetime of locals end with the recursive call.
400  * We do this by checking that no address of a local variable is
401  * stored or transmitted as an argument to a call.
402  *
403  * @return non-zero if it's ok to do tail recursion
404  */
405 static int check_lifetime_of_locals(ir_graph *irg)
406 {
407         ir_node *irg_frame;
408         int i;
409         ir_type *frame_tp = get_irg_frame_type(irg);
410
411         irg_frame = get_irg_frame(irg);
412         for (i = get_irn_n_outs(irg_frame) - 1; i >= 0; --i) {
413                 ir_node *succ = get_irn_out(irg_frame, i);
414
415                 if (is_Sel(succ)) {
416                         /* Check if we have compound arguments.
417                            For now, we cannot handle them, */
418                         if (get_entity_owner(get_Sel_entity(succ)) != frame_tp)
419                                 return 0;
420
421                         if (is_address_taken(succ))
422                                 return 0;
423                 }
424         }
425         return 1;
426 }
427
428 /**
429  * Examine irn and detect the recursion variant.
430  */
431 static tail_rec_variants find_variant(ir_node *irn, ir_node *call)
432 {
433         ir_node           *a, *b;
434         tail_rec_variants va, vb, res;
435
436         if (skip_Proj(skip_Proj(irn)) == call) {
437                 /* found it */
438                 return TR_DIRECT;
439         }
440         switch (get_irn_opcode(irn)) {
441         case iro_Add:
442                 /* try additive */
443                 a = get_Add_left(irn);
444                 if (get_nodes_block(a) != get_nodes_block(call)) {
445                         /* we are outside, ignore */
446                         va = TR_UNKNOWN;
447                 } else {
448                         va = find_variant(a, call);
449                         if (va == TR_BAD)
450                                 return TR_BAD;
451                 }
452                 b = get_Add_right(irn);
453                 if (get_nodes_block(b) != get_nodes_block(call)) {
454                         /* we are outside, ignore */
455                         vb = TR_UNKNOWN;
456                 } else {
457                         vb = find_variant(b, call);
458                         if (vb == TR_BAD)
459                                 return TR_BAD;
460                 }
461                 if (va == vb) {
462                         res = va;
463                 }
464                 else if (va == TR_UNKNOWN)
465                         res = vb;
466                 else if (vb == TR_UNKNOWN)
467                         res = va;
468                 else {
469                         /* they are different but none is TR_UNKNOWN -> incompatible */
470                         return TR_BAD;
471                 }
472                 if (res == TR_DIRECT || res == TR_ADD)
473                         return TR_ADD;
474                 /* not compatible */
475                 return TR_BAD;
476
477         case iro_Sub:
478                 /* try additive, but return value must be left */
479                 a = get_Sub_left(irn);
480                 if (get_nodes_block(a) != get_nodes_block(call)) {
481                         /* we are outside, ignore */
482                         va = TR_UNKNOWN;
483                 } else {
484                         va = find_variant(a, call);
485                         if (va == TR_BAD)
486                                 return TR_BAD;
487                 }
488                 b = get_Sub_right(irn);
489                 if (get_nodes_block(b) != get_nodes_block(call)) {
490                         /* we are outside, ignore */
491                         vb = TR_UNKNOWN;
492                 } else {
493                         vb = find_variant(b, call);
494                         if (vb != TR_UNKNOWN)
495                                 return TR_BAD;
496                 }
497                 res = va;
498                 if (res == TR_DIRECT || res == TR_ADD)
499                         return res;
500                 /* not compatible */
501                 return TR_BAD;
502
503         case iro_Mul:
504                 /* try multiplicative */
505                 a = get_Mul_left(irn);
506                 if (get_nodes_block(a) != get_nodes_block(call)) {
507                         /* we are outside, ignore */
508                         va = TR_UNKNOWN;
509                 } else {
510                         va = find_variant(a, call);
511                         if (va == TR_BAD)
512                                 return TR_BAD;
513                 }
514                 b = get_Mul_right(irn);
515                 if (get_nodes_block(b) != get_nodes_block(call)) {
516                         /* we are outside, ignore */
517                         vb = TR_UNKNOWN;
518                 } else {
519                         vb = find_variant(b, call);
520                         if (vb == TR_BAD)
521                                 return TR_BAD;
522                 }
523                 if (va == vb) {
524                         res = va;
525                 }
526                 else if (va == TR_UNKNOWN)
527                         res = vb;
528                 else if (vb == TR_UNKNOWN)
529                         res = va;
530                 else {
531                         /* they are different but none is TR_UNKNOWN -> incompatible */
532                         return TR_BAD;
533                 }
534                 if (res == TR_DIRECT || res == TR_MUL)
535                         return TR_MUL;
536                 /* not compatible */
537                 return TR_BAD;
538
539         case iro_Minus:
540                 /* try multiplicative */
541                 a = get_Minus_op(irn);
542                 res =  find_variant(a, call);
543                 if (res == TR_DIRECT)
544                         return TR_MUL;
545                 if (res == TR_MUL || res == TR_UNKNOWN)
546                         return res;
547                 /* not compatible */
548                 return TR_BAD;
549
550         default:
551                 return TR_UNKNOWN;
552         }
553 }
554
555
556 /*
557  * convert simple tail-calls into loops
558  */
559 void opt_tail_rec_irg(ir_graph *irg)
560 {
561         tr_env    env;
562         ir_node   *end_block;
563         int       i, n_ress, n_tail_calls = 0;
564         ir_node   *rets = NULL;
565         ir_type   *mtd_type, *call_type;
566         ir_entity *ent;
567         ir_graph  *rem;
568
569         assure_irg_properties(irg,
570                 IR_GRAPH_PROPERTY_MANY_RETURNS
571                 | IR_GRAPH_PROPERTY_NO_BADS
572                 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
573
574         FIRM_DBG_REGISTER(dbg, "firm.opt.tailrec");
575
576         if (! check_lifetime_of_locals(irg)) {
577                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
578                 return;
579         }
580
581         rem = current_ir_graph;
582         current_ir_graph = irg;
583
584         ent      = get_irg_entity(irg);
585         mtd_type = get_entity_type(ent);
586         n_ress   = get_method_n_ress(mtd_type);
587
588         env.variants = NULL;
589         env.n_ress   = n_ress;
590
591         if (n_ress > 0) {
592                 NEW_ARR_A(tail_rec_variants, env.variants, n_ress);
593
594                 for (i = 0; i < n_ress; ++i)
595                         env.variants[i] = TR_DIRECT;
596         }
597
598         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
599
600         end_block = get_irg_end_block(irg);
601         set_irn_link(end_block, NULL);
602
603         for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
604                 ir_node *ret = get_Block_cfgpred(end_block, i);
605                 ir_node *call, *call_ptr;
606                 int j;
607                 ir_node **ress;
608
609                 /* search all Returns of a block */
610                 if (! is_Return(ret))
611                         continue;
612
613                 /* check, if it's a Return self() */
614                 call = skip_Proj(get_Return_mem(ret));
615                 if (! is_Call(call))
616                         continue;
617
618                 /* the call must be in the same block as the return */
619                 if (get_nodes_block(call) != get_nodes_block(ret))
620                         continue;
621
622                 /* check if it's a recursive call */
623                 call_ptr = get_Call_ptr(call);
624
625                 if (! is_SymConst_addr_ent(call_ptr))
626                         continue;
627
628                 ent = get_SymConst_entity(call_ptr);
629                 if (!ent || get_entity_irg(ent) != irg)
630                         continue;
631
632                 /*
633                  * Check, that the types match. At least in C
634                  * this might fail.
635                  */
636                 mtd_type  = get_entity_type(ent);
637                 call_type = get_Call_type(call);
638
639                 if (mtd_type != call_type) {
640                         /*
641                          * Hmm, the types did not match, bad.
642                          * This can happen in C when no prototype is given
643                          * or K&R style is used.
644                          */
645                         DB((dbg, LEVEL_3, "  tail recursion fails because of call type mismatch: %+F != %+F\n", mtd_type, call_type));
646                         continue;
647                 }
648
649                 /* ok, mem is routed to a recursive call, check return args */
650                 ress = get_Return_res_arr(ret);
651                 for (j = get_Return_n_ress(ret) - 1; j >= 0; --j) {
652                         tail_rec_variants var = find_variant(ress[j], call);
653
654                         if (var >= TR_BAD) {
655                                 /* cannot be transformed */
656                                 break;
657                         }
658                         if (var == TR_DIRECT) {
659                                 var = env.variants[j];
660                         } else if (env.variants[j] == TR_DIRECT) {
661                                 env.variants[j] = var;
662                         }
663                         if (env.variants[j] != var) {
664                                 /* not compatible */
665                                 DB((dbg, LEVEL_3, "  tail recursion fails for %d return value of %+F\n", j, ret));
666                                 break;
667                         }
668                 }
669                 if (j >= 0)
670                         continue;
671
672                 /* here, we have found a call */
673                 set_irn_link(call, get_irn_link(end_block));
674                 set_irn_link(end_block, call);
675                 ++n_tail_calls;
676
677                 /* link all returns, we will need this */
678                 set_irn_link(ret, rets);
679                 rets = ret;
680         }
681
682         /* now, end_block->link contains the list of all tail calls */
683         if (n_tail_calls > 0) {
684                 DB((dbg, LEVEL_2, "  Performing tail recursion for graph %s and %d Calls\n",
685                     get_entity_ld_name(get_irg_entity(irg)), n_tail_calls));
686
687                 hook_tail_rec(irg, n_tail_calls);
688
689                 env.n_tail_calls = n_tail_calls;
690                 env.rets         = rets;
691                 do_opt_tail_rec(irg, &env);
692                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
693         } else {
694                 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
695         }
696         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
697         current_ir_graph = rem;
698 }
699
700 ir_graph_pass_t *opt_tail_rec_irg_pass(const char *name)
701 {
702         return def_graph_pass(name ? name : "tailrec", opt_tail_rec_irg);
703 }
704
705 /*
706  * optimize tail recursion away
707  */
708 void opt_tail_recursion(void)
709 {
710         size_t i, n;
711
712         FIRM_DBG_REGISTER(dbg, "firm.opt.tailrec");
713
714         DB((dbg, LEVEL_1, "Performing tail recursion ...\n"));
715         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
716                 ir_graph *irg = get_irp_irg(i);
717                 opt_tail_rec_irg(irg);
718         }
719 }
720
721 ir_prog_pass_t *opt_tail_recursion_pass(const char *name)
722 {
723         return def_prog_pass(name ? name : "tailrec", opt_tail_recursion);
724 }