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 Pointer based implementation of chordal interference graphs.
23 * @author Sebastian Hack
35 #include "irphase_t.h"
38 #include "irgraph_t.h"
45 #include "bechordal_t.h"
47 typedef struct _ptr_element_t ptr_element_t;
49 typedef union element_content {
51 ptr_element_t *element;
54 struct _ptr_element_t {
55 int kind; /* kind = 8888 ..> both are ir_nodes, = 3101 ..> first is another element, second an ir_node */
56 element_content content_first; /* could be a ptr_element or ir_node */
57 element_content content_second; /* could be a ptr_element or ir_node */
60 typedef struct _ptr_head_t {
61 struct list_head list;
62 ptr_element_t *element;
65 typedef struct _ifg_pointer_t {
66 const be_ifg_impl_t *impl;
67 const be_chordal_env_t *env;
70 ptr_head_t *curr_ptr_head;
71 ptr_element_t *curr_element;
74 typedef struct _ptr_iter_t {
75 const ifg_pointer_t *ifg;
77 ptr_head_t *curr_ptr_head;
78 ptr_head_t *first_head;
79 ptr_element_t *curr_element_t;
83 bitset_t *visited_neighbours;
86 /* PRIVATE FUNCTIONS */
88 static void *ptr_irn_data_init(ir_phase *ph, const ir_node *irn, void *data)
90 ptr_head_t *head = phase_alloc(ph, sizeof(*head));
93 INIT_LIST_HEAD(&head->list);
97 static ptr_element_t *ptr_get_new_element(ifg_pointer_t *ifg)
99 ptr_element_t *new_element = obstack_alloc(&ifg->obst, sizeof(ptr_element_t));
100 memset(new_element, 0, sizeof(*new_element));
104 static ptr_head_t *ptr_get_new_head(ifg_pointer_t *ifg)
106 ptr_head_t *new_element = obstack_alloc(&ifg->obst, sizeof(*new_element));
107 INIT_LIST_HEAD(&new_element->list);
111 static void write_pointers(bitset_t *live, ifg_pointer_t *ifg)
116 bitset_foreach_irn(ifg->env->irg, live, elm, live_irn) {
117 ptr_head_t *head = phase_get_or_set_irn_data(&ifg->ph, live_irn);
118 ptr_head_t *element = ptr_get_new_head(ifg);
120 element->element = ifg->curr_element; /* write current highest sub-clique for each node */
121 list_add(&element->list, &head->list);
125 static ptr_element_t *get_last_sub_clique(ifg_pointer_t *ifg, bitset_t *live, bitset_t *my_live, ir_node *irn)
127 ptr_element_t *element = ifg->curr_element;
128 ptr_element_t *res = NULL;
130 /* search the last sub-clique before the sub-clique that contains the node irn */
131 if (element && element->kind == 8888
132 && (element->content_first.irn == irn
133 || element->content_second.irn == irn)) /* contains the node we search and there is no other sub-clique before */
135 if (bitset_is_set(live, get_irn_idx(element->content_first.irn)) && element->content_first.irn != irn) /* irn is still alive and not the node we search a sub-clique for */
137 bitset_set(my_live, get_irn_idx(element->content_first.irn));
140 if (bitset_is_set(live, get_irn_idx(element->content_second.irn))&& element->content_second.irn != irn) /* irn is still alive and not the node we search a sub-clique for */
142 bitset_set(my_live, get_irn_idx(element->content_second.irn));
148 if (element && element->kind == 8889)
149 { /* this was a "single-node-clique" */
153 if (element && (element->kind == 3101)
154 && (element->content_second.irn == irn)) /* sub-clique contains node, return previous sub-clique */
156 res = element->content_first.element;
160 if (element && element->kind == 3101) /* look at previous sub-cliques if the contain the node you are searching for*/
162 if (bitset_is_set(live, get_irn_idx(element->content_second.irn))) /* irn is still alive */
164 bitset_set(my_live, get_irn_idx(element->content_second.irn));
166 ifg->curr_element = element->content_first.element;
167 res = get_last_sub_clique(ifg, live, my_live, irn);
178 static void find_neighbour_walker(ir_node *bl, void *data)
180 ifg_pointer_t *ifg = data;
181 struct list_head *head = get_block_border_head(ifg->env, bl);
184 ir_node *first = NULL;
185 bitset_t *live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
190 element_content last_irn;
191 element_content last_element;
194 last_element.element = NULL;
196 assert(is_Block(bl) && "There is no block to work on.");
198 foreach_border_head(head, b) /* follow the borders of the block */
200 ir_node *irn = b->irn;
201 ptr_element_t *element = NULL;
203 if (b->is_def) /* b is a new node */
205 bitset_set(live, get_irn_idx(irn));
206 if (last_element.element)
208 element = ptr_get_new_element(ifg);
209 element->content_first.element = last_element.element;
210 element->content_second.irn = b->irn;
211 element->kind = 3101; /* first is an element, second an ir_node */
213 last_element.element = element;
214 ifg->curr_element = element;
218 if (last_irn.irn) /* create new sub-clique */
220 element = ptr_get_new_element(ifg);
221 element->content_first.irn = last_irn.irn;
222 element->content_second.irn = b->irn;
223 element->kind = 8888; /* both are ir_nodes */
225 last_element.element = element;
226 ifg->curr_element = element;
231 last_irn.irn = b->irn; /* needed to create first sub-clique */
232 last_element.element = NULL;
240 if (was_def == 1) /* if there is a USE after a DEF... */
242 if (!last_element.element)
243 { /* there was only one element in the clique */
244 element = ptr_get_new_element(ifg);
245 element->kind = 8889; /* first is a node, second is NULL, because this is a "single-node-clique" */
246 element->content_first.irn = last_irn.irn;
249 ifg->curr_element = NULL;
253 write_pointers(live, ifg); /* ...write a pointer to the highest sub-clique for each living node. */
257 my_live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
258 last_element.element = get_last_sub_clique(ifg, live, my_live, irn);
260 /* check and add still living nodes */
261 if (bitset_popcnt(my_live) > 1)
263 if (last_element.element)
265 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
267 ptr_element_t *my_element = ptr_get_new_element(ifg);
268 my_element->content_first.element = last_element.element;
269 my_element->content_second.irn = my_irn;
270 my_element->kind = 3101; /* first is an element, second an ir_node */
272 last_element.element = my_element;
273 ifg->curr_element = my_element;
278 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
280 ptr_element_t *my_element = NULL;
281 if (!first && !was_first)
288 if (first && was_first)
290 my_element = ptr_get_new_element(ifg);
291 my_element->content_first.irn = first;
292 my_element->content_second.irn = my_irn;
293 my_element->kind = 8888; /* both are ir_nodes */
294 last_element.element = my_element;
295 ifg->curr_element = my_element;
300 my_element = ptr_get_new_element(ifg);
301 my_element->content_first.element = last_element.element;
302 my_element->content_second.irn = my_irn;
303 my_element->kind = 3101; /* first is an element, second an ir_node */
304 last_element.element = my_element;
305 ifg->curr_element = my_element;
314 if (bitset_popcnt(my_live) == 1) /* there is only one node left */
316 if (last_element.element)
318 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
320 ptr_element_t *my_element = ptr_get_new_element(ifg);
321 my_element->content_first.element = last_element.element;
322 my_element->content_second.irn = my_irn;
323 my_element->kind = 3101; /* first is an element, second an ir_node */
325 last_element.element = my_element;
326 ifg->curr_element = my_element;
331 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn);
333 ptr_element_t *my_element = ptr_get_new_element(ifg);
334 my_element->content_first.irn = my_irn;
335 my_element->content_second.irn = NULL;
336 my_element->kind = 8889;
337 last_element.element = my_element;
338 ifg->curr_element = my_element;
343 bitset_free(my_live);
344 bitset_remv_irn(live, irn);
350 static ir_node *get_first_irn(const ifg_pointer_t *ifg, ptr_iter_t *it)
352 ir_node *irn = phase_get_first_node(&ifg->ph);
362 static ir_node *get_next_irn(ptr_iter_t *it)
364 ir_node *irn = phase_get_next_node(&it->ifg->ph, it->curr_irn);
374 static ir_node *get_next_neighbour(ptr_iter_t *it)
378 ptr_element_t *element;
380 element = it->curr_element_t;
384 if (it->curr_ptr_head->list.next != &it->first_head->list)
386 head = list_entry(it->curr_ptr_head->list.next, ptr_head_t, list);
387 it->curr_ptr_head = head;
388 element = head->element;
391 return NULL; /* there are no more neighbours */
394 if (element && element->kind == 8889) /* node has no neighbours */
396 res = element->content_first.irn;
397 it->curr_element_t = NULL;
401 if (element && element->kind == 8888) /* node has only one more neighbour */
405 if (element->content_first.irn != it->irn)
407 res = element->content_first.irn;
409 it->curr_element_t = NULL;
414 it->curr_element_t = NULL;
416 res = get_next_neighbour(it);
422 if (element->content_second.irn != it->irn)
424 res = element->content_second.irn;
426 it->curr_element_t = element;
431 it->curr_element_t = element;
433 res = get_next_neighbour(it);
440 if (element && element->kind == 3101)
442 it->curr_element_t = element->content_first.element;
443 res = element->content_second.irn;
446 { /* element is only an ir_node */// TODO
447 it->curr_element_t = NULL;
453 if (res && !it->sub_call)
455 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
457 res = get_next_neighbour(it);
461 bitset_set(it->visited_neighbours, get_irn_idx(res));
468 static ir_node *get_first_neighbour(const ifg_pointer_t *ifg, ptr_iter_t *it, const ir_node *irn)
472 ptr_element_t *element;
473 bitset_t *bitsetvisited_neighbours = bitset_malloc(get_irg_last_idx(ifg->env->irg));
480 it->visited_neighbours = bitsetvisited_neighbours;
482 head = phase_get_irn_data(&ifg->ph, irn);
487 it->first_head = head;
488 head = list_entry(it->first_head->list.next, ptr_head_t, list); /* because first element is NULL */
489 it->curr_ptr_head = head;
490 element = head->element;
493 if (element && element->kind == 8889) /* node has no neighbours */
495 res = element->content_first.irn;
496 it->curr_element_t = NULL;
500 if (element && element->kind == 8888) /* node has only one neighbour */
504 if (element->content_first.irn != it->irn)
506 res = element->content_first.irn;
508 it->curr_element_t = NULL;
513 it->curr_element_t = NULL;
515 res = get_next_neighbour(it);
521 if (element->content_second.irn != it->irn)
523 res = element->content_second.irn;
524 it->curr_element_t = element;
530 it->curr_element_t = element;
532 res = get_next_neighbour(it);
538 if (element && element->kind == 3101)
540 it->curr_element_t = element->content_first.element;
541 res = element->content_second.irn;
544 { /* element is only an ir_node */
545 it->curr_element_t = NULL;
550 if (res && !it->sub_call)
552 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
554 res = get_next_neighbour(it);
558 bitset_set(it->visited_neighbours, get_irn_idx(res));
567 /* PUBLIC FUNCTIONS */
569 static void ifg_pointer_free(void *self)
571 ifg_pointer_t *ifg = self;
572 obstack_free(&ifg->obst, NULL);
573 phase_free(&ifg->ph);
578 static int ifg_pointer_connected(const void *self, const ir_node *a, const ir_node *b)
580 const ifg_pointer_t *ifg = self;
585 irn = get_first_neighbour(ifg, &it, a);
594 irn = get_next_neighbour(&it);
600 static ir_node *ifg_pointer_neighbours_begin(const void *self, void *iter, const ir_node *irn)
602 return get_first_neighbour(self, iter, irn);
605 static ir_node *ifg_pointer_neighbours_next(const void *self, void *iter)
608 return get_next_neighbour(iter);
611 static void ifg_pointer_neighbours_break(const void *self, void *iter)
613 ptr_iter_t *it = iter;
616 bitset_free(it->visited_neighbours);
619 static ir_node *ifg_pointer_nodes_begin(const void *self, void *iter)
621 return get_first_irn(self, iter);
624 static ir_node *ifg_pointer_nodes_next(const void *self, void *iter)
627 return get_next_irn(iter);
630 static void ifg_pointer_nodes_break(const void *self, void *iter)
637 static int ifg_pointer_degree(const void *self, const ir_node *irn)
642 irn = get_first_neighbour(self, &it, irn);
647 irn = get_next_neighbour(&it);
653 static const be_ifg_impl_t ifg_pointer_impl = {
658 ifg_pointer_connected,
659 ifg_pointer_neighbours_begin,
660 ifg_pointer_neighbours_next,
661 ifg_pointer_neighbours_break,
662 ifg_pointer_nodes_begin,
663 ifg_pointer_nodes_next,
664 ifg_pointer_nodes_break,
671 be_ifg_t *be_ifg_pointer_new(const be_chordal_env_t *env)
673 ifg_pointer_t *ifg = XMALLOC(ifg_pointer_t);
674 ifg->impl = &ifg_pointer_impl;
677 phase_init(&ifg->ph, "ptr_map", env->irg, PHASE_DEFAULT_GROWTH, ptr_irn_data_init, NULL);
678 obstack_init(&ifg->obst);
680 dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
682 obstack_finish(&ifg->obst);
683 return (be_ifg_t *) ifg;