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