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