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 /* Eindeutige Adresse zur Markierung von besuchten Knoten und zur
58 * Darstellung der unbekannten Methode. */
59 static void *MARK = &MARK;
61 static eset *entities = NULL;
63 /*--------------------------------------------------------------------------*/
65 /*--------------------------------------------------------------------------*/
68 /*--------------------------------------------------------------------------*/
69 /* Initialize datastructures, remove unwanted constructs, optimize */
70 /* call target computations. */
71 /*--------------------------------------------------------------------------*/
73 /** Returns the entity that contains the implementation of the inherited
74 * entity if available, else returns the entity passed. */
75 static entity *get_inherited_methods_implementation(entity *inh_meth) {
76 assert(get_atomic_ent_value(inh_meth) && "constant entity without value");
77 assert((get_irn_op(get_atomic_ent_value(inh_meth)) == op_SymConst) &&
78 (get_SymConst_kind(get_atomic_ent_value(inh_meth)) == symconst_addr_ent) &&
79 "Complex constant values not supported -- address of method should be straight constant!");
81 return get_SymConst_entity(get_atomic_ent_value(inh_meth));
84 /** Collect the entity representing the implementation of this
85 * entity (not the same if inherited) and all entities for overwriting
86 * implementations in "set".
87 * If the implementation of the method is not included in the
88 * compilation unit "open" is set to true.
89 * A recursive descend in the overwritten relation.
90 * Cycle-free, therefore must terminate.
93 * @param set A set of entities.
94 * @param size Number of entities in set.
97 static void collect_impls(entity *method, eset *set, int *size, bool *open) {
101 /* Add the implementation to the set if it contains an irg, else
102 remember that there are more methods called. */
104 if (get_entity_peculiarity(method) == peculiarity_inherited)
105 impl = get_inherited_methods_implementation(method);
107 if (get_entity_peculiarity(method) != peculiarity_description) {
108 eset_insert(set, impl);
112 /*- recursive descent -*/
113 for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
114 collect_impls(get_entity_overwrittenby(method, i), set, size, open);
117 /** Alle Methoden bestimmen, die die übergebene Methode überschreiben
118 * (und implementieren). In der zurückgegebenen Reihung kommt jede
119 * Methode nur einmal vor. Der Wert 'NULL' steht für unbekannte
120 * (externe) Methoden. Die zurückgegebene Reihung muß vom Aufrufer
121 * wieder freigegeben werden (siehe "DEL_ARR_F"). Gibt es überhaupt
122 * keine Methoden, die "method" überschreiben, so gibt die Methode
127 static entity ** get_impl_methods(entity * method) {
128 eset * set = eset_create();
133 /* Collect all method entities that can be called here */
134 collect_impls(method, set, &size, &open);
136 /* Vorgaenger einfuegen. */
137 if (size == 0 && !open) {
138 /* keine implementierte überschriebene Methode */
142 arr = NEW_ARR_F(entity *, size + 1);
143 arr[0] = NULL; /* Represents open method */
144 for (ent = eset_first(set); size > 0; ent = eset_next(set), --size)
148 arr = NEW_ARR_F(entity *, size);
149 for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size)
156 /** Analyze address computations.
158 * Compute for all Sel nodes the set of methods that can be selected.
159 * For each entity we store the set of subentities in the link field.
161 * Further do some optimizations:
162 * - Call standard optimizations for Sel nodes: this removes polymorphic
164 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
165 * For this we precomputed a map name->entity. Nowadays, we no more support
167 * - If the node is a Sel:
168 * If we found only a single method that can be called, replace the Sel
169 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
170 * as here we walk the type graph. In opt_polymorphy we only apply a local
173 * @param node The node to analyze
174 * @param env A map that maps names of entities to the entities.
176 static void sel_methods_walker(ir_node * node, void *env) {
177 pmap *ldname_map = env;
180 /* Call standard optimizations */
181 if (get_irn_op(node) == op_Sel) {
182 ir_node *new_node = optimize_in_place(node);
183 if (node != new_node)
184 exchange(node, new_node);
187 /* replace SymConst(name)-operations by SymConst(ent) */
188 if (get_irn_op(node) == op_SymConst) {
189 if (get_SymConst_kind(node) == symconst_addr_name) {
190 pmap_entry * entry = pmap_find(ldname_map, (void *) get_SymConst_name(node));
191 if (entry != NULL) { /* Method is declared in the compiled code */
192 assert(0 && "There should not be a SymConst[addr_name] addressing a method with an implementation"
193 "in this compilation unit. Use a SymConst[addr_ent].");
197 else if (get_irn_op(node) == op_Sel &&
198 is_Method_type(get_entity_type(get_Sel_entity(node)))) {
199 entity * ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
200 assert(get_entity_peculiarity(ent) != peculiarity_inherited);
202 if (!eset_contains(entities, ent)) {
203 /* Entity not yet handled. Find all (internal or external)
204 * implemented methods that overwrites this entity.
205 * This set is stored in the entity link. */
206 set_entity_link(ent, get_impl_methods(ent));
207 eset_insert(entities, ent);
210 /* -- As an add on we get an optimization that removes polymorphic calls.
211 This optimization is more powerful than that in transform_node_Sel(). -- */
212 arr = get_entity_link(ent);
215 * The Sel node never returns a pointer to a usable method.
216 * We could not call it, but it may be description:
217 * We call a method in a dead part of the program.
219 assert (get_entity_peculiarity(ent) == peculiarity_description);
221 else if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
222 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
226 * The Sel node returns only one possible method.
227 * So we could replace the Sel node by a SymConst.
228 * This method must exists.
230 set_irg_current_block(current_ir_graph, get_nodes_block(node));
231 assert(get_entity_peculiarity(get_SymConst_entity(get_atomic_ent_value(arr[0]))) ==
232 peculiarity_existent);
233 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]));
234 DBG_OPT_POLY(node, new_node);
235 exchange(node, new_node);
240 /** Initialize auxiliary data structures.
242 * Computes a set of entities that overwrite an entity and contain
243 * an implementation. The set is stored in the entity's link field.
245 * Further replaces Sel nodes where this set contains exactly one
246 * method by SymConst nodes.
247 * Finally asserts if there is a SymConst(name) if there could be a
249 static void sel_methods_init(void) {
251 pmap * ldname_map = pmap_create(); /* Map entity names to entities: to replace
252 SymConst(name) by SymConst(ent). */
253 assert(entities == NULL);
254 entities = eset_create();
255 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
256 entity * ent = get_irg_entity(get_irp_irg(i));
257 /* only external visible methods are allowed to call by a SymConst_ptr_name */
258 if (get_entity_visibility(ent) != visibility_local) {
259 pmap_insert(ldname_map, (void *) get_entity_ld_ident(ent), ent);
263 all_irg_walk(sel_methods_walker, NULL, ldname_map);
264 pmap_destroy(ldname_map);
267 /*--------------------------------------------------------------------------*/
268 /* Find free methods.
270 * We expect that each entity has an array with all implementations in its
272 /*--------------------------------------------------------------------------*/
275 * Returns an array of all methods that could be called at a Sel node.
276 * This array contains every entry only once.
278 * @param sel the Sel node
280 static entity ** get_Sel_arr(ir_node * sel) {
281 static entity ** NULL_ARRAY = NULL;
285 assert(sel && get_irn_op(sel) == op_Sel);
286 ent = get_Sel_entity(sel);
287 ent = get_inherited_methods_implementation(ent);
289 assert(is_Method_type(get_entity_type(ent))); /* what else? */
290 arr = get_entity_link(ent);
294 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
295 * kann für polymorphe (abstrakte) Methoden passieren. */
297 NULL_ARRAY = NEW_ARR_F(entity *, 0);
304 * Returns the number of possible called methods at a Sel node.
306 * @param sel the Sel node
308 static int get_Sel_n_methods(ir_node * sel) {
309 return ARR_LEN(get_Sel_arr(sel));
313 * Returns the ith possible called method entity at a Sel node.
315 static entity * get_Sel_method(ir_node * sel, int pos) {
316 entity ** arr = get_Sel_arr(sel);
317 assert(pos >= 0 && pos < ARR_LEN(arr));
321 static void free_mark(ir_node * node, eset * set);
323 static void free_mark_proj(ir_node * node, long n, eset * set) {
324 assert(get_irn_mode(node) == mode_T);
325 if (get_irn_link(node) == MARK) {
326 /* already visited */
329 set_irn_link(node, MARK);
330 switch (get_irn_opcode(node)) {
332 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
333 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
335 ir_node * pred = get_Proj_pred(node);
336 if (get_irn_link(pred) != MARK && get_irn_op(pred) == op_Tuple) {
337 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
339 /* nothing: da in "free_ana_walker" behandelt. */
345 free_mark(get_Tuple_pred(node, n), set);
349 free_mark_proj(get_Id_pred(node), n, set);
355 /* nothing: Die Operationen werden in free_ana_walker() selbst
360 assert(0 && "unexpected opcode or opcode not implemented");
363 set_irn_link(node, NULL);
367 * Called for predecessors nodes of "interesting" ones.
368 * Interesting ones include all nodes that can somehow make
371 * If a method (or a set of methods in case of polymorph calls) gets visible,
372 * add it to the set of 'free' methods
374 * @param node the current visited node
375 * @param set the set of all free methods
377 static void free_mark(ir_node *node, eset * set) {
380 if (get_irn_link(node) == MARK)
381 return; /* already visited */
383 set_irn_link(node, MARK);
385 switch (get_irn_opcode(node)) {
387 entity * ent = get_Sel_entity(node);
388 if (is_Method_type(get_entity_type(ent))) {
389 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
390 eset_insert(set, get_Sel_method(node, i));
396 if (get_SymConst_kind(node) == symconst_addr_ent) {
397 entity * ent = get_SymConst_entity(node);
398 if (is_Method_type(get_entity_type(ent))) {
399 eset_insert(set, ent);
402 assert(get_SymConst_kind(node) == symconst_addr_name);
403 /* nothing: SymConst points to extern method */
408 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
409 free_mark(get_Phi_pred(node, i), set);
413 free_mark(get_Id_pred(node), set);
416 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
419 /* nothing: Wird unten behandelt! */
422 set_irn_link(node, NULL);
426 * post-walker. Find method addresses.
428 static void free_ana_walker(ir_node *node, void *env) {
432 if (get_irn_link(node) == MARK) {
433 /* already visited */
436 switch (get_irn_opcode(node)) {
447 /* Sonderbehandlung, da der Funktionszeigereingang natürlich kein
450 set_irn_link(node, MARK);
451 for (i = get_Call_n_params(node) - 1; i >= 0; --i) {
452 ir_node *pred = get_Call_param(node, i);
453 if (mode_is_reference(get_irn_mode(pred))) {
454 free_mark(pred, set);
458 /* other nodes: Alle anderen Knoten nehmen wir als Verräter an, bis
459 * jemand das Gegenteil implementiert. */
461 set_irn_link(node, MARK);
462 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
463 ir_node * pred = get_irn_n(node, i);
464 if (mode_is_reference(get_irn_mode(pred))) {
465 free_mark(pred, set);
470 set_irn_link(node, NULL);
474 * Add all method addresses in global initializers to the set.
477 * We do NOT check the type here, just it it's an entity address.
478 * The reason for this is code like:
480 * void *p = function;
482 * which is sometimes used to anchor functions.
484 static void add_method_address(entity *ent, eset *set)
490 /* do not check uninitialized values */
491 if (get_entity_variability(ent) == variability_uninitialized)
494 if (is_atomic_entity(ent)) {
495 tp = get_entity_type(ent);
497 /* ignore methods: these of course reference it's address */
498 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 if (get_SymConst_kind(node) == symconst_addr_ent) {
651 entity * ent = get_SymConst_entity(node);
652 assert(ent && is_Method_type(get_entity_type(ent)));
653 eset_insert(methods, ent);
655 assert(get_SymConst_kind(node) == symconst_addr_name);
656 /* externe Methode (wegen fix_symconst!) */
657 eset_insert(methods, MARK); /* free method -> unknown */
661 /* polymorphe Methode */
662 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
663 entity * ent = get_Sel_method(node, i);
665 eset_insert(methods, ent);
667 eset_insert(methods, MARK);
676 case iro_Phi: /* Vereinigung */
677 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
678 callee_ana_node(get_Phi_pred(node, i), methods);
683 callee_ana_node(get_Mux_false(node), methods);
684 callee_ana_node(get_Mux_true(node), methods);
688 callee_ana_node(get_Id_pred(node), methods);
692 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
699 eset_insert(methods, MARK); /* free method -> unknown */
703 assert(0 && "invalid opcode or opcode not implemented");
707 set_irn_link(node, NULL);
711 static void callee_walker(ir_node * call, void * env) {
712 if (get_irn_op(call) == op_Call) {
713 eset * methods = eset_create();
715 entity ** arr = NEW_ARR_F(entity *, 0);
716 assert(get_irn_op(get_Call_ptr(call)) != op_Id);
717 callee_ana_node(get_Call_ptr(call), methods);
718 if (eset_contains(methods, MARK)) { /* unknown method */
719 ARR_APP1(entity *, arr, unknown_entity);
721 for (ent = eset_first(methods); ent; ent = eset_next(methods)) {
723 ARR_APP1(entity *, arr, ent);
726 #if 0 /* This generates Bad nodes when we don't want it.
727 Call it with a check for valid cgana information in local_optimize. */
728 if (ARR_LEN(arr) == 0 && get_opt_optimize() && get_opt_dyn_meth_dispatch()) {
729 /* Kann vorkommen, wenn der Vorgänger beispielsweise eine
730 * Sel-Operation war, die keine Methoden zurückgeben
731 * konnte. Wir ersetzen die Call-Operation ebenfalls durch
732 * eine Bad-Operation. Die Verlinkung muss wiederhergestellt
734 ir_node *mem = get_Call_mem(call);
735 turn_into_tuple (call, 5 /* pn_Call_max */);
736 set_Tuple_pred(call, pn_Call_M_regular , mem);
737 set_Tuple_pred(call, pn_Call_T_result , new_Bad());
738 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
739 set_Tuple_pred(call, pn_Call_X_except , new_Bad()); /* new_Jmp() ?? new_Raise() ?? */
740 set_Tuple_pred(call, pn_Call_M_except , new_Bad());
745 set_Call_callee_arr(call, ARR_LEN(arr), arr);
748 eset_destroy(methods);
753 static void remove_Tuples(ir_node * proj, void * env) {
755 if (get_irn_opcode(proj) != iro_Proj) return;
757 new = skip_Tuple(proj);
758 if (new != proj) exchange(proj, new);
762 /* Bestimmt für jede Call-Operation die Menge der aufrufbaren Methode
763 * und speichert das Ergebnis in der Call-Operation. (siehe
764 * "set_Call_callee"). "sel_methods" wird für den Aufbau benötigt und
765 * muss bereits aufgebaut sein. */
766 static void callee_ana(void) {
768 /* Alle Graphen analysieren. */
769 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
770 irg_walk_graph(get_irp_irg(i), callee_walker, remove_Tuples, NULL);
771 set_irg_callee_info_state(get_irp_irg(i), irg_callee_info_consistent);
773 set_irp_callee_info_state(irg_callee_info_consistent);
776 /*--------------------------------------------------------------------------*/
777 /* Cleanup after analyses. */
778 /*--------------------------------------------------------------------------*/
780 /** Frees intermediate data structures. */
781 static void sel_methods_dispose(void) {
784 for (ent = eset_first(entities); ent; ent = eset_next(entities)) {
785 entity ** arr = get_entity_link(ent);
789 set_entity_link(ent, NULL);
791 eset_destroy(entities);
795 /*--------------------------------------------------------------------------*/
796 /* Freeing the callee arrays. */
797 /*--------------------------------------------------------------------------*/
799 static void destruct_walker(ir_node * node, void * env) {
800 if (get_irn_op(node) == op_Call) {
801 remove_Call_callee_arr(node);
805 /*--------------------------------------------------------------------------*/
807 /*--------------------------------------------------------------------------*/
809 void cgana(int *length, entity ***free_methods) {
810 entity ** free_meths, **p;
812 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
814 free_meths = get_free_methods();
816 sel_methods_dispose();
818 /* Convert the flexible array to an array that can be handled
820 p = xmalloc(sizeof(*p) * ARR_LEN(free_meths));
821 memcpy(p, free_meths, ARR_LEN(free_meths) * sizeof(*p));
823 *length = ARR_LEN(free_meths);
826 DEL_ARR_F(free_meths);
829 void free_callee_info(ir_graph *irg) {
830 irg_walk_graph(irg, destruct_walker, NULL, NULL);
831 set_irg_callee_info_state(irg, irg_callee_info_none);
834 void free_irp_callee_info(void) {
836 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
837 free_callee_info(get_irp_irg(i));
841 /* Optimize the address expressions passed to call nodes.
843 * This optimization performs the following transformations for
845 * - All SymConst operations that refer to intern methods are replaced
846 * by Const operations referring to the corresponding entity.
847 * - Sel nodes, that select entities that are not overwritten are
848 * replaced by Const nodes referring to the selected entity.
849 * - Sel nodes, for which no method exists at all are replaced by Bad
851 * - Sel nodes with a pointer input that is an Alloc node are replaced
852 * by Const nodes referring to the entity that implements the method in
853 * the type given by the Alloc node.
855 void opt_call_addrs(void) {
857 sel_methods_dispose();