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