2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Default ifg implementation.
23 * @author Sebastian Hack
36 #include "irnodeset.h"
37 #include "irgraph_t.h"
46 #include "bechordal_t.h"
48 #include "beintlive_t.h"
50 typedef struct _ifg_std_t ifg_std_t;
53 const be_ifg_impl_t *impl;
54 const be_chordal_env_t *env;
57 static void ifg_std_free(void *self)
62 static int ifg_std_connected(const void *self, const ir_node *a, const ir_node *b)
64 const ifg_std_t *ifg = self;
65 return values_interfere(ifg->env->birg, a, b);
68 typedef struct _nodes_iter_t {
69 const be_chordal_env_t *env;
76 static void nodes_walker(ir_node *bl, void *data)
78 nodes_iter_t *it = data;
79 struct list_head *head = get_block_border_head(it->env, bl);
82 foreach_border_head(head, b) {
83 if (b->is_def && b->is_real) {
84 obstack_ptr_grow(&it->obst, b->irn);
90 static void find_nodes(const void *self, void *iter) {
91 const ifg_std_t *ifg = self;
92 nodes_iter_t *it = iter;
94 obstack_init(&it->obst);
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);
104 static INLINE void node_break(nodes_iter_t *it, int force)
106 if((it->curr >= it->n || force) && it->nodes) {
107 obstack_free(&it->obst, NULL);
112 static ir_node *get_next_node(void *iter)
114 nodes_iter_t *it = iter;
118 res = it->nodes[it->curr++];
125 static ir_node *ifg_std_nodes_begin(const void *self, void *iter)
127 find_nodes(self, iter);
128 return get_next_node(iter);
131 static ir_node *ifg_std_nodes_next(const void *self, void *iter)
134 return get_next_node(iter);
137 static void ifg_std_nodes_break(const void *self, void *iter)
143 typedef struct _adj_iter_t {
144 const be_chordal_env_t *env;
147 ir_nodeset_t neighbours;
148 ir_nodeset_iterator_t iter;
151 static void find_neighbour_walker(ir_node *block, void *data)
153 adj_iter_t *it = data;
154 struct list_head *head = get_block_border_head(it->env, block);
159 if(!be_is_live_in(it->env->birg->lv, block, it->irn) && block != get_nodes_block(it->irn))
162 foreach_border_head(head, b) {
163 ir_node *irn = b->irn;
169 break; /* if we reached the end of the node's lifetime we can safely break */
172 /* if any other node than the one in question starts living, add it to the set */
173 ir_nodeset_insert(&it->neighbours, irn);
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);
183 static void find_neighbours(const ifg_std_t *ifg, adj_iter_t *it, const ir_node *irn)
188 ir_nodeset_init(&it->neighbours);
190 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
192 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
195 static INLINE void neighbours_break(adj_iter_t *it, int force)
198 assert(it->valid == 1);
199 ir_nodeset_destroy(&it->neighbours);
203 static ir_node *get_next_neighbour(adj_iter_t *it) {
204 ir_node *res = ir_nodeset_iterator_next(&it->iter);
207 ir_nodeset_destroy(&it->neighbours);
212 static ir_node *ifg_std_neighbours_begin(const void *self, void *iter, const ir_node *irn)
214 adj_iter_t *it = iter;
215 find_neighbours(self, iter, irn);
216 return ir_nodeset_iterator_next(&it->iter);
219 static ir_node *ifg_std_neighbours_next(const void *self, void *iter)
222 return get_next_neighbour(iter);
225 static void ifg_std_neighbours_break(const void *self, void *iter)
228 neighbours_break(iter, 1);
231 typedef struct _cliques_iter_t {
233 const be_chordal_env_t *cenv;
237 struct list_head *bor;
241 static INLINE void free_clique_iter(cliques_iter_t *it) {
243 obstack_free(&it->ob, NULL);
244 del_pset(it->living);
247 static void get_blocks_dom_order(ir_node *blk, void *env) {
248 cliques_iter_t *it = env;
249 obstack_ptr_grow(&it->ob, blk);
252 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
256 * NOTE: Be careful when changing this function!
257 * First understand the control flow of consecutive calls.
259 static INLINE int get_next_clique(cliques_iter_t *it) {
261 /* continue in the block we left the last time */
262 for (; it->blk < it->n_blocks; it->blk++) {
263 int output_on_shrink = 0;
264 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
266 /* on entry to a new block set the first border ... */
268 it->bor = head->prev;
270 /* ... otherwise continue with the border we left the last time */
271 for (; it->bor != head; it->bor = it->bor->prev) {
272 border_t *b = list_entry(it->bor, border_t, list);
274 /* if its a definition irn starts living */
276 pset_insert_ptr(it->living, b->irn);
278 output_on_shrink = 1;
281 /* if its the last usage the irn dies */
283 /* before shrinking the set, return the current maximal clique */
284 if (output_on_shrink) {
288 /* fill the output buffer */
289 pset_foreach(it->living, irn)
290 it->buf[count++] = irn;
292 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
297 pset_remove_ptr(it->living, b->irn);
302 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
305 if (it->n_blocks != -1)
306 free_clique_iter(it);
311 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
313 const ifg_std_t *ifg = self;
314 cliques_iter_t *it = iter;
315 ir_node *start_bl = get_irg_start_block(ifg->env->irg);
317 obstack_init(&it->ob);
318 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
322 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
323 it->blocks = obstack_finish(&it->ob);
326 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
328 return get_next_clique(it);
331 static int ifg_std_cliques_next(const void *self, void *iter)
334 return get_next_clique(iter);
337 static void ifg_std_cliques_break(const void *self, void *iter)
340 free_clique_iter(iter);
344 static int ifg_std_degree(const void *self, const ir_node *irn)
348 find_neighbours(self, &it, irn);
349 degree = ir_nodeset_size(&it.neighbours);
350 neighbours_break(&it, 1);
354 static const be_ifg_impl_t ifg_std_impl = {
355 sizeof(nodes_iter_t),
357 sizeof(cliques_iter_t),
361 ifg_std_neighbours_begin,
362 ifg_std_neighbours_next,
363 ifg_std_neighbours_break,
367 ifg_std_cliques_begin,
368 ifg_std_cliques_next,
369 ifg_std_cliques_break,
373 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
375 ifg_std_t *ifg = xmalloc(sizeof(*ifg));
377 ifg->impl = &ifg_std_impl;
380 return (be_ifg_t *) ifg;