2 * Copyright (C) 1995-2008 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
34 #include "irnodeset.h"
35 #include "irgraph_t.h"
44 #include "bechordal_t.h"
46 #include "beintlive_t.h"
48 typedef struct _ifg_std_t ifg_std_t;
51 const be_ifg_impl_t *impl;
52 const be_chordal_env_t *env;
55 static void ifg_std_free(void *self)
60 static int ifg_std_connected(const void *self, const ir_node *a, const ir_node *b)
62 const ifg_std_t *ifg = self;
63 return be_values_interfere(ifg->env->birg->lv, a, b);
66 typedef struct _nodes_iter_t {
67 const be_chordal_env_t *env;
74 static void nodes_walker(ir_node *bl, void *data)
76 nodes_iter_t *it = data;
77 struct list_head *head = get_block_border_head(it->env, bl);
80 foreach_border_head(head, b) {
81 if (b->is_def && b->is_real) {
82 obstack_ptr_grow(&it->obst, b->irn);
88 static void find_nodes(const void *self, void *iter)
90 const ifg_std_t *ifg = self;
91 nodes_iter_t *it = iter;
93 obstack_init(&it->obst);
98 irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
99 obstack_ptr_grow(&it->obst, NULL);
100 it->nodes = obstack_finish(&it->obst);
103 static inline void node_break(nodes_iter_t *it, int force)
105 if ((it->curr >= it->n || force) && it->nodes) {
106 obstack_free(&it->obst, NULL);
111 static ir_node *get_next_node(void *iter)
113 nodes_iter_t *it = iter;
116 if (it->curr < it->n)
117 res = it->nodes[it->curr++];
124 static ir_node *ifg_std_nodes_begin(const void *self, void *iter)
126 find_nodes(self, iter);
127 return get_next_node(iter);
130 static ir_node *ifg_std_nodes_next(const void *self, void *iter)
133 return get_next_node(iter);
136 static void ifg_std_nodes_break(const void *self, void *iter)
142 typedef struct _adj_iter_t {
143 const be_chordal_env_t *env;
146 ir_nodeset_t neighbours;
147 ir_nodeset_iterator_t iter;
150 static void find_neighbour_walker(ir_node *block, void *data)
152 adj_iter_t *it = data;
153 struct list_head *head = get_block_border_head(it->env, block);
158 if (!be_is_live_in(it->env->birg->lv, block, it->irn) && block != get_nodes_block(it->irn))
161 foreach_border_head(head, b) {
162 ir_node *irn = b->irn;
164 if (irn == it->irn) {
168 break; /* if we reached the end of the node's lifetime we can safely break */
170 else if (b->is_def) {
171 /* if any other node than the one in question starts living, add it to the set */
172 ir_nodeset_insert(&it->neighbours, irn);
174 else if (!has_started) {
175 /* we only delete, if the live range in question has not yet started */
176 ir_nodeset_remove(&it->neighbours, irn);
182 static void find_neighbours(const ifg_std_t *ifg, adj_iter_t *it, const ir_node *irn)
187 ir_nodeset_init(&it->neighbours);
189 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
191 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
194 static inline void neighbours_break(adj_iter_t *it, int force)
197 assert(it->valid == 1);
198 ir_nodeset_destroy(&it->neighbours);
202 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)
244 obstack_free(&it->ob, NULL);
245 del_pset(it->living);
248 static void get_blocks_dom_order(ir_node *blk, void *env)
250 cliques_iter_t *it = env;
251 obstack_ptr_grow(&it->ob, blk);
254 #define pset_foreach(pset, irn) for (irn=pset_first(pset); irn; irn=pset_next(pset))
258 * NOTE: Be careful when changing this function!
259 * First understand the control flow of consecutive calls.
261 static inline int get_next_clique(cliques_iter_t *it)
264 /* continue in the block we left the last time */
265 for (; it->blk < it->n_blocks; it->blk++) {
266 int output_on_shrink = 0;
267 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
269 /* on entry to a new block set the first border ... */
271 it->bor = head->prev;
273 /* ... otherwise continue with the border we left the last time */
274 for (; it->bor != head; it->bor = it->bor->prev) {
275 border_t *b = list_entry(it->bor, border_t, list);
277 /* if its a definition irn starts living */
279 pset_insert_ptr(it->living, b->irn);
281 output_on_shrink = 1;
284 /* if its the last usage the irn dies */
286 /* before shrinking the set, return the current maximal clique */
287 if (output_on_shrink) {
291 /* fill the output buffer */
292 pset_foreach(it->living, irn)
293 it->buf[count++] = irn;
295 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
300 pset_remove_ptr(it->living, b->irn);
305 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
308 if (it->n_blocks != -1)
309 free_clique_iter(it);
314 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
316 const ifg_std_t *ifg = self;
317 cliques_iter_t *it = iter;
318 ir_node *start_bl = get_irg_start_block(ifg->env->irg);
320 obstack_init(&it->ob);
321 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
325 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
326 it->blocks = obstack_finish(&it->ob);
329 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
331 return get_next_clique(it);
334 static int ifg_std_cliques_next(const void *self, void *iter)
337 return get_next_clique(iter);
340 static void ifg_std_cliques_break(const void *self, void *iter)
343 free_clique_iter(iter);
347 static int ifg_std_degree(const void *self, const ir_node *irn)
351 find_neighbours(self, &it, irn);
352 degree = ir_nodeset_size(&it.neighbours);
353 neighbours_break(&it, 1);
357 static const be_ifg_impl_t ifg_std_impl = {
358 sizeof(nodes_iter_t),
360 sizeof(cliques_iter_t),
364 ifg_std_neighbours_begin,
365 ifg_std_neighbours_next,
366 ifg_std_neighbours_break,
370 ifg_std_cliques_begin,
371 ifg_std_cliques_next,
372 ifg_std_cliques_break,
376 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
378 ifg_std_t *ifg = XMALLOC(ifg_std_t);
380 ifg->impl = &ifg_std_impl;
383 return (be_ifg_t *) ifg;