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;
179 /* Call standard optimizations */
180 if (get_irn_op(node) == op_Sel) {
181 ir_node *new_node = optimize_in_place(node);
182 if (node != new_node) exchange(node, new_node);
185 /* replace SymConst(name)-operations by SymConst(ent) */
186 if (get_irn_op(node) == op_SymConst) {
187 if (get_SymConst_kind(node) == symconst_addr_name) {
188 pmap_entry * entry = pmap_find(ldname_map, (void *) get_SymConst_name(node));
189 if (entry != NULL) { /* Method is declared in the compiled code */
190 assert(0 && "There should not be a SymConst[addr_name] addressing a method with an implementation"
191 "in this compilation unit. Use a SymConst[addr_ent].");
195 else if (get_irn_op(node) == op_Sel &&
196 is_Method_type(get_entity_type(get_Sel_entity(node)))) {
197 entity * ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
198 assert(get_entity_peculiarity(ent) != peculiarity_inherited);
200 if (!eset_contains(entities, ent)) {
201 /* Entity noch nicht behandelt. Alle (intern oder extern)
202 * implementierten Methoden suchen, die diese Entity
203 * überschreiben. Die Menge an entity.link speichern. */
204 set_entity_link(ent, get_impl_methods(ent));
205 eset_insert(entities, ent);
208 /* -- As an add on we get an optimization that removes polymorphic calls.
209 This optimization is more powerful than that in transform_node_Sel. -- */
210 if (get_entity_link(ent) == NULL) {
211 /* Die Sel-Operation kann nie einen Zeiger auf eine aufrufbare
212 * Methode zurückgeben. Damit ist sie insbesondere nicht
213 * ausführbar und nicht erreichbar. */
214 /* It may be description: We call a method in a dead part of the program. */
215 assert (get_entity_peculiarity(ent) == peculiarity_description);
217 entity ** arr = get_entity_link(ent);
218 if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
219 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
221 /* Die Sel-Operation kann immer nur _einen_ Wert auf eine
222 * interne Methode zurückgeben. Wir können daher die
223 * Sel-Operation durch eine Const- bzw. SymConst-Operation
225 set_irg_current_block(current_ir_graph, get_nodes_block(node));
226 assert(get_entity_peculiarity(get_SymConst_entity(get_atomic_ent_value(arr[0]))) ==
227 peculiarity_existent);
228 new_node = copy_const_value(get_atomic_ent_value(arr[0]));
229 DBG_OPT_POLY(node, new_node);
230 exchange (node, new_node);
236 /** Initialize auxiliary data structures.
238 * Computes a set of entities that overwrite an entity and contain
239 * an impelementation. The set is stored in the entity's link field.
241 * Further replaces Sel nodes where this set contains exactly one
242 * method by SymConst nodes.
243 * Finally asserts if there is a SymConst(name) if there could be a
245 static void sel_methods_init(void) {
247 pmap * ldname_map = pmap_create(); /* Map entity names to entities: to replace
248 SymConst(name) by SymConst(ent). */
249 assert(entities == NULL);
250 entities = eset_create();
251 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
252 entity * ent = get_irg_entity(get_irp_irg(i));
253 /* Nur extern sichtbare Methoden können überhaupt mit SymConst_ptr_name
254 * aufgerufen werden. */
255 if (get_entity_visibility(ent) != visibility_local) {
256 pmap_insert(ldname_map, (void *) get_entity_ld_ident(ent), ent);
260 all_irg_walk(sel_methods_walker, NULL, ldname_map);
261 pmap_destroy(ldname_map);
264 /*--------------------------------------------------------------------------*/
265 /* Find free methods.
267 * We expect that each entity has an array with all implementations in its
269 /*--------------------------------------------------------------------------*/
272 * Returns an array of all methods that could be called at a Sel node.
273 * This array contains every entry only once.
275 static entity ** get_Sel_arr(ir_node * sel) {
276 static entity ** NULL_ARRAY = NULL;
280 assert(sel && get_irn_op(sel) == op_Sel);
281 ent = get_Sel_entity(sel);
282 ent = get_inherited_methods_implementation(ent);
284 assert(is_Method_type(get_entity_type(ent))); /* what else? */
285 arr = get_entity_link(ent);
289 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
290 * kann für polymorphe (abstrakte) Methoden passieren. */
292 NULL_ARRAY = NEW_ARR_F(entity *, 0);
299 * Returns the number of possible called methods at a Sel node.
301 static int get_Sel_n_methods(ir_node * sel) {
302 return ARR_LEN(get_Sel_arr(sel));
306 * Returns the ith possible called method entity at a Sel node.
308 static entity * get_Sel_method(ir_node * sel, int pos) {
309 entity ** arr = get_Sel_arr(sel);
310 assert(pos >= 0 && pos < ARR_LEN(arr));
314 static void free_mark(ir_node * node, eset * set);
316 static void free_mark_proj(ir_node * node, long n, eset * set) {
317 assert(get_irn_mode(node) == mode_T);
318 if (get_irn_link(node) == MARK) {
319 /* already visited */
322 set_irn_link(node, MARK);
323 switch (get_irn_opcode(node)) {
325 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
326 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
328 ir_node * pred = get_Proj_pred(node);
329 if (get_irn_link(pred) != MARK && get_irn_op(pred) == op_Tuple) {
330 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
332 /* nothing: da in "free_ana_walker" behandelt. */
338 free_mark(get_Tuple_pred(node, n), set);
342 free_mark_proj(get_Id_pred(node), n, set);
348 /* nothing: Die Operationen werden in "free_ana_walker" selbst
353 assert(0 && "unexpected opcode or opcode not implemented");
356 set_irn_link(node, NULL);
360 static void free_mark(ir_node * node, eset * set) {
363 if (get_irn_link(node) == MARK) {
364 return; /* already visited */
366 set_irn_link(node, MARK);
367 switch (get_irn_opcode(node)) {
369 entity * ent = get_Sel_entity(node);
370 if (is_Method_type(get_entity_type(ent))) {
371 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
372 eset_insert(set, get_Sel_method(node, i));
378 if (get_SymConst_kind(node) == symconst_addr_ent) {
379 entity * ent = get_SymConst_entity(node);
380 if (is_Method_type(get_entity_type(ent))) {
381 eset_insert(set, ent);
384 assert(get_SymConst_kind(node) == symconst_addr_name);
385 /* nothing: SymConst points to extern method */
390 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
391 free_mark(get_Phi_pred(node, i), set);
395 free_mark(get_Id_pred(node), set);
398 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
401 /* nothing: Wird unten behandelt! */
404 set_irn_link(node, NULL);
408 static void free_ana_walker(ir_node * node, eset * set) {
410 if (get_irn_link(node) == MARK) {
411 /* bereits in einem Zyklus besucht. */
414 switch (get_irn_opcode(node)) {
425 /* Sonderbehandlung, da der Funktionszeigereingang natürlich kein
428 set_irn_link(node, MARK);
429 for (i = get_Call_arity(node) - 1; i >= 0; --i) {
430 ir_node * pred = get_Call_param(node, i);
431 if (mode_is_reference(get_irn_mode(pred))) {
432 free_mark(pred, set);
436 /* other nodes: Alle anderen Knoten nehmen wir als Verräter an, bis
437 * jemand das Gegenteil implementiert. */
439 set_irn_link(node, MARK);
440 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
441 ir_node * pred = get_irn_n(node, i);
442 if (mode_is_reference(get_irn_mode(pred))) {
443 free_mark(pred, set);
448 set_irn_link(node, NULL);
451 /* Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
452 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
453 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
454 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
455 * auf eine echt externe Methode. */
456 static entity ** get_free_methods(void)
458 eset * set = eset_create();
460 entity ** arr = NEW_ARR_F(entity *, 0);
463 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
464 ir_graph * irg = get_irp_irg(i);
465 entity * ent = get_irg_entity(irg);
466 /* insert "external visible" methods. */
467 if (get_entity_visibility(ent) != visibility_local) {
468 eset_insert(set, ent);
470 /* Finde alle Methoden die in dieser Methode extern sichtbar werden,
471 z.B. da die Adresse einer Methode abgespeichert wird. */
472 irg_walk_graph(irg, NULL, (irg_walk_func *) free_ana_walker, set);
475 /* insert sticky methods, too */
476 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
477 entity * ent = get_irg_entity(get_irp_irg(i));
478 /* insert "external visible" methods. */
479 if (get_entity_stickyness (ent) == stickyness_sticky) {
480 eset_insert(set, ent);
484 /* Hauptprogramm ist auch dann frei, wenn es nicht "external
486 if (get_irp_main_irg()) {
487 eset_insert(set, get_irg_entity(get_irp_main_irg()));
489 /* Wandle Menge in Feld um. Effizienter. */
490 for (ent = eset_first(set); ent; ent = eset_next(set)) {
491 ARR_APP1(entity *, arr, ent);
498 /*--------------------------------------------------------------------------*/
499 /* Callee analysis. */
500 /*--------------------------------------------------------------------------*/
502 static void callee_ana_node(ir_node * node, eset * methods);
504 static void callee_ana_proj(ir_node * node, long n, eset * methods) {
505 assert(get_irn_mode(node) == mode_T);
506 if (get_irn_link(node) == MARK) {
507 /* already visited */
510 set_irn_link(node, MARK);
512 switch (get_irn_opcode(node)) {
514 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
515 * op_Tuple oder ein Knoten, der eine "freie Methode"
517 ir_node * pred = get_Proj_pred(node);
518 if (get_irn_link(pred) != MARK) {
519 if (get_irn_op(pred) == op_Tuple) {
520 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
522 eset_insert(methods, MARK); /* free method -> unknown */
529 callee_ana_node(get_Tuple_pred(node, n), methods);
533 callee_ana_proj(get_Id_pred(node), n, methods);
537 eset_insert(methods, MARK); /* free method -> unknown */
541 set_irn_link(node, NULL);
545 static void callee_ana_node(ir_node * node, eset * methods) {
548 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
549 /* rekursion verhindern */
550 if (get_irn_link(node) == MARK) {
551 /* already visited */
554 set_irn_link(node, MARK);
556 switch (get_irn_opcode(node)) {
558 if (get_SymConst_kind(node) == symconst_addr_ent) {
559 entity * ent = get_SymConst_entity(node);
560 assert(ent && is_Method_type(get_entity_type(ent)));
561 eset_insert(methods, ent);
563 assert(get_SymConst_kind(node) == symconst_addr_name);
564 /* externe Methode (wegen fix_symconst!) */
565 eset_insert(methods, MARK); /* free method -> unknown */
569 /* polymorphe Methode */
570 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
571 entity * ent = get_Sel_method(node, i);
573 eset_insert(methods, ent);
575 eset_insert(methods, MARK);
584 case iro_Phi: /* Vereinigung */
585 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
586 callee_ana_node(get_Phi_pred(node, i), methods);
591 callee_ana_node(get_Mux_false(node), methods);
592 callee_ana_node(get_Mux_true(node), methods);
596 callee_ana_node(get_Id_pred(node), methods);
600 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
607 eset_insert(methods, MARK); /* free method -> unknown */
611 assert(0 && "invalid opcode or opcode not implemented");
615 set_irn_link(node, NULL);
619 static void callee_walker(ir_node * call, void * env) {
620 if (get_irn_op(call) == op_Call) {
621 eset * methods = eset_create();
623 entity ** arr = NEW_ARR_F(entity *, 0);
624 assert(get_irn_op(get_Call_ptr(call)) != op_Id);
625 callee_ana_node(get_Call_ptr(call), methods);
626 if (eset_contains(methods, MARK)) { /* unknown method */
627 ARR_APP1(entity *, arr, unknown_entity);
629 for (ent = eset_first(methods); ent; ent = eset_next(methods)) {
631 ARR_APP1(entity *, arr, ent);
634 #if 0 /* This generates Bad nodes when we don't want it.
635 Call it with a check for valid cgana information in local_optimize. */
636 if (ARR_LEN(arr) == 0 && get_opt_optimize() && get_opt_dyn_meth_dispatch()) {
637 /* Kann vorkommen, wenn der Vorgänger beispielsweise eine
638 * Sel-Operation war, die keine Methoden zurückgeben
639 * konnte. Wir ersetzen die Call-Operation ebenfalls durch
640 * eine Bad-Operation. Die Verlinkung muss wiederhergestellt
642 ir_node *mem = get_Call_mem(call);
643 turn_into_tuple (call, 5 /* pn_Call_max */);
644 set_Tuple_pred(call, pn_Call_M_regular , mem);
645 set_Tuple_pred(call, pn_Call_T_result , new_Bad());
646 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
647 set_Tuple_pred(call, pn_Call_X_except , new_Bad()); /* new_Jmp() ?? new_Raise() ?? */
648 set_Tuple_pred(call, pn_Call_M_except , new_Bad());
653 set_Call_callee_arr(call, ARR_LEN(arr), arr);
656 eset_destroy(methods);
661 static void remove_Tuples(ir_node * proj, void * env) {
663 if (get_irn_opcode(proj) != iro_Proj) return;
665 new = skip_Tuple(proj);
666 if (new != proj) exchange(proj, new);
670 /* Bestimmt für jede Call-Operation die Menge der aufrufbaren Methode
671 * und speichert das Ergebnis in der Call-Operation. (siehe
672 * "set_Call_callee"). "sel_methods" wird für den Aufbau benötigt und
673 * muss bereits aufgebaut sein. */
674 static void callee_ana(void) {
676 /* Alle Graphen analysieren. */
677 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
678 irg_walk_graph(get_irp_irg(i), callee_walker, remove_Tuples, NULL);
679 set_irg_callee_info_state(get_irp_irg(i), irg_callee_info_consistent);
681 set_irp_callee_info_state(irg_callee_info_consistent);
684 /*--------------------------------------------------------------------------*/
685 /* Cleanup after analyses. */
686 /*--------------------------------------------------------------------------*/
688 /** Frees intermediate data structures. */
689 static void sel_methods_dispose(void) {
692 for (ent = eset_first(entities); ent; ent = eset_next(entities)) {
693 entity ** arr = get_entity_link(ent);
697 set_entity_link(ent, NULL);
699 eset_destroy(entities);
703 /*--------------------------------------------------------------------------*/
704 /* Freeing the callee arrays. */
705 /*--------------------------------------------------------------------------*/
707 static void destruct_walker(ir_node * node, void * env) {
708 if (get_irn_op(node) == op_Call) {
709 remove_Call_callee_arr(node);
713 /*--------------------------------------------------------------------------*/
715 /*--------------------------------------------------------------------------*/
717 void cgana(int *length, entity ***free_methods) {
718 entity ** free_meths, **p;
720 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
722 free_meths = get_free_methods();
724 sel_methods_dispose();
726 /* Convert the flexible array to an array that can be handled
728 p = xmalloc(sizeof(*p) * ARR_LEN(free_meths));
729 memcpy(p, free_meths, ARR_LEN(free_meths) * sizeof(*p));
731 *length = ARR_LEN(free_meths);
734 DEL_ARR_F(free_meths);
737 void free_callee_info(ir_graph *irg) {
738 irg_walk_graph(irg, destruct_walker, NULL, NULL);
739 set_irg_callee_info_state(irg, irg_callee_info_none);
742 void free_irp_callee_info(void) {
743 int i, n_irgs = get_irp_n_irgs();
744 for (i = 0; i < n_irgs; ++i) {
745 free_callee_info(get_irp_irg(i));
749 /* Optimize the address expressions passed to call nodes.
751 * This optimization performs the following transformations for
753 * - All SymConst operations that refer to intern methods are replaced
754 * by Const operations refering to the corresponding entity.
755 * - Sel nodes, that select entities that are not overwritten are
756 * replaced by Const nodes refering to the selected entity.
757 * - Sel nodes, for which no method exists at all are replaced by Bad
759 * - Sel nodes with a pointer input that is an Alloc node are replaced
760 * by Const nodes refering to the entity that implements the method in
761 * the type given by the Alloc node.
763 void opt_call_addrs(void) {
765 sel_methods_dispose();