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