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
26 * Interprocedural analysis to estimate the calling relation.
28 * This analysis computes all entities representing methods that
29 * can be called at a Call node. Further it computes a set of
30 * methods that are 'free', i.e., their adress is handled by
31 * the program directly, or they are visible external.
50 #include "dbginfo_t.h"
51 #include "iropt_dbg.h"
60 /* unambiguous address used as a mark. */
61 static void *MARK = &MARK;
63 static eset *entities = NULL;
65 /*--------------------------------------------------------------------------*/
67 /*--------------------------------------------------------------------------*/
70 /*--------------------------------------------------------------------------*/
71 /* Initialize datastructures, remove unwanted constructs, optimize */
72 /* call target computations. */
73 /*--------------------------------------------------------------------------*/
75 /** Collect the entity representing the implementation of this
76 * method (not the same if inherited) and all entities for overwriting
77 * implementations in parameter set.
78 * A recursive descend in the overwritten relation.
79 * Cycle-free, therefore must terminate.
81 * @param method the overwritten method
82 * @param set A set of entities.
84 * @return Number of entities in set.
86 static size_t collect_impls(ir_entity *method, eset *set)
91 if (get_entity_irg(method) != NULL) {
92 /* has an implementation */
93 eset_insert(set, method);
97 /*- recursive descent -*/
98 for (i = get_entity_n_overwrittenby(method); i > 0;)
99 size += collect_impls(get_entity_overwrittenby(method, --i), set);
104 * Determine all methods that overwrite the given method (and implement it).
105 * The returned array must be freed by the caller (see DEL_ARR_F).
106 * If the set of overwriting methods is empty, returns NULL.
108 * @param method the method
110 static ir_entity **get_impl_methods(ir_entity *method)
113 eset *set = eset_create();
116 /* Collect all method entities that can be called here */
117 size = collect_impls(method, set);
120 /* no overwriting methods found */
124 arr = NEW_ARR_F(ir_entity *, size);
125 for (ent = (ir_entity*) eset_first(set); size > 0; ent = (ir_entity*) eset_next(set))
132 /** Analyze address computations.
134 * Compute for all Sel nodes the set of methods that can be selected.
135 * For each entity we store the set of subentities in the link field.
137 * Further do some optimizations:
138 * - Call standard optimizations for Sel nodes: this removes polymorphic
140 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
141 * For this we precomputed a map name->entity. Nowadays, we no more support
143 * - If the node is a Sel:
144 * If we found only a single method that can be called, replace the Sel
145 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
146 * as here we walk the type graph. In opt_polymorphy we only apply a local
149 * @param node The node to analyze
150 * @param env A map that maps names of entities to the entities.
152 static void sel_methods_walker(ir_node *node, void *env)
157 /* Call standard optimizations */
159 ir_node *new_node = optimize_in_place(node);
160 if (node != new_node) {
161 exchange(node, new_node);
166 if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
167 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
169 if (!eset_contains(entities, ent)) {
170 /* Entity not yet handled. Find all (internal or external)
171 * implemented methods that overwrites this entity.
172 * This set is stored in the entity link. */
173 set_entity_link(ent, get_impl_methods(ent));
174 eset_insert(entities, ent);
177 /* -- As an add on we get an optimization that removes polymorphic calls.
178 This optimization is more powerful than that in transform_node_Sel(). -- */
179 arr = (ir_entity**) get_entity_link(ent);
182 * The Sel node never returns a pointer to a usable method.
183 * We could not call it, but it may be description:
184 * We call a method in a dead part of the program.
186 assert(get_entity_irg(ent) == NULL);
188 else if (get_opt_closed_world() && get_opt_dyn_meth_dispatch() &&
189 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
193 * The Sel node returns only one possible method.
194 * So we could replace the Sel node by a SymConst.
195 * This method must exists.
197 assert(get_entity_irg(arr[0]) != NULL);
198 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]), get_nodes_block(node));
199 DBG_OPT_POLY(node, new_node);
200 exchange(node, new_node);
206 * Initialize auxiliary data structures.
208 * Computes a set of entities that overwrite an entity and contain
209 * an implementation. The set is stored in the entity's link field.
211 * Further replaces Sel nodes where this set contains exactly one
212 * method by SymConst nodes.
213 * Finally asserts if there is a SymConst(name) if there could be a
216 static void sel_methods_init(void)
219 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
220 SymConst(name) by SymConst(ent). */
221 assert(entities == NULL);
222 entities = eset_create();
223 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
224 ir_entity * ent = get_irg_entity(get_irp_irg(i));
225 /* only external visible methods are allowed to call by a SymConst_ptr_name */
226 if (entity_is_externally_visible(ent)) {
227 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
231 all_irg_walk(sel_methods_walker, NULL, NULL);
232 pmap_destroy(ldname_map);
235 /*--------------------------------------------------------------------------*/
236 /* Find free methods.
238 * We expect that each entity has an array with all implementations in its
240 /*--------------------------------------------------------------------------*/
243 * Returns an array of all methods that could be called at a Sel node.
244 * This array contains every entry only once.
246 * @param sel the Sel node
248 static ir_entity ** get_Sel_arr(ir_node * sel)
250 static ir_entity ** NULL_ARRAY = NULL;
255 ent = get_Sel_entity(sel);
257 assert(is_Method_type(get_entity_type(ent))); /* what else? */
258 arr = (ir_entity**) get_entity_link(ent);
262 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
263 * kann f�r polymorphe (abstrakte) Methoden passieren. */
265 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
272 * Returns the number of possible called methods at a Sel node.
274 * @param sel the Sel node
276 static size_t get_Sel_n_methods(ir_node * sel)
278 return ARR_LEN(get_Sel_arr(sel));
282 * Returns the ith possible called method entity at a Sel node.
284 static ir_entity * get_Sel_method(ir_node * sel, size_t pos)
286 ir_entity ** arr = get_Sel_arr(sel);
287 assert(pos < ARR_LEN(arr));
292 static void free_mark(ir_node * node, eset * set);
294 static void free_mark_proj(ir_node * node, long n, eset * set)
296 assert(get_irn_mode(node) == mode_T);
297 if (get_irn_link(node) == MARK) {
298 /* already visited */
301 set_irn_link(node, MARK);
302 switch (get_irn_opcode(node)) {
304 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
305 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
307 ir_node * pred = get_Proj_pred(node);
308 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
309 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
311 /* nothing: da in "free_ana_walker" behandelt. */
317 free_mark(get_Tuple_pred(node, n), set);
321 free_mark_proj(get_Id_pred(node), n, set);
327 /* nothing: Die Operationen werden in free_ana_walker() selbst
332 assert(0 && "unexpected opcode or opcode not implemented");
335 // set_irn_link(node, NULL);
339 * Called for predecessors nodes of "interesting" ones.
340 * Interesting ones include all nodes that can somehow make
343 * If a method (or a set of methods in case of polymorph calls) gets visible,
344 * add it to the set of 'free' methods
346 * @param node the current visited node
347 * @param set the set of all free methods
349 static void free_mark(ir_node *node, eset * set)
351 if (get_irn_link(node) == MARK)
352 return; /* already visited */
354 set_irn_link(node, MARK);
356 switch (get_irn_opcode(node)) {
358 ir_entity *ent = get_Sel_entity(node);
359 if (is_method_entity(ent)) {
361 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
362 eset_insert(set, get_Sel_method(node, i));
368 if (get_SymConst_kind(node) == symconst_addr_ent) {
369 ir_entity *ent = get_SymConst_entity(node);
370 if (is_method_entity(ent)) {
371 eset_insert(set, ent);
379 for (i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
380 free_mark(get_Phi_pred(node, i), set);
385 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
394 * post-walker. Find method addresses.
396 static void free_ana_walker(ir_node *node, void *env)
398 eset *set = (eset*) env;
400 if (get_irn_link(node) == MARK) {
401 /* already visited */
404 switch (get_irn_opcode(node)) {
418 /* we must handle Call nodes specially, because their call address input
419 do not expose a method address. */
420 set_irn_link(node, MARK);
421 for (i = 0, n = get_Call_n_params(node); i < n; ++i) {
422 ir_node *pred = get_Call_param(node, i);
423 if (mode_is_reference(get_irn_mode(pred))) {
424 free_mark(pred, set);
431 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
432 * jemand das Gegenteil implementiert. */
433 set_irn_link(node, MARK);
434 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
435 ir_node *pred = get_irn_n(node, i);
436 if (mode_is_reference(get_irn_mode(pred))) {
437 free_mark(pred, set);
446 * Add all method addresses in global new style initializers to the set.
449 * We do NOT check the type here, just if it's an entity address.
450 * The reason for this is code like:
452 * void *p = function;
454 * which is sometimes used to anchor functions.
456 static void add_method_address_inititializer(ir_initializer_t *initializer,
462 switch (initializer->kind) {
463 case IR_INITIALIZER_CONST:
464 n = initializer->consti.value;
466 /* let's check if it's the address of a function */
467 if (is_SymConst_addr_ent(n)) {
468 ir_entity *ent = get_SymConst_entity(n);
470 if (is_Method_type(get_entity_type(ent)))
471 eset_insert(set, ent);
474 case IR_INITIALIZER_TARVAL:
475 case IR_INITIALIZER_NULL:
477 case IR_INITIALIZER_COMPOUND:
478 for (i = 0; i < initializer->compound.n_initializers; ++i) {
479 ir_initializer_t *sub_initializer
480 = initializer->compound.initializers[i];
481 add_method_address_inititializer(sub_initializer, set);
485 panic("invalid initializer found");
489 * Add all method addresses in global initializers to the set.
492 * We do NOT check the type here, just if it's an entity address.
493 * The reason for this is code like:
495 * void *p = function;
497 * which is sometimes used to anchor functions.
499 static void add_method_address(ir_entity *ent, eset *set)
503 /* ignore methods: these of course reference their addresses
504 * TODO: remove this later once this incorrect self-initialisation is gone
506 tp = get_entity_type(ent);
507 if (is_Method_type(tp))
510 if (ent->initializer != NULL) {
511 add_method_address_inititializer(get_entity_initializer(ent), set);
512 } else if (entity_has_compound_ent_values(ent)) {
514 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
515 ir_node *irn = get_compound_ent_value(ent, i);
517 /* let's check if it's the address of a function */
518 if (is_SymConst_addr_ent(irn)) {
519 ir_entity *ent2 = get_SymConst_entity(irn);
521 if (is_Method_type(get_entity_type(ent2)))
522 eset_insert(set, ent2);
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 size_t get_free_methods(ir_entity ***free_methods)
540 eset *free_set = eset_create();
548 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
550 irg = get_irp_irg(i);
551 ent = get_irg_entity(irg);
552 linkage = get_entity_linkage(ent);
554 if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
555 eset_insert(free_set, ent);
558 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
559 /* Find all method entities that gets "visible" through this graphs,
560 * for instance because their address is stored. */
561 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
562 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
565 /* insert all methods that are used in global variables initializers */
566 tp = get_glob_type();
567 for (j = 0, m = get_class_n_members(tp); j < m; ++j) {
568 ent = get_class_member(tp, j);
569 add_method_address(ent, free_set);
572 for (j = 0, m = get_compound_n_members(tp); j < m; ++j) {
573 ent = get_compound_member(tp, j);
574 add_method_address(ent, free_set);
577 /* the main program is even then "free", if it's not external visible. */
578 irg = get_irp_main_irg();
580 eset_insert(free_set, get_irg_entity(irg));
582 /* Finally, transform the set into an array. */
583 length = eset_count(free_set);
584 arr = XMALLOCN(ir_entity*, length);
585 for (i = 0, ent = (ir_entity*) eset_first(free_set); ent; ent = (ir_entity*) eset_next(free_set)) {
588 eset_destroy(free_set);
594 /*--------------------------------------------------------------------------*/
595 /* Callee analysis. */
596 /*--------------------------------------------------------------------------*/
598 static void callee_ana_node(ir_node * node, eset * methods);
600 static void callee_ana_proj(ir_node *node, long n, eset *methods)
602 assert(get_irn_mode(node) == mode_T);
603 if (get_irn_link(node) == MARK) {
604 /* already visited */
607 set_irn_link(node, MARK);
609 switch (get_irn_opcode(node)) {
611 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
612 * op_Tuple oder ein Knoten, der eine "freie Methode"
614 ir_node *pred = get_Proj_pred(node);
615 if (get_irn_link(pred) != MARK) {
616 if (is_Tuple(pred)) {
617 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
619 eset_insert(methods, unknown_entity); /* free method -> unknown */
626 callee_ana_node(get_Tuple_pred(node, n), methods);
630 eset_insert(methods, unknown_entity); /* free method -> unknown */
636 * Analyse a Call address.
638 * @param node the node representing the call address
639 * @param methods after call contains the set of all possibly called entities
641 static void callee_ana_node(ir_node *node, eset *methods)
643 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
644 /* Beware of recursion */
645 if (get_irn_link(node) == MARK) {
646 /* already visited */
649 set_irn_link(node, MARK);
651 switch (get_irn_opcode(node)) {
653 /* A direct address call. We tread this as an external
654 call and ignore it completely. */
655 eset_insert(methods, unknown_entity); /* free method -> unknown */
659 ir_entity *ent = get_SymConst_entity(node);
660 assert(ent && is_method_entity(ent));
661 eset_insert(methods, ent);
666 /* polymorphic method */
668 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
669 ir_entity *ent = get_Sel_method(node, i);
671 eset_insert(methods, ent);
673 eset_insert(methods, unknown_entity);
685 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
686 callee_ana_node(get_Phi_pred(node, i), methods);
692 callee_ana_node(get_Mux_false(node), methods);
693 callee_ana_node(get_Mux_true(node), methods);
697 callee_ana_node(get_Id_pred(node), methods);
701 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
708 eset_insert(methods, unknown_entity); /* free method -> unknown */
712 assert(0 && "invalid opcode or opcode not implemented");
718 * Walker: Analyses every Call node and calculates an array of possible
719 * callees for that call.
721 static void callee_walker(ir_node *call, void *env)
725 eset *methods = eset_create();
730 callee_ana_node(get_Call_ptr(call), methods);
731 arr = NEW_ARR_F(ir_entity *, eset_count(methods));
732 for (i = 0, ent = (ir_entity*) eset_first(methods); ent; ent = (ir_entity*) eset_next(methods)) {
734 /* we want the unknown_entity on the zero position for easy tests later */
735 if (ent == unknown_entity) {
737 arr[0] = unknown_entity;
741 set_Call_callee_arr(call, ARR_LEN(arr), arr);
743 eset_destroy(methods);
748 * Walker: Removes all tuple.
750 static void remove_Tuples(ir_node *proj, void *env)
754 if (! is_Proj(proj)) return;
756 nn = skip_Tuple(proj);
757 if (nn != proj) exchange(proj, nn);
762 * Determine for every Call the set of possibly called methods and stores it
763 * inside the Call (@see set_Call_callee()).
764 * Uses the sel_methods set with much be already calculated.
766 static void callee_ana(void)
769 /* analyse all graphs */
770 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
771 ir_graph *irg = get_irp_irg(i);
772 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
773 set_irg_callee_info_state(irg, irg_callee_info_consistent);
775 set_irp_callee_info_state(irg_callee_info_consistent);
778 /*--------------------------------------------------------------------------*/
779 /* Cleanup after analyses. */
780 /*--------------------------------------------------------------------------*/
782 /** Frees intermediate data structures. */
783 static void sel_methods_dispose(void)
787 for (ent = (ir_entity*) eset_first(entities); ent; ent = (ir_entity*) eset_next(entities)) {
788 ir_entity ** arr = (ir_entity**) get_entity_link(ent);
792 set_entity_link(ent, NULL);
794 eset_destroy(entities);
798 /*--------------------------------------------------------------------------*/
799 /* Freeing the callee arrays. */
800 /*--------------------------------------------------------------------------*/
802 static void destruct_walker(ir_node * node, void * env)
806 remove_Call_callee_arr(node);
810 /*--------------------------------------------------------------------------*/
812 /*--------------------------------------------------------------------------*/
814 size_t cgana(ir_entity ***free_methods)
817 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
819 length = get_free_methods(free_methods);
821 sel_methods_dispose();
825 void free_callee_info(ir_graph *irg)
827 irg_walk_graph(irg, destruct_walker, NULL, NULL);
828 set_irg_callee_info_state(irg, irg_callee_info_none);
831 void free_irp_callee_info(void)
834 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
835 free_callee_info(get_irp_irg(i));
839 /* Optimize the address expressions passed to call nodes.
841 * This optimization performs the following transformations for
843 * - All SymConst operations that refer to intern methods are replaced
844 * by Const operations referring to the corresponding entity.
845 * - Sel nodes, that select entities that are not overwritten are
846 * replaced by Const nodes referring to the selected entity.
847 * - Sel nodes, for which no method exists at all are replaced by Bad
849 * - Sel nodes with a pointer input that is an Alloc node are replaced
850 * by Const nodes referring to the entity that implements the method in
851 * the type given by the Alloc node.
853 void opt_call_addrs(void)
856 sel_methods_dispose();