4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
19 #include "irgraph_t.h"
26 #include "bechordal_t.h"
28 typedef struct _cli_head_t {
29 struct list_head list;
30 struct _cli_head_t *next_cli_head;
35 typedef struct _ifg_clique_t {
36 const be_ifg_impl_t *impl;
37 const be_chordal_env_t *env;
40 cli_head_t *curr_cli_head;
41 bitset_t *visited_nodes;
42 bitset_t *visited_neighbours;
45 typedef struct _cli_element_t {
46 struct list_head list;
50 typedef struct _cli_iter_t {
52 cli_head_t *curr_cli_head;
53 cli_element_t *curr_cli_element;
54 const ir_node *curr_irn;
57 /* PRIVATE FUNCTIONS */
58 static cli_head_t *get_new_cli_head(ifg_clique_t *ifg)
61 cli_head_t *new_cli_head;
63 if (ifg->cli_root == NULL)
65 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
66 INIT_LIST_HEAD(&new_cli_head->list);
67 ifg->cli_root = new_cli_head;
71 cli_head = ifg->cli_root;
72 while(!(cli_head->next_cli_head == NULL))
74 cli_head = cli_head->next_cli_head;
76 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
77 INIT_LIST_HEAD(&new_cli_head->list);
78 cli_head->next_cli_head = new_cli_head;
81 new_cli_head->min = NULL;
82 new_cli_head->max = NULL;
83 new_cli_head->next_cli_head = NULL;
84 ifg->curr_cli_head = new_cli_head;
89 static cli_element_t *get_new_cli_element(ifg_clique_t *ifg)
91 cli_element_t *cli_element;
93 cli_element = obstack_alloc(&ifg->obst, sizeof(*cli_element));
94 INIT_LIST_HEAD(&cli_element->list);
99 static void write_clique(nodeset *live_set, ifg_clique_t *ifg)
104 ir_node *max_node = NULL;
105 ir_node *min_node = NULL;
106 cli_element_t *new_element = NULL;
107 cli_element_t *element = NULL;
108 cli_head_t *cli_head = get_new_cli_head(ifg);
111 foreach_nodeset(live_set, live_irn)
113 /* test if node is max or min dominator*/
114 test_node = live_irn;
115 if (max_node == NULL)
117 max_node = test_node;
118 min_node = test_node;
122 test_int = value_dominates(test_node, max_node);
125 max_node = test_node;
127 test_int = value_dominates(min_node, test_node);
130 min_node = test_node;
134 list_for_each_entry(cli_element_t, element, &cli_head->list, list){
135 if(element->irn == live_irn){
142 new_element = get_new_cli_element(ifg);
143 new_element->irn = live_irn;
144 list_add(&new_element->list, &cli_head->list) ;
148 cli_head->min = min_node;
149 cli_head->max = max_node;
152 static cli_head_t *get_next_cli_head(const ir_node *irn, cli_iter_t *it) /* ...containing the node *irn */
155 cli_element_t *element;
157 int is_dominated_by_max;
160 if (it->curr_cli_head == NULL || it->curr_cli_head->next_cli_head == NULL) /* way back of recursion or this is the last clique */
162 it->curr_cli_head = NULL;
166 head = it->curr_cli_head->next_cli_head;
168 is_dominated_by_max = value_dominates(head->max, irn);
169 //dominates_min = value_dominates(irn, head->min);
171 if(head->min->node_nr == 2000)
174 if ((is_dominated_by_max) || (irn == head->max)) /* node could be in clique */
176 /* check if node is in clique */
177 list_for_each_entry(cli_element_t, element, &head->list, list)
179 if (&element->list != &head->list)
181 if (element->irn == irn)
182 { /* node is in clique */
183 it->curr_cli_head = head;
184 it->curr_cli_element = (void *) head; /* needed because the next element is searched with list.next of it->curr_cli_element */
190 if (it->curr_cli_head != head) /*node was not in clique */
192 it->curr_cli_head = head;
193 head = get_next_cli_head(irn, it);
198 it->curr_cli_head = head;
199 head = get_next_cli_head(irn, it);
204 static cli_element_t *get_next_element(const ir_node *irn, cli_iter_t *it) /* ... of the current clique, returns NULL if there were no more elements ..*/
206 cli_element_t *element = it->curr_cli_element;
207 cli_head_t *head = it->curr_cli_head;
209 if (!head || it->curr_cli_element == NULL) /* way back of recursion or there are no more heads */
211 it->curr_cli_element = NULL;
216 element = list_entry(element->list.next, cli_element_t, list);
218 if (&element->list == &head->list) /* Clique has no more elements */
220 head = get_next_cli_head(irn, it);
221 element = get_next_element(irn, it);
224 if (element && element->irn == irn) /* the node you are searching neighbors for */
226 it->curr_cli_element = element;
227 element = get_next_element(irn, it);
230 it->curr_cli_element = element;
236 static void find_nodes(const ifg_clique_t *ifg, cli_iter_t *it)
238 cli_element_t *element;
239 cli_head_t *cli_head = ifg->cli_root;
241 bitset_clear_all(ifg->visited_nodes);
243 assert(cli_head && "There is no root entry to work on!");
245 it->curr_cli_head = cli_head;
247 if (cli_head->list.next != &cli_head->list) /* if cli_head contains an element */
249 element = list_entry(cli_head->list.next, cli_element_t, list);
250 it->curr_cli_element = element;
256 static ir_node *get_next_node(cli_iter_t *it)
258 cli_head_t *cli_head = NULL;
259 cli_element_t *element = it->curr_cli_element;
266 if (!(&it->curr_cli_head->list == element->list.next))
269 element = list_entry(element->list.next, cli_element_t, list);
270 it->curr_cli_element = element;
272 else /* reached end of clique */
274 cli_head = it->curr_cli_head;
275 if (!(cli_head->next_cli_head == NULL))
278 cli_head = cli_head->next_cli_head;
279 it->curr_cli_head = cli_head;
280 element = list_entry(cli_head->list.next, cli_element_t, list);
281 it->curr_cli_element = element;
285 it->curr_cli_head = NULL;
286 it->curr_cli_element = NULL;
292 if (bitset_is_set(it->ifg->visited_nodes, get_irn_idx(irn)))
294 irn = get_next_node(it);
298 bitset_set(it->ifg->visited_nodes, get_irn_idx(irn));
305 static void find_neighbour_walker(ir_node *bl, void *data)
307 ifg_clique_t *ifg = data;
308 struct list_head *head = get_block_border_head(ifg->env, bl);
311 nodeset *live = new_nodeset(ifg->env->cls->n_regs);
313 assert(is_Block(bl) && "There is no block to work on.");
315 foreach_border_head(head, b) /* follow the borders of the block */
317 ir_node *irn = b->irn;
319 // if (b->irn->node_nr == 1869)
322 if (b->is_def) /* b is a new node */
324 nodeset_insert(live, irn);
332 if (was_def == 1) /* if there is a USE after a DEF... */
334 write_clique(live, ifg); /* ...add the clique. */
337 nodeset_remove(live, irn);
343 static void find_first_neighbour(const ifg_clique_t *ifg, cli_iter_t *it, const ir_node *irn)
345 cli_head_t *cli_head = ifg->cli_root;
346 cli_element_t *element;
348 int is_dominated_by_max = 0;
349 int dominates_min = 0;
350 int is_in_clique = 0;
352 it->curr_cli_head = cli_head;
354 bitset_clear_all(it->ifg->visited_neighbours);
356 assert(cli_head && "There is no root entry for a cli_head.");
358 is_dominated_by_max = value_dominates(cli_head->max, irn);
359 dominates_min = value_dominates(irn, cli_head->min);
361 if ((is_dominated_by_max) || (irn == cli_head->max)) /* node could be in clique */
363 /* check if node is in clique */
364 list_for_each_entry(cli_element_t, element, &cli_head->list, list)
366 if (&element->list != &cli_head->list)
368 if (element->irn == irn)
369 { /* node is in clique */
370 it->curr_cli_element = (void *) cli_head; /* needed because the next element is searched with list.next of it->curr_cli_element */
372 element = get_next_element(irn, it);
380 cli_head = get_next_cli_head(irn, it);
381 element = get_next_element(irn, it);
384 it->curr_cli_element = element;
390 static ir_node *get_next_neighbour(cli_iter_t *it)
393 cli_head_t *cli_head = it->curr_cli_head;
394 const ir_node *irn = it->curr_irn;
396 if (it->curr_cli_element != NULL)
397 res = it->curr_cli_element->irn;
401 it->curr_cli_element = get_next_element(irn, it);
405 if (bitset_contains_irn(it->ifg->visited_neighbours, res))
407 res = get_next_neighbour(it);
411 bitset_set(it->ifg->visited_neighbours, get_irn_idx(res));
419 /* PUBLIC FUNCTIONS */
421 static void ifg_clique_free(void *self)
423 ifg_clique_t *ifg = self;
424 obstack_free(&ifg->obst, NULL);
426 bitset_free(ifg->visited_neighbours);
427 bitset_free(ifg->visited_nodes);
432 static int ifg_clique_connected(const ifg_clique_t *ifg, const ir_node *a, const ir_node *b)
438 find_first_neighbour(ifg, &it, a);
440 irn = get_next_neighbour(&it);
448 irn = get_next_neighbour(&it);
454 static ir_node *ifg_clique_neighbours_begin(const void *self, void *iter, const ir_node *irn)
456 find_first_neighbour(self, iter, irn);
457 return get_next_neighbour(iter);
460 static ir_node *ifg_clique_neighbours_next(const void *self, void *iter)
462 return get_next_neighbour(iter);
465 static void ifg_clique_neighbours_break(const void *self, void *iter)
470 static ir_node *ifg_clique_nodes_begin(const void *self, void *iter)
472 find_nodes(self, iter);
473 return get_next_node(iter);
476 static ir_node *ifg_clique_nodes_next(const void *self, void *iter)
478 return get_next_node(iter);
481 static void ifg_clique_nodes_break(const void *self, void *iter)
486 static int ifg_clique_degree(const void *self, const ir_node *irn)
491 find_first_neighbour(self, &it, irn);
493 irn = get_next_neighbour(&it);
497 irn = get_next_neighbour(&it);
503 static const be_ifg_impl_t ifg_clique_impl = {
508 ifg_clique_connected,
509 ifg_clique_neighbours_begin,
510 ifg_clique_neighbours_next,
511 ifg_clique_neighbours_break,
512 ifg_clique_nodes_begin,
513 ifg_clique_nodes_next,
514 ifg_clique_nodes_break,
521 be_ifg_t *be_ifg_clique_new(const be_chordal_env_t *env)
523 ifg_clique_t *ifg = xmalloc(sizeof(*ifg));
524 bitset_t *bitset_visnodes = bitset_malloc(get_irg_last_idx(env->irg));
525 bitset_t *bitset_visneighbours = bitset_malloc(get_irg_last_idx(env->irg));
526 ifg->visited_nodes = bitset_visnodes;
527 ifg->visited_neighbours = bitset_visneighbours;
528 ifg->impl = &ifg_clique_impl;
531 ifg->cli_root = NULL;
532 obstack_init(&ifg->obst);
534 dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
536 obstack_finish(&ifg->obst);
537 return (be_ifg_t *) ifg;