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 Clique calculation for chordal ifg.
23 * @author Sebastian Hack
35 #include "irgraph_t.h"
41 #include "beintlive_t.h"
43 #include "bechordal_t.h"
45 typedef struct _cli_head_t {
46 struct list_head list;
47 struct _cli_head_t *next_cli_head;
52 typedef struct _ifg_clique_t {
53 const be_ifg_impl_t *impl;
54 const be_chordal_env_t *env;
57 cli_head_t *curr_cli_head;
60 typedef struct _cli_element_t {
61 struct list_head list;
65 typedef struct _cli_iter_t {
66 const ifg_clique_t *ifg;
67 cli_head_t *curr_cli_head;
68 cli_element_t *curr_cli_element;
69 const ir_node *curr_irn;
70 bitset_t *visited_neighbours;
71 bitset_t *visited_nodes;
74 /* PRIVATE FUNCTIONS */
75 static cli_head_t *get_new_cli_head(ifg_clique_t *ifg)
78 cli_head_t *new_cli_head;
80 if (ifg->cli_root == NULL)
82 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
83 INIT_LIST_HEAD(&new_cli_head->list);
84 ifg->cli_root = new_cli_head;
88 cli_head = ifg->cli_root;
89 while(!(cli_head->next_cli_head == NULL))
91 cli_head = cli_head->next_cli_head;
93 new_cli_head = obstack_alloc(&ifg->obst, sizeof(*new_cli_head));
94 INIT_LIST_HEAD(&new_cli_head->list);
95 cli_head->next_cli_head = new_cli_head;
98 new_cli_head->min = NULL;
99 new_cli_head->max = NULL;
100 new_cli_head->next_cli_head = NULL;
101 ifg->curr_cli_head = new_cli_head;
106 static cli_element_t *get_new_cli_element(ifg_clique_t *ifg)
108 cli_element_t *cli_element;
110 cli_element = obstack_alloc(&ifg->obst, sizeof(*cli_element));
111 INIT_LIST_HEAD(&cli_element->list);
116 static void write_clique(ir_nodeset_t *live_set, ifg_clique_t *ifg)
121 ir_node *max_node = NULL;
122 ir_node *min_node = NULL;
123 cli_element_t *new_element = NULL;
124 cli_element_t *element = NULL;
125 cli_head_t *cli_head = get_new_cli_head(ifg);
127 ir_nodeset_iterator_t iter;
129 foreach_ir_nodeset(live_set, live_irn, iter)
131 /* test if node is max or min dominator*/
132 test_node = live_irn;
133 if (max_node == NULL)
135 max_node = test_node;
136 min_node = test_node;
140 test_int = value_dominates(test_node, max_node);
143 max_node = test_node;
145 test_int = value_dominates(min_node, test_node);
148 min_node = test_node;
152 list_for_each_entry(cli_element_t, element, &cli_head->list, list){
153 if(element->irn == live_irn){
160 new_element = get_new_cli_element(ifg);
161 new_element->irn = live_irn;
162 list_add(&new_element->list, &cli_head->list) ;
166 cli_head->min = min_node;
167 cli_head->max = max_node;
170 static cli_head_t *get_next_cli_head(const ir_node *irn, cli_iter_t *it) /* ...containing the node *irn */
173 cli_element_t *element;
175 int is_dominated_by_max;
178 if (it->curr_cli_head == NULL || it->curr_cli_head->next_cli_head == NULL) /* way back of recursion or this is the last clique */
180 it->curr_cli_head = NULL;
184 head = it->curr_cli_head->next_cli_head;
186 is_dominated_by_max = value_dominates(head->max, irn);
187 //dominates_min = value_dominates(irn, head->min);
189 if ((is_dominated_by_max) || (irn == head->max)) /* node could be in clique */
191 /* check if node is in clique */
192 list_for_each_entry(cli_element_t, element, &head->list, list)
194 if (&element->list != &head->list)
196 if (element->irn == irn)
198 /* node is in clique */
199 it->curr_cli_head = head;
200 /* needed because the next element is searched with list.next of it->curr_cli_element */
201 it->curr_cli_element = (void *) head;
207 if (it->curr_cli_head != head) /*node was not in clique */
209 it->curr_cli_head = head;
210 head = get_next_cli_head(irn, it);
215 it->curr_cli_head = head;
216 head = get_next_cli_head(irn, it);
221 /* ... of the current clique, returns NULL if there were no more elements ..*/
222 static cli_element_t *get_next_element(const ir_node *irn, cli_iter_t *it)
224 cli_element_t *element = it->curr_cli_element;
225 cli_head_t *head = it->curr_cli_head;
227 if (!head || it->curr_cli_element == NULL) /* way back of recursion or there are no more heads */
229 it->curr_cli_element = NULL;
234 element = list_entry(element->list.next, cli_element_t, list);
236 if (&element->list == &head->list) /* Clique has no more elements */
238 head = get_next_cli_head(irn, it);
239 element = get_next_element(irn, it);
242 if (element && element->irn == irn) /* the node you are searching neighbors for */
244 it->curr_cli_element = element;
245 element = get_next_element(irn, it);
248 it->curr_cli_element = element;
254 static void find_nodes(const ifg_clique_t *ifg, cli_iter_t *it)
256 cli_element_t *element;
257 cli_head_t *cli_head = ifg->cli_root;
259 bitset_t *bitset_visnodes = bitset_malloc(get_irg_last_idx(ifg->env->irg));
261 it->visited_nodes = bitset_visnodes;
262 it->curr_cli_head = cli_head;
264 assert(cli_head && "There is no root entry to work on!");
266 if (cli_head->list.next != &cli_head->list) /* if cli_head contains an element */
268 element = list_entry(cli_head->list.next, cli_element_t, list);
269 it->curr_cli_element = element;
273 static ir_node *get_next_node(cli_iter_t *it)
275 cli_head_t *cli_head = NULL;
276 cli_element_t *element = it->curr_cli_element;
283 if (!(&it->curr_cli_head->list == element->list.next))
286 element = list_entry(element->list.next, cli_element_t, list);
287 it->curr_cli_element = element;
289 else /* reached end of clique */
291 cli_head = it->curr_cli_head;
292 if (!(cli_head->next_cli_head == NULL))
295 cli_head = cli_head->next_cli_head;
296 it->curr_cli_head = cli_head;
297 element = list_entry(cli_head->list.next, cli_element_t, list);
298 it->curr_cli_element = element;
302 it->curr_cli_head = NULL;
303 it->curr_cli_element = NULL;
309 if (bitset_is_set(it->visited_nodes, get_irn_idx(irn)))
311 irn = get_next_node(it);
315 bitset_set(it->visited_nodes, get_irn_idx(irn));
322 static void find_neighbour_walker(ir_node *bl, void *data)
324 ifg_clique_t *ifg = data;
325 struct list_head *head = get_block_border_head(ifg->env, bl);
330 ir_nodeset_init(&live);
332 assert(is_Block(bl) && "There is no block to work on.");
334 foreach_border_head(head, b) /* follow the borders of the block */
336 ir_node *irn = b->irn;
338 if (b->is_def) /* b is a new node */
340 ir_nodeset_insert(&live, irn);
348 if (was_def == 1) /* if there is a USE after a DEF... */
350 write_clique(&live, ifg); /* ...add the clique. */
353 ir_nodeset_remove(&live, irn);
356 ir_nodeset_destroy(&live);
359 static void find_first_neighbour(const ifg_clique_t *ifg, cli_iter_t *it, const ir_node *irn)
361 cli_head_t *cli_head = ifg->cli_root;
362 cli_element_t *element;
363 bitset_t *bitset_visneighbours = bitset_malloc(get_irg_last_idx(ifg->env->irg));
365 int is_dominated_by_max = 0;
366 int dominates_min = 0;
367 int is_in_clique = 0;
369 it->curr_cli_head = cli_head;
371 it->visited_neighbours = bitset_visneighbours;
373 assert(cli_head && "There is no root entry for a cli_head.");
375 is_dominated_by_max = value_dominates(cli_head->max, irn);
376 dominates_min = value_dominates(irn, cli_head->min);
378 if ((is_dominated_by_max) || (irn == cli_head->max)) /* node could be in clique */
380 /* check if node is in clique */
381 list_for_each_entry(cli_element_t, element, &cli_head->list, list)
383 if (element->irn == irn) /* node is in clique */
385 it->curr_cli_element = (void *) cli_head; /* needed because the next element is searched with list.next of it->curr_cli_element */
387 element = get_next_element(irn, it);
394 cli_head = get_next_cli_head(irn, it);
395 element = get_next_element(irn, it);
398 it->curr_cli_element = element;
402 static ir_node *get_next_neighbour(cli_iter_t *it)
405 const ir_node *irn = it->curr_irn;
407 if (it->curr_cli_element != NULL)
408 res = it->curr_cli_element->irn;
412 it->curr_cli_element = get_next_element(irn, it);
416 if (bitset_contains_irn(it->visited_neighbours, res))
418 res = get_next_neighbour(it);
422 bitset_set(it->visited_neighbours, get_irn_idx(res));
430 /* PUBLIC FUNCTIONS */
432 static void ifg_clique_free(void *self)
434 ifg_clique_t *ifg = self;
435 obstack_free(&ifg->obst, NULL);
440 static int ifg_clique_connected(const void *self, const ir_node *a, const ir_node *b)
442 const ifg_clique_t *ifg = self;
447 find_first_neighbour(ifg, &it, a);
449 irn = get_next_neighbour(&it);
457 irn = get_next_neighbour(&it);
463 static ir_node *ifg_clique_neighbours_begin(const void *self, void *iter, const ir_node *irn)
465 find_first_neighbour(self, iter, irn);
466 return get_next_neighbour(iter);
469 static ir_node *ifg_clique_neighbours_next(const void *self, void *iter)
472 return get_next_neighbour(iter);
475 static void ifg_clique_neighbours_break(const void *self, void *iter)
477 cli_iter_t *it = iter;
480 bitset_free(it->visited_neighbours);
483 static ir_node *ifg_clique_nodes_begin(const void *self, void *iter)
485 find_nodes(self, iter);
486 return get_next_node(iter);
489 static ir_node *ifg_clique_nodes_next(const void *self, void *iter)
492 return get_next_node(iter);
495 static void ifg_clique_nodes_break(const void *self, void *iter)
497 cli_iter_t *it = iter;
500 bitset_free(it->visited_nodes);
503 static int ifg_clique_degree(const void *self, const ir_node *irn)
508 find_first_neighbour(self, &it, irn);
510 irn = get_next_neighbour(&it);
514 irn = get_next_neighbour(&it);
520 static const be_ifg_impl_t ifg_clique_impl = {
525 ifg_clique_connected,
526 ifg_clique_neighbours_begin,
527 ifg_clique_neighbours_next,
528 ifg_clique_neighbours_break,
529 ifg_clique_nodes_begin,
530 ifg_clique_nodes_next,
531 ifg_clique_nodes_break,
538 be_ifg_t *be_ifg_clique_new(const be_chordal_env_t *env)
540 ifg_clique_t *ifg = XMALLOC(ifg_clique_t);
542 ifg->impl = &ifg_clique_impl;
545 ifg->cli_root = NULL;
546 obstack_init(&ifg->obst);
548 dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
550 obstack_finish(&ifg->obst);
551 return (be_ifg_t *) ifg;