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)
133 return get_next_node(iter);
136 static void ifg_std_nodes_break(const void *self, void *iter)
141 typedef struct _adj_iter_t {
142 const be_chordal_env_t *env;
145 ir_nodeset_t neighbours;
146 ir_nodeset_iterator_t iter;
149 static void find_neighbour_walker(ir_node *block, void *data)
151 adj_iter_t *it = data;
152 struct list_head *head = get_block_border_head(it->env, block);
157 if(!be_is_live_in(it->env->birg->lv, block, it->irn) && block != get_nodes_block(it->irn))
160 foreach_border_head(head, b) {
161 ir_node *irn = b->irn;
167 break; /* if we reached the end of the node's lifetime we can safely break */
170 /* if any other node than the one in question starts living, add it to the set */
171 ir_nodeset_insert(&it->neighbours, irn);
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);
181 static void find_neighbours(const ifg_std_t *ifg, adj_iter_t *it, const ir_node *irn)
186 ir_nodeset_init(&it->neighbours);
188 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
190 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
193 static INLINE void neighbours_break(adj_iter_t *it, int force)
195 assert(it->valid == 1);
196 ir_nodeset_destroy(&it->neighbours);
200 static ir_node *get_next_neighbour(adj_iter_t *it) {
201 ir_node *res = ir_nodeset_iterator_next(&it->iter);
204 ir_nodeset_destroy(&it->neighbours);
209 static ir_node *ifg_std_neighbours_begin(const void *self, void *iter, const ir_node *irn)
211 adj_iter_t *it = iter;
212 find_neighbours(self, iter, irn);
213 return ir_nodeset_iterator_next(&it->iter);
216 static ir_node *ifg_std_neighbours_next(const void *self, void *iter)
218 return get_next_neighbour(iter);
221 static void ifg_std_neighbours_break(const void *self, void *iter)
223 neighbours_break(iter, 1);
226 typedef struct _cliques_iter_t {
228 const be_chordal_env_t *cenv;
232 struct list_head *bor;
236 static INLINE void free_clique_iter(cliques_iter_t *it) {
238 obstack_free(&it->ob, NULL);
239 del_pset(it->living);
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);
247 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
251 * NOTE: Be careful when changing this function!
252 * First understand the control flow of consecutive calls.
254 static INLINE int get_next_clique(cliques_iter_t *it) {
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]);
261 /* on entry to a new block set the first border ... */
263 it->bor = head->prev;
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);
269 /* if its a definition irn starts living */
271 pset_insert_ptr(it->living, b->irn);
273 output_on_shrink = 1;
276 /* if its the last usage the irn dies */
278 /* before shrinking the set, return the current maximal clique */
279 if (output_on_shrink) {
283 /* fill the output buffer */
284 pset_foreach(it->living, irn)
285 it->buf[count++] = irn;
287 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
292 pset_remove_ptr(it->living, b->irn);
297 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
300 if (it->n_blocks != -1)
301 free_clique_iter(it);
306 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
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);
312 obstack_init(&it->ob);
313 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
317 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
318 it->blocks = obstack_finish(&it->ob);
321 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
323 return get_next_clique(it);
326 static int ifg_std_cliques_next(const void *self, void *iter)
328 return get_next_clique(iter);
331 static void ifg_std_cliques_break(const void *self, void *iter)
333 free_clique_iter(iter);
337 static int ifg_std_degree(const void *self, const ir_node *irn)
341 find_neighbours(self, &it, irn);
342 degree = ir_nodeset_size(&it.neighbours);
343 neighbours_break(&it, 1);
347 static const be_ifg_impl_t ifg_std_impl = {
348 sizeof(nodes_iter_t),
350 sizeof(cliques_iter_t),
354 ifg_std_neighbours_begin,
355 ifg_std_neighbours_next,
356 ifg_std_neighbours_break,
360 ifg_std_cliques_begin,
361 ifg_std_cliques_next,
362 ifg_std_cliques_break,
366 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
368 ifg_std_t *ifg = xmalloc(sizeof(*ifg));
370 ifg->impl = &ifg_std_impl;
373 return (be_ifg_t *) ifg;