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
33 #include "irphase_t.h"
36 #include "irgraph_t.h"
43 #include "bechordal_t.h"
45 typedef struct _ptr_element_t ptr_element_t;
47 typedef union element_content {
49 ptr_element_t *element;
52 struct _ptr_element_t {
53 int kind; /* kind = 8888 ..> both are ir_nodes, = 3101 ..> first is another element, second an ir_node */
54 element_content content_first; /* could be a ptr_element or ir_node */
55 element_content content_second; /* could be a ptr_element or ir_node */
58 typedef struct _ptr_head_t {
59 struct list_head list;
60 ptr_element_t *element;
63 typedef struct _ifg_pointer_t {
64 const be_ifg_impl_t *impl;
65 const be_chordal_env_t *env;
68 ptr_head_t *curr_ptr_head;
69 ptr_element_t *curr_element;
72 typedef struct _ptr_iter_t {
73 const ifg_pointer_t *ifg;
75 ptr_head_t *curr_ptr_head;
76 ptr_head_t *first_head;
77 ptr_element_t *curr_element_t;
81 bitset_t *visited_neighbours;
84 /* PRIVATE FUNCTIONS */
86 static void *ptr_irn_data_init(ir_phase *ph, const ir_node *irn, void *data)
88 ptr_head_t *head = phase_alloc(ph, sizeof(*head));
91 INIT_LIST_HEAD(&head->list);
95 static ptr_element_t *ptr_get_new_element(ifg_pointer_t *ifg)
97 ptr_element_t *new_element = obstack_alloc(&ifg->obst, sizeof(ptr_element_t));
98 memset(new_element, 0, sizeof(*new_element));
102 static ptr_head_t *ptr_get_new_head(ifg_pointer_t *ifg)
104 ptr_head_t *new_element = obstack_alloc(&ifg->obst, sizeof(*new_element));
105 INIT_LIST_HEAD(&new_element->list);
109 static void write_pointers(bitset_t *live, ifg_pointer_t *ifg)
114 bitset_foreach_irn(ifg->env->irg, live, elm, live_irn) {
115 ptr_head_t *head = phase_get_or_set_irn_data(&ifg->ph, live_irn);
116 ptr_head_t *element = ptr_get_new_head(ifg);
118 element->element = ifg->curr_element; /* write current highest sub-clique for each node */
119 list_add(&element->list, &head->list);
123 static ptr_element_t *get_last_sub_clique(ifg_pointer_t *ifg, bitset_t *live, bitset_t *my_live, ir_node *irn)
125 ptr_element_t *element = ifg->curr_element;
126 ptr_element_t *res = NULL;
128 /* search the last sub-clique before the sub-clique that contains the node irn */
129 if (element && element->kind == 8888
130 && (element->content_first.irn == irn
131 || element->content_second.irn == irn)) /* contains the node we search and there is no other sub-clique before */
133 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 */
135 bitset_set(my_live, get_irn_idx(element->content_first.irn));
138 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 */
140 bitset_set(my_live, get_irn_idx(element->content_second.irn));
146 if (element && element->kind == 8889)
147 { /* this was a "single-node-clique" */
151 if (element && (element->kind == 3101)
152 && (element->content_second.irn == irn)) /* sub-clique contains node, return previous sub-clique */
154 res = element->content_first.element;
158 if (element && element->kind == 3101) /* look at previous sub-cliques if the contain the node you are searching for*/
160 if (bitset_is_set(live, get_irn_idx(element->content_second.irn))) /* irn is still alive */
162 bitset_set(my_live, get_irn_idx(element->content_second.irn));
164 ifg->curr_element = element->content_first.element;
165 res = get_last_sub_clique(ifg, live, my_live, irn);
176 static void find_neighbour_walker(ir_node *bl, void *data)
178 ifg_pointer_t *ifg = data;
179 struct list_head *head = get_block_border_head(ifg->env, bl);
182 ir_node *first = NULL;
183 bitset_t *live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
188 element_content last_irn;
189 element_content last_element;
192 last_element.element = NULL;
194 assert(is_Block(bl) && "There is no block to work on.");
196 foreach_border_head(head, b) /* follow the borders of the block */
198 ir_node *irn = b->irn;
199 ptr_element_t *element = NULL;
201 if (b->is_def) /* b is a new node */
203 bitset_set(live, get_irn_idx(irn));
204 if (last_element.element)
206 element = ptr_get_new_element(ifg);
207 element->content_first.element = last_element.element;
208 element->content_second.irn = b->irn;
209 element->kind = 3101; /* first is an element, second an ir_node */
211 last_element.element = element;
212 ifg->curr_element = element;
216 if (last_irn.irn) /* create new sub-clique */
218 element = ptr_get_new_element(ifg);
219 element->content_first.irn = last_irn.irn;
220 element->content_second.irn = b->irn;
221 element->kind = 8888; /* both are ir_nodes */
223 last_element.element = element;
224 ifg->curr_element = element;
229 last_irn.irn = b->irn; /* needed to create first sub-clique */
230 last_element.element = NULL;
238 if (was_def == 1) /* if there is a USE after a DEF... */
240 if (!last_element.element)
241 { /* there was only one element in the clique */
242 element = ptr_get_new_element(ifg);
243 element->kind = 8889; /* first is a node, second is NULL, because this is a "single-node-clique" */
244 element->content_first.irn = last_irn.irn;
247 ifg->curr_element = NULL;
251 write_pointers(live, ifg); /* ...write a pointer to the highest sub-clique for each living node. */
255 my_live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
256 last_element.element = get_last_sub_clique(ifg, live, my_live, irn);
258 /* check and add still living nodes */
259 if (bitset_popcnt(my_live) > 1)
261 if (last_element.element)
263 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
265 ptr_element_t *my_element = ptr_get_new_element(ifg);
266 my_element->content_first.element = last_element.element;
267 my_element->content_second.irn = my_irn;
268 my_element->kind = 3101; /* first is an element, second an ir_node */
270 last_element.element = my_element;
271 ifg->curr_element = my_element;
276 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
278 ptr_element_t *my_element = NULL;
279 if (!first && !was_first)
286 if (first && was_first)
288 my_element = ptr_get_new_element(ifg);
289 my_element->content_first.irn = first;
290 my_element->content_second.irn = my_irn;
291 my_element->kind = 8888; /* both are ir_nodes */
292 last_element.element = my_element;
293 ifg->curr_element = my_element;
298 my_element = ptr_get_new_element(ifg);
299 my_element->content_first.element = last_element.element;
300 my_element->content_second.irn = my_irn;
301 my_element->kind = 3101; /* first is an element, second an ir_node */
302 last_element.element = my_element;
303 ifg->curr_element = my_element;
312 if (bitset_popcnt(my_live) == 1) /* there is only one node left */
314 if (last_element.element)
316 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
318 ptr_element_t *my_element = ptr_get_new_element(ifg);
319 my_element->content_first.element = last_element.element;
320 my_element->content_second.irn = my_irn;
321 my_element->kind = 3101; /* first is an element, second an ir_node */
323 last_element.element = my_element;
324 ifg->curr_element = my_element;
329 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn);
331 ptr_element_t *my_element = ptr_get_new_element(ifg);
332 my_element->content_first.irn = my_irn;
333 my_element->content_second.irn = NULL;
334 my_element->kind = 8889;
335 last_element.element = my_element;
336 ifg->curr_element = my_element;
341 bitset_free(my_live);
342 bitset_remv_irn(live, irn);
348 static ir_node *get_first_irn(const ifg_pointer_t *ifg, ptr_iter_t *it)
350 ir_node *irn = phase_get_first_node(&ifg->ph);
360 static ir_node *get_next_irn(ptr_iter_t *it)
362 ir_node *irn = phase_get_next_node(&it->ifg->ph, it->curr_irn);
372 static ir_node *get_next_neighbour(ptr_iter_t *it)
376 ptr_element_t *element;
378 element = it->curr_element_t;
382 if (it->curr_ptr_head->list.next != &it->first_head->list)
384 head = list_entry(it->curr_ptr_head->list.next, ptr_head_t, list);
385 it->curr_ptr_head = head;
386 element = head->element;
389 return NULL; /* there are no more neighbours */
392 if (element && element->kind == 8889) /* node has no neighbours */
394 res = element->content_first.irn;
395 it->curr_element_t = NULL;
399 if (element && element->kind == 8888) /* node has only one more neighbour */
403 if (element->content_first.irn != it->irn)
405 res = element->content_first.irn;
407 it->curr_element_t = NULL;
412 it->curr_element_t = NULL;
414 res = get_next_neighbour(it);
420 if (element->content_second.irn != it->irn)
422 res = element->content_second.irn;
424 it->curr_element_t = element;
429 it->curr_element_t = element;
431 res = get_next_neighbour(it);
438 if (element && element->kind == 3101)
440 it->curr_element_t = element->content_first.element;
441 res = element->content_second.irn;
444 { /* element is only an ir_node */// TODO
445 it->curr_element_t = NULL;
451 if (res && !it->sub_call)
453 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
455 res = get_next_neighbour(it);
459 bitset_set(it->visited_neighbours, get_irn_idx(res));
466 static ir_node *get_first_neighbour(const ifg_pointer_t *ifg, ptr_iter_t *it, const ir_node *irn)
470 ptr_element_t *element;
471 bitset_t *bitsetvisited_neighbours = bitset_malloc(get_irg_last_idx(ifg->env->irg));
478 it->visited_neighbours = bitsetvisited_neighbours;
480 head = phase_get_irn_data(&ifg->ph, irn);
485 it->first_head = head;
486 head = list_entry(it->first_head->list.next, ptr_head_t, list); /* because first element is NULL */
487 it->curr_ptr_head = head;
488 element = head->element;
491 if (element && element->kind == 8889) /* node has no neighbours */
493 res = element->content_first.irn;
494 it->curr_element_t = NULL;
498 if (element && element->kind == 8888) /* node has only one neighbour */
502 if (element->content_first.irn != it->irn)
504 res = element->content_first.irn;
506 it->curr_element_t = NULL;
511 it->curr_element_t = NULL;
513 res = get_next_neighbour(it);
519 if (element->content_second.irn != it->irn)
521 res = element->content_second.irn;
522 it->curr_element_t = element;
528 it->curr_element_t = element;
530 res = get_next_neighbour(it);
536 if (element && element->kind == 3101)
538 it->curr_element_t = element->content_first.element;
539 res = element->content_second.irn;
542 { /* element is only an ir_node */
543 it->curr_element_t = NULL;
548 if (res && !it->sub_call)
550 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
552 res = get_next_neighbour(it);
556 bitset_set(it->visited_neighbours, get_irn_idx(res));
565 /* PUBLIC FUNCTIONS */
567 static void ifg_pointer_free(void *self)
569 ifg_pointer_t *ifg = self;
570 obstack_free(&ifg->obst, NULL);
571 phase_free(&ifg->ph);
576 static int ifg_pointer_connected(const void *self, const ir_node *a, const ir_node *b)
578 const ifg_pointer_t *ifg = self;
583 irn = get_first_neighbour(ifg, &it, a);
592 irn = get_next_neighbour(&it);
598 static ir_node *ifg_pointer_neighbours_begin(const void *self, void *iter, const ir_node *irn)
600 return get_first_neighbour(self, iter, irn);
603 static ir_node *ifg_pointer_neighbours_next(const void *self, void *iter)
606 return get_next_neighbour(iter);
609 static void ifg_pointer_neighbours_break(const void *self, void *iter)
611 ptr_iter_t *it = iter;
614 bitset_free(it->visited_neighbours);
617 static ir_node *ifg_pointer_nodes_begin(const void *self, void *iter)
619 return get_first_irn(self, iter);
622 static ir_node *ifg_pointer_nodes_next(const void *self, void *iter)
625 return get_next_irn(iter);
628 static void ifg_pointer_nodes_break(const void *self, void *iter)
635 static int ifg_pointer_degree(const void *self, const ir_node *irn)
640 irn = get_first_neighbour(self, &it, irn);
645 irn = get_next_neighbour(&it);
651 static const be_ifg_impl_t ifg_pointer_impl = {
656 ifg_pointer_connected,
657 ifg_pointer_neighbours_begin,
658 ifg_pointer_neighbours_next,
659 ifg_pointer_neighbours_break,
660 ifg_pointer_nodes_begin,
661 ifg_pointer_nodes_next,
662 ifg_pointer_nodes_break,
669 be_ifg_t *be_ifg_pointer_new(const be_chordal_env_t *env)
671 ifg_pointer_t *ifg = XMALLOC(ifg_pointer_t);
672 ifg->impl = &ifg_pointer_impl;
675 phase_init(&ifg->ph, "ptr_map", env->irg, PHASE_DEFAULT_GROWTH, ptr_irn_data_init, NULL);
676 obstack_init(&ifg->obst);
678 dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
680 obstack_finish(&ifg->obst);
681 return (be_ifg_t *) ifg;