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.
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 "set".
80 * If the implementation of the method is not included in the
81 * compilation unit "open" is set to true.
82 * A recursive descend in the overwritten relation.
83 * Cycle-free, therefore must terminate.
86 * @param set A set of entities.
87 * @param size Number of entities in set.
90 static void collect_impls(ir_entity *method, eset *set, int *size, int *open)
94 if (get_entity_irg(method) != NULL) {
95 eset_insert(set, method);
99 /*- recursive descent -*/
100 for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
101 collect_impls(get_entity_overwrittenby(method, i), set, size, open);
104 /** Alle Methoden bestimmen, die die �bergebene Methode �berschreiben
105 * (und implementieren). In der zur�ckgegebenen Reihung kommt jede
106 * Methode nur einmal vor. Der Wert 'NULL' steht f�r unbekannte
107 * (externe) Methoden. Die zur�ckgegebene Reihung mu� vom Aufrufer
108 * wieder freigegeben werden (siehe "DEL_ARR_F"). Gibt es �berhaupt
109 * keine Methoden, die "method" �berschreiben, so gibt die Methode
114 static ir_entity ** get_impl_methods(ir_entity * method)
116 eset * set = eset_create();
121 /* Collect all method entities that can be called here */
122 collect_impls(method, set, &size, &open);
124 /* Vorgaenger einfuegen. */
125 if (size == 0 && !open) {
126 /* keine implementierte �berschriebene Methode */
130 arr = NEW_ARR_F(ir_entity *, size + 1);
131 arr[0] = NULL; /* Represents open method */
132 for (ent = eset_first(set); size > 0; ent = eset_next(set), --size)
136 arr = NEW_ARR_F(ir_entity *, size);
137 for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size)
144 /** Analyze address computations.
146 * Compute for all Sel nodes the set of methods that can be selected.
147 * For each entity we store the set of subentities in the link field.
149 * Further do some optimizations:
150 * - Call standard optimizations for Sel nodes: this removes polymorphic
152 * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
153 * For this we precomputed a map name->entity. Nowadays, we no more support
155 * - If the node is a Sel:
156 * If we found only a single method that can be called, replace the Sel
157 * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
158 * as here we walk the type graph. In opt_polymorphy we only apply a local
161 * @param node The node to analyze
162 * @param env A map that maps names of entities to the entities.
164 static void sel_methods_walker(ir_node *node, void *env)
169 /* Call standard optimizations */
171 ir_node *new_node = optimize_in_place(node);
172 if (node != new_node) {
173 exchange(node, new_node);
178 if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
179 ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
181 if (!eset_contains(entities, ent)) {
182 /* Entity not yet handled. Find all (internal or external)
183 * implemented methods that overwrites this entity.
184 * This set is stored in the entity link. */
185 set_entity_link(ent, get_impl_methods(ent));
186 eset_insert(entities, ent);
189 /* -- As an add on we get an optimization that removes polymorphic calls.
190 This optimization is more powerful than that in transform_node_Sel(). -- */
191 arr = get_entity_link(ent);
194 * The Sel node never returns a pointer to a usable method.
195 * We could not call it, but it may be description:
196 * We call a method in a dead part of the program.
198 assert(get_entity_irg(ent) == NULL);
200 else if (get_opt_closed_world() && get_opt_dyn_meth_dispatch() &&
201 (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
205 * The Sel node returns only one possible method.
206 * So we could replace the Sel node by a SymConst.
207 * This method must exists.
209 set_irg_current_block(current_ir_graph, get_nodes_block(node));
210 assert(get_entity_irg(arr[0]) != NULL);
211 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]));
212 DBG_OPT_POLY(node, new_node);
213 exchange(node, new_node);
219 * Initialize auxiliary data structures.
221 * Computes a set of entities that overwrite an entity and contain
222 * an implementation. The set is stored in the entity's link field.
224 * Further replaces Sel nodes where this set contains exactly one
225 * method by SymConst nodes.
226 * Finally asserts if there is a SymConst(name) if there could be a
229 static void sel_methods_init(void)
232 pmap *ldname_map = pmap_create(); /* Map entity names to entities: to replace
233 SymConst(name) by SymConst(ent). */
234 assert(entities == NULL);
235 entities = eset_create();
236 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
237 ir_entity * ent = get_irg_entity(get_irp_irg(i));
238 /* only external visible methods are allowed to call by a SymConst_ptr_name */
239 if (entity_is_externally_visible(ent)) {
240 pmap_insert(ldname_map, (void *)get_entity_ld_ident(ent), ent);
244 all_irg_walk(sel_methods_walker, NULL, NULL);
245 pmap_destroy(ldname_map);
248 /*--------------------------------------------------------------------------*/
249 /* Find free methods.
251 * We expect that each entity has an array with all implementations in its
253 /*--------------------------------------------------------------------------*/
256 * Returns an array of all methods that could be called at a Sel node.
257 * This array contains every entry only once.
259 * @param sel the Sel node
261 static ir_entity ** get_Sel_arr(ir_node * sel)
263 static ir_entity ** NULL_ARRAY = NULL;
268 ent = get_Sel_entity(sel);
270 assert(is_Method_type(get_entity_type(ent))); /* what else? */
271 arr = get_entity_link(ent);
275 /* "NULL" zeigt an, dass keine Implementierung existiert. Dies
276 * kann f�r polymorphe (abstrakte) Methoden passieren. */
278 NULL_ARRAY = NEW_ARR_F(ir_entity *, 0);
285 * Returns the number of possible called methods at a Sel node.
287 * @param sel the Sel node
289 static int get_Sel_n_methods(ir_node * sel)
291 return ARR_LEN(get_Sel_arr(sel));
295 * Returns the ith possible called method entity at a Sel node.
297 static ir_entity * get_Sel_method(ir_node * sel, int pos)
299 ir_entity ** arr = get_Sel_arr(sel);
300 assert(pos >= 0 && pos < ARR_LEN(arr));
305 static void free_mark(ir_node * node, eset * set);
307 static void free_mark_proj(ir_node * node, long n, eset * set)
309 assert(get_irn_mode(node) == mode_T);
310 if (get_irn_link(node) == MARK) {
311 /* already visited */
314 set_irn_link(node, MARK);
315 switch (get_irn_opcode(node)) {
317 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
318 * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
320 ir_node * pred = get_Proj_pred(node);
321 if (get_irn_link(pred) != MARK && is_Tuple(pred)) {
322 free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
324 /* nothing: da in "free_ana_walker" behandelt. */
330 free_mark(get_Tuple_pred(node, n), set);
334 free_mark_proj(get_Id_pred(node), n, set);
340 /* nothing: Die Operationen werden in free_ana_walker() selbst
345 assert(0 && "unexpected opcode or opcode not implemented");
348 // set_irn_link(node, NULL);
352 * Called for predecessors nodes of "interesting" ones.
353 * Interesting ones include all nodes that can somehow make
356 * If a method (or a set of methods in case of polymorph calls) gets visible,
357 * add it to the set of 'free' methods
359 * @param node the current visited node
360 * @param set the set of all free methods
362 static void free_mark(ir_node *node, eset * set)
366 if (get_irn_link(node) == MARK)
367 return; /* already visited */
369 set_irn_link(node, MARK);
371 switch (get_irn_opcode(node)) {
373 ir_entity *ent = get_Sel_entity(node);
374 if (is_method_entity(ent)) {
375 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
376 eset_insert(set, get_Sel_method(node, i));
382 if (get_SymConst_kind(node) == symconst_addr_ent) {
383 ir_entity *ent = get_SymConst_entity(node);
384 if (is_method_entity(ent)) {
385 eset_insert(set, ent);
391 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
392 free_mark(get_Phi_pred(node, i), set);
396 free_mark_proj(get_Proj_pred(node), get_Proj_proj(node), set);
405 * post-walker. Find method addresses.
407 static void free_ana_walker(ir_node *node, void *env)
412 if (get_irn_link(node) == MARK) {
413 /* already visited */
416 switch (get_irn_opcode(node)) {
428 /* we must handle Call nodes specially, because their call address input
429 do not expose a method address. */
430 set_irn_link(node, MARK);
431 for (i = get_Call_n_params(node) - 1; i >= 0; --i) {
432 ir_node *pred = get_Call_param(node, i);
433 if (mode_is_reference(get_irn_mode(pred))) {
434 free_mark(pred, set);
439 /* other nodes: Alle anderen Knoten nehmen wir als Verr�ter an, bis
440 * jemand das Gegenteil implementiert. */
441 set_irn_link(node, MARK);
442 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
443 ir_node *pred = get_irn_n(node, i);
444 if (mode_is_reference(get_irn_mode(pred))) {
445 free_mark(pred, set);
453 * Add all method addresses in global new style initializers to the set.
456 * We do NOT check the type here, just it it's an entity address.
457 * The reason for this is code like:
459 * void *p = function;
461 * which is sometimes used to anchor functions.
463 static void add_method_address_inititializer(ir_initializer_t *initializer,
469 switch (initializer->kind) {
470 case IR_INITIALIZER_CONST:
471 n = initializer->consti.value;
473 /* let's check if it's the address of a function */
475 ir_entity *ent = get_Global_entity(n);
477 if (is_Method_type(get_entity_type(ent)))
478 eset_insert(set, ent);
481 case IR_INITIALIZER_TARVAL:
482 case IR_INITIALIZER_NULL:
484 case IR_INITIALIZER_COMPOUND:
485 for (i = 0; i < initializer->compound.n_initializers; ++i) {
486 ir_initializer_t *sub_initializer
487 = initializer->compound.initializers[i];
488 add_method_address_inititializer(sub_initializer, set);
492 panic("invalid initializer found");
496 * Add all method addresses in global initializers to the set.
499 * We do NOT check the type here, just it it's an entity address.
500 * The reason for this is code like:
502 * void *p = function;
504 * which is sometimes used to anchor functions.
506 static void add_method_address(ir_entity *ent, eset *set)
512 /* ignore methods: these of course reference it's address
513 * TODO: remove this later once this incorrect self-intialisation is gone
515 tp = get_entity_type(ent);
516 if (is_Method_type(tp))
519 if (ent->initializer != NULL) {
520 add_method_address_inititializer(get_entity_initializer(ent), set);
521 } else if (entity_has_compound_ent_values(ent)) {
522 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
523 n = get_compound_ent_value(ent, i);
525 /* let's check if it's the address of a function */
527 ir_entity *ent = get_Global_entity(n);
529 if (is_Method_type(get_entity_type(ent)))
530 eset_insert(set, ent);
537 * returns a list of 'free' methods, i.e., the methods that can be called
538 * from external or via function pointers.
540 * Die Datenstrukturen für sel-Methoden (sel_methods) muß vor dem
541 * Aufruf von "get_free_methods" aufgebaut sein. Die (internen)
542 * SymConst(name)-Operationen müssen in passende SymConst(ent)-Operationen
543 * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
544 * auf eine echt externe Methode.
546 static ir_entity **get_free_methods(int *length)
548 eset *free_set = eset_create();
555 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
557 irg = get_irp_irg(i);
558 ent = get_irg_entity(irg);
559 linkage = get_entity_linkage(ent);
561 if (entity_is_externally_visible(ent)
562 || (linkage & IR_LINKAGE_HIDDEN_USER)) {
563 eset_insert(free_set, ent);
566 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
567 /* Find all method entities that gets "visible" through this graphs,
568 * for instance because their address is stored. */
569 irg_walk_graph(irg, firm_clear_link, free_ana_walker, free_set);
570 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
573 /* insert all methods that are used in global variables initializers */
574 tp = get_glob_type();
575 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
576 ent = get_class_member(tp, i);
577 add_method_address(ent, free_set);
580 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
581 ent = get_class_member(tp, i);
582 add_method_address(ent, free_set);
585 /* the main program is even then "free", if it's not external visible. */
586 irg = get_irp_main_irg();
588 eset_insert(free_set, get_irg_entity(irg));
590 /* Finally, transform the set into an array. */
591 *length = eset_count(free_set);
592 arr = XMALLOCN(ir_entity*, *length);
593 for (i = 0, ent = eset_first(free_set); ent; ent = eset_next(free_set)) {
596 eset_destroy(free_set);
601 /*--------------------------------------------------------------------------*/
602 /* Callee analysis. */
603 /*--------------------------------------------------------------------------*/
605 static void callee_ana_node(ir_node * node, eset * methods);
607 static void callee_ana_proj(ir_node *node, long n, eset *methods)
609 assert(get_irn_mode(node) == mode_T);
610 if (get_irn_link(node) == MARK) {
611 /* already visited */
614 set_irn_link(node, MARK);
616 switch (get_irn_opcode(node)) {
618 /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
619 * op_Tuple oder ein Knoten, der eine "freie Methode"
621 ir_node *pred = get_Proj_pred(node);
622 if (get_irn_link(pred) != MARK) {
623 if (is_Tuple(pred)) {
624 callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
626 eset_insert(methods, unknown_entity); /* free method -> unknown */
633 callee_ana_node(get_Tuple_pred(node, n), methods);
637 eset_insert(methods, unknown_entity); /* free method -> unknown */
643 * Analyse a Call address.
645 * @param node the node representing the call address
646 * @param methods after call contains the set of all possibly called entities
648 static void callee_ana_node(ir_node *node, eset *methods)
652 assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
653 /* Beware of recursion */
654 if (get_irn_link(node) == MARK) {
655 /* already visited */
658 set_irn_link(node, MARK);
660 switch (get_irn_opcode(node)) {
662 /* A direct address call. We tread this as an external
663 call and ignore it completely. */
664 eset_insert(methods, unknown_entity); /* free method -> unknown */
668 ir_entity *ent = get_SymConst_entity(node);
669 assert(ent && is_method_entity(ent));
670 eset_insert(methods, ent);
675 /* polymorphic method */
676 for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
677 ir_entity *ent = get_Sel_method(node, i);
679 eset_insert(methods, ent);
681 eset_insert(methods, unknown_entity);
691 for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
692 callee_ana_node(get_Phi_pred(node, i), methods);
697 callee_ana_node(get_Mux_false(node), methods);
698 callee_ana_node(get_Mux_true(node), methods);
702 callee_ana_node(get_Id_pred(node), methods);
706 callee_ana_proj(get_Proj_pred(node), get_Proj_proj(node), methods);
713 eset_insert(methods, unknown_entity); /* free method -> unknown */
717 assert(0 && "invalid opcode or opcode not implemented");
723 * Walker: Analyses every Call node and calculates an array of possible
724 * callees for that call.
726 static void callee_walker(ir_node *call, void *env)
730 eset *methods = eset_create();
735 callee_ana_node(get_Call_ptr(call), methods);
736 arr = NEW_ARR_F(ir_entity *, eset_count(methods));
737 for (i = 0, ent = eset_first(methods); ent; ent = eset_next(methods)) {
739 /* we want the unknown_entity on the zero position for easy tests later */
740 if (ent == unknown_entity) {
742 arr[0] = unknown_entity;
746 set_Call_callee_arr(call, ARR_LEN(arr), arr);
748 eset_destroy(methods);
753 * Walker: Removes all tuple.
755 static void remove_Tuples(ir_node *proj, void *env)
759 if (! is_Proj(proj)) return;
761 nn = skip_Tuple(proj);
762 if (nn != proj) exchange(proj, nn);
767 * Determine for every Call the set of possibly called methods and stores it
768 * inside the Call (@see set_Call_callee()).
769 * Uses the sel_methods set with much be already calculated.
771 static void callee_ana(void)
774 /* analyse all graphs */
775 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
776 ir_graph *irg = get_irp_irg(i);
777 irg_walk_graph(irg, callee_walker, remove_Tuples, NULL);
778 set_irg_callee_info_state(irg, irg_callee_info_consistent);
780 set_irp_callee_info_state(irg_callee_info_consistent);
783 /*--------------------------------------------------------------------------*/
784 /* Cleanup after analyses. */
785 /*--------------------------------------------------------------------------*/
787 /** Frees intermediate data structures. */
788 static void sel_methods_dispose(void)
792 for (ent = eset_first(entities); ent; ent = eset_next(entities)) {
793 ir_entity ** arr = get_entity_link(ent);
797 set_entity_link(ent, NULL);
799 eset_destroy(entities);
803 /*--------------------------------------------------------------------------*/
804 /* Freeing the callee arrays. */
805 /*--------------------------------------------------------------------------*/
807 static void destruct_walker(ir_node * node, void * env)
811 remove_Call_callee_arr(node);
815 /*--------------------------------------------------------------------------*/
817 /*--------------------------------------------------------------------------*/
819 void cgana(int *length, ir_entity ***free_methods)
821 /* Optimize Sel/SymConst nodes and compute all methods that implement an entity. */
823 *free_methods = get_free_methods(length);
825 sel_methods_dispose();
828 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)
837 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
838 free_callee_info(get_irp_irg(i));
842 /* Optimize the address expressions passed to call nodes.
844 * This optimization performs the following transformations for
846 * - All SymConst operations that refer to intern methods are replaced
847 * by Const operations referring to the corresponding entity.
848 * - Sel nodes, that select entities that are not overwritten are
849 * replaced by Const nodes referring to the selected entity.
850 * - Sel nodes, for which no method exists at all are replaced by Bad
852 * - Sel nodes with a pointer input that is an Alloc node are replaced
853 * by Const nodes referring to the entity that implements the method in
854 * the type given by the Alloc node.
856 void opt_call_addrs(void)
859 sel_methods_dispose();