11d708f64f06d64fdd1706f8611b9fbe7fff5132
[libfirm] / ir / opt / ircgopt.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Removal of unreachable methods.
23  * @author   Hubert Schmid
24  * @date     09.06.2002
25  * @version  $Id$
26  */
27
28 /*
29  * Entfernen von nicht erreichbaren (aufrufbaren) Methoden. Die Menge
30  * der nicht erreichbaren Methoden wird aus der Abschätzung der
31  * Aufrufrelation bestimmt.
32  */
33 #include "config.h"
34
35 #include "ircgopt.h"
36
37 #include "debug.h"
38 #include "array.h"
39 #include "irprog.h"
40 #include "irgwalk.h"
41 #include "irloop_t.h"
42 #include "irflag_t.h"
43 #include "ircons.h"
44 #include "cgana.h"
45 #include "irtools.h"
46
47 DEBUG_ONLY(static firm_dbg_module_t *dbg);
48
49 /**
50  * Walker: adds Call operations to a head's link list.
51  */
52 static void collect_call(ir_node *node, void *env) {
53         ir_node *head = env;
54
55         if (is_Call(node)) {
56                 set_irn_link(node, get_irn_link(head));
57                 set_irn_link(head, node);
58         }
59 }
60
61 /**
62  * Type walker, set the peculiarity of entities which graphs
63  * gets removed to peculiarity_description.
64  */
65 static void make_entity_to_description(type_or_ent tore, void *env) {
66         if (is_entity(tore.ent)) {
67                 ir_entity *ent = tore.ent;
68
69                 if ((is_Method_type(get_entity_type(ent)))                        &&
70                         (get_entity_peculiarity(ent) != peculiarity_description)      &&
71                         (get_entity_visibility(ent)  != visibility_external_allocated)   ) {
72                                 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
73                                 if (get_entity_link(impl) != env) {
74                                         set_entity_peculiarity(ent, peculiarity_description);
75                                 }
76                 }
77         }
78 }
79
80 /* garbage collect methods: mark and remove */
81 void gc_irgs(int n_keep, ir_entity ** keep_arr) {
82         void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
83         int i;
84
85         FIRM_DBG_REGISTER(dbg, "firm.opt.cgopt");
86
87         if (n_keep >= get_irp_n_irgs()) {
88                 /* Shortcut. Obviously we have to keep all methods. */
89                 return;
90         }
91
92         DB((dbg, LEVEL_1, "dead method elimination\n"));
93
94         /* Mark entities that are alive.  */
95         if (n_keep > 0) {
96                 ir_entity **marked = NEW_ARR_F(ir_entity *, n_keep);
97                 for (i = 0; i < n_keep; ++i) {
98                         marked[i] = keep_arr[i];
99                         set_entity_link(marked[i], MARK);
100                         DB((dbg, LEVEL_1, "  method %+F kept alive.\n", marked[i]));
101                 }
102
103                 for (i = 0; i < ARR_LEN(marked); ++i) {
104                         /* check for extern methods, these don't have an IRG */
105                         if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
106                                 ir_graph *irg = get_entity_irg(marked[i]);
107                                 ir_node *node = get_irg_end(irg);
108
109                                 /* collect calls */
110                                 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
111                                 irg_walk_graph(irg, firm_clear_link, collect_call, node);
112
113                                 /* iterate calls */
114                                 for (node = get_irn_link(node); node; node = get_irn_link(node)) {
115                                         int i;
116                                         assert(is_Call(node));
117
118                                         for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
119                                                 ir_entity *ent = get_Call_callee(node, i);
120
121                                                 if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
122                                                         set_entity_link(ent, MARK);
123                                                         ARR_APP1(ir_entity *, marked, ent);
124
125                                                         DB((dbg, LEVEL_1, "  method %+F can be called from Call %+F: kept alive.\n",
126                                                             ent, node));
127                                                 }
128                                         }
129                                 }
130                                 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
131                         }
132                 }
133                 DEL_ARR_F(marked);
134         }
135
136         /* clean */
137         type_walk(make_entity_to_description, NULL, MARK);
138         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
139                 ir_graph  *irg = get_irp_irg(i);
140                 ir_entity *ent = get_irg_entity(irg);
141                 /* Removing any graph invalidates all interprocedural loop trees. */
142                 if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
143                     get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
144                         free_loop_information(irg);
145                 }
146                 if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
147
148                         DB((dbg, LEVEL_1, "  freeing method %+F\n",     ent));
149                         remove_irp_irg(irg);
150                         set_entity_peculiarity(ent, peculiarity_description);
151                 }
152                 set_entity_link(ent, NULL);
153         }
154 }
155
156 /**
157  * Wrapper for running gc_irgs() as an ir_prog pass.
158  */
159 static void pass_wrapper(void)
160 {
161     ir_entity **keep_methods;
162     int         arr_len;
163
164     /* Analysis that finds the free methods,
165        i.e. methods that are dereferenced.
166        Optimizes polymorphic calls :-). */
167     cgana(&arr_len, &keep_methods);
168
169     /* Remove methods that are never called. */
170     gc_irgs(arr_len, keep_methods);
171
172     free(keep_methods);
173 }
174
175 ir_prog_pass_t *gc_irgs_pass(const char *name, int verify, int dump)
176 {
177         return def_prog_pass(name ? name : "cgana", verify, dump, pass_wrapper);
178 }