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