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