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