besched: Use sched_foreach_{after,reverse_before}().
[libfirm] / ir / ana / cgana.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief      Intraprozedural analyses to estimate the call graph.
9  * @author     Hubert Schmid
10  * @date       09.06.2002
11  * @brief
12  *  Interprocedural analysis to estimate the calling relation.
13  *
14  *  This analysis computes all entities representing methods that
15  *  can be called at a Call node.  Further it computes a set of
16  *  methods that are 'free', i.e., their adress is handled by
17  *  the program directly, or they are visible external.
18  */
19 #include "config.h"
20
21 #include <string.h>
22
23 #include "cgana.h"
24
25 #include "xmalloc.h"
26 #include "irnode_t.h"
27 #include "irmode_t.h"
28 #include "irprog_t.h"
29 #include "irgwalk.h"
30 #include "ircons.h"
31 #include "irgmod.h"
32 #include "iropt.h"
33 #include "irtools.h"
34
35 #include "irflag_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
38
39 #include "pmap.h"
40 #include "array.h"
41 #include "error.h"
42
43 #include "irdump.h"
44
45 /* unambiguous address used as a mark. */
46 static void *MARK = &MARK;
47
48 static pset *entities = NULL;
49
50 /*--------------------------------------------------------------------------*/
51 /* The analysis                                                             */
52 /*--------------------------------------------------------------------------*/
53
54
55 /*--------------------------------------------------------------------------*/
56 /* Initialize datastructures, remove unwanted constructs, optimize          */
57 /* call target computations.                                                */
58 /*--------------------------------------------------------------------------*/
59
60 /** Collect the entity representing the implementation of this
61  *  method (not the same if inherited) and all entities for overwriting
62  *  implementations in parameter set.
63  *  A recursive descend in the overwritten relation.
64  *  Cycle-free, therefore must terminate.
65  *
66  * @param method   the overwritten method
67  * @param set      A set of entities.
68  *
69  * @return Number of entities in set.
70  */
71 static size_t collect_impls(ir_entity *method, pset *set)
72 {
73         size_t i;
74         size_t size = 0;
75
76         if (get_entity_irg(method) != NULL) {
77                 /* has an implementation */
78                 pset_insert_ptr(set, method);
79                 ++size;
80         }
81
82         /*- recursive descent -*/
83         for (i = get_entity_n_overwrittenby(method); i > 0;)
84                 size += collect_impls(get_entity_overwrittenby(method, --i), set);
85         return size;
86 }
87
88 /**
89  * Determine all methods that overwrite the given method (and implement it).
90  * The returned array must be freed by the caller (see DEL_ARR_F).
91  * If the set of overwriting methods is empty, returns NULL.
92  *
93  * @param method  the method
94  */
95 static ir_entity **get_impl_methods(ir_entity *method)
96 {
97         ir_entity **arr;
98         pset      *set = pset_new_ptr_default();
99         size_t    size;
100
101         /* Collect all method entities that can be called here */
102         size = collect_impls(method, set);
103
104         if (size == 0) {
105                 /* no overwriting methods found */
106                 arr = NULL;
107         } else {
108                 arr = NEW_ARR_F(ir_entity *, size);
109                 foreach_pset(set, ir_entity, ent) {
110                         arr[--size] = ent;
111                 }
112         }
113         del_pset(set);
114         return arr;
115 }
116
117 /** Analyze address computations.
118  *
119  *  Compute for all Sel nodes the set of methods that can be selected.
120  *  For each entity we store the set of subentities in the link field.
121  *
122  *  Further do some optimizations:
123  *  - Call standard optimizations for Sel nodes: this removes polymorphic
124  *    calls.
125  *  - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
126  *    For this we precomputed a map name->entity.  Nowadays, we no more support
127  *    this and assert.
128  *  - If the node is a Sel:
129  *    If we found only a single method that can be called, replace the Sel
130  *    by a SymConst.  This is more powerful than the analysis in opt_polymorphy,
131  *    as here we walk the type graph.  In opt_polymorphy we only apply a local
132  *    pattern.
133  *
134  *  @param node  The node to analyze
135  *  @param env   A map that maps names of entities to the entities.
136  */
137 static void sel_methods_walker(ir_node *node, void *env)
138 {
139         ir_entity **arr;
140         (void)env;
141
142         /* Call standard optimizations */
143         if (is_Sel(node)) {
144                 ir_node *new_node = optimize_in_place(node);
145                 if (node != new_node) {
146                         exchange(node, new_node);
147                         node = new_node;
148                 }
149         }
150
151         if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
152                 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
153
154                 if (!pset_find_ptr(entities, ent)) {
155                         /* Entity not yet handled. Find all (internal or external)
156                          * implemented methods that overwrites this entity.
157                          * This set is stored in the entity link. */
158                         set_entity_link(ent, get_impl_methods(ent));
159                         pset_insert_ptr(entities, ent);
160                 }
161
162                 /* -- As an add on we get an optimization that removes polymorphic calls.
163                 This optimization is more powerful than that in transform_node_Sel().  -- */
164                 arr = (ir_entity**) get_entity_link(ent);
165                 if (arr == NULL) {
166                         /*
167                          * The Sel node never returns a pointer to a usable method.
168                          * We could not call it, but it may be description:
169                          * We call a method in a dead part of the program.
170                          */
171                         assert(get_entity_irg(ent) == NULL);
172                 }
173         }
174 }
175
176 /**
177  * Initialize auxiliary data structures.
178  *
179  * Computes a set of entities that overwrite an entity and contain
180  * an implementation. The set is stored in the entity's link field.
181  *
182  * Further replaces Sel nodes where this set contains exactly one
183  * method by SymConst nodes.
184  * Finally asserts if there is a SymConst(name) if there could be a
185  * SymConst(ent).
186  */
187 static void sel_methods_init(void)
188 {
189         size_t i, n;
190         pmap *ldname_map = pmap_create();   /* Map entity names to entities: to replace
191                                                SymConst(name) by SymConst(ent). */
192         assert(entities == NULL);
193         entities = pset_new_ptr_default();
194         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
195                 ir_entity * ent = get_irg_entity(get_irp_irg(i));
196                 /* only external visible methods are allowed to call by a SymConst_ptr_name */
197                 if (entity_is_externally_visible(ent)) {
198                         pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
199                 }
200         }
201
202         all_irg_walk(sel_methods_walker, NULL, NULL);
203         pmap_destroy(ldname_map);
204 }
205
206 /*--------------------------------------------------------------------------*/
207 /* Find free methods.
208  *
209  * We expect that each entity has an array with all implementations in its
210  * link field.                                                              */
211 /*--------------------------------------------------------------------------*/
212
213 /**
214  * Returns an array of all methods that could be called at a Sel node.
215  * This array contains every entry only once.
216  *
217  * @param sel  the Sel node
218  */
219 static ir_entity ** get_Sel_arr(ir_node * sel)
220 {
221         static ir_entity ** NULL_ARRAY = NULL;
222
223         ir_entity *const ent = get_Sel_entity(sel);
224         assert(is_Method_type(get_entity_type(ent))); /* what else? */
225
226         ir_entity **const arr = (ir_entity**)get_entity_link(ent);
227         if (arr) {
228                 return arr;
229         } else {
230                 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
231                  * kann f�r polymorphe (abstrakte) Methoden passieren. */
232                 if (!NULL_ARRAY) {
233                         NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
234                 }
235                 return NULL_ARRAY;
236         }
237 }
238
239 /**
240  * Returns the number of possible called methods at a Sel node.
241  *
242  * @param sel  the Sel node
243  */
244 static size_t get_Sel_n_methods(ir_node * sel)
245 {
246         return ARR_LEN(get_Sel_arr(sel));
247 }
248
249 /**
250  * Returns the ith possible called method entity at a Sel node.
251  */
252 static ir_entity * get_Sel_method(ir_node * sel, size_t pos)
253 {
254         ir_entity ** arr = get_Sel_arr(sel);
255         assert(pos < ARR_LEN(arr));
256         return arr[pos];
257 }
258
259 /* forward */
260 static void free_mark(ir_node *node, pset *set);
261
262 static void free_mark_proj(ir_node *node, long n, pset *set)
263 {
264         assert(get_irn_mode(node) == mode_T);
265         if (get_irn_link(node) == MARK) {
266                 /* already visited */
267                 return;
268         }
269         set_irn_link(node, MARK);
270         switch (get_irn_opcode(node)) {
271         case iro_Proj: {
272                 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
273                  * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
274                  * wird. */
275                 ir_node * pred = get_Proj_pred(node);
276                 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
277                         free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
278                 } else {
279                         /* nothing: da in "free_ana_walker" behandelt. */
280                 }
281                 break;
282         }
283
284         case iro_Tuple:
285                 free_mark(get_Tuple_pred(node, n), set);
286                 break;
287
288         case iro_Id:
289                 free_mark_proj(get_Id_pred(node), n, set);
290                 break;
291
292         case iro_Start:
293         case iro_Alloc:
294         case iro_Load:
295                 /* nothing: Die Operationen werden in free_ana_walker() selbst
296                  * behandelt. */
297                 break;
298
299         default:
300                 assert(0 && "unexpected opcode or opcode not implemented");
301                 break;
302         }
303         // set_irn_link(node, NULL);
304 }
305
306 /**
307  * Called for predecessors nodes of "interesting" ones.
308  * Interesting ones include all nodes that can somehow make
309  * a method visible.
310  *
311  * If a method (or a set of methods in case of polymorph calls) gets visible,
312  * add it to the set of 'free' methods
313  *
314  * @param node  the current visited node
315  * @param set   the set of all free methods
316  */
317 static void free_mark(ir_node *node, pset *set)
318 {
319         if (get_irn_link(node) == MARK)
320                 return; /* already visited */
321
322         set_irn_link(node, MARK);
323
324         switch (get_irn_opcode(node)) {
325         case iro_Sel: {
326                 ir_entity *ent = get_Sel_entity(node);
327                 if (is_method_entity(ent)) {
328                         size_t i, n;
329                         for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
330                                 pset_insert_ptr(set, get_Sel_method(node, i));
331                         }
332                 }
333                 break;
334         }
335         case iro_SymConst:
336                 if (get_SymConst_kind(node) == symconst_addr_ent) {
337                         ir_entity *ent = get_SymConst_entity(node);
338                         if (is_method_entity(ent)) {
339                                 pset_insert_ptr(set, ent);
340                         }
341                 }
342                 break;
343
344         case iro_Phi:
345         {
346                 int i, n;
347                 for (i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
348                         free_mark(get_Phi_pred(node, i), set);
349                 }
350                 break;
351         }
352         case iro_Proj:
353                 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
354                 break;
355         default:
356                 /* nothing: */
357                 break;
358         }
359 }
360
361 /**
362  * post-walker. Find method addresses.
363  */
364 static void free_ana_walker(ir_node *node, void *env)
365 {
366         pset *set = (pset*) env;
367
368         if (get_irn_link(node) == MARK) {
369                 /* already visited */
370                 return;
371         }
372         switch (get_irn_opcode(node)) {
373                 /* special nodes */
374         case iro_Sel:
375         case iro_SymConst:
376         case iro_Const:
377         case iro_Phi:
378         case iro_Id:
379         case iro_Proj:
380         case iro_Tuple:
381                 /* nothing */
382                 break;
383         case iro_Call:
384         {
385                 size_t i, n;
386                 /* we must handle Call nodes specially, because their call address input
387                    do not expose a method address. */
388                 set_irn_link(node, MARK);
389                 for (i = 0, n = get_Call_n_params(node); i < n; ++i) {
390                         ir_node *pred = get_Call_param(node, i);
391                         if (mode_is_reference(get_irn_mode(pred))) {
392                                 free_mark(pred, set);
393                         }
394                 }
395                 break;
396         }
397         default: {
398                 int i;
399                 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
400                  * jemand das Gegenteil implementiert. */
401                 set_irn_link(node, MARK);
402                 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
403                         ir_node *pred = get_irn_n(node, i);
404                         if (mode_is_reference(get_irn_mode(pred))) {
405                                 free_mark(pred, set);
406                         }
407                 }
408                 break;
409         }
410         }
411 }
412
413 /**
414  * Add all method addresses in global new style initializers to the set.
415  *
416  * @note
417  * We do NOT check the type here, just if it's an entity address.
418  * The reason for this is code like:
419  *
420  * void *p = function;
421  *
422  * which is sometimes used to anchor functions.
423  */
424 static void add_method_address_inititializer(ir_initializer_t *initializer,
425                                              pset *set)
426 {
427         ir_node *n;
428         size_t  i;
429
430         switch (initializer->kind) {
431         case IR_INITIALIZER_CONST:
432                 n = initializer->consti.value;
433
434                 /* let's check if it's the address of a function */
435                 if (is_SymConst_addr_ent(n)) {
436                         ir_entity *ent = get_SymConst_entity(n);
437
438                         if (is_Method_type(get_entity_type(ent)))
439                                 pset_insert_ptr(set, ent);
440                 }
441                 return;
442         case IR_INITIALIZER_TARVAL:
443         case IR_INITIALIZER_NULL:
444                 return;
445         case IR_INITIALIZER_COMPOUND:
446                 for (i = 0; i < initializer->compound.n_initializers; ++i) {
447                         ir_initializer_t *sub_initializer
448                                 = initializer->compound.initializers[i];
449                         add_method_address_inititializer(sub_initializer, set);
450                 }
451                 return;
452         }
453         panic("invalid initializer found");
454 }
455
456 /**
457  * Add all method addresses in global initializers to the set.
458  *
459  * @note
460  * We do NOT check the type here, just if it's an entity address.
461  * The reason for this is code like:
462  *
463  * void *p = function;
464  *
465  * which is sometimes used to anchor functions.
466  */
467 static void add_method_address(ir_entity *ent, pset *set)
468 {
469         ir_type *tp;
470
471         /* ignore methods: these of course reference their addresses
472          * TODO: remove this later once this incorrect self-initialisation is gone
473          */
474         tp = get_entity_type(ent);
475         if (is_Method_type(tp))
476                 return;
477
478         if (ent->initializer != NULL) {
479                 add_method_address_inititializer(get_entity_initializer(ent), set);
480         }
481 }
482
483 /**
484  * returns a list of 'free' methods, i.e., the methods that can be called
485  * from external or via function pointers.
486  *
487  * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
488  * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
489  * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
490  * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
491  * auf eine echt externe Methode.
492  */
493 static size_t get_free_methods(ir_entity ***free_methods)
494 {
495         pset *free_set = pset_new_ptr_default();
496         size_t i, n, j, m;
497         ir_entity **arr;
498         ir_graph *irg;
499         ir_type *tp;
500         size_t length;
501
502         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
503                 ir_linkage linkage;
504                 irg = get_irp_irg(i);
505                 ir_entity *const ent = get_irg_entity(irg);
506                 linkage = get_entity_linkage(ent);
507
508                 if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
509                         pset_insert_ptr(free_set, ent);
510                 }
511
512                 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
513                 /* Find all method entities that gets "visible" through this graphs,
514                  * for instance because their address is stored. */
515                 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
516                 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
517         }
518
519         /* insert all methods that are used in global variables initializers */
520         tp = get_glob_type();
521         for (j = 0, m = get_class_n_members(tp); j < m; ++j) {
522                 ir_entity *const ent = get_class_member(tp, j);
523                 add_method_address(ent, free_set);
524         }
525         tp = get_tls_type();
526         for (j = 0, m = get_compound_n_members(tp); j < m; ++j) {
527                 ir_entity *const ent = get_compound_member(tp, j);
528                 add_method_address(ent, free_set);
529         }
530
531         /* the main program is even then "free", if it's not external visible. */
532         irg = get_irp_main_irg();
533         if (irg != NULL)
534                 pset_insert_ptr(free_set, get_irg_entity(irg));
535
536         /* Finally, transform the set into an array. */
537         length = pset_count(free_set);
538         arr = XMALLOCN(ir_entity*, length);
539         i = 0;
540         foreach_pset(free_set, ir_entity, ent) {
541                 arr[i++] = ent;
542         }
543         del_pset(free_set);
544
545         *free_methods = arr;
546         return length;
547 }
548
549 /*--------------------------------------------------------------------------*/
550 /* Callee analysis.                                                         */
551 /*--------------------------------------------------------------------------*/
552
553 static void callee_ana_node(ir_node *node, pset *methods);
554
555 static void callee_ana_proj(ir_node *node, long n, pset *methods)
556 {
557         assert(get_irn_mode(node) == mode_T);
558         if (get_irn_link(node) == MARK) {
559                 /* already visited */
560                 return;
561         }
562         set_irn_link(node, MARK);
563
564         switch (get_irn_opcode(node)) {
565         case iro_Proj: {
566                 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
567                  * op_Tuple oder ein Knoten, der eine "freie Methode"
568                  * zur�ckgibt. */
569                 ir_node *pred = get_Proj_pred(node);
570                 if (get_irn_link(pred) != MARK) {
571                         if (is_Tuple(pred)) {
572                                 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
573                         } else {
574                                 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
575                         }
576                 }
577                 break;
578         }
579
580         case iro_Tuple:
581                 callee_ana_node(get_Tuple_pred(node, n), methods);
582                 break;
583
584         default:
585                 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
586                 break;
587         }
588 }
589
590 /**
591  * Analyse a Call address.
592  *
593  * @param node     the node representing the call address
594  * @param methods  after call contains the set of all possibly called entities
595  */
596 static void callee_ana_node(ir_node *node, pset *methods)
597 {
598         assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
599         /* Beware of recursion */
600         if (get_irn_link(node) == MARK) {
601                 /* already visited */
602                 return;
603         }
604         set_irn_link(node, MARK);
605
606         switch (get_irn_opcode(node)) {
607         case iro_Const:
608                 /* A direct address call. We tread this as an external
609                    call and ignore it completely. */
610                 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
611                 break;
612
613         case iro_SymConst: {
614                 ir_entity *ent = get_SymConst_entity(node);
615                 assert(ent && is_method_entity(ent));
616                 pset_insert_ptr(methods, ent);
617                 break;
618         }
619
620         case iro_Sel: {
621                 /* polymorphic method */
622                 size_t i, n;
623                 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
624                         ir_entity *ent = get_Sel_method(node, i);
625                         if (ent != NULL) {
626                                 pset_insert_ptr(methods, ent);
627                         } else {
628                                 pset_insert_ptr(methods, get_unknown_entity());
629                         }
630                 }
631                 break;
632         }
633
634         case iro_Bad:
635                 /* nothing */
636                 break;
637
638         case iro_Phi: {
639                 int i;
640                 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
641                         callee_ana_node(get_Phi_pred(node, i), methods);
642                 }
643                 break;
644         }
645
646         case iro_Mux:
647                 callee_ana_node(get_Mux_false(node), methods);
648                 callee_ana_node(get_Mux_true(node), methods);
649                 break;
650
651         case iro_Id:
652                 callee_ana_node(get_Id_pred(node), methods);
653                 break;
654
655         case iro_Proj:
656                 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
657                 break;
658
659         case iro_Add:
660         case iro_Sub:
661         case iro_Conv:
662                 /* extern */
663                 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
664                 break;
665
666         default:
667                 assert(0 && "invalid opcode or opcode not implemented");
668                 break;
669         }
670 }
671
672 /**
673  * Walker: Analyses every Call node and calculates an array of possible
674  * callees for that call.
675  */
676 static void callee_walker(ir_node *call, void *env)
677 {
678         (void) env;
679         if (is_Call(call)) {
680                 pset *methods = pset_new_ptr_default();
681                 ir_entity **arr;
682                 size_t i;
683
684                 callee_ana_node(get_Call_ptr(call), methods);
685                 arr = NEW_ARR_F(ir_entity*, pset_count(methods));
686                 i = 0;
687                 foreach_pset(methods, ir_entity, ent) {
688                         arr[i] = ent;
689                         /* we want the unknown_entity on the zero position for easy tests later */
690                         if (is_unknown_entity(ent)) {
691                                 arr[i] = arr[0];
692                                 arr[0] = get_unknown_entity();
693                         }
694                         ++i;
695                 }
696                 set_Call_callee_arr(call, ARR_LEN(arr), arr);
697                 DEL_ARR_F(arr);
698                 del_pset(methods);
699         }
700 }
701
702 /**
703  * Walker: Removes all tuple.
704  */
705 static void remove_Tuples(ir_node *proj, void *env)
706 {
707         ir_node *nn;
708         (void) env;
709         if (! is_Proj(proj)) return;
710
711         nn = skip_Tuple(proj);
712         if (nn != proj) exchange(proj, nn);
713 }
714
715
716 /**
717  * Determine for every Call the set of possibly called methods and stores it
718  * inside the Call (@see set_Call_callee()).
719  * Uses the sel_methods set with much be already calculated.
720  */
721 static void callee_ana(void)
722 {
723         size_t i, n;
724         /* analyse all graphs */
725         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
726                 ir_graph *irg = get_irp_irg(i);
727                 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
728                 set_irg_callee_info_state(irg, irg_callee_info_consistent);
729         }
730         set_irp_callee_info_state(irg_callee_info_consistent);
731 }
732
733 /*--------------------------------------------------------------------------*/
734 /* Cleanup after analyses.                                                  */
735 /*--------------------------------------------------------------------------*/
736
737 /** Frees intermediate data structures. */
738 static void sel_methods_dispose(void)
739 {
740         assert(entities);
741         foreach_pset(entities, ir_entity, ent) {
742                 ir_entity **arr = (ir_entity**) get_entity_link(ent);
743                 if (arr != NULL) {
744                         DEL_ARR_F(arr);
745                 }
746                 set_entity_link(ent, NULL);
747         }
748         del_pset(entities);
749         entities = NULL;
750 }
751
752 static void destruct_walker(ir_node * node, void * env)
753 {
754         (void) env;
755         if (is_Call(node)) {
756                 remove_Call_callee_arr(node);
757         }
758 }
759
760 size_t cgana(ir_entity ***free_methods)
761 {
762         size_t length;
763         /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
764         sel_methods_init();
765         length = get_free_methods(free_methods);
766         callee_ana();
767         sel_methods_dispose();
768         return length;
769 }
770
771 void free_callee_info(ir_graph *irg)
772 {
773         irg_walk_graph(irg, destruct_walker, NULL, NULL);
774         set_irg_callee_info_state(irg, irg_callee_info_none);
775 }
776
777 void free_irp_callee_info(void)
778 {
779         size_t i, n;
780         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
781                 free_callee_info(get_irp_irg(i));
782         }
783 }
784
785 void opt_call_addrs(void)
786 {
787         /* Optimize the address expressions passed to call nodes.
788          *
789          * This optimization performs the following transformations for
790          * all ir graphs:
791          * - All SymConst operations that refer to intern methods are replaced
792          *   by Const operations referring to the corresponding entity.
793          * - Sel nodes, that select entities that are not overwritten are
794          *   replaced by Const nodes referring to the selected entity.
795          * - Sel nodes, for which no method exists at all are replaced by Bad
796          *   nodes.
797          * - Sel nodes with a pointer input that is an Alloc node are replaced
798          *   by Const nodes referring to the entity that implements the method in
799          *   the type given by the Alloc node.
800          */
801         sel_methods_init();
802         sel_methods_dispose();
803 }