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