Fixed warnings
[libfirm] / ir / ir / ircgopt.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include "ircgopt.h"
38
39 #include "array.h"
40 #include "irprog.h"
41 #include "irgwalk.h"
42 #include "irloop_t.h"
43 #include "irflag_t.h"
44 #include "ircons.h"
45 #include "irtools.h"
46
47 /**
48  * Walker: adds Call operations to a head's link list.
49  */
50 static void collect_call(ir_node * node, void *env) {
51   ir_node *head = env;
52
53   if (get_irn_op(node) == op_Call) {
54     set_irn_link(node, get_irn_link(head));
55     set_irn_link(head, node);
56   }
57 }
58
59 static void make_entity_to_description(type_or_ent *tore, void *env) {
60   if (get_kind(tore) == k_entity) {
61     ir_entity *ent = (ir_entity *)tore;
62
63     if ((is_Method_type(get_entity_type(ent)))                        &&
64         (get_entity_peculiarity(ent) != peculiarity_description)      &&
65         (get_entity_visibility(ent)  != visibility_external_allocated)   ) {
66       ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
67       if (get_entity_link(impl) != env) {
68         set_entity_peculiarity(ent, peculiarity_description);
69       }
70     }
71   }
72 }
73
74 /* garbage collect methods: mark and remove */
75 void gc_irgs(int n_keep, ir_entity ** keep_arr) {
76   void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
77   int i;
78
79   if (!get_opt_dead_method_elimination()) return;
80
81   if (n_keep >= get_irp_n_irgs()) {
82     /* Shortcut. Obviously we have to keep all methods. */
83     return;
84   }
85
86   /* Mark entities that are alive.  */
87   if (n_keep > 0) {
88     ir_entity ** marked = NEW_ARR_F(ir_entity *, n_keep);
89     for (i = 0; i < n_keep; ++i) {
90       marked[i] = keep_arr[i];
91       set_entity_link(marked[i], MARK);
92       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
93         printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
94       }
95     }
96
97     for (i = 0; i < ARR_LEN(marked); ++i) {
98       /* check for extern methods, these don't have an IRG */
99       if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
100         ir_graph * irg = get_entity_irg(marked[i]);
101         ir_node * node = get_irg_end(irg);
102
103         /* collect calls */
104         irg_walk_graph(irg, firm_clear_link, collect_call, node);
105
106         /* iterate calls */
107         for (node = get_irn_link(node); node; node = get_irn_link(node)) {
108           int i;
109           assert(get_irn_op(node) == op_Call);
110
111           for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
112             ir_entity * ent = get_Call_callee(node, i);
113
114             if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
115               set_entity_link(ent, MARK);
116               ARR_APP1(ir_entity *, marked, ent);
117               if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
118                 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
119                        get_entity_ld_name(ent), get_irn_node_nr(node));
120               }
121             }
122           }
123         }
124       }
125     }
126     DEL_ARR_F(marked);
127   }
128
129   /* clean */
130   type_walk(make_entity_to_description, NULL, MARK);
131   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
132     ir_graph * irg = get_irp_irg(i);
133     ir_entity * ent = get_irg_entity(irg);
134     /* Removing any graph invalidates all interprocedural loop trees. */
135     if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
136         get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
137       free_loop_information(irg);
138     }
139     if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
140       remove_irp_irg(irg);
141       set_entity_peculiarity(ent, peculiarity_description);
142       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
143         printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
144       }
145     }
146     set_entity_link(ent, NULL);
147   }
148 }