b4e0aae5edcbfda081fa04f50ca89899d1ca7b6b
[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_t.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_t.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 values_interfere(ifg->env->birg, 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         const ifg_std_t *ifg = self;
90         nodes_iter_t *it = iter;
91
92         obstack_init(&it->obst);
93         it->n     = 0;
94         it->curr  = 0;
95         it->env   = ifg->env;
96
97         irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
98         obstack_ptr_grow(&it->obst, NULL);
99         it->nodes = obstack_finish(&it->obst);
100 }
101
102 static INLINE void node_break(nodes_iter_t *it, int force)
103 {
104         if((it->curr >= it->n || force) && it->nodes) {
105                 obstack_free(&it->obst, NULL);
106                 it->nodes = NULL;
107         }
108 }
109
110 static ir_node *get_next_node(void *iter)
111 {
112         nodes_iter_t *it = iter;
113         ir_node *res     = NULL;
114
115         if(it->curr < it->n)
116                 res = it->nodes[it->curr++];
117
118         node_break(it, 0);
119
120         return res;
121 }
122
123 static ir_node *ifg_std_nodes_begin(const void *self, void *iter)
124 {
125         find_nodes(self, iter);
126         return get_next_node(iter);
127 }
128
129 static ir_node *ifg_std_nodes_next(const void *self, void *iter)
130 {
131         (void) self;
132         return get_next_node(iter);
133 }
134
135 static void ifg_std_nodes_break(const void *self, void *iter)
136 {
137         (void) self;
138         node_break(iter, 1);
139 }
140
141 typedef struct _adj_iter_t {
142         const be_chordal_env_t *env;
143         const ir_node        *irn;
144         int                   valid;
145         ir_nodeset_t          neighbours;
146         ir_nodeset_iterator_t iter;
147 } adj_iter_t;
148
149 static void find_neighbour_walker(ir_node *block, void *data)
150 {
151         adj_iter_t *it          = data;
152         struct list_head *head  = get_block_border_head(it->env, block);
153
154         border_t *b;
155         int has_started = 0;
156
157         if(!be_is_live_in(it->env->birg->lv, block, it->irn) && block != get_nodes_block(it->irn))
158                 return;
159
160         foreach_border_head(head, b) {
161                 ir_node *irn = b->irn;
162
163                 if(irn == it->irn) {
164                         if(b->is_def)
165                                 has_started = 1;
166                         else
167                                 break; /* if we reached the end of the node's lifetime we can safely break */
168                 }
169                 else if(b->is_def) {
170                         /* if any other node than the one in question starts living, add it to the set */
171                         ir_nodeset_insert(&it->neighbours, irn);
172                 }
173                 else if(!has_started) {
174                         /* we only delete, if the live range in question has not yet started */
175                         ir_nodeset_remove(&it->neighbours, irn);
176                 }
177
178         }
179 }
180
181 static void find_neighbours(const ifg_std_t *ifg, adj_iter_t *it, const ir_node *irn)
182 {
183         it->env         = ifg->env;
184         it->irn         = irn;
185         it->valid       = 1;
186         ir_nodeset_init(&it->neighbours);
187
188         dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
189
190         ir_nodeset_iterator_init(&it->iter, &it->neighbours);
191 }
192
193 static INLINE void neighbours_break(adj_iter_t *it, int force)
194 {
195         (void) force;
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         (void) self;
220         return get_next_neighbour(iter);
221 }
222
223 static void ifg_std_neighbours_break(const void *self, void *iter)
224 {
225         (void) self;
226         neighbours_break(iter, 1);
227 }
228
229 typedef struct _cliques_iter_t {
230         struct obstack ob;
231         const be_chordal_env_t *cenv;
232         ir_node **buf;
233         ir_node **blocks;
234         int n_blocks, blk;
235         struct list_head *bor;
236         pset *living;
237 } cliques_iter_t;
238
239 static INLINE void free_clique_iter(cliques_iter_t *it) {
240         it->n_blocks = -1;
241         obstack_free(&it->ob, NULL);
242         del_pset(it->living);
243 }
244
245 static void get_blocks_dom_order(ir_node *blk, void *env) {
246         cliques_iter_t *it = env;
247         obstack_ptr_grow(&it->ob, blk);
248 }
249
250 #define pset_foreach(pset, irn)  for(irn=pset_first(pset); irn; irn=pset_next(pset))
251
252
253 /**
254  * NOTE: Be careful when changing this function!
255  *       First understand the control flow of consecutive calls.
256  */
257 static INLINE int get_next_clique(cliques_iter_t *it) {
258
259         /* continue in the block we left the last time */
260         for (; it->blk < it->n_blocks; it->blk++) {
261                 int output_on_shrink = 0;
262                 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
263
264                 /* on entry to a new block set the first border ... */
265                 if (!it->bor)
266                         it->bor = head->prev;
267
268                 /* ... otherwise continue with the border we left the last time */
269                 for (; it->bor != head; it->bor = it->bor->prev) {
270                         border_t *b = list_entry(it->bor, border_t, list);
271
272                         /* if its a definition irn starts living */
273                         if (b->is_def) {
274                                 pset_insert_ptr(it->living, b->irn);
275                                 if (b->is_real)
276                                         output_on_shrink = 1;
277                         } else
278
279                         /* if its the last usage the irn dies */
280                         {
281                                 /* before shrinking the set, return the current maximal clique */
282                                 if (output_on_shrink) {
283                                         int count = 0;
284                                         ir_node *irn;
285
286                                         /* fill the output buffer */
287                                         pset_foreach(it->living, irn)
288                                                 it->buf[count++] = irn;
289
290                                         assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
291
292                                         return count;
293                                 }
294
295                                 pset_remove_ptr(it->living, b->irn);
296                         }
297                 }
298
299                 it->bor = NULL;
300                 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
301         }
302
303         if (it->n_blocks != -1)
304                 free_clique_iter(it);
305
306         return -1;
307 }
308
309 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
310 {
311         const ifg_std_t *ifg = self;
312         cliques_iter_t *it = iter;
313         ir_node *start_bl = get_irg_start_block(ifg->env->irg);
314
315         obstack_init(&it->ob);
316         dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
317
318         it->cenv     = ifg->env;
319         it->buf      = buf;
320         it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
321         it->blocks   = obstack_finish(&it->ob);
322         it->blk      = 0;
323         it->bor      = NULL;
324         it->living   = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
325
326         return get_next_clique(it);
327 }
328
329 static int ifg_std_cliques_next(const void *self, void *iter)
330 {
331         (void) self;
332         return get_next_clique(iter);
333 }
334
335 static void ifg_std_cliques_break(const void *self, void *iter)
336 {
337         (void) self;
338         free_clique_iter(iter);
339 }
340
341
342 static int ifg_std_degree(const void *self, const ir_node *irn)
343 {
344         adj_iter_t it;
345         int degree;
346         find_neighbours(self, &it, irn);
347         degree = ir_nodeset_size(&it.neighbours);
348         neighbours_break(&it, 1);
349         return degree;
350 }
351
352 static const be_ifg_impl_t ifg_std_impl = {
353         sizeof(nodes_iter_t),
354         sizeof(adj_iter_t),
355         sizeof(cliques_iter_t),
356
357         ifg_std_free,
358         ifg_std_connected,
359         ifg_std_neighbours_begin,
360         ifg_std_neighbours_next,
361         ifg_std_neighbours_break,
362         ifg_std_nodes_begin,
363         ifg_std_nodes_next,
364         ifg_std_nodes_break,
365         ifg_std_cliques_begin,
366         ifg_std_cliques_next,
367         ifg_std_cliques_break,
368         ifg_std_degree
369 };
370
371 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
372 {
373         ifg_std_t *ifg = XMALLOC(ifg_std_t);
374
375         ifg->impl = &ifg_std_impl;
376         ifg->env  = env;
377
378         return (be_ifg_t *) ifg;
379 }