2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Intraprozedural analyses to estimate the call graph.
9 * @author Hubert Schmid
12 * Interprocedural analysis to estimate the calling relation.
14 * This analysis computes all entities representing methods that
15 * can be called at a Call node. Further it computes a set of
16 * methods that are 'free', i.e., their adress is handled by
17 * the program directly, or they are visible external.
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
45 /* unambiguous address used as a mark. */
46 static void *MARK = &MARK;
48 static pset *entities = NULL;
50 /*--------------------------------------------------------------------------*/
52 /*--------------------------------------------------------------------------*/
55 /*--------------------------------------------------------------------------*/
56 /* Initialize datastructures, remove unwanted constructs, optimize */
57 /* call target computations. */
58 /*--------------------------------------------------------------------------*/
60 /** Collect the entity representing the implementation of this
61 * method (not the same if inherited) and all entities for overwriting
62 * implementations in parameter set.
63 * A recursive descend in the overwritten relation.
64 * Cycle-free, therefore must terminate.
66 * @param method the overwritten method
67 * @param set A set of entities.
69 * @return Number of entities in set.
71 static size_t collect_impls(ir_entity *method, pset *set)
76 if (get_entity_irg(method) != NULL) {
77 /* has an implementation */
78 pset_insert_ptr(set, method);
82 /*- recursive descent -*/
83 for (i = get_entity_n_overwrittenby(method); i > 0;)
84 size += collect_impls(get_entity_overwrittenby(method, --i), set);
89 * Determine all methods that overwrite the given method (and implement it).
90 * The returned array must be freed by the caller (see DEL_ARR_F).
91 * If the set of overwriting methods is empty, returns NULL.
93 * @param method the method
95 static ir_entity **get_impl_methods(ir_entity *method)
98 pset *set = pset_new_ptr_default();
101 /* Collect all method entities that can be called here */
102 size = collect_impls(method, set);
105 /* no overwriting methods found */
108 arr = NEW_ARR_F(ir_entity *, size);
109 foreach_pset(set, ir_entity, ent) {
117 /** Analyze address computations.
119 * Compute for all Sel nodes the set of methods that can be selected.
120 * For each entity we store the set of subentities in the link field.
122 * Further do some optimizations:
123 * - Call standard optimizations for Sel nodes: this removes polymorphic
125 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
126 * For this we precomputed a map name->entity. Nowadays, we no more support
128 * - If the node is a Sel:
129 * If we found only a single method that can be called, replace the Sel
130 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
131 * as here we walk the type graph. In opt_polymorphy we only apply a local
134 * @param node The node to analyze
135 * @param env A map that maps names of entities to the entities.
137 static void sel_methods_walker(ir_node *node, void *env)
142 /* Call standard optimizations */
144 ir_node *new_node = optimize_in_place(node);
145 if (node != new_node) {
146 exchange(node, new_node);
151 if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
152 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
154 if (!pset_find_ptr(entities, ent)) {
155 /* Entity not yet handled. Find all (internal or external)
156 * implemented methods that overwrites this entity.
157 * This set is stored in the entity link. */
158 set_entity_link(ent, get_impl_methods(ent));
159 pset_insert_ptr(entities, ent);
162 /* -- As an add on we get an optimization that removes polymorphic calls.
163 This optimization is more powerful than that in transform_node_Sel(). -- */
164 arr = (ir_entity**) get_entity_link(ent);
167 * The Sel node never returns a pointer to a usable method.
168 * We could not call it, but it may be description:
169 * We call a method in a dead part of the program.
171 assert(get_entity_irg(ent) == NULL);
177 * Initialize auxiliary data structures.
179 * Computes a set of entities that overwrite an entity and contain
180 * an implementation. The set is stored in the entity's link field.
182 * Further replaces Sel nodes where this set contains exactly one
183 * method by SymConst nodes.
184 * Finally asserts if there is a SymConst(name) if there could be a
187 static void sel_methods_init(void)
190 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
191 SymConst(name) by SymConst(ent). */
192 assert(entities == NULL);
193 entities = pset_new_ptr_default();
194 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
195 ir_entity * ent = get_irg_entity(get_irp_irg(i));
196 /* only external visible methods are allowed to call by a SymConst_ptr_name */
197 if (entity_is_externally_visible(ent)) {
198 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
202 all_irg_walk(sel_methods_walker, NULL, NULL);
203 pmap_destroy(ldname_map);
206 /*--------------------------------------------------------------------------*/
207 /* Find free methods.
209 * We expect that each entity has an array with all implementations in its
211 /*--------------------------------------------------------------------------*/
214 * Returns an array of all methods that could be called at a Sel node.
215 * This array contains every entry only once.
217 * @param sel the Sel node
219 static ir_entity ** get_Sel_arr(ir_node * sel)
221 static ir_entity ** NULL_ARRAY = NULL;
223 ir_entity *const ent = get_Sel_entity(sel);
224 assert(is_Method_type(get_entity_type(ent))); /* what else? */
226 ir_entity **const arr = (ir_entity**)get_entity_link(ent);
230 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
231 * kann f�r polymorphe (abstrakte) Methoden passieren. */
233 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
240 * Returns the number of possible called methods at a Sel node.
242 * @param sel the Sel node
244 static size_t get_Sel_n_methods(ir_node * sel)
246 return ARR_LEN(get_Sel_arr(sel));
250 * Returns the ith possible called method entity at a Sel node.
252 static ir_entity * get_Sel_method(ir_node * sel, size_t pos)
254 ir_entity ** arr = get_Sel_arr(sel);
255 assert(pos < ARR_LEN(arr));
260 static void free_mark(ir_node *node, pset *set);
262 static void free_mark_proj(ir_node *node, long n, pset *set)
264 assert(get_irn_mode(node) == mode_T);
265 if (get_irn_link(node) == MARK) {
266 /* already visited */
269 set_irn_link(node, MARK);
270 switch (get_irn_opcode(node)) {
272 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
273 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
275 ir_node * pred = get_Proj_pred(node);
276 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
277 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
279 /* nothing: da in "free_ana_walker" behandelt. */
285 free_mark(get_Tuple_pred(node, n), set);
289 free_mark_proj(get_Id_pred(node), n, set);
295 /* nothing: Die Operationen werden in free_ana_walker() selbst
300 assert(0 && "unexpected opcode or opcode not implemented");
303 // set_irn_link(node, NULL);
307 * Called for predecessors nodes of "interesting" ones.
308 * Interesting ones include all nodes that can somehow make
311 * If a method (or a set of methods in case of polymorph calls) gets visible,
312 * add it to the set of 'free' methods
314 * @param node the current visited node
315 * @param set the set of all free methods
317 static void free_mark(ir_node *node, pset *set)
319 if (get_irn_link(node) == MARK)
320 return; /* already visited */
322 set_irn_link(node, MARK);
324 switch (get_irn_opcode(node)) {
326 ir_entity *ent = get_Sel_entity(node);
327 if (is_method_entity(ent)) {
329 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
330 pset_insert_ptr(set, get_Sel_method(node, i));
336 if (get_SymConst_kind(node) == symconst_addr_ent) {
337 ir_entity *ent = get_SymConst_entity(node);
338 if (is_method_entity(ent)) {
339 pset_insert_ptr(set, ent);
347 for (i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
348 free_mark(get_Phi_pred(node, i), set);
353 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
362 * post-walker. Find method addresses.
364 static void free_ana_walker(ir_node *node, void *env)
366 pset *set = (pset*) env;
368 if (get_irn_link(node) == MARK) {
369 /* already visited */
372 switch (get_irn_opcode(node)) {
386 /* we must handle Call nodes specially, because their call address input
387 do not expose a method address. */
388 set_irn_link(node, MARK);
389 for (i = 0, n = get_Call_n_params(node); i < n; ++i) {
390 ir_node *pred = get_Call_param(node, i);
391 if (mode_is_reference(get_irn_mode(pred))) {
392 free_mark(pred, set);
399 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
400 * jemand das Gegenteil implementiert. */
401 set_irn_link(node, MARK);
402 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
403 ir_node *pred = get_irn_n(node, i);
404 if (mode_is_reference(get_irn_mode(pred))) {
405 free_mark(pred, set);
414 * Add all method addresses in global new style initializers to the set.
417 * We do NOT check the type here, just if it's an entity address.
418 * The reason for this is code like:
420 * void *p = function;
422 * which is sometimes used to anchor functions.
424 static void add_method_address_inititializer(ir_initializer_t *initializer,
430 switch (initializer->kind) {
431 case IR_INITIALIZER_CONST:
432 n = initializer->consti.value;
434 /* let's check if it's the address of a function */
435 if (is_SymConst_addr_ent(n)) {
436 ir_entity *ent = get_SymConst_entity(n);
438 if (is_Method_type(get_entity_type(ent)))
439 pset_insert_ptr(set, ent);
442 case IR_INITIALIZER_TARVAL:
443 case IR_INITIALIZER_NULL:
445 case IR_INITIALIZER_COMPOUND:
446 for (i = 0; i < initializer->compound.n_initializers; ++i) {
447 ir_initializer_t *sub_initializer
448 = initializer->compound.initializers[i];
449 add_method_address_inititializer(sub_initializer, set);
453 panic("invalid initializer found");
457 * Add all method addresses in global initializers to the set.
460 * We do NOT check the type here, just if it's an entity address.
461 * The reason for this is code like:
463 * void *p = function;
465 * which is sometimes used to anchor functions.
467 static void add_method_address(ir_entity *ent, pset *set)
471 /* ignore methods: these of course reference their addresses
472 * TODO: remove this later once this incorrect self-initialisation is gone
474 tp = get_entity_type(ent);
475 if (is_Method_type(tp))
478 if (ent->initializer != NULL) {
479 add_method_address_inititializer(get_entity_initializer(ent), set);
484 * returns a list of 'free' methods, i.e., the methods that can be called
485 * from external or via function pointers.
487 * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
488 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
489 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
490 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
491 * auf eine echt externe Methode.
493 static size_t get_free_methods(ir_entity ***free_methods)
495 pset *free_set = pset_new_ptr_default();
502 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
504 irg = get_irp_irg(i);
505 ir_entity *const ent = get_irg_entity(irg);
506 linkage = get_entity_linkage(ent);
508 if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
509 pset_insert_ptr(free_set, ent);
512 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
513 /* Find all method entities that gets "visible" through this graphs,
514 * for instance because their address is stored. */
515 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
516 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
519 /* insert all methods that are used in global variables initializers */
520 tp = get_glob_type();
521 for (j = 0, m = get_class_n_members(tp); j < m; ++j) {
522 ir_entity *const ent = get_class_member(tp, j);
523 add_method_address(ent, free_set);
526 for (j = 0, m = get_compound_n_members(tp); j < m; ++j) {
527 ir_entity *const ent = get_compound_member(tp, j);
528 add_method_address(ent, free_set);
531 /* the main program is even then "free", if it's not external visible. */
532 irg = get_irp_main_irg();
534 pset_insert_ptr(free_set, get_irg_entity(irg));
536 /* Finally, transform the set into an array. */
537 length = pset_count(free_set);
538 arr = XMALLOCN(ir_entity*, length);
540 foreach_pset(free_set, ir_entity, ent) {
549 /*--------------------------------------------------------------------------*/
550 /* Callee analysis. */
551 /*--------------------------------------------------------------------------*/
553 static void callee_ana_node(ir_node *node, pset *methods);
555 static void callee_ana_proj(ir_node *node, long n, pset *methods)
557 assert(get_irn_mode(node) == mode_T);
558 if (get_irn_link(node) == MARK) {
559 /* already visited */
562 set_irn_link(node, MARK);
564 switch (get_irn_opcode(node)) {
566 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
567 * op_Tuple oder ein Knoten, der eine "freie Methode"
569 ir_node *pred = get_Proj_pred(node);
570 if (get_irn_link(pred) != MARK) {
571 if (is_Tuple(pred)) {
572 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
574 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
581 callee_ana_node(get_Tuple_pred(node, n), methods);
585 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
591 * Analyse a Call address.
593 * @param node the node representing the call address
594 * @param methods after call contains the set of all possibly called entities
596 static void callee_ana_node(ir_node *node, pset *methods)
598 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
599 /* Beware of recursion */
600 if (get_irn_link(node) == MARK) {
601 /* already visited */
604 set_irn_link(node, MARK);
606 switch (get_irn_opcode(node)) {
608 /* A direct address call. We tread this as an external
609 call and ignore it completely. */
610 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
614 ir_entity *ent = get_SymConst_entity(node);
615 assert(ent && is_method_entity(ent));
616 pset_insert_ptr(methods, ent);
621 /* polymorphic method */
623 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
624 ir_entity *ent = get_Sel_method(node, i);
626 pset_insert_ptr(methods, ent);
628 pset_insert_ptr(methods, get_unknown_entity());
640 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
641 callee_ana_node(get_Phi_pred(node, i), methods);
647 callee_ana_node(get_Mux_false(node), methods);
648 callee_ana_node(get_Mux_true(node), methods);
652 callee_ana_node(get_Id_pred(node), methods);
656 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
663 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
667 assert(0 && "invalid opcode or opcode not implemented");
673 * Walker: Analyses every Call node and calculates an array of possible
674 * callees for that call.
676 static void callee_walker(ir_node *call, void *env)
680 pset *methods = pset_new_ptr_default();
684 callee_ana_node(get_Call_ptr(call), methods);
685 arr = NEW_ARR_F(ir_entity*, pset_count(methods));
687 foreach_pset(methods, ir_entity, ent) {
689 /* we want the unknown_entity on the zero position for easy tests later */
690 if (is_unknown_entity(ent)) {
692 arr[0] = get_unknown_entity();
696 set_Call_callee_arr(call, ARR_LEN(arr), arr);
703 * Walker: Removes all tuple.
705 static void remove_Tuples(ir_node *proj, void *env)
709 if (! is_Proj(proj)) return;
711 nn = skip_Tuple(proj);
712 if (nn != proj) exchange(proj, nn);
717 * Determine for every Call the set of possibly called methods and stores it
718 * inside the Call (@see set_Call_callee()).
719 * Uses the sel_methods set with much be already calculated.
721 static void callee_ana(void)
724 /* analyse all graphs */
725 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
726 ir_graph *irg = get_irp_irg(i);
727 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
728 set_irg_callee_info_state(irg, irg_callee_info_consistent);
730 set_irp_callee_info_state(irg_callee_info_consistent);
733 /*--------------------------------------------------------------------------*/
734 /* Cleanup after analyses. */
735 /*--------------------------------------------------------------------------*/
737 /** Frees intermediate data structures. */
738 static void sel_methods_dispose(void)
741 foreach_pset(entities, ir_entity, ent) {
742 ir_entity **arr = (ir_entity**) get_entity_link(ent);
746 set_entity_link(ent, NULL);
752 static void destruct_walker(ir_node * node, void * env)
756 remove_Call_callee_arr(node);
760 size_t cgana(ir_entity ***free_methods)
763 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
765 length = get_free_methods(free_methods);
767 sel_methods_dispose();
771 void free_callee_info(ir_graph *irg)
773 irg_walk_graph(irg, destruct_walker, NULL, NULL);
774 set_irg_callee_info_state(irg, irg_callee_info_none);
777 void free_irp_callee_info(void)
780 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
781 free_callee_info(get_irp_irg(i));
785 void opt_call_addrs(void)
787 /* Optimize the address expressions passed to call nodes.
789 * This optimization performs the following transformations for
791 * - All SymConst operations that refer to intern methods are replaced
792 * by Const operations referring to the corresponding entity.
793 * - Sel nodes, that select entities that are not overwritten are
794 * replaced by Const nodes referring to the selected entity.
795 * - Sel nodes, for which no method exists at all are replaced by Bad
797 * - Sel nodes with a pointer input that is an Alloc node are replaced
798 * by Const nodes referring to the entity that implements the method in
799 * the type given by the Alloc node.
802 sel_methods_dispose();