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);
66 foreach_border_head(head, b) {
67 if (b->is_def && b->is_real) {
68 obstack_ptr_grow(&it->obst, b->irn);
74 static void find_nodes(const be_ifg_t *ifg, nodes_iter_t *iter)
76 obstack_init(&iter->obst);
81 irg_block_walk_graph(ifg->env->irg, nodes_walker, NULL, iter);
82 obstack_ptr_grow(&iter->obst, NULL);
83 iter->nodes = (ir_node**)obstack_finish(&iter->obst);
86 static inline void node_break(nodes_iter_t *it, int force)
88 if ((it->curr >= it->n || force) && it->nodes) {
89 obstack_free(&it->obst, NULL);
94 static ir_node *get_next_node(nodes_iter_t *it)
99 res = it->nodes[it->curr++];
106 ir_node *be_ifg_nodes_begin(const be_ifg_t *ifg, nodes_iter_t *iter)
108 find_nodes(ifg, iter);
109 return get_next_node(iter);
112 ir_node *be_ifg_nodes_next(nodes_iter_t *iter)
114 return get_next_node(iter);
117 void be_ifg_nodes_break(nodes_iter_t *iter)
122 static void find_neighbour_walker(ir_node *block, void *data)
124 neighbours_iter_t *it = (neighbours_iter_t*)data;
125 struct list_head *head = get_block_border_head(it->env, block);
126 be_lv_t *lv = be_get_irg_liveness(it->env->irg);
131 if (!be_is_live_in(lv, block, it->irn) && block != get_nodes_block(it->irn))
134 foreach_border_head(head, b) {
135 ir_node *irn = b->irn;
137 if (irn == it->irn) {
141 break; /* if we reached the end of the node's lifetime we can safely break */
143 else if (b->is_def) {
144 /* if any other node than the one in question starts living, add it to the set */
145 ir_nodeset_insert(&it->neighbours, irn);
147 else if (!has_started) {
148 /* we only delete, if the live range in question has not yet started */
149 ir_nodeset_remove(&it->neighbours, irn);
155 static void find_neighbours(const be_ifg_t *ifg, neighbours_iter_t *it, const ir_node *irn)
160 ir_nodeset_init(&it->neighbours);
162 dom_tree_walk(get_nodes_block(irn), find_neighbour_walker, NULL, it);
164 ir_nodeset_iterator_init(&it->iter, &it->neighbours);
167 static inline void neighbours_break(neighbours_iter_t *it, int force)
170 assert(it->valid == 1);
171 ir_nodeset_destroy(&it->neighbours);
175 static ir_node *get_next_neighbour(neighbours_iter_t *it)
177 ir_node *res = ir_nodeset_iterator_next(&it->iter);
180 ir_nodeset_destroy(&it->neighbours);
185 ir_node *be_ifg_neighbours_begin(const be_ifg_t *ifg, neighbours_iter_t *iter,
188 find_neighbours(ifg, iter, irn);
189 return ir_nodeset_iterator_next(&iter->iter);
192 ir_node *be_ifg_neighbours_next(neighbours_iter_t *iter)
194 return get_next_neighbour(iter);
197 void be_ifg_neighbours_break(neighbours_iter_t *iter)
199 neighbours_break(iter, 1);
202 static inline void free_clique_iter(cliques_iter_t *it)
205 obstack_free(&it->ob, NULL);
206 del_pset(it->living);
209 static void get_blocks_dom_order(ir_node *blk, void *env)
211 cliques_iter_t *it = (cliques_iter_t*)env;
212 obstack_ptr_grow(&it->ob, blk);
216 * NOTE: Be careful when changing this function!
217 * First understand the control flow of consecutive calls.
219 static inline int get_next_clique(cliques_iter_t *it)
222 /* continue in the block we left the last time */
223 for (; it->blk < it->n_blocks; it->blk++) {
224 int output_on_shrink = 0;
225 struct list_head *head = get_block_border_head(it->cenv, it->blocks[it->blk]);
227 /* on entry to a new block set the first border ... */
229 it->bor = head->prev;
231 /* ... otherwise continue with the border we left the last time */
232 for (; it->bor != head; it->bor = it->bor->prev) {
233 border_t *b = list_entry(it->bor, border_t, list);
235 /* if its a definition irn starts living */
237 pset_insert_ptr(it->living, b->irn);
239 output_on_shrink = 1;
242 /* if its the last usage the irn dies */
244 /* before shrinking the set, return the current maximal clique */
245 if (output_on_shrink) {
249 /* fill the output buffer */
250 for (irn = (ir_node*)pset_first(it->living); irn != NULL;
251 irn = (ir_node*)pset_next(it->living)) {
252 it->buf[count++] = irn;
255 assert(count > 0 && "We have a 'last usage', so there must be sth. in it->living");
260 pset_remove_ptr(it->living, b->irn);
265 assert(0 == pset_count(it->living) && "Something has survived! (At the end of the block it->living must be empty)");
268 if (it->n_blocks != -1)
269 free_clique_iter(it);
274 int be_ifg_cliques_begin(const be_ifg_t *ifg, cliques_iter_t *it,
277 ir_node *start_bl = get_irg_start_block(ifg->env->irg);
279 obstack_init(&it->ob);
280 dom_tree_walk(start_bl, get_blocks_dom_order, NULL, it);
284 it->n_blocks = obstack_object_size(&it->ob) / sizeof(void *);
285 it->blocks = (ir_node**)obstack_finish(&it->ob);
288 it->living = pset_new_ptr(2 * arch_register_class_n_regs(it->cenv->cls));
290 return get_next_clique(it);
293 int be_ifg_cliques_next(cliques_iter_t *iter)
295 return get_next_clique(iter);
298 void be_ifg_cliques_break(cliques_iter_t *iter)
300 free_clique_iter(iter);
303 int be_ifg_degree(const be_ifg_t *ifg, const ir_node *irn)
305 neighbours_iter_t it;
307 find_neighbours(ifg, &it, irn);
308 degree = ir_nodeset_size(&it.neighbours);
309 neighbours_break(&it, 1);
313 be_ifg_t *be_create_ifg(const be_chordal_env_t *env)
315 be_ifg_t *ifg = XMALLOC(be_ifg_t);
321 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
323 nodes_iter_t nodes_it;
324 neighbours_iter_t neigh_it;
325 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
329 fprintf(file, "graph G {\n\tgraph [");
331 cb->graph_attr(file, self);
332 fprintf(file, "];\n");
335 cb->at_begin(file, self);
337 be_ifg_foreach_node(ifg, &nodes_it, n) {
338 if (cb->is_dump_node && cb->is_dump_node(self, n)) {
339 int idx = get_irn_idx(n);
340 bitset_set(nodes, idx);
341 fprintf(file, "\tnode [");
343 cb->node_attr(file, self, n);
344 fprintf(file, "]; n%d;\n", idx);
348 /* Check, if all neighbours are indeed connected to the node. */
349 be_ifg_foreach_node(ifg, &nodes_it, n) {
350 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
351 int n_idx = get_irn_idx(n);
352 int m_idx = get_irn_idx(m);
354 if (n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
355 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
357 cb->edge_attr(file, self, n, m);
358 fprintf(file, "];\n");
364 cb->at_end(file, self);
366 fprintf(file, "}\n");
370 static void int_comp_rec(be_ifg_t *ifg, ir_node *n, bitset_t *seen)
372 neighbours_iter_t neigh_it;
375 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
376 if (bitset_is_set(seen, get_irn_idx(m)))
379 if (arch_get_irn_register_req(m)->type & arch_register_req_type_ignore)
382 bitset_set(seen, get_irn_idx(m));
383 int_comp_rec(ifg, m, seen);
388 static int int_component_stat(ir_graph *irg, be_ifg_t *ifg)
391 nodes_iter_t nodes_it;
392 bitset_t *seen = bitset_malloc(get_irg_last_idx(irg));
396 be_ifg_foreach_node(ifg, &nodes_it, n) {
397 if (bitset_is_set(seen, get_irn_idx(n)))
400 if (arch_get_irn_register_req(n)->type & arch_register_req_type_ignore)
404 bitset_set(seen, get_irn_idx(n));
405 int_comp_rec(ifg, n, seen);
412 void be_ifg_stat(ir_graph *irg, be_ifg_t *ifg, be_ifg_stat_t *stat)
414 nodes_iter_t nodes_it;
415 neighbours_iter_t neigh_it;
416 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
419 memset(stat, 0, sizeof(stat[0]));
421 be_ifg_foreach_node(ifg, &nodes_it, n) {
423 be_ifg_foreach_neighbour(ifg, &neigh_it, n, m) {
424 bitset_set(nodes, get_irn_idx(n));
425 stat->n_edges += !bitset_is_set(nodes, get_irn_idx(m));
429 stat->n_comps = int_component_stat(irg, ifg);