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