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 Removal of unreachable methods.
23 * @author Hubert Schmid
29 * Entfernen von nicht erreichbaren (aufrufbaren) Methoden. Die Menge
30 * der nicht erreichbaren Methoden wird aus der Abschätzung der
31 * Aufrufrelation bestimmt.
48 DEBUG_ONLY(static firm_dbg_module_t *dbg);
51 * Walker: adds Call operations to a head's link list.
53 static void collect_call(ir_node *node, void *env) {
57 set_irn_link(node, get_irn_link(head));
58 set_irn_link(head, node);
63 * Type walker, set the peculiarity of entities which graphs
64 * gets removed to peculiarity_description.
66 static void make_entity_to_description(type_or_ent tore, void *env) {
67 if (is_entity(tore.ent)) {
68 ir_entity *ent = tore.ent;
70 if ((is_Method_type(get_entity_type(ent))) &&
71 (get_entity_peculiarity(ent) != peculiarity_description) &&
72 (get_entity_visibility(ent) != visibility_external_allocated) ) {
73 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
74 if (get_entity_link(impl) != env) {
75 set_entity_peculiarity(ent, peculiarity_description);
81 /* garbage collect methods: mark and remove */
82 void gc_irgs(int n_keep, ir_entity ** keep_arr) {
83 void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
86 FIRM_DBG_REGISTER(dbg, "firm.opt.cgopt");
88 if (n_keep >= get_irp_n_irgs()) {
89 /* Shortcut. Obviously we have to keep all methods. */
93 DB((dbg, LEVEL_1, "dead method elimination\n"));
95 /* Mark entities that are alive. */
97 ir_entity **marked = NEW_ARR_F(ir_entity *, n_keep);
98 for (i = 0; i < n_keep; ++i) {
99 marked[i] = keep_arr[i];
100 set_entity_link(marked[i], MARK);
101 DB((dbg, LEVEL_1, " method %+F kept alive.\n", marked[i]));
104 for (i = 0; i < ARR_LEN(marked); ++i) {
105 /* check for extern methods, these don't have an IRG */
106 if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
107 ir_graph *irg = get_entity_irg(marked[i]);
108 ir_node *node = get_irg_end(irg);
111 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
112 irg_walk_graph(irg, firm_clear_link, collect_call, node);
115 for (node = get_irn_link(node); node; node = get_irn_link(node)) {
117 assert(is_Call(node));
119 for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
120 ir_entity *ent = get_Call_callee(node, i);
122 if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
123 set_entity_link(ent, MARK);
124 ARR_APP1(ir_entity *, marked, ent);
126 DB((dbg, LEVEL_1, " method %+F can be called from Call %+F: kept alive.\n",
131 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
138 type_walk(make_entity_to_description, NULL, MARK);
139 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
140 ir_graph *irg = get_irp_irg(i);
141 ir_entity *ent = get_irg_entity(irg);
142 /* Removing any graph invalidates all interprocedural loop trees. */
143 if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
144 get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
145 free_loop_information(irg);
147 if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
149 DB((dbg, LEVEL_1, " freeing method %+F\n", ent));
151 set_entity_peculiarity(ent, peculiarity_description);
153 set_entity_link(ent, NULL);
158 * Wrapper for running gc_irgs() as an ir_prog pass.
160 static void pass_wrapper(void)
162 ir_entity **keep_methods;
165 /* Analysis that finds the free methods,
166 i.e. methods that are dereferenced.
167 Optimizes polymorphic calls :-). */
168 cgana(&arr_len, &keep_methods);
170 /* Remove methods that are never called. */
171 gc_irgs(arr_len, keep_methods);
176 ir_prog_pass_t *gc_irgs_pass(const char *name)
178 return def_prog_pass(name ? name : "cgana", pass_wrapper);