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