2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Intraprozedural analyses to estimate the call graph.
23 * @author Hubert Schmid
27 * Interprocedural analysis to estimate the calling relation.
29 * This analysis computes all entities representing methods that
30 * can be called at a Call node. Further it computes a set of
31 * methods that are 'free', i.e., their adress is handled by
32 * the program directly, or they are visible external.
55 #include "dbginfo_t.h"
56 #include "iropt_dbg.h"
68 /* unambiguous address used as a mark. */
69 static void *MARK = &MARK;
71 static eset *entities = NULL;
73 /*--------------------------------------------------------------------------*/
75 /*--------------------------------------------------------------------------*/
78 /*--------------------------------------------------------------------------*/
79 /* Initialize datastructures, remove unwanted constructs, optimize */
80 /* call target computations. */
81 /*--------------------------------------------------------------------------*/
83 /** Returns the entity that contains the implementation of the inherited
84 * entity if available, else returns the entity passed. */
85 static ir_entity *get_inherited_methods_implementation(ir_entity *inh_meth) {
86 assert(get_atomic_ent_value(inh_meth) && "constant entity without value");
87 assert((get_irn_op(get_atomic_ent_value(inh_meth)) == op_SymConst) &&
88 (get_SymConst_kind(get_atomic_ent_value(inh_meth)) == symconst_addr_ent) &&
89 "Complex constant values not supported -- address of method should be straight constant!");
91 return get_SymConst_entity(get_atomic_ent_value(inh_meth));
94 /** Collect the entity representing the implementation of this
95 * method (not the same if inherited) and all entities for overwriting
96 * implementations in "set".
97 * If the implementation of the method is not included in the
98 * compilation unit "open" is set to true.
99 * A recursive descend in the overwritten relation.
100 * Cycle-free, therefore must terminate.
103 * @param set A set of entities.
104 * @param size Number of entities in set.
107 static void collect_impls(ir_entity *method, eset *set, int *size, int *open) {
111 /* Add the implementation to the set if it contains an irg, else
112 remember that there are more methods called. */
114 if (get_entity_peculiarity(method) == peculiarity_inherited)
115 impl = get_inherited_methods_implementation(method);
117 if (get_entity_peculiarity(method) != peculiarity_description) {
118 eset_insert(set, impl);
122 /*- recursive descent -*/
123 for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
124 collect_impls(get_entity_overwrittenby(method, i), set, size, open);
127 /** Alle Methoden bestimmen, die die übergebene Methode überschreiben
128 * (und implementieren). In der zurückgegebenen Reihung kommt jede
129 * Methode nur einmal vor. Der Wert 'NULL' steht für unbekannte
130 * (externe) Methoden. Die zurückgegebene Reihung muß vom Aufrufer
131 * wieder freigegeben werden (siehe "DEL_ARR_F"). Gibt es überhaupt
132 * keine Methoden, die "method" überschreiben, so gibt die Methode
137 static ir_entity ** get_impl_methods(ir_entity * method) {
138 eset * set = eset_create();
143 /* Collect all method entities that can be called here */
144 collect_impls(method, set, &size, &open);
146 /* Vorgaenger einfuegen. */
147 if (size == 0 && !open) {
148 /* keine implementierte überschriebene Methode */
152 arr = NEW_ARR_F(ir_entity *, size + 1);
153 arr[0] = NULL; /* Represents open method */
154 for (ent = eset_first(set); size > 0; ent = eset_next(set), --size)
158 arr = NEW_ARR_F(ir_entity *, size);
159 for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size)
166 /** Analyze address computations.
168 * Compute for all Sel nodes the set of methods that can be selected.
169 * For each entity we store the set of subentities in the link field.
171 * Further do some optimizations:
172 * - Call standard optimizations for Sel nodes: this removes polymorphic
174 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
175 * For this we precomputed a map name->entity. Nowadays, we no more support
177 * - If the node is a Sel:
178 * If we found only a single method that can be called, replace the Sel
179 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
180 * as here we walk the type graph. In opt_polymorphy we only apply a local
183 * @param node The node to analyze
184 * @param env A map that maps names of entities to the entities.
186 static void sel_methods_walker(ir_node *node, void *env) {
187 pmap *ldname_map = env;
190 /* Call standard optimizations */
192 ir_node *new_node = optimize_in_place(node);
193 if (node != new_node)
194 exchange(node, new_node);
197 /* replace SymConst(name)-operations by SymConst(ent) */
198 if (is_SymConst(node)) {
199 if (get_SymConst_kind(node) == symconst_addr_name) {
200 pmap_entry *entry = pmap_find(ldname_map, get_SymConst_name(node));
201 if (entry != NULL) { /* Method is declared in the compiled code */
202 assert(!"There should not be a SymConst[addr_name] addressing a method with an implementation"
203 "in this compilation unit. Use a SymConst[addr_ent].");
206 } else if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
207 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
208 assert(get_entity_peculiarity(ent) != peculiarity_inherited);
210 if (!eset_contains(entities, ent)) {
211 /* Entity not yet handled. Find all (internal or external)
212 * implemented methods that overwrites this entity.
213 * This set is stored in the entity link. */
214 set_entity_link(ent, get_impl_methods(ent));
215 eset_insert(entities, ent);
218 /* -- As an add on we get an optimization that removes polymorphic calls.
219 This optimization is more powerful than that in transform_node_Sel(). -- */
220 arr = get_entity_link(ent);
223 * The Sel node never returns a pointer to a usable method.
224 * We could not call it, but it may be description:
225 * We call a method in a dead part of the program.
227 assert(get_entity_peculiarity(ent) == peculiarity_description);
229 else if (get_opt_closed_world() && get_opt_dyn_meth_dispatch() &&
230 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
234 * The Sel node returns only one possible method.
235 * So we could replace the Sel node by a SymConst.
236 * This method must exists.
238 set_irg_current_block(current_ir_graph, get_nodes_block(node));
239 assert(get_entity_peculiarity(get_SymConst_entity(get_atomic_ent_value(arr[0]))) ==
240 peculiarity_existent);
241 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]));
242 DBG_OPT_POLY(node, new_node);
243 exchange(node, new_node);
249 * Initialize auxiliary data structures.
251 * Computes a set of entities that overwrite an entity and contain
252 * an implementation. The set is stored in the entity's link field.
254 * Further replaces Sel nodes where this set contains exactly one
255 * method by SymConst nodes.
256 * Finally asserts if there is a SymConst(name) if there could be a
259 static void sel_methods_init(void) {
261 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
262 SymConst(name) by SymConst(ent). */
263 assert(entities == NULL);
264 entities = eset_create();
265 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
266 ir_entity * ent = get_irg_entity(get_irp_irg(i));
267 /* only external visible methods are allowed to call by a SymConst_ptr_name */
268 if (get_entity_visibility(ent) != visibility_local) {
269 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
273 all_irg_walk(sel_methods_walker, NULL, ldname_map);
274 pmap_destroy(ldname_map);
277 /*--------------------------------------------------------------------------*/
278 /* Find free methods.
280 * We expect that each entity has an array with all implementations in its
282 /*--------------------------------------------------------------------------*/
285 * Returns an array of all methods that could be called at a Sel node.
286 * This array contains every entry only once.
288 * @param sel the Sel node
290 static ir_entity ** get_Sel_arr(ir_node * sel) {
291 static ir_entity ** NULL_ARRAY = NULL;
296 ent = get_Sel_entity(sel);
297 ent = get_inherited_methods_implementation(ent);
299 assert(is_Method_type(get_entity_type(ent))); /* what else? */
300 arr = get_entity_link(ent);
304 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
305 * kann für polymorphe (abstrakte) Methoden passieren. */
307 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
314 * Returns the number of possible called methods at a Sel node.
316 * @param sel the Sel node
318 static int get_Sel_n_methods(ir_node * sel) {
319 return ARR_LEN(get_Sel_arr(sel));
323 * Returns the ith possible called method entity at a Sel node.
325 static ir_entity * get_Sel_method(ir_node * sel, int pos) {
326 ir_entity ** arr = get_Sel_arr(sel);
327 assert(pos >= 0 && pos < ARR_LEN(arr));
332 static void free_mark(ir_node * node, eset * set);
334 static void free_mark_proj(ir_node * node, long n, eset * set) {
335 assert(get_irn_mode(node) == mode_T);
336 if (get_irn_link(node) == MARK) {
337 /* already visited */
340 set_irn_link(node, MARK);
341 switch (get_irn_opcode(node)) {
343 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
344 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
346 ir_node * pred = get_Proj_pred(node);
347 if (get_irn_link(pred) != MARK && get_irn_op(pred) == op_Tuple) {
348 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
350 /* nothing: da in "free_ana_walker" behandelt. */
356 free_mark(get_Tuple_pred(node, n), set);
360 free_mark_proj(get_Id_pred(node), n, set);
366 /* nothing: Die Operationen werden in free_ana_walker() selbst
371 assert(0 && "unexpected opcode or opcode not implemented");
374 set_irn_link(node, NULL);
378 * Called for predecessors nodes of "interesting" ones.
379 * Interesting ones include all nodes that can somehow make
382 * If a method (or a set of methods in case of polymorph calls) gets visible,
383 * add it to the set of 'free' methods
385 * @param node the current visited node
386 * @param set the set of all free methods
388 static void free_mark(ir_node *node, eset * set) {
391 if (get_irn_link(node) == MARK)
392 return; /* already visited */
394 set_irn_link(node, MARK);
396 switch (get_irn_opcode(node)) {
398 ir_entity *ent = get_Sel_entity(node);
399 if (is_method_entity(ent)) {
400 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
401 eset_insert(set, get_Sel_method(node, i));
407 if (get_SymConst_kind(node) == symconst_addr_ent) {
408 ir_entity *ent = get_SymConst_entity(node);
409 if (is_method_entity(ent)) {
410 eset_insert(set, ent);
413 assert(get_SymConst_kind(node) == symconst_addr_name);
414 /* nothing: SymConst points to extern method */
419 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
420 free_mark(get_Phi_pred(node, i), set);
424 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
430 set_irn_link(node, NULL);
434 * post-walker. Find method addresses.
436 static void free_ana_walker(ir_node *node, void *env) {
440 if (get_irn_link(node) == MARK) {
441 /* already visited */
444 switch (get_irn_opcode(node)) {
456 /* we must handle Call nodes specially, because their call address input
457 do not expose a method address. */
458 set_irn_link(node, MARK);
459 for (i = get_Call_n_params(node) - 1; i >= 0; --i) {
460 ir_node *pred = get_Call_param(node, i);
461 if (mode_is_reference(get_irn_mode(pred))) {
462 free_mark(pred, set);
467 /* other nodes: Alle anderen Knoten nehmen wir als Verräter an, bis
468 * jemand das Gegenteil implementiert. */
469 set_irn_link(node, MARK);
470 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
471 ir_node *pred = get_irn_n(node, i);
472 if (mode_is_reference(get_irn_mode(pred))) {
473 free_mark(pred, set);
478 set_irn_link(node, NULL);
482 * Add all method addresses in global initializers to the set.
485 * We do NOT check the type here, just it it's an entity address.
486 * The reason for this is code like:
488 * void *p = function;
490 * which is sometimes used to anchor functions.
492 static void add_method_address(ir_entity *ent, eset *set)
498 /* do not check uninitialized values */
499 if (get_entity_variability(ent) == variability_uninitialized)
502 if (is_atomic_entity(ent)) {
503 tp = get_entity_type(ent);
505 /* ignore methods: these of course reference it's address */
506 if (is_Method_type(tp))
509 /* let's check if it's the address of a function */
510 n = get_atomic_ent_value(ent);
511 if (is_SymConst_addr_ent(n)) {
512 ent = get_SymConst_entity(n);
514 if (is_Method_type(get_entity_type(ent)))
515 eset_insert(set, ent);
518 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
519 n = get_compound_ent_value(ent, i);
521 /* let's check if it's the address of a function */
522 if (is_SymConst_addr_ent(n)) {
523 ir_entity *ent = get_SymConst_entity(n);
525 if (is_Method_type(get_entity_type(ent)))
526 eset_insert(set, ent);
533 * returns a list of 'free' methods, i.e., the methods that can be called
534 * from external or via function pointers.
536 * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
537 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
538 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
539 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
540 * auf eine echt externe Methode.
542 static ir_entity **get_free_methods(int *length)
544 eset *free_set = eset_create();
551 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
552 irg = get_irp_irg(i);
553 ent = get_irg_entity(irg);
554 /* insert "external visible" methods. */
555 if (get_entity_visibility(ent) != visibility_local) {
556 eset_insert(free_set, ent);
558 /* insert "sticky" methods. */
559 if (get_entity_stickyness(ent) == stickyness_sticky) {
560 eset_insert(free_set, ent);
563 /* Find all method entities that gets "visible" trough this graphs,
564 * for instance because their address is stored. */
565 irg_walk_graph(irg, NULL, free_ana_walker, free_set);
568 /* insert all methods that are used in global variables initializers */
569 tp = get_glob_type();
570 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
571 ent = get_class_member(tp, i);
572 add_method_address(ent, free_set);
575 for (i = get_struct_n_members(tp) - 1; i >= 0; --i) {
576 ent = get_struct_member(tp, i);
577 add_method_address(ent, free_set);
580 /* the main program is even then "free", if it's not external visible. */
581 irg = get_irp_main_irg();
583 eset_insert(free_set, get_irg_entity(irg));
585 /* Finally, transform the set into an array. */
586 *length = eset_count(free_set);
587 arr = xmalloc(sizeof(ir_entity *) * (*length));
588 for (i = 0, ent = eset_first(free_set); ent; ent = eset_next(free_set)) {
591 eset_destroy(free_set);
596 /*--------------------------------------------------------------------------*/
597 /* Callee analysis. */
598 /*--------------------------------------------------------------------------*/
600 static void callee_ana_node(ir_node * node, eset * methods);
602 static void callee_ana_proj(ir_node *node, long n, eset *methods) {
603 assert(get_irn_mode(node) == mode_T);
604 if (get_irn_link(node) == MARK) {
605 /* already visited */
608 set_irn_link(node, MARK);
610 switch (get_irn_opcode(node)) {
612 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
613 * op_Tuple oder ein Knoten, der eine "freie Methode"
615 ir_node *pred = get_Proj_pred(node);
616 if (get_irn_link(pred) != MARK) {
617 if (is_Tuple(pred)) {
618 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
620 eset_insert(methods, unknown_entity); /* free method -> unknown */
627 callee_ana_node(get_Tuple_pred(node, n), methods);
631 eset_insert(methods, unknown_entity); /* free method -> unknown */
635 set_irn_link(node, NULL);
639 * Analyse a Call address.
641 * @param node the node representing the call address
642 * @param methods after call contains the set of all possibly called entities
644 static void callee_ana_node(ir_node *node, eset *methods) {
647 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
648 /* Beware of recursion */
649 if (get_irn_link(node) == MARK) {
650 /* already visited */
653 set_irn_link(node, MARK);
655 switch (get_irn_opcode(node)) {
657 /* A direct address call. We tread this as an external
658 call and ignore it completely. */
659 eset_insert(methods, unknown_entity); /* free method -> unknown */
662 if (get_SymConst_kind(node) == symconst_addr_ent) {
663 ir_entity *ent = get_SymConst_entity(node);
664 assert(ent && is_method_entity(ent));
665 eset_insert(methods, ent);
667 assert(get_SymConst_kind(node) == symconst_addr_name);
668 /* external method (because fix_symconst()!) */
669 eset_insert(methods, unknown_entity); /* free method -> unknown */
673 /* polymorphic method */
674 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
675 ir_entity *ent = get_Sel_method(node, i);
677 eset_insert(methods, ent);
679 eset_insert(methods, unknown_entity);
689 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
690 callee_ana_node(get_Phi_pred(node, i), methods);
695 callee_ana_node(get_Mux_false(node), methods);
696 callee_ana_node(get_Mux_true(node), methods);
700 for (i = get_Psi_n_conds(node) - 1; i >= 0; --i) {
701 callee_ana_node(get_Psi_val(node, i), methods);
703 callee_ana_node(get_Psi_default(node), methods);
707 callee_ana_node(get_Id_pred(node), methods);
711 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
718 eset_insert(methods, unknown_entity); /* free method -> unknown */
722 assert(0 && "invalid opcode or opcode not implemented");
726 set_irn_link(node, NULL);
730 * Walker: Analyses every Call node and calculates an array of possible
731 * callees for that call.
733 static void callee_walker(ir_node *call, void *env) {
736 eset *methods = eset_create();
741 callee_ana_node(get_Call_ptr(call), methods);
742 arr = NEW_ARR_F(ir_entity *, eset_count(methods));
743 for (i = 0, ent = eset_first(methods); ent; ent = eset_next(methods)) {
745 /* we want the unknown_entity on the zero position for easy tests later */
746 if (ent == unknown_entity) {
748 arr[0] = unknown_entity;
752 set_Call_callee_arr(call, ARR_LEN(arr), arr);
754 eset_destroy(methods);
759 * Walker: Removes all tuple.
761 static void remove_Tuples(ir_node *proj, void *env) {
764 if (! is_Proj(proj)) return;
766 nn = skip_Tuple(proj);
767 if (nn != proj) exchange(proj, nn);
772 * Determine for every Call the set of possibly called methods and stores it
773 * inside the Call (@see set_Call_callee()).
774 * Uses the sel_methods set with much be already calculated.
776 static void callee_ana(void) {
778 /* analyse all graphs */
779 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
780 ir_graph *irg = get_irp_irg(i);
781 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
782 set_irg_callee_info_state(irg, irg_callee_info_consistent);
784 set_irp_callee_info_state(irg_callee_info_consistent);
787 /*--------------------------------------------------------------------------*/
788 /* Cleanup after analyses. */
789 /*--------------------------------------------------------------------------*/
791 /** Frees intermediate data structures. */
792 static void sel_methods_dispose(void) {
795 for (ent = eset_first(entities); ent; ent = eset_next(entities)) {
796 ir_entity ** arr = get_entity_link(ent);
800 set_entity_link(ent, NULL);
802 eset_destroy(entities);
806 /*--------------------------------------------------------------------------*/
807 /* Freeing the callee arrays. */
808 /*--------------------------------------------------------------------------*/
810 static void destruct_walker(ir_node * node, void * env) {
813 remove_Call_callee_arr(node);
817 /*--------------------------------------------------------------------------*/
819 /*--------------------------------------------------------------------------*/
821 void cgana(int *length, ir_entity ***free_methods) {
822 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
824 *free_methods = get_free_methods(length);
826 sel_methods_dispose();
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();