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