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 Interface for interference graphs.
23 * @author Sebastian Hack
31 #include "lc_opts_enum.h"
43 #include "becopystat.h"
44 #include "becopyopt.h"
47 #include "beintlive_t.h"
49 void be_ifg_free(be_ifg_t *self)
54 int be_ifg_connected(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
56 be_lv_t *lv = be_get_irg_liveness(ifg->env->irg);
57 return be_values_interfere(lv, a, b);
60 static void nodes_walker(ir_node *bl, void *data)
62 nodes_iter_t *it = (nodes_iter_t*)data;
63 struct list_head *head = get_block_border_head(it->env, bl);
65 foreach_border_head(head, b) {
66 if (b->is_def && b->is_real) {
67 obstack_ptr_grow(&it->obst, b->irn);
73 static void find_nodes(const be_ifg_t *ifg, nodes_iter_t *iter)
75 obstack_init(&iter->obst);
80 irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
81 obstack_ptr_grow(&iter->obst, NULL);
82 iter->nodes = (ir_node**)obstack_finish(&iter->obst);
85 static inline void node_break(nodes_iter_t *it, int force)
87 if ((it->curr >= it->n || force) && it->nodes) {
88 obstack_free(&it->obst, NULL);
93 static ir_node *get_next_node(nodes_iter_t *it)
98 res = it->nodes[it->curr++];
105 ir_node *be_ifg_nodes_begin(const be_ifg_t *ifg, nodes_iter_t *iter)
107 find_nodes(ifg, iter);
108 return get_next_node(iter);
111 ir_node *be_ifg_nodes_next(nodes_iter_t *iter)
113 return get_next_node(iter);
116 void be_ifg_nodes_break(nodes_iter_t *iter)
121 static void find_neighbour_walker(ir_node *block, void *data)
123 neighbours_iter_t *it = (neighbours_iter_t*)data;
124 struct list_head *head = get_block_border_head(it->env, block);
125 be_lv_t *lv = be_get_irg_liveness(it->env->irg);
129 if (!be_is_live_in(lv, block, it->irn) && block != get_nodes_block(it->irn))
132 foreach_border_head(head, b) {
133 ir_node *irn = b->irn;
135 if (irn == it->irn) {
139 break; /* if we reached the end of the node's lifetime we can safely break */
141 else if (b->is_def) {
142 /* if any other node than the one in question starts living, add it to the set */
143 ir_nodeset_insert(&it->neighbours, irn);
145 else if (!has_started) {
146 /* we only delete, if the live range in question has not yet started */
147 ir_nodeset_remove(&it->neighbours, irn);
153 static void find_neighbours(const be_ifg_t *ifg, neighbours_iter_t *it, const ir_node *irn)
158 ir_nodeset_init(&it->neighbours);
160 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
162 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
165 static inline void neighbours_break(neighbours_iter_t *it, int force)
168 assert(it->valid == 1);
169 ir_nodeset_destroy(&it->neighbours);
173 static ir_node *get_next_neighbour(neighbours_iter_t *it)
175 ir_node *res = ir_nodeset_iterator_next(&it->iter);
178 ir_nodeset_destroy(&it->neighbours);
183 ir_node *be_ifg_neighbours_begin(const be_ifg_t *ifg, neighbours_iter_t *iter,
186 find_neighbours(ifg, iter, irn);
187 return get_next_neighbour(iter);
190 ir_node *be_ifg_neighbours_next(neighbours_iter_t *iter)
192 return get_next_neighbour(iter);
195 void be_ifg_neighbours_break(neighbours_iter_t *iter)
197 neighbours_break(iter, 1);
200 static inline void free_clique_iter(cliques_iter_t *it)
203 obstack_free(&it->ob, NULL);
204 del_pset(it->living);
207 static void get_blocks_dom_order(ir_node *blk, void *env)
209 cliques_iter_t *it = (cliques_iter_t*)env;
210 obstack_ptr_grow(&it->ob, blk);
214 * NOTE: Be careful when changing this function!
215 * First understand the control flow of consecutive calls.
217 static inline int get_next_clique(cliques_iter_t *it)
220 /* continue in the block we left the last time */
221 for (; it->blk < it->n_blocks; it->blk++) {
222 int output_on_shrink = 0;
223 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
225 /* on entry to a new block set the first border ... */
227 it->bor = head->prev;
229 /* ... otherwise continue with the border we left the last time */
230 for (; it->bor != head; it->bor = it->bor->prev) {
231 border_t *b = list_entry(it->bor, border_t, list);
233 /* if its a definition irn starts living */
235 pset_insert_ptr(it->living, b->irn);
237 output_on_shrink = 1;
240 /* if its the last usage the irn dies */
242 /* before shrinking the set, return the current maximal clique */
243 if (output_on_shrink) {
246 /* fill the output buffer */
247 foreach_pset(it->living, ir_node, irn) {
248 it->buf[count++] = irn;
251 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
256 pset_remove_ptr(it->living, b->irn);
261 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
264 if (it->n_blocks != -1)
265 free_clique_iter(it);
270 int be_ifg_cliques_begin(const be_ifg_t *ifg, cliques_iter_t *it,
273 obstack_init(&it->ob);
274 dom_tree_walk_irg(ifg->env->irg, get_blocks_dom_order, NULL, it);
278 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
279 it->blocks = (ir_node**)obstack_finish(&it->ob);
282 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
284 return get_next_clique(it);
287 int be_ifg_cliques_next(cliques_iter_t *iter)
289 return get_next_clique(iter);
292 void be_ifg_cliques_break(cliques_iter_t *iter)
294 free_clique_iter(iter);
297 int be_ifg_degree(const be_ifg_t *ifg, const ir_node *irn)
299 neighbours_iter_t it;
301 find_neighbours(ifg, &it, irn);
302 degree = ir_nodeset_size(&it.neighbours);
303 neighbours_break(&it, 1);
307 be_ifg_t *be_create_ifg(const be_chordal_env_t *env)
309 be_ifg_t *ifg = XMALLOC(be_ifg_t);
315 static void int_comp_rec(be_ifg_t *ifg, ir_node *n, bitset_t *seen)
317 neighbours_iter_t neigh_it;
319 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
320 if (bitset_is_set(seen, get_irn_idx(m)))
323 arch_register_req_t const *const req = arch_get_irn_register_req(m);
324 if (arch_register_req_is(req, ignore))
327 bitset_set(seen, get_irn_idx(m));
328 int_comp_rec(ifg, m, seen);
333 static int int_component_stat(ir_graph *irg, be_ifg_t *ifg)
336 nodes_iter_t nodes_it;
337 bitset_t *seen = bitset_malloc(get_irg_last_idx(irg));
341 be_ifg_foreach_node(ifg, &nodes_it, n) {
342 if (bitset_is_set(seen, get_irn_idx(n)))
345 arch_register_req_t const *const req = arch_get_irn_register_req(n);
346 if (arch_register_req_is(req, ignore))
350 bitset_set(seen, get_irn_idx(n));
351 int_comp_rec(ifg, n, seen);
358 void be_ifg_stat(ir_graph *irg, be_ifg_t *ifg, be_ifg_stat_t *stat)
360 nodes_iter_t nodes_it;
361 neighbours_iter_t neigh_it;
362 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
365 memset(stat, 0, sizeof(stat[0]));
367 be_ifg_foreach_node(ifg, &nodes_it, n) {
369 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
370 bitset_set(nodes, get_irn_idx(n));
371 stat->n_edges += !bitset_is_set(nodes, get_irn_idx(m));
375 stat->n_comps = int_component_stat(irg, ifg);