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"
59 /* unambiguous address used as a mark. */
60 static void *MARK = &MARK;
62 static pset *entities = NULL;
64 /*--------------------------------------------------------------------------*/
66 /*--------------------------------------------------------------------------*/
69 /*--------------------------------------------------------------------------*/
70 /* Initialize datastructures, remove unwanted constructs, optimize */
71 /* call target computations. */
72 /*--------------------------------------------------------------------------*/
74 /** Collect the entity representing the implementation of this
75 * method (not the same if inherited) and all entities for overwriting
76 * implementations in parameter set.
77 * A recursive descend in the overwritten relation.
78 * Cycle-free, therefore must terminate.
80 * @param method the overwritten method
81 * @param set A set of entities.
83 * @return Number of entities in set.
85 static size_t collect_impls(ir_entity *method, pset *set)
90 if (get_entity_irg(method) != NULL) {
91 /* has an implementation */
92 pset_insert_ptr(set, method);
96 /*- recursive descent -*/
97 for (i = get_entity_n_overwrittenby(method); i > 0;)
98 size += collect_impls(get_entity_overwrittenby(method, --i), set);
103 * Determine all methods that overwrite the given method (and implement it).
104 * The returned array must be freed by the caller (see DEL_ARR_F).
105 * If the set of overwriting methods is empty, returns NULL.
107 * @param method the method
109 static ir_entity **get_impl_methods(ir_entity *method)
112 pset *set = pset_new_ptr_default();
115 /* Collect all method entities that can be called here */
116 size = collect_impls(method, set);
119 /* no overwriting methods found */
122 arr = NEW_ARR_F(ir_entity *, size);
123 foreach_pset(set, ir_entity, ent) {
131 /** Analyze address computations.
133 * Compute for all Sel nodes the set of methods that can be selected.
134 * For each entity we store the set of subentities in the link field.
136 * Further do some optimizations:
137 * - Call standard optimizations for Sel nodes: this removes polymorphic
139 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
140 * For this we precomputed a map name->entity. Nowadays, we no more support
142 * - If the node is a Sel:
143 * If we found only a single method that can be called, replace the Sel
144 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
145 * as here we walk the type graph. In opt_polymorphy we only apply a local
148 * @param node The node to analyze
149 * @param env A map that maps names of entities to the entities.
151 static void sel_methods_walker(ir_node *node, void *env)
156 /* Call standard optimizations */
158 ir_node *new_node = optimize_in_place(node);
159 if (node != new_node) {
160 exchange(node, new_node);
165 if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
166 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
168 if (!pset_find_ptr(entities, ent)) {
169 /* Entity not yet handled. Find all (internal or external)
170 * implemented methods that overwrites this entity.
171 * This set is stored in the entity link. */
172 set_entity_link(ent, get_impl_methods(ent));
173 pset_insert_ptr(entities, ent);
176 /* -- As an add on we get an optimization that removes polymorphic calls.
177 This optimization is more powerful than that in transform_node_Sel(). -- */
178 arr = (ir_entity**) get_entity_link(ent);
181 * The Sel node never returns a pointer to a usable method.
182 * We could not call it, but it may be description:
183 * We call a method in a dead part of the program.
185 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);
207 * Initialize auxiliary data structures.
209 * Computes a set of entities that overwrite an entity and contain
210 * an implementation. The set is stored in the entity's link field.
212 * Further replaces Sel nodes where this set contains exactly one
213 * method by SymConst nodes.
214 * Finally asserts if there is a SymConst(name) if there could be a
217 static void sel_methods_init(void)
220 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
221 SymConst(name) by SymConst(ent). */
222 assert(entities == NULL);
223 entities = pset_new_ptr_default();
224 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
225 ir_entity * ent = get_irg_entity(get_irp_irg(i));
226 /* only external visible methods are allowed to call by a SymConst_ptr_name */
227 if (entity_is_externally_visible(ent)) {
228 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
232 all_irg_walk(sel_methods_walker, NULL, NULL);
233 pmap_destroy(ldname_map);
236 /*--------------------------------------------------------------------------*/
237 /* Find free methods.
239 * We expect that each entity has an array with all implementations in its
241 /*--------------------------------------------------------------------------*/
244 * Returns an array of all methods that could be called at a Sel node.
245 * This array contains every entry only once.
247 * @param sel the Sel node
249 static ir_entity ** get_Sel_arr(ir_node * sel)
251 static ir_entity ** NULL_ARRAY = NULL;
256 ent = get_Sel_entity(sel);
258 assert(is_Method_type(get_entity_type(ent))); /* what else? */
259 arr = (ir_entity**) get_entity_link(ent);
263 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
264 * kann f�r polymorphe (abstrakte) Methoden passieren. */
266 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
273 * Returns the number of possible called methods at a Sel node.
275 * @param sel the Sel node
277 static size_t get_Sel_n_methods(ir_node * sel)
279 return ARR_LEN(get_Sel_arr(sel));
283 * Returns the ith possible called method entity at a Sel node.
285 static ir_entity * get_Sel_method(ir_node * sel, size_t pos)
287 ir_entity ** arr = get_Sel_arr(sel);
288 assert(pos < ARR_LEN(arr));
293 static void free_mark(ir_node *node, pset *set);
295 static void free_mark_proj(ir_node *node, long n, pset *set)
297 assert(get_irn_mode(node) == mode_T);
298 if (get_irn_link(node) == MARK) {
299 /* already visited */
302 set_irn_link(node, MARK);
303 switch (get_irn_opcode(node)) {
305 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
306 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
308 ir_node * pred = get_Proj_pred(node);
309 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
310 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
312 /* nothing: da in "free_ana_walker" behandelt. */
318 free_mark(get_Tuple_pred(node, n), set);
322 free_mark_proj(get_Id_pred(node), n, set);
328 /* nothing: Die Operationen werden in free_ana_walker() selbst
333 assert(0 && "unexpected opcode or opcode not implemented");
336 // set_irn_link(node, NULL);
340 * Called for predecessors nodes of "interesting" ones.
341 * Interesting ones include all nodes that can somehow make
344 * If a method (or a set of methods in case of polymorph calls) gets visible,
345 * add it to the set of 'free' methods
347 * @param node the current visited node
348 * @param set the set of all free methods
350 static void free_mark(ir_node *node, pset *set)
352 if (get_irn_link(node) == MARK)
353 return; /* already visited */
355 set_irn_link(node, MARK);
357 switch (get_irn_opcode(node)) {
359 ir_entity *ent = get_Sel_entity(node);
360 if (is_method_entity(ent)) {
362 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
363 pset_insert_ptr(set, get_Sel_method(node, i));
369 if (get_SymConst_kind(node) == symconst_addr_ent) {
370 ir_entity *ent = get_SymConst_entity(node);
371 if (is_method_entity(ent)) {
372 pset_insert_ptr(set, ent);
380 for (i = 0, n = get_Phi_n_preds(node); i < n; ++i) {
381 free_mark(get_Phi_pred(node, i), set);
386 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
395 * post-walker. Find method addresses.
397 static void free_ana_walker(ir_node *node, void *env)
399 pset *set = (pset*) env;
401 if (get_irn_link(node) == MARK) {
402 /* already visited */
405 switch (get_irn_opcode(node)) {
419 /* we must handle Call nodes specially, because their call address input
420 do not expose a method address. */
421 set_irn_link(node, MARK);
422 for (i = 0, n = get_Call_n_params(node); i < n; ++i) {
423 ir_node *pred = get_Call_param(node, i);
424 if (mode_is_reference(get_irn_mode(pred))) {
425 free_mark(pred, set);
432 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
433 * jemand das Gegenteil implementiert. */
434 set_irn_link(node, MARK);
435 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
436 ir_node *pred = get_irn_n(node, i);
437 if (mode_is_reference(get_irn_mode(pred))) {
438 free_mark(pred, set);
447 * Add all method addresses in global new style initializers to the set.
450 * We do NOT check the type here, just if it's an entity address.
451 * The reason for this is code like:
453 * void *p = function;
455 * which is sometimes used to anchor functions.
457 static void add_method_address_inititializer(ir_initializer_t *initializer,
463 switch (initializer->kind) {
464 case IR_INITIALIZER_CONST:
465 n = initializer->consti.value;
467 /* let's check if it's the address of a function */
468 if (is_SymConst_addr_ent(n)) {
469 ir_entity *ent = get_SymConst_entity(n);
471 if (is_Method_type(get_entity_type(ent)))
472 pset_insert_ptr(set, ent);
475 case IR_INITIALIZER_TARVAL:
476 case IR_INITIALIZER_NULL:
478 case IR_INITIALIZER_COMPOUND:
479 for (i = 0; i < initializer->compound.n_initializers; ++i) {
480 ir_initializer_t *sub_initializer
481 = initializer->compound.initializers[i];
482 add_method_address_inititializer(sub_initializer, set);
486 panic("invalid initializer found");
490 * Add all method addresses in global initializers to the set.
493 * We do NOT check the type here, just if it's an entity address.
494 * The reason for this is code like:
496 * void *p = function;
498 * which is sometimes used to anchor functions.
500 static void add_method_address(ir_entity *ent, pset *set)
504 /* ignore methods: these of course reference their addresses
505 * TODO: remove this later once this incorrect self-initialisation is gone
507 tp = get_entity_type(ent);
508 if (is_Method_type(tp))
511 if (ent->initializer != NULL) {
512 add_method_address_inititializer(get_entity_initializer(ent), set);
517 * returns a list of 'free' methods, i.e., the methods that can be called
518 * from external or via function pointers.
520 * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
521 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
522 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
523 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
524 * auf eine echt externe Methode.
526 static size_t get_free_methods(ir_entity ***free_methods)
528 pset *free_set = pset_new_ptr_default();
535 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
537 irg = get_irp_irg(i);
538 ir_entity *const ent = get_irg_entity(irg);
539 linkage = get_entity_linkage(ent);
541 if ((linkage & IR_LINKAGE_HIDDEN_USER) || entity_is_externally_visible(ent)) {
542 pset_insert_ptr(free_set, ent);
545 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
546 /* Find all method entities that gets "visible" through this graphs,
547 * for instance because their address is stored. */
548 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
549 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
552 /* insert all methods that are used in global variables initializers */
553 tp = get_glob_type();
554 for (j = 0, m = get_class_n_members(tp); j < m; ++j) {
555 ir_entity *const ent = get_class_member(tp, j);
556 add_method_address(ent, free_set);
559 for (j = 0, m = get_compound_n_members(tp); j < m; ++j) {
560 ir_entity *const ent = get_compound_member(tp, j);
561 add_method_address(ent, free_set);
564 /* the main program is even then "free", if it's not external visible. */
565 irg = get_irp_main_irg();
567 pset_insert_ptr(free_set, get_irg_entity(irg));
569 /* Finally, transform the set into an array. */
570 length = pset_count(free_set);
571 arr = XMALLOCN(ir_entity*, length);
573 foreach_pset(free_set, ir_entity, ent) {
582 /*--------------------------------------------------------------------------*/
583 /* Callee analysis. */
584 /*--------------------------------------------------------------------------*/
586 static void callee_ana_node(ir_node *node, pset *methods);
588 static void callee_ana_proj(ir_node *node, long n, pset *methods)
590 assert(get_irn_mode(node) == mode_T);
591 if (get_irn_link(node) == MARK) {
592 /* already visited */
595 set_irn_link(node, MARK);
597 switch (get_irn_opcode(node)) {
599 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
600 * op_Tuple oder ein Knoten, der eine "freie Methode"
602 ir_node *pred = get_Proj_pred(node);
603 if (get_irn_link(pred) != MARK) {
604 if (is_Tuple(pred)) {
605 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
607 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
614 callee_ana_node(get_Tuple_pred(node, n), methods);
618 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
624 * Analyse a Call address.
626 * @param node the node representing the call address
627 * @param methods after call contains the set of all possibly called entities
629 static void callee_ana_node(ir_node *node, pset *methods)
631 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
632 /* Beware of recursion */
633 if (get_irn_link(node) == MARK) {
634 /* already visited */
637 set_irn_link(node, MARK);
639 switch (get_irn_opcode(node)) {
641 /* A direct address call. We tread this as an external
642 call and ignore it completely. */
643 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
647 ir_entity *ent = get_SymConst_entity(node);
648 assert(ent && is_method_entity(ent));
649 pset_insert_ptr(methods, ent);
654 /* polymorphic method */
656 for (i = 0, n = get_Sel_n_methods(node); i < n; ++i) {
657 ir_entity *ent = get_Sel_method(node, i);
659 pset_insert_ptr(methods, ent);
661 pset_insert_ptr(methods, get_unknown_entity());
673 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
674 callee_ana_node(get_Phi_pred(node, i), methods);
680 callee_ana_node(get_Mux_false(node), methods);
681 callee_ana_node(get_Mux_true(node), methods);
685 callee_ana_node(get_Id_pred(node), methods);
689 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
696 pset_insert_ptr(methods, get_unknown_entity()); /* free method -> unknown */
700 assert(0 && "invalid opcode or opcode not implemented");
706 * Walker: Analyses every Call node and calculates an array of possible
707 * callees for that call.
709 static void callee_walker(ir_node *call, void *env)
713 pset *methods = pset_new_ptr_default();
717 callee_ana_node(get_Call_ptr(call), methods);
718 arr = NEW_ARR_F(ir_entity*, pset_count(methods));
720 foreach_pset(methods, ir_entity, ent) {
722 /* we want the unknown_entity on the zero position for easy tests later */
723 if (is_unknown_entity(ent)) {
725 arr[0] = get_unknown_entity();
729 set_Call_callee_arr(call, ARR_LEN(arr), arr);
736 * Walker: Removes all tuple.
738 static void remove_Tuples(ir_node *proj, void *env)
742 if (! is_Proj(proj)) return;
744 nn = skip_Tuple(proj);
745 if (nn != proj) exchange(proj, nn);
750 * Determine for every Call the set of possibly called methods and stores it
751 * inside the Call (@see set_Call_callee()).
752 * Uses the sel_methods set with much be already calculated.
754 static void callee_ana(void)
757 /* analyse all graphs */
758 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
759 ir_graph *irg = get_irp_irg(i);
760 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
761 set_irg_callee_info_state(irg, irg_callee_info_consistent);
763 set_irp_callee_info_state(irg_callee_info_consistent);
766 /*--------------------------------------------------------------------------*/
767 /* Cleanup after analyses. */
768 /*--------------------------------------------------------------------------*/
770 /** Frees intermediate data structures. */
771 static void sel_methods_dispose(void)
774 foreach_pset(entities, ir_entity, ent) {
775 ir_entity **arr = (ir_entity**) get_entity_link(ent);
779 set_entity_link(ent, NULL);
785 static void destruct_walker(ir_node * node, void * env)
789 remove_Call_callee_arr(node);
793 size_t cgana(ir_entity ***free_methods)
796 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
798 length = get_free_methods(free_methods);
800 sel_methods_dispose();
804 void free_callee_info(ir_graph *irg)
806 irg_walk_graph(irg, destruct_walker, NULL, NULL);
807 set_irg_callee_info_state(irg, irg_callee_info_none);
810 void free_irp_callee_info(void)
813 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
814 free_callee_info(get_irp_irg(i));
818 void opt_call_addrs(void)
820 /* Optimize the address expressions passed to call nodes.
822 * This optimization performs the following transformations for
824 * - All SymConst operations that refer to intern methods are replaced
825 * by Const operations referring to the corresponding entity.
826 * - Sel nodes, that select entities that are not overwritten are
827 * replaced by Const nodes referring to the selected entity.
828 * - Sel nodes, for which no method exists at all are replaced by Bad
830 * - Sel nodes with a pointer input that is an Alloc node are replaced
831 * by Const nodes referring to the entity that implements the method in
832 * the type given by the Alloc node.
835 sel_methods_dispose();