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