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 Removal of unreachable methods.
23 * @author Matthias Braun
27 #include "iroptimize.h"
38 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
40 static void visit_entity(ir_entity *entity);
42 static void visit_node(ir_node *node, void *env)
47 if (is_SymConst(node)) {
48 if (!SYMCONST_HAS_ENT(get_SymConst_kind(node)))
50 entity = get_SymConst_entity(node);
51 } else if (is_Sel(node)) {
52 entity = get_Sel_entity(node);
60 static void start_visit_node(ir_node *node)
62 ir_graph *irg = get_irn_irg(node);
64 if (get_irg_visited(irg) < get_max_irg_visited()) {
65 set_irg_visited(irg, get_max_irg_visited());
67 irg_walk_2(node, visit_node, NULL, NULL);
70 static void visit_initializer(ir_initializer_t *initializer)
72 switch (initializer->kind) {
73 case IR_INITIALIZER_CONST:
74 start_visit_node(initializer->consti.value);
76 case IR_INITIALIZER_TARVAL:
77 case IR_INITIALIZER_NULL:
80 case IR_INITIALIZER_COMPOUND: {
82 for (i = 0; i < initializer->compound.n_initializers; ++i) {
83 ir_initializer_t *subinitializer
84 = initializer->compound.initializers[i];
85 visit_initializer(subinitializer);
90 panic("invalid initializer found");
93 static void visit_entity(ir_entity *entity)
97 if (entity_visited(entity))
99 mark_entity_visited(entity);
101 if (entity->initializer != NULL) {
102 visit_initializer(entity->initializer);
103 } else if (entity_has_compound_ent_values(entity)) {
105 size_t n_members = get_compound_ent_n_values(entity);
106 for (i = 0; i < n_members; ++i) {
107 ir_node *node = get_compound_ent_value(entity, i);
108 start_visit_node(node);
112 irg = get_entity_irg(entity);
114 start_visit_node(get_irg_end(irg));
118 static void visit_segment(ir_type *segment)
120 int n_entities = get_compound_n_members(segment);
123 for (i = 0; i < n_entities; ++i) {
124 ir_entity *entity = get_compound_member(segment, i);
125 if (get_entity_visibility(entity) != ir_visibility_default
126 && !(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER))
129 visit_entity(entity);
133 static void garbage_collect_in_segment(ir_type *segment)
137 for (i = get_compound_n_members(segment)-1; i >= 0; --i) {
138 ir_entity *entity = get_compound_member(segment, i);
140 if (entity_visited(entity))
143 DB((dbg, LEVEL_1, " removing entity %+F\n", entity));
149 void garbage_collect_entities(void)
154 FIRM_DBG_REGISTER(dbg, "firm.opt.garbagecollect");
156 /* start a type walk for all externally visible entities */
157 irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
158 inc_master_type_visited();
159 inc_max_irg_visited();
161 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
162 ir_type *type = get_segment_type(s);
163 mark_type_visited(type);
168 /* remove graphs of non-visited functions
169 * (we have to count backwards so we can safely call remove_irp_irg
170 * while iterating) */
171 for (i = get_irp_n_irgs(); i > 0;) {
172 ir_graph *irg = get_irp_irg(--i);
173 ir_entity *entity = get_irg_entity(irg);
175 if (entity_visited(entity))
178 DB((dbg, LEVEL_1, " freeing method %+F\n", entity));
182 /* we can now remove all non-visited (global) entities */
183 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
184 ir_type *type = get_segment_type(s);
185 garbage_collect_in_segment(type);
187 irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
190 ir_prog_pass_t *garbage_collect_entities_pass(const char *name)
192 return def_prog_pass(name ? name : "garbage_collect",
193 garbage_collect_entities);