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