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