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
32 #include "lc_opts_enum.h"
45 #include "becopystat.h"
46 #include "becopyopt.h"
49 #include "beintlive_t.h"
51 void be_ifg_free(be_ifg_t *self)
56 int be_ifg_connected(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
58 be_lv_t *lv = be_get_irg_liveness(ifg->env->irg);
59 return be_values_interfere(lv, a, b);
62 static void nodes_walker(ir_node *bl, void *data)
64 nodes_iter_t *it = (nodes_iter_t*)data;
65 struct list_head *head = get_block_border_head(it->env, bl);
68 foreach_border_head(head, b) {
69 if (b->is_def && b->is_real) {
70 obstack_ptr_grow(&it->obst, b->irn);
76 static void find_nodes(const be_ifg_t *ifg, nodes_iter_t *iter)
78 obstack_init(&iter->obst);
83 irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
84 obstack_ptr_grow(&iter->obst, NULL);
85 iter->nodes = (ir_node**)obstack_finish(&iter->obst);
88 static inline void node_break(nodes_iter_t *it, int force)
90 if ((it->curr >= it->n || force) && it->nodes) {
91 obstack_free(&it->obst, NULL);
96 static ir_node *get_next_node(nodes_iter_t *it)
100 if (it->curr < it->n)
101 res = it->nodes[it->curr++];
108 ir_node *be_ifg_nodes_begin(const be_ifg_t *ifg, nodes_iter_t *iter)
110 find_nodes(ifg, iter);
111 return get_next_node(iter);
114 ir_node *be_ifg_nodes_next(nodes_iter_t *iter)
116 return get_next_node(iter);
119 void be_ifg_nodes_break(nodes_iter_t *iter)
124 static void find_neighbour_walker(ir_node *block, void *data)
126 neighbours_iter_t *it = (neighbours_iter_t*)data;
127 struct list_head *head = get_block_border_head(it->env, block);
128 be_lv_t *lv = be_get_irg_liveness(it->env->irg);
133 if (!be_is_live_in(lv, block, it->irn) && block != get_nodes_block(it->irn))
136 foreach_border_head(head, b) {
137 ir_node *irn = b->irn;
139 if (irn == it->irn) {
143 break; /* if we reached the end of the node's lifetime we can safely break */
145 else if (b->is_def) {
146 /* if any other node than the one in question starts living, add it to the set */
147 ir_nodeset_insert(&it->neighbours, irn);
149 else if (!has_started) {
150 /* we only delete, if the live range in question has not yet started */
151 ir_nodeset_remove(&it->neighbours, irn);
157 static void find_neighbours(const be_ifg_t *ifg, neighbours_iter_t *it, const ir_node *irn)
162 ir_nodeset_init(&it->neighbours);
164 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
166 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
169 static inline void neighbours_break(neighbours_iter_t *it, int force)
172 assert(it->valid == 1);
173 ir_nodeset_destroy(&it->neighbours);
177 static ir_node *get_next_neighbour(neighbours_iter_t *it)
179 ir_node *res = ir_nodeset_iterator_next(&it->iter);
182 ir_nodeset_destroy(&it->neighbours);
187 ir_node *be_ifg_neighbours_begin(const be_ifg_t *ifg, neighbours_iter_t *iter,
190 find_neighbours(ifg, iter, irn);
191 return ir_nodeset_iterator_next(&iter->iter);
194 ir_node *be_ifg_neighbours_next(neighbours_iter_t *iter)
196 return get_next_neighbour(iter);
199 void be_ifg_neighbours_break(neighbours_iter_t *iter)
201 neighbours_break(iter, 1);
204 static inline void free_clique_iter(cliques_iter_t *it)
207 obstack_free(&it->ob, NULL);
208 del_pset(it->living);
211 static void get_blocks_dom_order(ir_node *blk, void *env)
213 cliques_iter_t *it = (cliques_iter_t*)env;
214 obstack_ptr_grow(&it->ob, blk);
218 * NOTE: Be careful when changing this function!
219 * First understand the control flow of consecutive calls.
221 static inline int get_next_clique(cliques_iter_t *it)
224 /* continue in the block we left the last time */
225 for (; it->blk < it->n_blocks; it->blk++) {
226 int output_on_shrink = 0;
227 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
229 /* on entry to a new block set the first border ... */
231 it->bor = head->prev;
233 /* ... otherwise continue with the border we left the last time */
234 for (; it->bor != head; it->bor = it->bor->prev) {
235 border_t *b = list_entry(it->bor, border_t, list);
237 /* if its a definition irn starts living */
239 pset_insert_ptr(it->living, b->irn);
241 output_on_shrink = 1;
244 /* if its the last usage the irn dies */
246 /* before shrinking the set, return the current maximal clique */
247 if (output_on_shrink) {
251 /* fill the output buffer */
252 for (irn = (ir_node*)pset_first(it->living); irn != NULL;
253 irn = (ir_node*)pset_next(it->living)) {
254 it->buf[count++] = irn;
257 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
262 pset_remove_ptr(it->living, b->irn);
267 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
270 if (it->n_blocks != -1)
271 free_clique_iter(it);
276 int be_ifg_cliques_begin(const be_ifg_t *ifg, cliques_iter_t *it,
279 ir_node *start_bl = get_irg_start_block(ifg->env->irg);
281 obstack_init(&it->ob);
282 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
286 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
287 it->blocks = (ir_node**)obstack_finish(&it->ob);
290 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
292 return get_next_clique(it);
295 int be_ifg_cliques_next(cliques_iter_t *iter)
297 return get_next_clique(iter);
300 void be_ifg_cliques_break(cliques_iter_t *iter)
302 free_clique_iter(iter);
305 int be_ifg_degree(const be_ifg_t *ifg, const ir_node *irn)
307 neighbours_iter_t it;
309 find_neighbours(ifg, &it, irn);
310 degree = ir_nodeset_size(&it.neighbours);
311 neighbours_break(&it, 1);
315 be_ifg_t *be_create_ifg(const be_chordal_env_t *env)
317 be_ifg_t *ifg = XMALLOC(be_ifg_t);
323 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
325 nodes_iter_t nodes_it;
326 neighbours_iter_t neigh_it;
327 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
331 fprintf(file, "graph G {\n\tgraph [");
333 cb->graph_attr(file, self);
334 fprintf(file, "];\n");
337 cb->at_begin(file, self);
339 be_ifg_foreach_node(ifg, &nodes_it, n) {
340 if (cb->is_dump_node && cb->is_dump_node(self, n)) {
341 int idx = get_irn_idx(n);
342 bitset_set(nodes, idx);
343 fprintf(file, "\tnode [");
345 cb->node_attr(file, self, n);
346 fprintf(file, "]; n%d;\n", idx);
350 /* Check, if all neighbours are indeed connected to the node. */
351 be_ifg_foreach_node(ifg, &nodes_it, n) {
352 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
353 int n_idx = get_irn_idx(n);
354 int m_idx = get_irn_idx(m);
356 if (n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
357 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
359 cb->edge_attr(file, self, n, m);
360 fprintf(file, "];\n");
366 cb->at_end(file, self);
368 fprintf(file, "}\n");
372 static void int_comp_rec(be_ifg_t *ifg, ir_node *n, bitset_t *seen)
374 neighbours_iter_t neigh_it;
377 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
378 if (bitset_contains_irn(seen, m))
381 if (arch_get_irn_register_req(m)->type & arch_register_req_type_ignore)
384 bitset_add_irn(seen, m);
385 int_comp_rec(ifg, m, seen);
390 static int int_component_stat(ir_graph *irg, be_ifg_t *ifg)
393 nodes_iter_t nodes_it;
394 bitset_t *seen = bitset_irg_malloc(irg);
398 be_ifg_foreach_node(ifg, &nodes_it, n) {
399 if (bitset_contains_irn(seen, n))
402 if (arch_get_irn_register_req(n)->type & arch_register_req_type_ignore)
406 bitset_add_irn(seen, n);
407 int_comp_rec(ifg, n, seen);
414 void be_ifg_stat(ir_graph *irg, be_ifg_t *ifg, be_ifg_stat_t *stat)
416 nodes_iter_t nodes_it;
417 neighbours_iter_t neigh_it;
418 bitset_t *nodes = bitset_irg_malloc(irg);
421 memset(stat, 0, sizeof(stat[0]));
423 be_ifg_foreach_node(ifg, &nodes_it, n) {
425 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
426 bitset_add_irn(nodes, n);
427 stat->n_edges += !bitset_contains_irn(nodes, m);
431 stat->n_comps = int_component_stat(irg, ifg);