3 * File name: ir/ana/cgana.c
4 * Purpose: Intraprozedural analyses to estimate the call graph.
5 * Author: Hubert Schmid
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Interprocedural analysis to estimate the calling relation.
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.
44 #include "dbginfo_t.h"
45 #include "iropt_dbg.h"
57 /* unambiguous address used as a mark. */
58 static void *MARK = &MARK;
60 static eset *entities = NULL;
62 /*--------------------------------------------------------------------------*/
64 /*--------------------------------------------------------------------------*/
67 /*--------------------------------------------------------------------------*/
68 /* Initialize datastructures, remove unwanted constructs, optimize */
69 /* call target computations. */
70 /*--------------------------------------------------------------------------*/
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!");
80 return get_SymConst_entity(get_atomic_ent_value(inh_meth));
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.
92 * @param set A set of entities.
93 * @param size Number of entities in set.
96 static void collect_impls(entity *method, eset *set, int *size, int *open) {
100 /* Add the implementation to the set if it contains an irg, else
101 remember that there are more methods called. */
103 if (get_entity_peculiarity(method) == peculiarity_inherited)
104 impl = get_inherited_methods_implementation(method);
106 if (get_entity_peculiarity(method) != peculiarity_description) {
107 eset_insert(set, impl);
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);
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
126 static entity ** get_impl_methods(entity * method) {
127 eset * set = eset_create();
132 /* Collect all method entities that can be called here */
133 collect_impls(method, set, &size, &open);
135 /* Vorgaenger einfuegen. */
136 if (size == 0 && !open) {
137 /* keine implementierte überschriebene Methode */
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)
147 arr = NEW_ARR_F(entity *, size);
148 for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size)
155 /** Analyze address computations.
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.
160 * Further do some optimizations:
161 * - Call standard optimizations for Sel nodes: this removes polymorphic
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
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
172 * @param node The node to analyze
173 * @param env A map that maps names of entities to the entities.
175 static void sel_methods_walker(ir_node * node, void *env) {
176 pmap *ldname_map = env;
179 /* Call standard optimizations */
181 ir_node *new_node = optimize_in_place(node);
182 if (node != new_node)
183 exchange(node, new_node);
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].");
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);
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);
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);
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.
218 assert (get_entity_peculiarity(ent) == peculiarity_description);
220 else if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
221 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
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.
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);
239 /** Initialize auxiliary data structures.
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.
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
248 static void sel_methods_init(void) {
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);
262 all_irg_walk(sel_methods_walker, NULL, ldname_map);
263 pmap_destroy(ldname_map);
266 /*--------------------------------------------------------------------------*/
267 /* Find free methods.
269 * We expect that each entity has an array with all implementations in its
271 /*--------------------------------------------------------------------------*/
274 * Returns an array of all methods that could be called at a Sel node.
275 * This array contains every entry only once.
277 * @param sel the Sel node
279 static entity ** get_Sel_arr(ir_node * sel) {
280 static entity ** NULL_ARRAY = NULL;
285 ent = get_Sel_entity(sel);
286 ent = get_inherited_methods_implementation(ent);
288 assert(is_Method_type(get_entity_type(ent))); /* what else? */
289 arr = get_entity_link(ent);
293 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
294 * kann für polymorphe (abstrakte) Methoden passieren. */
296 NULL_ARRAY = NEW_ARR_F(entity *, 0);
303 * Returns the number of possible called methods at a Sel node.
305 * @param sel the Sel node
307 static int get_Sel_n_methods(ir_node * sel) {
308 return ARR_LEN(get_Sel_arr(sel));
312 * Returns the ith possible called method entity at a Sel node.
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));
320 static void free_mark(ir_node * node, eset * set);
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 */
328 set_irn_link(node, MARK);
329 switch (get_irn_opcode(node)) {
331 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
332 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
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);
338 /* nothing: da in "free_ana_walker" behandelt. */
344 free_mark(get_Tuple_pred(node, n), set);
348 free_mark_proj(get_Id_pred(node), n, set);
354 /* nothing: Die Operationen werden in free_ana_walker() selbst
359 assert(0 && "unexpected opcode or opcode not implemented");
362 set_irn_link(node, NULL);
366 * Called for predecessors nodes of "interesting" ones.
367 * Interesting ones include all nodes that can somehow make
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
373 * @param node the current visited node
374 * @param set the set of all free methods
376 static void free_mark(ir_node *node, eset * set) {
379 if (get_irn_link(node) == MARK)
380 return; /* already visited */
382 set_irn_link(node, MARK);
384 switch (get_irn_opcode(node)) {
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));
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);
401 assert(get_SymConst_kind(node) == symconst_addr_name);
402 /* nothing: SymConst points to extern method */
407 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
408 free_mark(get_Phi_pred(node, i), set);
412 free_mark(get_Id_pred(node), set);
415 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
418 /* nothing: Wird unten behandelt! */
421 set_irn_link(node, NULL);
425 * post-walker. Find method addresses.
427 static void free_ana_walker(ir_node *node, void *env) {
431 if (get_irn_link(node) == MARK) {
432 /* already visited */
435 switch (get_irn_opcode(node)) {
446 /* Sonderbehandlung, da der Funktionszeigereingang natürlich kein
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);
457 /* other nodes: Alle anderen Knoten nehmen wir als Verräter an, bis
458 * jemand das Gegenteil implementiert. */
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);
469 set_irn_link(node, NULL);
473 * Add all method addresses in global initializers to the set.
476 * We do NOT check the type here, just it it's an entity address.
477 * The reason for this is code like:
479 * void *p = function;
481 * which is sometimes used to anchor functions.
483 static void add_method_address(entity *ent, eset *set)
489 /* do not check uninitialized values */
490 if (get_entity_variability(ent) == variability_uninitialized)
493 if (is_atomic_entity(ent)) {
494 tp = get_entity_type(ent);
496 /* ignore methods: these of course reference it's address */
497 if (is_Method_type(tp))
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);
506 if (is_Method_type(get_entity_type(ent)))
507 eset_insert(set, ent);
512 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
513 n = get_compound_ent_value(ent, i);
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);
520 if (is_Method_type(get_entity_type(ent)))
521 eset_insert(set, ent);
529 * returns a list of 'free' methods, i.e., the methods that can be called
530 * from external or via function pointers.
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.
538 static entity ** get_free_methods(void)
540 eset *free_set = eset_create();
542 entity **arr = NEW_ARR_F(entity *, 0);
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);
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);
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);
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);
573 add_method_address(ent, free_set);
576 /* the main program is even then "free", if it's not external visible. */
577 irg = get_irp_main_irg();
579 eset_insert(free_set, get_irg_entity(irg));
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);
585 eset_destroy(free_set);
590 /*--------------------------------------------------------------------------*/
591 /* Callee analysis. */
592 /*--------------------------------------------------------------------------*/
594 static void callee_ana_node(ir_node * node, eset * methods);
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 */
602 set_irn_link(node, MARK);
604 switch (get_irn_opcode(node)) {
606 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
607 * op_Tuple oder ein Knoten, der eine "freie Methode"
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);
614 eset_insert(methods, MARK); /* free method -> unknown */
621 callee_ana_node(get_Tuple_pred(node, n), methods);
625 callee_ana_proj(get_Id_pred(node), n, methods);
629 eset_insert(methods, MARK); /* free method -> unknown */
633 set_irn_link(node, NULL);
637 static void callee_ana_node(ir_node * node, eset * methods) {
640 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
641 /* rekursion verhindern */
642 if (get_irn_link(node) == MARK) {
643 /* already visited */
646 set_irn_link(node, MARK);
648 switch (get_irn_opcode(node)) {
650 /* A direct address call. We tread this as an external
651 call and ignore it completely. */
652 eset_insert(methods, MARK); /* free method -> unknown */
655 if (get_SymConst_kind(node) == symconst_addr_ent) {
656 entity * ent = get_SymConst_entity(node);
657 assert(ent && is_Method_type(get_entity_type(ent)));
658 eset_insert(methods, ent);
660 assert(get_SymConst_kind(node) == symconst_addr_name);
661 /* externe Methode (wegen fix_symconst!) */
662 eset_insert(methods, MARK); /* free method -> unknown */
666 /* polymorphe Methode */
667 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
668 entity * ent = get_Sel_method(node, i);
670 eset_insert(methods, ent);
672 eset_insert(methods, MARK);
681 case iro_Phi: /* Vereinigung */
682 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
683 callee_ana_node(get_Phi_pred(node, i), methods);
688 callee_ana_node(get_Mux_false(node), methods);
689 callee_ana_node(get_Mux_true(node), methods);
693 callee_ana_node(get_Id_pred(node), methods);
697 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
704 eset_insert(methods, MARK); /* free method -> unknown */
708 assert(0 && "invalid opcode or opcode not implemented");
712 set_irn_link(node, NULL);
716 static void callee_walker(ir_node * call, void * env) {
718 eset * methods = eset_create();
720 entity ** arr = NEW_ARR_F(entity *, 0);
721 assert(get_irn_op(get_Call_ptr(call)) != op_Id);
722 callee_ana_node(get_Call_ptr(call), methods);
723 if (eset_contains(methods, MARK)) { /* unknown method */
724 ARR_APP1(entity *, arr, unknown_entity);
726 for (ent = eset_first(methods); ent; ent = eset_next(methods)) {
728 ARR_APP1(entity *, arr, ent);
731 #if 0 /* This generates Bad nodes when we don't want it.
732 Call it with a check for valid cgana information in local_optimize. */
733 if (ARR_LEN(arr) == 0 && get_opt_optimize() && get_opt_dyn_meth_dispatch()) {
734 /* Kann vorkommen, wenn der Vorgänger beispielsweise eine
735 * Sel-Operation war, die keine Methoden zurückgeben
736 * konnte. Wir ersetzen die Call-Operation ebenfalls durch
737 * eine Bad-Operation. Die Verlinkung muss wiederhergestellt
739 ir_node *mem = get_Call_mem(call);
740 turn_into_tuple (call, 5 /* pn_Call_max */);
741 set_Tuple_pred(call, pn_Call_M_regular , mem);
742 set_Tuple_pred(call, pn_Call_T_result , new_Bad());
743 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
744 set_Tuple_pred(call, pn_Call_X_except , new_Bad()); /* new_Jmp() ?? new_Raise() ?? */
745 set_Tuple_pred(call, pn_Call_M_except , new_Bad());
750 set_Call_callee_arr(call, ARR_LEN(arr), arr);
753 eset_destroy(methods);
758 static void remove_Tuples(ir_node * proj, void * env) {
760 if (get_irn_opcode(proj) != iro_Proj) return;
762 new = skip_Tuple(proj);
763 if (new != proj) exchange(proj, new);
767 /* Bestimmt für jede Call-Operation die Menge der aufrufbaren Methode
768 * und speichert das Ergebnis in der Call-Operation. (siehe
769 * "set_Call_callee"). "sel_methods" wird für den Aufbau benötigt und
770 * muss bereits aufgebaut sein. */
771 static void callee_ana(void) {
773 /* Alle Graphen analysieren. */
774 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
775 ir_graph *irg = get_irp_irg(i);
776 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
777 set_irg_callee_info_state(irg, irg_callee_info_consistent);
779 set_irp_callee_info_state(irg_callee_info_consistent);
782 /*--------------------------------------------------------------------------*/
783 /* Cleanup after analyses. */
784 /*--------------------------------------------------------------------------*/
786 /** Frees intermediate data structures. */
787 static void sel_methods_dispose(void) {
790 for (ent = eset_first(entities); ent; ent = eset_next(entities)) {
791 entity ** arr = get_entity_link(ent);
795 set_entity_link(ent, NULL);
797 eset_destroy(entities);
801 /*--------------------------------------------------------------------------*/
802 /* Freeing the callee arrays. */
803 /*--------------------------------------------------------------------------*/
805 static void destruct_walker(ir_node * node, void * env) {
807 remove_Call_callee_arr(node);
811 /*--------------------------------------------------------------------------*/
813 /*--------------------------------------------------------------------------*/
815 void cgana(int *length, entity ***free_methods) {
816 entity ** free_meths, **p;
818 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
820 free_meths = get_free_methods();
822 sel_methods_dispose();
824 /* Convert the flexible array to an array that can be handled
826 p = xmalloc(sizeof(*p) * ARR_LEN(free_meths));
827 memcpy(p, free_meths, ARR_LEN(free_meths) * sizeof(*p));
829 *length = ARR_LEN(free_meths);
832 DEL_ARR_F(free_meths);
835 void free_callee_info(ir_graph *irg) {
836 irg_walk_graph(irg, destruct_walker, NULL, NULL);
837 set_irg_callee_info_state(irg, irg_callee_info_none);
840 void free_irp_callee_info(void) {
842 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
843 free_callee_info(get_irp_irg(i));
847 /* Optimize the address expressions passed to call nodes.
849 * This optimization performs the following transformations for
851 * - All SymConst operations that refer to intern methods are replaced
852 * by Const operations referring to the corresponding entity.
853 * - Sel nodes, that select entities that are not overwritten are
854 * replaced by Const nodes referring to the selected entity.
855 * - Sel nodes, for which no method exists at all are replaced by Bad
857 * - Sel nodes with a pointer input that is an Alloc node are replaced
858 * by Const nodes referring to the entity that implements the method in
859 * the type given by the Alloc node.
861 void opt_call_addrs(void) {
863 sel_methods_dispose();