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 values_interfere(ifg->env->birg, 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) {
89 const ifg_std_t *ifg = self;
90 nodes_iter_t *it = iter;
92 obstack_init(&it->obst);
97 irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
98 obstack_ptr_grow(&it->obst, NULL);
99 it->nodes = obstack_finish(&it->obst);
102 static inline void node_break(nodes_iter_t *it, int force)
104 if((it->curr >= it->n || force) && it->nodes) {
105 obstack_free(&it->obst, NULL);
110 static ir_node *get_next_node(void *iter)
112 nodes_iter_t *it = iter;
116 res = it->nodes[it->curr++];
123 static ir_node *ifg_std_nodes_begin(const void *self, void *iter)
125 find_nodes(self, iter);
126 return get_next_node(iter);
129 static ir_node *ifg_std_nodes_next(const void *self, void *iter)
132 return get_next_node(iter);
135 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)
196 assert(it->valid == 1);
197 ir_nodeset_destroy(&it->neighbours);
201 static ir_node *get_next_neighbour(adj_iter_t *it) {
202 ir_node *res = ir_nodeset_iterator_next(&it->iter);
205 ir_nodeset_destroy(&it->neighbours);
210 static ir_node *ifg_std_neighbours_begin(const void *self, void *iter, const ir_node *irn)
212 adj_iter_t *it = iter;
213 find_neighbours(self, iter, irn);
214 return ir_nodeset_iterator_next(&it->iter);
217 static ir_node *ifg_std_neighbours_next(const void *self, void *iter)
220 return get_next_neighbour(iter);
223 static void ifg_std_neighbours_break(const void *self, void *iter)
226 neighbours_break(iter, 1);
229 typedef struct _cliques_iter_t {
231 const be_chordal_env_t *cenv;
235 struct list_head *bor;
239 static inline void free_clique_iter(cliques_iter_t *it) {
241 obstack_free(&it->ob, NULL);
242 del_pset(it->living);
245 static void get_blocks_dom_order(ir_node *blk, void *env) {
246 cliques_iter_t *it = env;
247 obstack_ptr_grow(&it->ob, blk);
250 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
254 * NOTE: Be careful when changing this function!
255 * First understand the control flow of consecutive calls.
257 static inline int get_next_clique(cliques_iter_t *it) {
259 /* continue in the block we left the last time */
260 for (; it->blk < it->n_blocks; it->blk++) {
261 int output_on_shrink = 0;
262 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
264 /* on entry to a new block set the first border ... */
266 it->bor = head->prev;
268 /* ... otherwise continue with the border we left the last time */
269 for (; it->bor != head; it->bor = it->bor->prev) {
270 border_t *b = list_entry(it->bor, border_t, list);
272 /* if its a definition irn starts living */
274 pset_insert_ptr(it->living, b->irn);
276 output_on_shrink = 1;
279 /* if its the last usage the irn dies */
281 /* before shrinking the set, return the current maximal clique */
282 if (output_on_shrink) {
286 /* fill the output buffer */
287 pset_foreach(it->living, irn)
288 it->buf[count++] = irn;
290 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
295 pset_remove_ptr(it->living, b->irn);
300 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
303 if (it->n_blocks != -1)
304 free_clique_iter(it);
309 static int ifg_std_cliques_begin(const void *self, void *iter, ir_node **buf)
311 const ifg_std_t *ifg = self;
312 cliques_iter_t *it = iter;
313 ir_node *start_bl = get_irg_start_block(ifg->env->irg);
315 obstack_init(&it->ob);
316 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
320 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
321 it->blocks = obstack_finish(&it->ob);
324 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
326 return get_next_clique(it);
329 static int ifg_std_cliques_next(const void *self, void *iter)
332 return get_next_clique(iter);
335 static void ifg_std_cliques_break(const void *self, void *iter)
338 free_clique_iter(iter);
342 static int ifg_std_degree(const void *self, const ir_node *irn)
346 find_neighbours(self, &it, irn);
347 degree = ir_nodeset_size(&it.neighbours);
348 neighbours_break(&it, 1);
352 static const be_ifg_impl_t ifg_std_impl = {
353 sizeof(nodes_iter_t),
355 sizeof(cliques_iter_t),
359 ifg_std_neighbours_begin,
360 ifg_std_neighbours_next,
361 ifg_std_neighbours_break,
365 ifg_std_cliques_begin,
366 ifg_std_cliques_next,
367 ifg_std_cliques_break,
371 be_ifg_t *be_ifg_std_new(const be_chordal_env_t *env)
373 ifg_std_t *ifg = XMALLOC(ifg_std_t);
375 ifg->impl = &ifg_std_impl;
378 return (be_ifg_t *) ifg;