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