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