2 * Copyright (C) 1995-2011 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.
52 #include "dbginfo_t.h"
53 #include "iropt_dbg.h"
62 /* unambiguous address used as a mark. */
63 static void *MARK = &MARK;
65 static eset *entities = NULL;
67 /*--------------------------------------------------------------------------*/
69 /*--------------------------------------------------------------------------*/
72 /*--------------------------------------------------------------------------*/
73 /* Initialize datastructures, remove unwanted constructs, optimize */
74 /* call target computations. */
75 /*--------------------------------------------------------------------------*/
77 /** Collect the entity representing the implementation of this
78 * method (not the same if inherited) and all entities for overwriting
79 * implementations in parameter set.
80 * A recursive descend in the overwritten relation.
81 * Cycle-free, therefore must terminate.
83 * @param method the overwritten method
84 * @param set A set of entities.
86 * @return Number of entities in set.
88 static size_t collect_impls(ir_entity *method, eset *set)
93 if (get_entity_irg(method) != NULL) {
94 /* has an implementation */
95 eset_insert(set, method);
99 /*- recursive descent -*/
100 for (i = get_entity_n_overwrittenby(method); i > 0;)
101 size += collect_impls(get_entity_overwrittenby(method, --i), set);
106 * Determine all methods that overwrite the given method (and implement it).
107 * The returned array must be freed by the caller (see DEL_ARR_F).
108 * If the set of overwriting methods is empty, returns NULL.
110 * @param method the method
112 static ir_entity **get_impl_methods(ir_entity *method)
115 eset *set = eset_create();
118 /* Collect all method entities that can be called here */
119 size = collect_impls(method, set);
122 /* no overwriting methods found */
126 arr = NEW_ARR_F(ir_entity *, size);
127 for (ent = (ir_entity*) eset_first(set); size > 0; ent = (ir_entity*) eset_next(set))
134 /** Analyze address computations.
136 * Compute for all Sel nodes the set of methods that can be selected.
137 * For each entity we store the set of subentities in the link field.
139 * Further do some optimizations:
140 * - Call standard optimizations for Sel nodes: this removes polymorphic
142 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
143 * For this we precomputed a map name->entity. Nowadays, we no more support
145 * - If the node is a Sel:
146 * If we found only a single method that can be called, replace the Sel
147 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
148 * as here we walk the type graph. In opt_polymorphy we only apply a local
151 * @param node The node to analyze
152 * @param env A map that maps names of entities to the entities.
154 static void sel_methods_walker(ir_node *node, void *env)
159 /* Call standard optimizations */
161 ir_node *new_node = optimize_in_place(node);
162 if (node != new_node) {
163 exchange(node, new_node);
168 if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
169 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
171 if (!eset_contains(entities, ent)) {
172 /* Entity not yet handled. Find all (internal or external)
173 * implemented methods that overwrites this entity.
174 * This set is stored in the entity link. */
175 set_entity_link(ent, get_impl_methods(ent));
176 eset_insert(entities, ent);
179 /* -- As an add on we get an optimization that removes polymorphic calls.
180 This optimization is more powerful than that in transform_node_Sel(). -- */
181 arr = (ir_entity**) get_entity_link(ent);
184 * The Sel node never returns a pointer to a usable method.
185 * We could not call it, but it may be description:
186 * We call a method in a dead part of the program.
188 assert(get_entity_irg(ent) == NULL);
190 else if (get_opt_closed_world() && get_opt_dyn_meth_dispatch() &&
191 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
195 * The Sel node returns only one possible method.
196 * So we could replace the Sel node by a SymConst.
197 * This method must exists.
199 assert(get_entity_irg(arr[0]) != NULL);
200 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]), get_nodes_block(node));
201 DBG_OPT_POLY(node, new_node);
202 exchange(node, new_node);
208 * Initialize auxiliary data structures.
210 * Computes a set of entities that overwrite an entity and contain
211 * an implementation. The set is stored in the entity's link field.
213 * Further replaces Sel nodes where this set contains exactly one
214 * method by SymConst nodes.
215 * Finally asserts if there is a SymConst(name) if there could be a
218 static void sel_methods_init(void)
221 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
222 SymConst(name) by SymConst(ent). */
223 assert(entities == NULL);
224 entities = eset_create();
225 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
226 ir_entity * ent = get_irg_entity(get_irp_irg(i));
227 /* only external visible methods are allowed to call by a SymConst_ptr_name */
228 if (entity_is_externally_visible(ent)) {
229 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
233 all_irg_walk(sel_methods_walker, NULL, NULL);
234 pmap_destroy(ldname_map);
237 /*--------------------------------------------------------------------------*/
238 /* Find free methods.
240 * We expect that each entity has an array with all implementations in its
242 /*--------------------------------------------------------------------------*/
245 * Returns an array of all methods that could be called at a Sel node.
246 * This array contains every entry only once.
248 * @param sel the Sel node
250 static ir_entity ** get_Sel_arr(ir_node * sel)
252 static ir_entity ** NULL_ARRAY = NULL;
257 ent = get_Sel_entity(sel);
259 assert(is_Method_type(get_entity_type(ent))); /* what else? */
260 arr = (ir_entity**) get_entity_link(ent);
264 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
265 * kann f�r polymorphe (abstrakte) Methoden passieren. */
267 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
274 * Returns the number of possible called methods at a Sel node.
276 * @param sel the Sel node
278 static size_t get_Sel_n_methods(ir_node * sel)
280 return ARR_LEN(get_Sel_arr(sel));
284 * Returns the ith possible called method entity at a Sel node.
286 static ir_entity * get_Sel_method(ir_node * sel, size_t pos)
288 ir_entity ** arr = get_Sel_arr(sel);
289 assert(pos < ARR_LEN(arr));
294 static void free_mark(ir_node * node, eset * set);
296 static void free_mark_proj(ir_node * node, long n, eset * set)
298 assert(get_irn_mode(node) == mode_T);
299 if (get_irn_link(node) == MARK) {
300 /* already visited */
303 set_irn_link(node, MARK);
304 switch (get_irn_opcode(node)) {
306 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
307 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
309 ir_node * pred = get_Proj_pred(node);
310 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
311 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
313 /* nothing: da in "free_ana_walker" behandelt. */
319 free_mark(get_Tuple_pred(node, n), set);
323 free_mark_proj(get_Id_pred(node), n, set);
329 /* nothing: Die Operationen werden in free_ana_walker() selbst
334 assert(0 && "unexpected opcode or opcode not implemented");
337 // set_irn_link(node, NULL);
341 * Called for predecessors nodes of "interesting" ones.
342 * Interesting ones include all nodes that can somehow make
345 * If a method (or a set of methods in case of polymorph calls) gets visible,
346 * add it to the set of 'free' methods
348 * @param node the current visited node
349 * @param set the set of all free methods
351 static void free_mark(ir_node *node, eset * set)
355 if (get_irn_link(node) == MARK)
356 return; /* already visited */
358 set_irn_link(node, MARK);
360 switch (get_irn_opcode(node)) {
362 ir_entity *ent = get_Sel_entity(node);
363 if (is_method_entity(ent)) {
364 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
365 eset_insert(set, get_Sel_method(node, i));
371 if (get_SymConst_kind(node) == symconst_addr_ent) {
372 ir_entity *ent = get_SymConst_entity(node);
373 if (is_method_entity(ent)) {
374 eset_insert(set, ent);
382 for (i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
383 free_mark(get_Phi_pred(node, i), set);
388 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
397 * post-walker. Find method addresses.
399 static void free_ana_walker(ir_node *node, void *env)
401 eset *set = (eset*) env;
404 if (get_irn_link(node) == MARK) {
405 /* already visited */
408 switch (get_irn_opcode(node)) {
422 /* we must handle Call nodes specially, because their call address input
423 do not expose a method address. */
424 set_irn_link(node, MARK);
425 for (i = 0, n = get_Call_n_params(node); i < n; ++i) {
426 ir_node *pred = get_Call_param(node, i);
427 if (mode_is_reference(get_irn_mode(pred))) {
428 free_mark(pred, set);
434 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
435 * jemand das Gegenteil implementiert. */
436 set_irn_link(node, MARK);
437 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
438 ir_node *pred = get_irn_n(node, i);
439 if (mode_is_reference(get_irn_mode(pred))) {
440 free_mark(pred, set);
448 * Add all method addresses in global new style initializers to the set.
451 * We do NOT check the type here, just if it's an entity address.
452 * The reason for this is code like:
454 * void *p = function;
456 * which is sometimes used to anchor functions.
458 static void add_method_address_inititializer(ir_initializer_t *initializer,
464 switch (initializer->kind) {
465 case IR_INITIALIZER_CONST:
466 n = initializer->consti.value;
468 /* let's check if it's the address of a function */
470 ir_entity *ent = get_Global_entity(n);
472 if (is_Method_type(get_entity_type(ent)))
473 eset_insert(set, ent);
476 case IR_INITIALIZER_TARVAL:
477 case IR_INITIALIZER_NULL:
479 case IR_INITIALIZER_COMPOUND:
480 for (i = 0; i < initializer->compound.n_initializers; ++i) {
481 ir_initializer_t *sub_initializer
482 = initializer->compound.initializers[i];
483 add_method_address_inititializer(sub_initializer, set);
487 panic("invalid initializer found");
491 * Add all method addresses in global initializers to the set.
494 * We do NOT check the type here, just if it's an entity address.
495 * The reason for this is code like:
497 * void *p = function;
499 * which is sometimes used to anchor functions.
501 static void add_method_address(ir_entity *ent, eset *set)
505 /* ignore methods: these of course reference their addresses
506 * TODO: remove this later once this incorrect self-initialisation is gone
508 tp = get_entity_type(ent);
509 if (is_Method_type(tp))
512 if (ent->initializer != NULL) {
513 add_method_address_inititializer(get_entity_initializer(ent), set);
514 } else if (entity_has_compound_ent_values(ent)) {
516 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
517 ir_node *irn = get_compound_ent_value(ent, i);
519 /* let's check if it's the address of a function */
520 if (is_Global(irn)) {
521 ir_entity *ent = get_Global_entity(irn);
523 if (is_Method_type(get_entity_type(ent)))
524 eset_insert(set, ent);
531 * returns a list of 'free' methods, i.e., the methods that can be called
532 * from external or via function pointers.
534 * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
535 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
536 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
537 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
538 * auf eine echt externe Methode.
540 static size_t get_free_methods(ir_entity ***free_methods)
542 eset *free_set = eset_create();
550 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
552 irg = get_irp_irg(i);
553 ent = get_irg_entity(irg);
554 linkage = get_entity_linkage(ent);
556 if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
557 eset_insert(free_set, ent);
560 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
561 /* Find all method entities that gets "visible" through this graphs,
562 * for instance because their address is stored. */
563 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
564 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
567 /* insert all methods that are used in global variables initializers */
568 tp = get_glob_type();
569 for (j = 0, m = get_class_n_members(tp); j < m; ++j) {
570 ent = get_class_member(tp, j);
571 add_method_address(ent, free_set);
574 for (j = 0, m = get_compound_n_members(tp); j < m; ++j) {
575 ent = get_compound_member(tp, j);
576 add_method_address(ent, free_set);
579 /* the main program is even then "free", if it's not external visible. */
580 irg = get_irp_main_irg();
582 eset_insert(free_set, get_irg_entity(irg));
584 /* Finally, transform the set into an array. */
585 length = eset_count(free_set);
586 arr = XMALLOCN(ir_entity*, length);
587 for (i = 0, ent = (ir_entity*) eset_first(free_set); ent; ent = (ir_entity*) eset_next(free_set)) {
590 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)
604 assert(get_irn_mode(node) == mode_T);
605 if (get_irn_link(node) == MARK) {
606 /* already visited */
609 set_irn_link(node, MARK);
611 switch (get_irn_opcode(node)) {
613 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
614 * op_Tuple oder ein Knoten, der eine "freie Methode"
616 ir_node *pred = get_Proj_pred(node);
617 if (get_irn_link(pred) != MARK) {
618 if (is_Tuple(pred)) {
619 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
621 eset_insert(methods, unknown_entity); /* free method -> unknown */
628 callee_ana_node(get_Tuple_pred(node, n), methods);
632 eset_insert(methods, unknown_entity); /* free method -> unknown */
638 * Analyse a Call address.
640 * @param node the node representing the call address
641 * @param methods after call contains the set of all possibly called entities
643 static void callee_ana_node(ir_node *node, eset *methods)
645 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
646 /* Beware of recursion */
647 if (get_irn_link(node) == MARK) {
648 /* already visited */
651 set_irn_link(node, MARK);
653 switch (get_irn_opcode(node)) {
655 /* A direct address call. We tread this as an external
656 call and ignore it completely. */
657 eset_insert(methods, unknown_entity); /* free method -> unknown */
661 ir_entity *ent = get_SymConst_entity(node);
662 assert(ent && is_method_entity(ent));
663 eset_insert(methods, ent);
668 /* polymorphic method */
670 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
671 ir_entity *ent = get_Sel_method(node, i);
673 eset_insert(methods, ent);
675 eset_insert(methods, unknown_entity);
687 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
688 callee_ana_node(get_Phi_pred(node, i), methods);
694 callee_ana_node(get_Mux_false(node), methods);
695 callee_ana_node(get_Mux_true(node), methods);
699 callee_ana_node(get_Id_pred(node), methods);
703 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
710 eset_insert(methods, unknown_entity); /* free method -> unknown */
714 assert(0 && "invalid opcode or opcode not implemented");
720 * Walker: Analyses every Call node and calculates an array of possible
721 * callees for that call.
723 static void callee_walker(ir_node *call, void *env)
727 eset *methods = eset_create();
732 callee_ana_node(get_Call_ptr(call), methods);
733 arr = NEW_ARR_F(ir_entity *, eset_count(methods));
734 for (i = 0, ent = (ir_entity*) eset_first(methods); ent; ent = (ir_entity*) eset_next(methods)) {
736 /* we want the unknown_entity on the zero position for easy tests later */
737 if (ent == unknown_entity) {
739 arr[0] = unknown_entity;
743 set_Call_callee_arr(call, ARR_LEN(arr), arr);
745 eset_destroy(methods);
750 * Walker: Removes all tuple.
752 static void remove_Tuples(ir_node *proj, void *env)
756 if (! is_Proj(proj)) return;
758 nn = skip_Tuple(proj);
759 if (nn != proj) exchange(proj, nn);
764 * Determine for every Call the set of possibly called methods and stores it
765 * inside the Call (@see set_Call_callee()).
766 * Uses the sel_methods set with much be already calculated.
768 static void callee_ana(void)
771 /* analyse all graphs */
772 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
773 ir_graph *irg = get_irp_irg(i);
774 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
775 set_irg_callee_info_state(irg, irg_callee_info_consistent);
777 set_irp_callee_info_state(irg_callee_info_consistent);
780 /*--------------------------------------------------------------------------*/
781 /* Cleanup after analyses. */
782 /*--------------------------------------------------------------------------*/
784 /** Frees intermediate data structures. */
785 static void sel_methods_dispose(void)
789 for (ent = (ir_entity*) eset_first(entities); ent; ent = (ir_entity*) eset_next(entities)) {
790 ir_entity ** arr = (ir_entity**) get_entity_link(ent);
794 set_entity_link(ent, NULL);
796 eset_destroy(entities);
800 /*--------------------------------------------------------------------------*/
801 /* Freeing the callee arrays. */
802 /*--------------------------------------------------------------------------*/
804 static void destruct_walker(ir_node * node, void * env)
808 remove_Call_callee_arr(node);
812 /*--------------------------------------------------------------------------*/
814 /*--------------------------------------------------------------------------*/
816 size_t cgana(ir_entity ***free_methods)
819 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
821 length = get_free_methods(free_methods);
823 sel_methods_dispose();
827 void free_callee_info(ir_graph *irg)
829 irg_walk_graph(irg, destruct_walker, NULL, NULL);
830 set_irg_callee_info_state(irg, irg_callee_info_none);
833 void free_irp_callee_info(void)
836 for (i = 0, n = get_irp_n_irgs(); i < n; ++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)
858 sel_methods_dispose();