be_abi_put_ignore_regs returns now number of ignore registers as unsigned
[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
22  * @brief   Default ifg implementation.
23  * @author  Sebastian Hack
24  * @date    18.11.2005
25  * @version $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32
33 #include "list.h"
34
35 #include "irnode_t.h"
36 #include "irnodeset.h"
37 #include "irgraph_t.h"
38 #include "irgwalk.h"
39 #include "irtools.h"
40
41 #include "bearch_t.h"
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 #include "beirg_t.h"
48 #include "beintlive_t.h"
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         return values_interfere(ifg->env->birg, a, b);
66 }
67
68 typedef struct _nodes_iter_t {
69         const be_chordal_env_t *env;
70         struct obstack         obst;
71         int                    n;
72         int                    curr;
73         ir_node                **nodes;
74 } nodes_iter_t;
75
76 static void nodes_walker(ir_node *bl, void *data)
77 {
78         nodes_iter_t     *it   = data;
79         struct list_head *head = get_block_border_head(it->env, bl);
80         border_t         *b;
81
82         foreach_border_head(head, b) {
83                 if (b->is_def && b->is_real) {
84                         obstack_ptr_grow(&it->obst, b->irn);
85                         it->n++;
86                 }
87         }
88 }
89
90 static void find_nodes(const void *self, void *iter) {
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         return get_next_node(iter);
134 }
135
136 static void ifg_std_nodes_break(const void *self, void *iter)
137 {
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         assert(it->valid == 1);
196         ir_nodeset_destroy(&it->neighbours);
197         it->valid = 0;
198 }
199
200 static ir_node *get_next_neighbour(adj_iter_t *it) {
201         ir_node *res = ir_nodeset_iterator_next(&it->iter);
202
203         if (res == NULL) {
204                 ir_nodeset_destroy(&it->neighbours);
205         }
206         return res;
207 }
208
209 static ir_node *ifg_std_neighbours_begin(const void *self, void *iter, const ir_node *irn)
210 {
211         adj_iter_t *it = iter;
212         find_neighbours(self, iter, irn);
213         return ir_nodeset_iterator_next(&it->iter);
214 }
215
216 static ir_node *ifg_std_neighbours_next(const void *self, void *iter)
217 {
218         return get_next_neighbour(iter);
219 }
220
221 static void ifg_std_neighbours_break(const void *self, void *iter)
222 {
223         neighbours_break(iter, 1);
224 }
225
226 typedef struct _cliques_iter_t {
227         struct obstack ob;
228         const be_chordal_env_t *cenv;
229         ir_node **buf;
230         ir_node **blocks;
231         int n_blocks, blk;
232         struct list_head *bor;
233         pset *living;
234 } cliques_iter_t;
235
236 static INLINE void free_clique_iter(cliques_iter_t *it) {
237         it->n_blocks = -1;
238         obstack_free(&it->ob, NULL);
239         del_pset(it->living);
240 }
241
242 static void get_blocks_dom_order(ir_node *blk, void *env) {
243         cliques_iter_t *it = env;
244         obstack_ptr_grow(&it->ob, blk);
245 }
246
247 #define pset_foreach(pset, irn)  for(irn=pset_first(pset); irn; irn=pset_next(pset))
248
249
250 /**
251  * NOTE: Be careful when changing this function!
252  *       First understand the control flow of consecutive calls.
253  */
254 static INLINE int get_next_clique(cliques_iter_t *it) {
255
256         /* continue in the block we left the last time */
257         for (; it->blk < it->n_blocks; it->blk++) {
258                 int output_on_shrink = 0;
259                 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
260
261                 /* on entry to a new block set the first border ... */
262                 if (!it->bor)
263                         it->bor = head->prev;
264
265                 /* ... otherwise continue with the border we left the last time */
266                 for (; it->bor != head; it->bor = it->bor->prev) {
267                         border_t *b = list_entry(it->bor, border_t, list);
268
269                         /* if its a definition irn starts living */
270                         if (b->is_def) {
271                                 pset_insert_ptr(it->living, b->irn);
272                                 if (b->is_real)
273                                         output_on_shrink = 1;
274                         } else
275
276                         /* if its the last usage the irn dies */
277                         {
278                                 /* before shrinking the set, return the current maximal clique */
279                                 if (output_on_shrink) {
280                                         int count = 0;
281                                         ir_node *irn;
282
283                                         /* fill the output buffer */
284                                         pset_foreach(it->living, irn)
285                                                 it->buf[count++] = irn;
286
287                                         assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
288
289                                         return count;
290                                 }
291
292                                 pset_remove_ptr(it->living, b->irn);
293                         }
294                 }
295
296                 it->bor = NULL;
297                 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
298         }
299
300         if (it->n_blocks != -1)
301                 free_clique_iter(it);
302
303         return -1;
304 }
305
306 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
307 {
308         const ifg_std_t *ifg = self;
309         cliques_iter_t *it = iter;
310         ir_node *start_bl = get_irg_start_block(ifg->env->irg);
311
312         obstack_init(&it->ob);
313         dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
314
315         it->cenv     = ifg->env;
316         it->buf      = buf;
317         it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
318         it->blocks   = obstack_finish(&it->ob);
319         it->blk      = 0;
320         it->bor      = NULL;
321         it->living   = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
322
323         return get_next_clique(it);
324 }
325
326 static int ifg_std_cliques_next(const void *self, void *iter)
327 {
328         return get_next_clique(iter);
329 }
330
331 static void ifg_std_cliques_break(const void *self, void *iter)
332 {
333         free_clique_iter(iter);
334 }
335
336
337 static int ifg_std_degree(const void *self, const ir_node *irn)
338 {
339         adj_iter_t it;
340         int degree;
341         find_neighbours(self, &it, irn);
342         degree = ir_nodeset_size(&it.neighbours);
343         neighbours_break(&it, 1);
344         return degree;
345 }
346
347 static const be_ifg_impl_t ifg_std_impl = {
348         sizeof(nodes_iter_t),
349         sizeof(adj_iter_t),
350         sizeof(cliques_iter_t),
351
352         ifg_std_free,
353         ifg_std_connected,
354         ifg_std_neighbours_begin,
355         ifg_std_neighbours_next,
356         ifg_std_neighbours_break,
357         ifg_std_nodes_begin,
358         ifg_std_nodes_next,
359         ifg_std_nodes_break,
360         ifg_std_cliques_begin,
361         ifg_std_cliques_next,
362         ifg_std_cliques_break,
363         ifg_std_degree
364 };
365
366 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
367 {
368         ifg_std_t *ifg = xmalloc(sizeof(*ifg));
369
370         ifg->impl = &ifg_std_impl;
371         ifg->env  = env;
372
373         return (be_ifg_t *) ifg;
374 }