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