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