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"
27 #include "benodesets.h"
29 typedef struct _cli_head_t {
30 struct list_head list;
31 struct _cli_head_t *next_cli_head;
36 typedef struct _ifg_clique_t {
37 const be_ifg_impl_t *impl;
38 const be_chordal_env_t *env;
41 cli_head_t *curr_cli_head;
44 typedef struct _cli_element_t {
45 struct list_head list;
49 typedef struct _cli_iter_t {
50 const ifg_clique_t *ifg;
51 cli_head_t *curr_cli_head;
52 cli_element_t *curr_cli_element;
53 const ir_node *curr_irn;
54 bitset_t *visited_neighbours;
55 bitset_t *visited_nodes;
58 /* PRIVATE FUNCTIONS */
59 static cli_head_t *get_new_cli_head(ifg_clique_t *ifg)
62 cli_head_t *new_cli_head;
64 if (ifg->cli_root == NULL)
66 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
67 INIT_LIST_HEAD(&new_cli_head->list);
68 ifg->cli_root = new_cli_head;
72 cli_head = ifg->cli_root;
73 while(!(cli_head->next_cli_head == NULL))
75 cli_head = cli_head->next_cli_head;
77 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
78 INIT_LIST_HEAD(&new_cli_head->list);
79 cli_head->next_cli_head = new_cli_head;
82 new_cli_head->min = NULL;
83 new_cli_head->max = NULL;
84 new_cli_head->next_cli_head = NULL;
85 ifg->curr_cli_head = new_cli_head;
90 static cli_element_t *get_new_cli_element(ifg_clique_t *ifg)
92 cli_element_t *cli_element;
94 cli_element = obstack_alloc(&ifg->obst, sizeof(*cli_element));
95 INIT_LIST_HEAD(&cli_element->list);
100 static void write_clique(nodeset *live_set, ifg_clique_t *ifg)
105 ir_node *max_node = NULL;
106 ir_node *min_node = NULL;
107 cli_element_t *new_element = NULL;
108 cli_element_t *element = NULL;
109 cli_head_t *cli_head = get_new_cli_head(ifg);
112 foreach_nodeset(live_set, live_irn)
114 /* test if node is max or min dominator*/
115 test_node = live_irn;
116 if (max_node == NULL)
118 max_node = test_node;
119 min_node = test_node;
123 test_int = value_dominates(test_node, max_node);
126 max_node = test_node;
128 test_int = value_dominates(min_node, test_node);
131 min_node = test_node;
135 list_for_each_entry(cli_element_t, element, &cli_head->list, list){
136 if(element->irn == live_irn){
143 new_element = get_new_cli_element(ifg);
144 new_element->irn = live_irn;
145 list_add(&new_element->list, &cli_head->list) ;
149 cli_head->min = min_node;
150 cli_head->max = max_node;
153 static cli_head_t *get_next_cli_head(const ir_node *irn, cli_iter_t *it) /* ...containing the node *irn */
156 cli_element_t *element;
158 int is_dominated_by_max;
161 if (it->curr_cli_head == NULL || it->curr_cli_head->next_cli_head == NULL) /* way back of recursion or this is the last clique */
163 it->curr_cli_head = NULL;
167 head = it->curr_cli_head->next_cli_head;
169 is_dominated_by_max = value_dominates(head->max, irn);
170 //dominates_min = value_dominates(irn, head->min);
172 if ((is_dominated_by_max) || (irn == head->max)) /* node could be in clique */
174 /* check if node is in clique */
175 list_for_each_entry(cli_element_t, element, &head->list, list)
177 if (&element->list != &head->list)
179 if (element->irn == irn)
180 { /* node is in clique */
181 it->curr_cli_head = head;
182 it->curr_cli_element = (void *) head; /* needed because the next element is searched with list.next of it->curr_cli_element */
188 if (it->curr_cli_head != head) /*node was not in clique */
190 it->curr_cli_head = head;
191 head = get_next_cli_head(irn, it);
196 it->curr_cli_head = head;
197 head = get_next_cli_head(irn, it);
202 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 ..*/
204 cli_element_t *element = it->curr_cli_element;
205 cli_head_t *head = it->curr_cli_head;
207 if (!head || it->curr_cli_element == NULL) /* way back of recursion or there are no more heads */
209 it->curr_cli_element = NULL;
214 element = list_entry(element->list.next, cli_element_t, list);
216 if (&element->list == &head->list) /* Clique has no more elements */
218 head = get_next_cli_head(irn, it);
219 element = get_next_element(irn, it);
222 if (element && element->irn == irn) /* the node you are searching neighbors for */
224 it->curr_cli_element = element;
225 element = get_next_element(irn, it);
228 it->curr_cli_element = element;
234 static void find_nodes(const ifg_clique_t *ifg, cli_iter_t *it)
236 cli_element_t *element;
237 cli_head_t *cli_head = ifg->cli_root;
239 bitset_t *bitset_visnodes = bitset_malloc(get_irg_last_idx(ifg->env->irg));
241 it->visited_nodes = bitset_visnodes;
242 it->curr_cli_head = cli_head;
244 assert(cli_head && "There is no root entry to work on!");
246 if (cli_head->list.next != &cli_head->list) /* if cli_head contains an element */
248 element = list_entry(cli_head->list.next, cli_element_t, list);
249 it->curr_cli_element = element;
255 static ir_node *get_next_node(cli_iter_t *it)
257 cli_head_t *cli_head = NULL;
258 cli_element_t *element = it->curr_cli_element;
265 if (!(&it->curr_cli_head->list == element->list.next))
268 element = list_entry(element->list.next, cli_element_t, list);
269 it->curr_cli_element = element;
271 else /* reached end of clique */
273 cli_head = it->curr_cli_head;
274 if (!(cli_head->next_cli_head == NULL))
277 cli_head = cli_head->next_cli_head;
278 it->curr_cli_head = cli_head;
279 element = list_entry(cli_head->list.next, cli_element_t, list);
280 it->curr_cli_element = element;
284 it->curr_cli_head = NULL;
285 it->curr_cli_element = NULL;
291 if (bitset_is_set(it->visited_nodes, get_irn_idx(irn)))
293 irn = get_next_node(it);
297 bitset_set(it->visited_nodes, get_irn_idx(irn));
304 static void find_neighbour_walker(ir_node *bl, void *data)
306 ifg_clique_t *ifg = data;
307 struct list_head *head = get_block_border_head(ifg->env, bl);
310 nodeset *live = new_nodeset(ifg->env->cls->n_regs);
312 assert(is_Block(bl) && "There is no block to work on.");
314 foreach_border_head(head, b) /* follow the borders of the block */
316 ir_node *irn = b->irn;
318 if (b->is_def) /* b is a new node */
320 nodeset_insert(live, irn);
328 if (was_def == 1) /* if there is a USE after a DEF... */
330 write_clique(live, ifg); /* ...add the clique. */
333 nodeset_remove(live, irn);
339 static void find_first_neighbour(const ifg_clique_t *ifg, cli_iter_t *it, const ir_node *irn)
341 cli_head_t *cli_head = ifg->cli_root;
342 cli_element_t *element;
343 bitset_t *bitset_visneighbours = bitset_malloc(get_irg_last_idx(ifg->env->irg));
345 int is_dominated_by_max = 0;
346 int dominates_min = 0;
347 int is_in_clique = 0;
349 it->curr_cli_head = cli_head;
351 it->visited_neighbours = bitset_visneighbours;
353 assert(cli_head && "There is no root entry for a cli_head.");
355 is_dominated_by_max = value_dominates(cli_head->max, irn);
356 dominates_min = value_dominates(irn, cli_head->min);
358 if ((is_dominated_by_max) || (irn == cli_head->max)) /* node could be in clique */
360 /* check if node is in clique */
361 list_for_each_entry(cli_element_t, element, &cli_head->list, list)
363 if (element->irn == irn) /* node is in clique */
365 it->curr_cli_element = (void *) cli_head; /* needed because the next element is searched with list.next of it->curr_cli_element */
367 element = get_next_element(irn, it);
374 cli_head = get_next_cli_head(irn, it);
375 element = get_next_element(irn, it);
378 it->curr_cli_element = element;
384 static ir_node *get_next_neighbour(cli_iter_t *it)
387 const ir_node *irn = it->curr_irn;
389 if (it->curr_cli_element != NULL)
390 res = it->curr_cli_element->irn;
394 it->curr_cli_element = get_next_element(irn, it);
398 if (bitset_contains_irn(it->visited_neighbours, res))
400 res = get_next_neighbour(it);
404 bitset_set(it->visited_neighbours, get_irn_idx(res));
412 /* PUBLIC FUNCTIONS */
414 static void ifg_clique_free(void *self)
416 ifg_clique_t *ifg = self;
417 obstack_free(&ifg->obst, NULL);
422 static int ifg_clique_connected(const void *self, const ir_node *a, const ir_node *b)
424 const ifg_clique_t *ifg = self;
429 find_first_neighbour(ifg, &it, a);
431 irn = get_next_neighbour(&it);
439 irn = get_next_neighbour(&it);
445 static ir_node *ifg_clique_neighbours_begin(const void *self, void *iter, const ir_node *irn)
447 find_first_neighbour(self, iter, irn);
448 return get_next_neighbour(iter);
451 static ir_node *ifg_clique_neighbours_next(const void *self, void *iter)
453 return get_next_neighbour(iter);
456 static void ifg_clique_neighbours_break(const void *self, void *iter)
458 cli_iter_t *it = iter;
460 bitset_free(it->visited_neighbours);
465 static ir_node *ifg_clique_nodes_begin(const void *self, void *iter)
467 find_nodes(self, iter);
468 return get_next_node(iter);
471 static ir_node *ifg_clique_nodes_next(const void *self, void *iter)
473 return get_next_node(iter);
476 static void ifg_clique_nodes_break(const void *self, void *iter)
478 cli_iter_t *it = iter;
480 bitset_free(it->visited_nodes);
485 static int ifg_clique_degree(const void *self, const ir_node *irn)
490 find_first_neighbour(self, &it, irn);
492 irn = get_next_neighbour(&it);
496 irn = get_next_neighbour(&it);
502 static const be_ifg_impl_t ifg_clique_impl = {
507 ifg_clique_connected,
508 ifg_clique_neighbours_begin,
509 ifg_clique_neighbours_next,
510 ifg_clique_neighbours_break,
511 ifg_clique_nodes_begin,
512 ifg_clique_nodes_next,
513 ifg_clique_nodes_break,
520 be_ifg_t *be_ifg_clique_new(const be_chordal_env_t *env)
522 ifg_clique_t *ifg = xmalloc(sizeof(*ifg));
524 ifg->impl = &ifg_clique_impl;
527 ifg->cli_root = NULL;
528 obstack_init(&ifg->obst);
530 dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
532 obstack_finish(&ifg->obst);
533 return (be_ifg_t *) ifg;