Fix warnings.
[libfirm] / ir / opt / ircgopt.c
1 /*
2  * Copyright (C) 1995-2011 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  */
26
27 /*
28  * Entfernen von nicht erreichbaren (aufrufbaren) Methoden. Die Menge
29  * der nicht erreichbaren Methoden wird aus der Abschätzung der
30  * Aufrufrelation bestimmt.
31  */
32 #include "config.h"
33
34 #include "ircgopt.h"
35
36 #include "debug.h"
37 #include "array.h"
38 #include "irprog.h"
39 #include "irgwalk.h"
40 #include "irloop_t.h"
41 #include "irflag_t.h"
42 #include "ircons.h"
43 #include "cgana.h"
44 #include "irtools.h"
45 #include "irpass.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 {
54         ir_node *head = (ir_node*)env;
55
56         if (is_Call(node)) {
57                 set_irn_link(node, get_irn_link(head));
58                 set_irn_link(head, node);
59         }
60 }
61
62 /* garbage collect methods: mark and remove */
63 void gc_irgs(size_t n_keep, ir_entity ** keep_arr)
64 {
65         void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
66         size_t i, n;
67
68         FIRM_DBG_REGISTER(dbg, "firm.opt.cgopt");
69
70         if (n_keep >= get_irp_n_irgs()) {
71                 /* Shortcut. Obviously we have to keep all methods. */
72                 return;
73         }
74
75         DB((dbg, LEVEL_1, "dead method elimination\n"));
76
77         /* Mark entities that are alive.  */
78         if (n_keep > 0) {
79                 ir_entity **marked = NEW_ARR_F(ir_entity *, n_keep);
80                 size_t    idx;
81
82                 for (idx = 0; idx < n_keep; ++idx) {
83                         marked[idx] = keep_arr[idx];
84                         set_entity_link(marked[idx], MARK);
85                         DB((dbg, LEVEL_1, "  method %+F kept alive.\n", marked[idx]));
86                 }
87
88                 for (idx = 0; idx < ARR_LEN(marked); ++idx) {
89                         ir_graph *irg = get_entity_irg(marked[idx]);
90                         ir_node *node;
91
92                         if (irg == NULL)
93                                 continue;
94
95                         node = get_irg_end(irg);
96
97                         /* collect calls */
98                         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
99                         irg_walk_graph(irg, firm_clear_link, collect_call, node);
100
101                         /* iterate calls */
102                         for (node = (ir_node*)get_irn_link(node); node != NULL;
103                              node = (ir_node*)get_irn_link(node)) {
104                                 size_t i;
105                                 assert(is_Call(node));
106
107                                 for (i = get_Call_n_callees(node); i > 0;) {
108                                         ir_entity *ent = get_Call_callee(node, --i);
109
110                                         if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
111                                                 set_entity_link(ent, MARK);
112                                                 ARR_APP1(ir_entity *, marked, ent);
113
114                                                 DB((dbg, LEVEL_1, "  method %+F can be called from Call %+F: kept alive.\n",
115                                                         ent, node));
116                                         }
117                                 }
118                         }
119                         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
120                 }
121                 DEL_ARR_F(marked);
122         }
123
124         /* clean */
125         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
126                 ir_graph  *irg = get_irp_irg(i);
127                 ir_entity *ent = get_irg_entity(irg);
128
129                 if (get_entity_link(ent) == MARK)
130                         continue;
131
132                 DB((dbg, LEVEL_1, "  freeing method %+F\n", ent));
133                 remove_irp_irg(irg);
134
135                 free_entity(ent);
136         }
137 }
138
139 /**
140  * Wrapper for running gc_irgs() as an ir_prog pass.
141  */
142 static void pass_wrapper(void)
143 {
144     ir_entity **keep_methods;
145     size_t    arr_len;
146
147     /* Analysis that finds the free methods,
148        i.e. methods that are dereferenced.
149        Optimizes polymorphic calls :-). */
150     arr_len = cgana(&keep_methods);
151
152     /* Remove methods that are never called. */
153     gc_irgs(arr_len, keep_methods);
154
155     xfree(keep_methods);
156 }
157
158 ir_prog_pass_t *gc_irgs_pass(const char *name)
159 {
160         return def_prog_pass(name ? name : "cgana", pass_wrapper);
161 }