added new licence header
[libfirm] / ir / be / beifg_pointer.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file   beifg_pointer.c
22  * @date   18.11.2005
23  * @author Sebastian Hack
24  *
25  * Copyright (C) 2005 Universitaet Karlsruhe
26  * Released under the GPL
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdlib.h>
34
35 #include "belive_t.h"
36 #include "list.h"
37 #include "irphase_t.h"
38
39 #include "irnode_t.h"
40 #include "irgraph_t.h"
41 #include "irgwalk.h"
42 #include "irbitset.h"
43
44 #include "be_t.h"
45 #include "bera.h"
46 #include "beifg_t.h"
47 #include "bechordal_t.h"
48
49 typedef struct _ptr_element_t ptr_element_t;
50
51 typedef union element_content {
52         ir_node *irn;
53         ptr_element_t *element;
54 } element_content;
55
56 struct _ptr_element_t {
57         int kind; /* kind = 8888 ..> both are ir_nodes, = 3101 ..> first is another element, second an ir_node */
58         element_content content_first; /* could be a ptr_element or ir_node */
59         element_content content_second; /* could be a ptr_element or ir_node */
60 };
61
62 typedef struct _ptr_head_t {
63         struct list_head list;
64         ptr_element_t *element;
65 } ptr_head_t;
66
67 typedef struct _ifg_pointer_t {
68         const be_ifg_impl_t *impl;
69         const be_chordal_env_t *env;
70         ir_phase ph;
71         struct obstack obst;
72         ptr_head_t *curr_ptr_head;
73         ptr_element_t *curr_element;
74         pmap *node_map;
75 } ifg_pointer_t;
76
77 typedef struct _ptr_iter_t {
78         const ifg_pointer_t *ifg;
79         const ir_node *irn;
80         ptr_head_t *curr_ptr_head;
81         ptr_head_t *first_head;
82         ptr_element_t *curr_element_t;
83         ir_node *curr_irn;
84         int get_first;
85         int sub_call;
86         bitset_t *visited_neighbours;
87 } ptr_iter_t;
88
89 /* PRIVATE FUNCTIONS */
90
91 static void *ptr_irn_data_init(ir_phase *ph, ir_node *irn, void *data)
92 {
93         ptr_head_t *head = phase_alloc(ph, sizeof(*head));
94         INIT_LIST_HEAD(&head->list);
95         return head;
96 }
97
98 static ptr_element_t *ptr_get_new_element(ifg_pointer_t *ifg)
99 {
100         ptr_element_t *new_element = obstack_alloc(&ifg->obst, sizeof(ptr_element_t));
101         memset(new_element, 0, sizeof(*new_element));
102         return new_element;
103 }
104
105 static ptr_head_t *ptr_get_new_head(ifg_pointer_t *ifg)
106 {
107         ptr_head_t *new_element = obstack_alloc(&ifg->obst, sizeof(*new_element));
108         INIT_LIST_HEAD(&new_element->list);
109         return new_element;
110 }
111
112 static void write_pointers(bitset_t *live, ifg_pointer_t *ifg)
113 {
114         ir_node *live_irn;
115         bitset_pos_t elm;
116
117         bitset_foreach_irn(ifg->env->irg, live, elm, live_irn)
118         {
119                 ptr_head_t *head = phase_get_or_set_irn_data(&ifg->ph, live_irn);
120                 ptr_head_t *element = ptr_get_new_head(ifg);
121                 ir_node *irn = NULL;
122
123 #if 0
124                 // Matze: huh, what is this?!? node numbers aren't in any way deterministic AFAIK
125                 if (live_irn->node_nr == 1883 || live_irn->node_nr == 1858)
126                         irn = NULL;
127 #endif
128
129                 element->element = ifg->curr_element; /* write current highest sub-clique for each node */
130                 list_add(&element->list, &head->list);
131
132                 /* the following code is to find all nodes, it should be replaced by a "keywalker" of irphase */
133                 irn = pmap_get(ifg->node_map, live_irn);
134                 if (!irn)
135                         pmap_insert(ifg->node_map, live_irn, live_irn);
136         }
137 }
138
139 static ptr_element_t *get_last_sub_clique(ifg_pointer_t *ifg, bitset_t *live, bitset_t *my_live, ir_node *irn)
140 {
141         ptr_element_t *element = ifg->curr_element;
142         ptr_element_t *res = NULL;
143
144         /* search the last sub-clique before the sub-clique that contains the node irn */
145         if (element && element->kind == 8888
146                 && (element->content_first.irn == irn
147                 || element->content_second.irn == irn)) /* contains the node we search and there is no other sub-clique before */
148         {
149                 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 */
150                 {
151                         bitset_set(my_live, get_irn_idx(element->content_first.irn));
152                 }
153
154                 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 */
155                 {
156                         bitset_set(my_live, get_irn_idx(element->content_second.irn));
157                 }
158                 res = NULL;
159         }
160         else
161         {
162                 if (element && element->kind == 8889)
163                 { /* this was a "single-node-clique" */
164                         res = NULL;
165                 }
166
167                 if (element && (element->kind == 3101)
168                         && (element->content_second.irn == irn)) /* sub-clique contains node, return previous sub-clique */
169                 {
170                         res = element->content_first.element;
171                 }
172                 else
173                 {
174                         if (element && element->kind == 3101) /* look at previous sub-cliques if the contain the node you are searching for*/
175                         {
176                                 if (bitset_is_set(live, get_irn_idx(element->content_second.irn))) /* irn is still alive */
177                                 {
178                                         bitset_set(my_live, get_irn_idx(element->content_second.irn));
179                                 }
180                                 ifg->curr_element = element->content_first.element;
181                                 res = get_last_sub_clique(ifg, live, my_live, irn);
182                         }
183                         else
184                         {
185                                 res = NULL;
186                         }
187                 }
188         }
189         return res;
190 }
191
192 static void find_neighbour_walker(ir_node *bl, void *data)
193 {
194         ifg_pointer_t *ifg      = data;
195         struct list_head *head  = get_block_border_head(ifg->env, bl);
196         border_t *b;
197         bitset_t *live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
198         bitset_t *my_live;
199         bitset_pos_t my_elm;
200         ir_node *my_irn;
201         element_content last_irn;
202         element_content last_element;
203         int was_def = 0;
204         ir_node *first = NULL;
205         int was_first = 0;
206
207         last_irn.irn = NULL;
208         last_element.element = NULL;
209
210         assert(is_Block(bl) && "There is no block to work on.");
211
212         foreach_border_head(head, b) /* follow the borders of the block */
213         {
214                 ir_node *irn = b->irn;
215                 ptr_element_t *element = NULL;
216
217 #if 0
218                 // ?!?
219                 if (irn->node_nr == 1883 || irn->node_nr == 1858)
220                         i=1;
221 #endif
222
223                 if (b->is_def) /* b is a new node */
224                 {
225                         bitset_set(live, get_irn_idx(irn));
226                         if (last_element.element)
227                         {
228                                 element = ptr_get_new_element(ifg);
229                                 element->content_first.element = last_element.element;
230                                 element->content_second.irn = b->irn;
231                                 element->kind = 3101; /* first is an element, second an ir_node */
232
233                                 last_element.element = element;
234                                 ifg->curr_element = element;
235                         }
236                         else
237                         {
238                                 if (last_irn.irn)       /* create new sub-clique */
239                                 {
240                                         element = ptr_get_new_element(ifg);
241                                         element->content_first.irn = last_irn.irn;
242                                         element->content_second.irn = b->irn;
243                                         element->kind = 8888; /* both are ir_nodes */
244
245 #if 0
246                                         // ?!?
247                                         if (irn->node_nr == 1883 || irn->node_nr == 1858 || irn->node_nr == 1936)
248                                                 i=1;
249 #endif
250
251
252                                         last_element.element = element;
253                                         ifg->curr_element = element;
254                                         last_irn.irn = NULL;
255                                 }
256                                 else
257                                 {
258                                         last_irn.irn = b->irn;  /* needed to create first sub-clique */
259                                         last_element.element = NULL;
260                                 }
261                         }
262
263                         was_def = 1;
264                 }
265                 else
266                 {
267                         if (was_def == 1) /* if there is a USE after a DEF... */
268                         {
269                                 if (!last_element.element)
270                                 { /* there was only one element in the clique */
271                                         element = ptr_get_new_element(ifg);
272                                         element->kind = 8889; /* first is a node, second is NULL, because this is a "single-node-clique" */
273                                         element->content_first.irn = last_irn.irn;
274                                         last_irn.irn = NULL;
275                                         element = NULL;
276                                         ifg->curr_element = NULL;
277                                 }
278
279
280                                 write_pointers(live, ifg); /* ...write a pointer to the highest sub-clique for each living node. */
281                                 was_def = 0;
282                         }
283
284                         my_live = bitset_malloc(get_irg_last_idx(ifg->env->irg));
285                         last_element.element = get_last_sub_clique(ifg, live, my_live, irn);
286
287                         /* check and add still living nodes */
288                         //bitset_remv_irn(my_live, irn);
289                         if (bitset_popcnt(my_live) > 1)
290                         {
291                                 if (last_element.element)
292                                 {
293                                         bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
294                                         {
295                                                 ptr_element_t *my_element = ptr_get_new_element(ifg);
296                                                 my_element->content_first.element = last_element.element;
297                                                 my_element->content_second.irn = my_irn;
298                                                 my_element->kind = 3101; /* first is an element, second an ir_node */
299
300                                                 last_element.element = my_element;
301                                                 ifg->curr_element = my_element;
302                                         }
303                                 }
304                                 else
305                                 {
306                                         bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
307                                         {
308                                                 ptr_element_t *my_element = NULL;
309                                                 if (!first && !was_first)
310                                                 {
311                                                         first = my_irn;
312                                                         was_first = 1;
313                                                 }
314                                                 else
315                                                 {
316                                                         if (first && was_first)
317                                                         {
318                                                                 my_element = ptr_get_new_element(ifg);
319                                                                 my_element->content_first.irn = first;
320                                                                 my_element->content_second.irn = my_irn;
321                                                                 my_element->kind = 8888; /* both are ir_nodes */
322                                                                 last_element.element = my_element;
323                                                                 ifg->curr_element = my_element;
324
325 #if 0
326                                                                 // ?!?
327                                                                 if (my_irn->node_nr == 1883 || my_irn->node_nr == 1858 || my_irn->node_nr == 1936)
328                                                                         i=1;
329 #endif
330
331
332                                                                 first = NULL;
333                                                         }
334                                                         else
335                                                         {
336                                                                 my_element = ptr_get_new_element(ifg);
337                                                                 my_element->content_first.element = last_element.element;
338                                                                 my_element->content_second.irn = my_irn;
339                                                                 my_element->kind = 3101; /* first is an element, second an ir_node */
340                                                                 last_element.element = my_element;
341                                                                 ifg->curr_element = my_element;
342                                                         }
343                                                 }
344                                         }
345                                         was_first = 0;
346                                 }
347                         }
348                         else
349                         {
350                                 if (bitset_popcnt(my_live) == 1) /* there is only one node left */
351                                 {
352                                         if (last_element.element)
353                                         {
354                                                 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn)
355                                                 {
356                                                         ptr_element_t *my_element = ptr_get_new_element(ifg);
357                                                         my_element->content_first.element = last_element.element;
358                                                         my_element->content_second.irn = my_irn;
359                                                         my_element->kind = 3101; /* first is an element, second an ir_node */
360
361                                                         last_element.element = my_element;
362                                                         ifg->curr_element = my_element;
363                                                 }
364                                         }
365                                         else
366                                         {
367                                                 bitset_foreach_irn(ifg->env->irg, my_live, my_elm, my_irn);
368                                                 {
369                                                         ptr_element_t *my_element = ptr_get_new_element(ifg);
370                                                         my_element->content_first.irn = my_irn;
371                                                         my_element->content_second.irn = NULL;
372                                                         my_element->kind = 8889;
373                                                         last_element.element =  my_element;
374                                                         ifg->curr_element = my_element;
375                                                 }
376                                         }
377                                 }
378                         }
379                         bitset_free(my_live);
380                         bitset_remv_irn(live, irn);
381                 }
382         }
383         bitset_free(live);
384 }
385
386 static ir_node *get_first_irn(const ifg_pointer_t *ifg, ptr_iter_t *it)
387 {
388         /* this should be replaced using a keywalker of the irphase &ifg.ph */
389         ir_node *irn;
390         pmap_entry *entry;
391
392         it->ifg = ifg;
393         entry = pmap_first(ifg->node_map);
394
395         if (!entry)
396                 return NULL;
397
398         irn =  entry->value;
399         it->curr_irn = irn;
400
401         return irn;
402 }
403
404 static ir_node *get_next_irn(ptr_iter_t *it)
405 {
406         /* this should be replaced using a keywalker of the irphase &ifg.ph */
407         ir_node *irn;
408         pmap_entry *entry;
409
410         irn = it->curr_irn;
411         entry = pmap_next(it->ifg->node_map);
412
413         if (!entry)
414                 return NULL;
415
416         it->curr_irn = entry->value;
417
418         return entry->value;
419 }
420
421 static ir_node *get_next_neighbour(ptr_iter_t *it)
422 {
423         ir_node *res;
424         ptr_head_t *head;
425         ptr_element_t *element;
426
427         element = it->curr_element_t;
428
429         if (element == NULL)
430         {
431 #if 0
432                 // ?!?
433                 if (it->irn->node_nr == 1883 || it->irn->node_nr == 1858)
434                         i=1;
435 #endif
436
437                 if (it->curr_ptr_head->list.next != &it->first_head->list)
438                 {
439                         head = list_entry(it->curr_ptr_head->list.next, ptr_head_t, list);
440                         it->curr_ptr_head = head;
441                         element = head->element;
442                 }
443                 else
444                         return NULL; /* there are no more neighbours */
445         }
446
447         if (element && element->kind == 8889) /* node has no neighbours */
448         {
449                 res = element->content_first.irn;
450                 it->curr_element_t = NULL;
451         }
452         else
453         {
454                 if (element && element->kind == 8888) /* node has only one more neighbour */
455                 {
456                         if (it->get_first)
457                         {
458                                 if (element->content_first.irn != it->irn)
459                                 {
460                                         res = element->content_first.irn;
461                                         it->get_first = 0;
462                                         it->curr_element_t = NULL;
463                                 }
464                                 else
465                                 {
466                                         it->get_first = 0;
467                                         it->curr_element_t = NULL;
468                                         it->sub_call++;
469                                         res = get_next_neighbour(it);
470                                         it->sub_call--;
471                                 }
472                         }
473                         else
474                         {
475                                 if (element->content_second.irn != it->irn)
476                                 {
477                                         res = element->content_second.irn;
478                                         it->get_first = 1;
479                                         it->curr_element_t = element;
480                                 }
481                                 else
482                                 {
483                                         it->get_first = 1;
484                                         it->curr_element_t = element;
485                                         it->sub_call++;
486                                         res = get_next_neighbour(it);
487                                         it->sub_call--;
488                                 }
489                         }
490                 }
491                 else
492                 {
493                         if (element && element->kind == 3101)
494                         {
495                                 it->curr_element_t = element->content_first.element;
496                                 res = element->content_second.irn;
497                         }
498                         else
499                         { /* element is only an ir_node */// TODO
500                                 it->curr_element_t = NULL;
501                                 return NULL;
502                         }
503                 }
504         }
505
506         if (res && !it->sub_call)
507         {
508                 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
509                 {
510                         res = get_next_neighbour(it);
511                 }
512                 else
513                 {
514                         bitset_set(it->visited_neighbours, get_irn_idx(res));
515                 }
516         }
517
518         return res;
519 }
520
521 static ir_node *get_first_neighbour(const ifg_pointer_t *ifg, ptr_iter_t *it, const ir_node *irn)
522 {
523         ir_node *res;
524         ptr_head_t *head;
525         ptr_element_t *element;
526         bitset_t *bitsetvisited_neighbours = bitset_malloc(get_irg_last_idx(ifg->env->irg));
527
528         it->ifg = ifg;
529         it->irn = irn;
530         it->get_first = 0;
531         it->sub_call = 0;
532
533         it->visited_neighbours = bitsetvisited_neighbours;
534
535         head = phase_get_irn_data(&ifg->ph, irn);
536         if (!head)
537                 return NULL;
538         else
539         {
540                 it->first_head = head;
541                 head = list_entry(it->first_head->list.next, ptr_head_t, list); /* because first element is NULL */
542                 it->curr_ptr_head = head;
543                 element = head->element;
544         }
545
546         if (element && element->kind == 8889) /* node has no neighbours */
547         {
548                 res = element->content_first.irn;
549                 it->curr_element_t = NULL;
550         }
551         else
552         {
553                 if (element && element->kind == 8888) /* node has only one neighbour */
554                 {
555                         if (it->get_first)
556                         {
557                                 if (element->content_first.irn != it->irn)
558                                 {
559                                         res = element->content_first.irn;
560                                         it->get_first = 0;
561                                         it->curr_element_t = NULL;
562                                 }
563                                 else
564                                 {
565                                         it->get_first = 0;
566                                         it->curr_element_t = NULL;
567                                         it->sub_call++;
568                                         res = get_next_neighbour(it);
569                                         it->sub_call--;
570                                 }
571                         }
572                         else
573                         {
574                                 if (element->content_second.irn != it->irn)
575                                 {
576                                         res = element->content_second.irn;
577                                         it->curr_element_t = element;
578                                         it->get_first = 1;
579                                 }
580                                 else
581                                 {
582                                         it->get_first = 1;
583                                         it->curr_element_t = element;
584                                         it->sub_call++;
585                                         res = get_next_neighbour(it);
586                                         it->sub_call--;
587                                 }
588                         }
589                 }
590                 else
591                         if (element && element->kind == 3101)
592                         {
593                                 it->curr_element_t = element->content_first.element;
594                                 res = element->content_second.irn;
595                         }
596                         else
597                         { /* element is only an ir_node */
598                                 it->curr_element_t = NULL;
599                                 return NULL;
600                         }
601         }
602
603         if (res && !it->sub_call)
604         {
605                 if (bitset_contains_irn(it->visited_neighbours, res) || res == it->irn)
606                 {
607                         res = get_next_neighbour(it);
608                 }
609                 else
610                 {
611                         bitset_set(it->visited_neighbours, get_irn_idx(res));
612                 }
613         }
614
615         return res;
616 }
617
618
619
620 /* PUBLIC FUNCTIONS */
621
622 static void ifg_pointer_free(void *self)
623 {
624         ifg_pointer_t *ifg = self;
625         obstack_free(&ifg->obst, NULL);
626         phase_free(&ifg->ph);
627
628         free(self);
629 }
630
631 static int ifg_pointer_connected(const void *self, const ir_node *a, const ir_node *b)
632 {
633         const ifg_pointer_t *ifg = self;
634         int connected = -1;
635         ptr_iter_t it;
636         ir_node *irn = NULL;
637
638         irn = get_first_neighbour(ifg, &it, a);
639         connected = 0;
640         while (irn != NULL)
641         {
642                 if (irn == b)
643                 {
644                         connected = 1;
645                         break;
646                 }
647                 irn = get_next_neighbour(&it);
648         }
649
650         return connected;
651 }
652
653 static ir_node *ifg_pointer_neighbours_begin(const void *self, void *iter, const ir_node *irn)
654 {
655         return get_first_neighbour(self, iter, irn);
656 }
657
658 static ir_node *ifg_pointer_neighbours_next(const void *self, void *iter)
659 {
660         return get_next_neighbour(iter);
661 }
662
663 static void ifg_pointer_neighbours_break(const void *self, void *iter)
664 {
665         ptr_iter_t *it = iter;
666
667         bitset_free(it->visited_neighbours);
668
669         return;
670 }
671
672 static ir_node *ifg_pointer_nodes_begin(const void *self, void *iter)
673 {
674         return get_first_irn(self, iter);
675 }
676
677 static ir_node *ifg_pointer_nodes_next(const void *self, void *iter)
678 {
679         return get_next_irn(iter);
680 }
681
682 static void ifg_pointer_nodes_break(const void *self, void *iter)
683 {
684         return;
685 }
686
687 static int ifg_pointer_degree(const void *self, const ir_node *irn)
688 {
689         int degree = -1;
690         ptr_iter_t it;
691
692         irn = get_first_neighbour(self, &it, irn);
693         degree = 0;
694         while (irn != NULL)
695         {
696                 degree++;
697                 irn = get_next_neighbour(&it);
698         }
699
700         return degree;
701 }
702
703 static const be_ifg_impl_t ifg_pointer_impl = {
704         sizeof(ptr_iter_t),
705         sizeof(ptr_iter_t),
706         0,
707         ifg_pointer_free,
708         ifg_pointer_connected,
709         ifg_pointer_neighbours_begin,
710         ifg_pointer_neighbours_next,
711         ifg_pointer_neighbours_break,
712         ifg_pointer_nodes_begin,
713         ifg_pointer_nodes_next,
714         ifg_pointer_nodes_break,
715         NULL,
716         NULL,
717         NULL,
718         ifg_pointer_degree
719 };
720
721 be_ifg_t *be_ifg_pointer_new(const be_chordal_env_t *env)
722 {
723         ifg_pointer_t *ifg      = xmalloc(sizeof(*ifg));
724         ifg->impl               = &ifg_pointer_impl;
725         ifg->env                        = env;
726
727         ifg->node_map           = pmap_create(); /* to find all nodes, should be replaced by a "keywalker" of irphase */
728
729         phase_init(&ifg->ph, "ptr_map", env->irg, PHASE_DEFAULT_GROWTH, ptr_irn_data_init, NULL);
730         obstack_init(&ifg->obst);
731
732         dom_tree_walk_irg(env->irg, find_neighbour_walker, NULL, ifg);
733
734         obstack_finish(&ifg->obst);
735         return (be_ifg_t *) ifg;
736 }