Rework linkage types in firm.
[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 #include "irpass.h"
47
48 DEBUG_ONLY(static firm_dbg_module_t *dbg);
49
50 /**
51  * Walker: adds Call operations to a head's link list.
52  */
53 static void collect_call(ir_node *node, void *env) {
54         ir_node *head = 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(int n_keep, ir_entity ** keep_arr)
64 {
65         void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
66         int i;
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                 for (i = 0; i < n_keep; ++i) {
81                         marked[i] = keep_arr[i];
82                         set_entity_link(marked[i], MARK);
83                         DB((dbg, LEVEL_1, "  method %+F kept alive.\n", marked[i]));
84                 }
85
86                 for (i = 0; i < ARR_LEN(marked); ++i) {
87                         ir_graph *irg = get_entity_irg(marked[i]);
88                         ir_node *node;
89
90                         if (irg == NULL)
91                                 continue;
92
93                         node = get_irg_end(irg);
94
95                         /* collect calls */
96                         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
97                         irg_walk_graph(irg, firm_clear_link, collect_call, node);
98
99                         /* iterate calls */
100                         for (node = get_irn_link(node); node; node = get_irn_link(node)) {
101                                 int i;
102                                 assert(is_Call(node));
103
104                                 for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
105                                         ir_entity *ent = get_Call_callee(node, i);
106
107                                         if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
108                                                 set_entity_link(ent, MARK);
109                                                 ARR_APP1(ir_entity *, marked, ent);
110
111                                                 DB((dbg, LEVEL_1, "  method %+F can be called from Call %+F: kept alive.\n",
112                                                         ent, node));
113                                         }
114                                 }
115                         }
116                         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
117                 }
118                 DEL_ARR_F(marked);
119         }
120
121         /* clean */
122         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
123                 ir_graph  *irg = get_irp_irg(i);
124                 ir_entity *ent = get_irg_entity(irg);
125
126                 if (get_entity_link(ent) == MARK)
127                         continue;
128
129                 DB((dbg, LEVEL_1, "  freeing method %+F\n",     ent));
130                 remove_irp_irg(irg);
131
132                 remove_class_member(get_entity_owner(ent), ent);
133         }
134 }
135
136 /**
137  * Wrapper for running gc_irgs() as an ir_prog pass.
138  */
139 static void pass_wrapper(void)
140 {
141     ir_entity **keep_methods;
142     int         arr_len;
143
144     /* Analysis that finds the free methods,
145        i.e. methods that are dereferenced.
146        Optimizes polymorphic calls :-). */
147     cgana(&arr_len, &keep_methods);
148
149     /* Remove methods that are never called. */
150     gc_irgs(arr_len, keep_methods);
151
152     free(keep_methods);
153 }
154
155 ir_prog_pass_t *gc_irgs_pass(const char *name)
156 {
157         return def_prog_pass(name ? name : "cgana", pass_wrapper);
158 }