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