5 * File name: ir/ana/rta.c
6 * Purpose: Intraprozedural analyses to improve the call graph estimate.
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Intraprozedurale Analyse zur Abschätzung der Aufrufrelation. Es
17 * die Menge der instantiierten Klassen bestimmt, und daraus existierende Methoden
26 /* # define RTA_SET */
28 typedef struct rta_set_elt
30 struct rta_set_elt *_next;
34 typedef struct rta_set
39 # define SET_T rta_set_t
41 # else /* if defined RTA_SET */
43 # define new_set eset_create
44 # define delete_set(SET) eset_destroy(SET)
45 # define set_insert(SET,ELT) eset_insert(SET,ELT)
46 # define set_contains(SET,ELT) eset_contains(SET,ELT)
47 # endif /* defined RTA_SET */
49 static SET_T *_live_classes = NULL;
50 static SET_T *_live_fields = NULL;
51 static SET_T *_called_methods = NULL;
53 /* cache computed results */
54 static SET_T *_live_graphs = NULL;
55 static SET_T *_dead_graphs = NULL;
58 /* Reinvent the wheel, err, set. */
59 /* eset uses obstacks, which fucks up the graph somehow */
60 static rta_set_t *new_set ()
62 rta_set_t *set = (rta_set_t*) xmalloc (sizeof (rta_set_t));
69 static void delete_set (rta_set_t *set)
71 rta_set_elt_t *elt = set->_list;
74 rta_set_elt_t *old = elt;
85 static void set_insert (rta_set_t *set, void *obj)
87 rta_set_elt_t *elt = (rta_set_elt_t*) xmalloc (sizeof (rta_set_elt_t));
90 elt->_next = set->_list;
94 static int set_contains (rta_set_t *set, void *obj)
96 rta_set_elt_t *elt = set->_list;
99 if (elt->_obj == obj) {
108 # endif /* defined RTA_SET */
112 static void rta_act (ir_node *node, void *env)
114 opcode op = get_irn_opcode (node);
116 if (iro_Call == op) {
119 ir_node *ptr = get_Call_ptr (node);
120 // TODO: test: ptr.op == Const
122 if (iro_Sel == get_irn_opcode (ptr)) {
123 ent = get_Sel_entity (ptr);
127 set_insert (_called_methods, ent);
129 } else if (iro_Load == op) {
130 ir_node *ptr = get_Load_ptr (node);
133 if (iro_Sel == get_irn_opcode (ptr)) {
134 ent = get_Sel_entity (ptr);
137 set_insert (_live_fields, ent);
139 } else if (iro_Store == op) {
140 ir_node *ptr = get_Store_ptr (node);
143 if (iro_Sel == get_irn_opcode (ptr)) {
144 ent = get_Sel_entity (ptr);
147 set_insert (_live_fields, ent);
149 } else if (iro_Alloc == op) {
150 type *type = get_Alloc_type (node);
151 set_insert (_live_classes, type);
155 static void rta_fill_graph (ir_graph* graph)
157 irg_walk (get_irg_end_block (graph), rta_act, NULL, NULL);
160 static void rta_fill_all ()
164 for (i = 0; i < get_irp_n_irgs(); i++) {
165 rta_fill_graph (get_irp_irg (i));
169 static int is_call_target (entity *method)
175 static ir_graph *get_implementing_graph (entity *method)
177 ir_graph *graph = get_entity_irg (method);
181 int n_over = get_entity_n_overwrites (method);
183 for (i = 0; (NULL == graph) && (i < n_over); i ++) {
184 entity *over = get_entity_overwrites (method, i);
185 graph = get_implementing_graph (over);
189 assert (graph && "no graph");
194 static int has_live_call (entity *method, ir_graph *graph)
196 int has_call = FALSE;
199 if (get_irg_ent (graph) != method) {
203 if (is_call_target (method)) {
207 n_over = get_entity_n_overwrittenby (method);
208 for (i = 0; !has_call && (i < n_over); i ++) {
209 entity *over = get_entity_overwrittenby(method, i);
210 has_call |= has_live_call (over, graph);
216 static int has_graph (type *clazz, ir_graph *graph)
218 int has_the_graph = FALSE;
222 if (set_contains (_live_classes, clazz)) {
223 int n_meth = get_class_n_members (clazz);
225 for (i = 0; i < n_meth; i ++) {
226 entity *member = get_class_member (clazz, i);
227 if (is_method_type (get_entity_type (member))) {
228 ir_graph *impl = get_entity_irg (member);
235 } /* _live_classes.contains (clazz) */
237 n_sub = get_class_n_subtypes (clazz);
238 for (i = 0; !has_the_graph && (i < n_sub); i ++) {
239 type *sub = get_class_subtype (clazz, i);
241 has_the_graph |= has_graph (sub, graph);
244 return (has_the_graph);
247 static int has_live_class (entity *method, ir_graph *graph)
249 int has_class = FALSE;
254 if (get_implementing_graph (method) != graph) {
258 clazz = get_entity_owner (method);
259 if (has_graph (clazz, graph)) {
263 n_over = get_entity_n_overwrittenby (method);
264 for (i = 0; !has_class && (i < n_over); i ++) {
265 entity *over = get_entity_overwrittenby (method, i);
266 has_class |= has_live_class (over, graph);
275 fprintf (stdout, "BEGIN %s(%i): %s\n", __FILE__, __LINE__, __FUNCTION__);
277 _live_classes = new_set ();
278 _live_fields = new_set ();
279 _called_methods = new_set ();
281 _live_graphs = new_set ();
282 _dead_graphs = new_set ();
284 fprintf (stdout, "FILL %s(%i): %s\n", __FILE__, __LINE__, __FUNCTION__);
286 fprintf (stdout, "DONE %s(%i): %s\n", __FILE__, __LINE__, __FUNCTION__);
297 delete_set (_live_classes);
298 _live_classes = NULL;
302 delete_set (_live_fields);
306 if (_called_methods) {
307 delete_set (_called_methods);
308 _called_methods = NULL;
312 delete_set (_live_graphs);
317 delete_set (_dead_graphs);
322 int rta_is_alive_class (type *clazz)
324 return (set_contains (_live_classes, clazz));
327 int rta_is_alive_graph (ir_graph *graph)
329 if (set_contains (_live_graphs, graph)) {
333 if (set_contains (_dead_graphs, graph)) {
337 entity *meth = get_irg_ent (graph);
339 if (has_live_call (meth, graph) && has_live_class (meth, graph)) {
340 set_insert (_live_graphs, graph);
344 set_insert (_dead_graphs, graph);
350 int rta_is_alive_field (entity *field)
352 return (set_contains (_live_fields, field));
359 * Revision 1.2 2004/06/11 18:25:39 liekweg
362 * Revision 1.1 2004/06/11 18:24:18 liekweg