the java frontend keeps methods in class types
[libfirm] / ir / be / beifg_std.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   Default ifg implementation.
23  * @author  Sebastian Hack
24  * @date    18.11.2005
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include <stdlib.h>
30
31 #include "list.h"
32
33 #include "irnode_t.h"
34 #include "irnodeset.h"
35 #include "irgraph_t.h"
36 #include "irgwalk.h"
37 #include "irtools.h"
38
39 #include "bearch.h"
40 #include "be_t.h"
41 #include "belive_t.h"
42 #include "bera.h"
43 #include "beifg_t.h"
44 #include "bechordal_t.h"
45 #include "beirg.h"
46 #include "beintlive_t.h"
47
48 typedef struct _ifg_std_t ifg_std_t;
49
50 struct _ifg_std_t {
51         const be_ifg_impl_t    *impl;
52         const be_chordal_env_t *env;
53 };
54
55 static void ifg_std_free(void *self)
56 {
57         free(self);
58 }
59
60 static int ifg_std_connected(const void *self, const ir_node *a, const ir_node *b)
61 {
62         const ifg_std_t *ifg = self;
63         return be_values_interfere(ifg->env->birg->lv, a, b);
64 }
65
66 typedef struct _nodes_iter_t {
67         const be_chordal_env_t *env;
68         struct obstack         obst;
69         int                    n;
70         int                    curr;
71         ir_node                **nodes;
72 } nodes_iter_t;
73
74 static void nodes_walker(ir_node *bl, void *data)
75 {
76         nodes_iter_t     *it   = data;
77         struct list_head *head = get_block_border_head(it->env, bl);
78         border_t         *b;
79
80         foreach_border_head(head, b) {
81                 if (b->is_def && b->is_real) {
82                         obstack_ptr_grow(&it->obst, b->irn);
83                         it->n++;
84                 }
85         }
86 }
87
88 static void find_nodes(const void *self, void *iter)
89 {
90         const ifg_std_t *ifg = self;
91         nodes_iter_t *it = iter;
92
93         obstack_init(&it->obst);
94         it->n     = 0;
95         it->curr  = 0;
96         it->env   = ifg->env;
97
98         irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
99         obstack_ptr_grow(&it->obst, NULL);
100         it->nodes = obstack_finish(&it->obst);
101 }
102
103 static inline void node_break(nodes_iter_t *it, int force)
104 {
105         if ((it->curr >= it->n || force) && it->nodes) {
106                 obstack_free(&it->obst, NULL);
107                 it->nodes = NULL;
108         }
109 }
110
111 static ir_node *get_next_node(void *iter)
112 {
113         nodes_iter_t *it = iter;
114         ir_node *res     = NULL;
115
116         if (it->curr < it->n)
117                 res = it->nodes[it->curr++];
118
119         node_break(it, 0);
120
121         return res;
122 }
123
124 static ir_node *ifg_std_nodes_begin(const void *self, void *iter)
125 {
126         find_nodes(self, iter);
127         return get_next_node(iter);
128 }
129
130 static ir_node *ifg_std_nodes_next(const void *self, void *iter)
131 {
132         (void) self;
133         return get_next_node(iter);
134 }
135
136 static void ifg_std_nodes_break(const void *self, void *iter)
137 {
138         (void) self;
139         node_break(iter, 1);
140 }
141
142 typedef struct _adj_iter_t {
143         const be_chordal_env_t *env;
144         const ir_node        *irn;
145         int                   valid;
146         ir_nodeset_t          neighbours;
147         ir_nodeset_iterator_t iter;
148 } adj_iter_t;
149
150 static void find_neighbour_walker(ir_node *block, void *data)
151 {
152         adj_iter_t *it          = data;
153         struct list_head *head  = get_block_border_head(it->env, block);
154
155         border_t *b;
156         int has_started = 0;
157
158         if (!be_is_live_in(it->env->birg->lv, block, it->irn) && block != get_nodes_block(it->irn))
159                 return;
160
161         foreach_border_head(head, b) {
162                 ir_node *irn = b->irn;
163
164                 if (irn == it->irn) {
165                         if (b->is_def)
166                                 has_started = 1;
167                         else
168                                 break; /* if we reached the end of the node's lifetime we can safely break */
169                 }
170                 else if (b->is_def) {
171                         /* if any other node than the one in question starts living, add it to the set */
172                         ir_nodeset_insert(&it->neighbours, irn);
173                 }
174                 else if (!has_started) {
175                         /* we only delete, if the live range in question has not yet started */
176                         ir_nodeset_remove(&it->neighbours, irn);
177                 }
178
179         }
180 }
181
182 static void find_neighbours(const ifg_std_t *ifg, adj_iter_t *it, const ir_node *irn)
183 {
184         it->env         = ifg->env;
185         it->irn         = irn;
186         it->valid       = 1;
187         ir_nodeset_init(&it->neighbours);
188
189         dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
190
191         ir_nodeset_iterator_init(&it->iter, &it->neighbours);
192 }
193
194 static inline void neighbours_break(adj_iter_t *it, int force)
195 {
196         (void) force;
197         assert(it->valid == 1);
198         ir_nodeset_destroy(&it->neighbours);
199         it->valid = 0;
200 }
201
202 static ir_node *get_next_neighbour(adj_iter_t *it)
203 {
204         ir_node *res = ir_nodeset_iterator_next(&it->iter);
205
206         if (res == NULL) {
207                 ir_nodeset_destroy(&it->neighbours);
208         }
209         return res;
210 }
211
212 static ir_node *ifg_std_neighbours_begin(const void *self, void *iter, const ir_node *irn)
213 {
214         adj_iter_t *it = iter;
215         find_neighbours(self, iter, irn);
216         return ir_nodeset_iterator_next(&it->iter);
217 }
218
219 static ir_node *ifg_std_neighbours_next(const void *self, void *iter)
220 {
221         (void) self;
222         return get_next_neighbour(iter);
223 }
224
225 static void ifg_std_neighbours_break(const void *self, void *iter)
226 {
227         (void) self;
228         neighbours_break(iter, 1);
229 }
230
231 typedef struct _cliques_iter_t {
232         struct obstack ob;
233         const be_chordal_env_t *cenv;
234         ir_node **buf;
235         ir_node **blocks;
236         int n_blocks, blk;
237         struct list_head *bor;
238         pset *living;
239 } cliques_iter_t;
240
241 static inline void free_clique_iter(cliques_iter_t *it)
242 {
243         it->n_blocks = -1;
244         obstack_free(&it->ob, NULL);
245         del_pset(it->living);
246 }
247
248 static void get_blocks_dom_order(ir_node *blk, void *env)
249 {
250         cliques_iter_t *it = env;
251         obstack_ptr_grow(&it->ob, blk);
252 }
253
254 #define pset_foreach(pset, irn)  for (irn=pset_first(pset); irn; irn=pset_next(pset))
255
256
257 /**
258  * NOTE: Be careful when changing this function!
259  *       First understand the control flow of consecutive calls.
260  */
261 static inline int get_next_clique(cliques_iter_t *it)
262 {
263
264         /* continue in the block we left the last time */
265         for (; it->blk < it->n_blocks; it->blk++) {
266                 int output_on_shrink = 0;
267                 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
268
269                 /* on entry to a new block set the first border ... */
270                 if (!it->bor)
271                         it->bor = head->prev;
272
273                 /* ... otherwise continue with the border we left the last time */
274                 for (; it->bor != head; it->bor = it->bor->prev) {
275                         border_t *b = list_entry(it->bor, border_t, list);
276
277                         /* if its a definition irn starts living */
278                         if (b->is_def) {
279                                 pset_insert_ptr(it->living, b->irn);
280                                 if (b->is_real)
281                                         output_on_shrink = 1;
282                         } else
283
284                         /* if its the last usage the irn dies */
285                         {
286                                 /* before shrinking the set, return the current maximal clique */
287                                 if (output_on_shrink) {
288                                         int count = 0;
289                                         ir_node *irn;
290
291                                         /* fill the output buffer */
292                                         pset_foreach(it->living, irn)
293                                                 it->buf[count++] = irn;
294
295                                         assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
296
297                                         return count;
298                                 }
299
300                                 pset_remove_ptr(it->living, b->irn);
301                         }
302                 }
303
304                 it->bor = NULL;
305                 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
306         }
307
308         if (it->n_blocks != -1)
309                 free_clique_iter(it);
310
311         return -1;
312 }
313
314 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
315 {
316         const ifg_std_t *ifg = self;
317         cliques_iter_t *it = iter;
318         ir_node *start_bl = get_irg_start_block(ifg->env->irg);
319
320         obstack_init(&it->ob);
321         dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
322
323         it->cenv     = ifg->env;
324         it->buf      = buf;
325         it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
326         it->blocks   = obstack_finish(&it->ob);
327         it->blk      = 0;
328         it->bor      = NULL;
329         it->living   = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
330
331         return get_next_clique(it);
332 }
333
334 static int ifg_std_cliques_next(const void *self, void *iter)
335 {
336         (void) self;
337         return get_next_clique(iter);
338 }
339
340 static void ifg_std_cliques_break(const void *self, void *iter)
341 {
342         (void) self;
343         free_clique_iter(iter);
344 }
345
346
347 static int ifg_std_degree(const void *self, const ir_node *irn)
348 {
349         adj_iter_t it;
350         int degree;
351         find_neighbours(self, &it, irn);
352         degree = ir_nodeset_size(&it.neighbours);
353         neighbours_break(&it, 1);
354         return degree;
355 }
356
357 static const be_ifg_impl_t ifg_std_impl = {
358         sizeof(nodes_iter_t),
359         sizeof(adj_iter_t),
360         sizeof(cliques_iter_t),
361
362         ifg_std_free,
363         ifg_std_connected,
364         ifg_std_neighbours_begin,
365         ifg_std_neighbours_next,
366         ifg_std_neighbours_break,
367         ifg_std_nodes_begin,
368         ifg_std_nodes_next,
369         ifg_std_nodes_break,
370         ifg_std_cliques_begin,
371         ifg_std_cliques_next,
372         ifg_std_cliques_break,
373         ifg_std_degree
374 };
375
376 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
377 {
378         ifg_std_t *ifg = XMALLOC(ifg_std_t);
379
380         ifg->impl = &ifg_std_impl;
381         ifg->env  = env;
382
383         return (be_ifg_t *) ifg;
384 }