adapted to new callback
[libfirm] / ir / be / beifg.c
1 /**
2  * @file   beifg.c
3  * @date   18.11.2005
4  * @author Sebastian Hack
5  *
6  * Copyright (C) 2005 Universitaet Karlsruhe
7  * Released under the GPL
8  */
9
10 #include <stdlib.h>
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #ifdef HAVE_MALLOC_H
17 #include <malloc.h>
18 #endif
19
20 #ifdef __linux__
21 #include <malloc.h>
22 #endif /* __linux__ */
23
24 #ifdef HAVE_ALLOCA_H
25 #include <alloca.h>
26 #endif
27
28 #ifdef WITH_LIBCORE
29 #include <libcore/lc_opts.h>
30 #include <libcore/lc_opts_enum.h>
31 #include <libcore/lc_timing.h>
32 #endif /* WITH_LIBCORE */
33
34 #include "bitset.h"
35
36 #include "irgwalk.h"
37 #include "irnode_t.h"
38 #include "irprintf.h"
39 #include "irtools.h"
40 #include "beifg_t.h"
41 #include "beifg_impl.h"
42 #include "irphase.h"
43 #include "irphase_t.h"
44 #include "bechordal.h"
45
46 #include "becopystat.h"
47 #include "becopyopt.h"
48
49 /** Defines values for the ifg performance test */
50 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
51 #define BE_CH_PERFORMANCETEST_COUNT (500)
52
53 typedef struct _coloring_t coloring_t;
54
55 struct _coloring_t {
56         phase_t ph;
57         const arch_env_t *arch_env;
58         ir_graph *irg;
59 };
60
61 size_t (be_ifg_nodes_iter_size)(const void *self)
62 {
63         const be_ifg_t *ifg = self;
64         return ifg->impl->nodes_iter_size;
65 }
66
67 size_t (be_ifg_neighbours_iter_size)(const void *self)
68 {
69         const be_ifg_t *ifg = self;
70         return ifg->impl->neighbours_iter_size;
71 }
72
73 size_t (be_ifg_cliques_iter_size)(const void *self)
74 {
75         const be_ifg_t *ifg = self;
76         return ifg->impl->cliques_iter_size;
77 }
78
79 static void *regs_irn_data_init(phase_t *ph, ir_node *irn, void *data)
80 {
81         coloring_t *coloring = (coloring_t *) ph;
82         return (void *) arch_get_irn_register(coloring->arch_env, irn);
83 }
84
85 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
86 {
87         phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init);
88         c->arch_env = aenv;
89         c->irg = irg;
90         return c;
91 }
92
93 static void get_irn_color(ir_node *irn, void *c)
94 {
95         coloring_t *coloring = c;
96         phase_get_or_set_irn_data(&coloring->ph, irn);
97 }
98
99 static void restore_irn_color(ir_node *irn, void *c)
100 {
101         coloring_t *coloring = c;
102         const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
103         if(reg)
104                 arch_set_irn_register(coloring->arch_env, irn, reg);
105 }
106
107 void coloring_save(coloring_t *c)
108 {
109         irg_walk_graph(c->irg, NULL, get_irn_color, c);
110 }
111
112 void coloring_restore(coloring_t *c)
113 {
114         irg_walk_graph(c->irg, NULL, restore_irn_color, c);
115 }
116
117 void (be_ifg_free)(void *self)
118 {
119         be_ifg_t *ifg = self;
120         ifg->impl->free(self);
121 }
122
123 int (be_ifg_connected)(const void *self, const ir_node *a, const ir_node *b)
124 {
125         const be_ifg_t *ifg = self;
126         return ifg->impl->connected(self, a, b);
127 }
128
129 ir_node *(be_ifg_neighbours_begin)(const void *self, void *iter, const ir_node *irn)
130 {
131         const be_ifg_t *ifg = self;
132         return ifg->impl->neighbours_begin(self, iter, irn);
133 }
134
135 ir_node *(be_ifg_neighbours_next)(const void *self, void *iter)
136 {
137         const be_ifg_t *ifg = self;
138         return ifg->impl->neighbours_next(self, iter);
139 }
140
141 void (be_ifg_neighbours_break)(const void *self, void *iter)
142 {
143         const be_ifg_t *ifg = self;
144         ifg->impl->neighbours_break(self, iter);
145 }
146
147 ir_node *(be_ifg_nodes_begin)(const void *self, void *iter)
148 {
149         const be_ifg_t *ifg = self;
150         return ifg->impl->nodes_begin(self, iter);
151 }
152
153 ir_node *(be_ifg_nodes_next)(const void *self, void *iter)
154 {
155         const be_ifg_t *ifg = self;
156         return ifg->impl->nodes_next(self, iter);
157 }
158
159 void (be_ifg_nodes_break)(const void *self, void *iter)
160 {
161         const be_ifg_t *ifg = self;
162         ifg->impl->nodes_break(self, iter);
163 }
164
165 int (be_ifg_cliques_begin)(const void *self, void *iter, ir_node **buf)
166 {
167         const be_ifg_t *ifg = self;
168         return ifg->impl->cliques_begin(self, iter, buf);
169 }
170
171 int (be_ifg_cliques_next)(const void *self, void *iter)
172 {
173         const be_ifg_t *ifg = self;
174         return ifg->impl->cliques_next(self, iter);
175 }
176
177 void (be_ifg_cliques_break)(const void *self, void *iter)
178 {
179         const be_ifg_t *ifg = self;
180         ifg->impl->cliques_break(self, iter);
181 }
182
183 int (be_ifg_degree)(const void *self, const ir_node *irn)
184 {
185         const be_ifg_t *ifg = self;
186         return ifg->impl->degree(self, irn);
187 }
188
189
190 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
191 {
192         int degree = be_ifg_degree(ifg, irn);
193         void *iter = be_ifg_neighbours_iter_alloca(ifg);
194
195         ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
196
197         ir_node *curr;
198         int i, j;
199
200         i = 0;
201         be_ifg_foreach_neighbour(ifg, iter, irn, curr)
202                 neighbours[i++] = curr;
203
204         for(i = 0; i < degree; ++i) {
205                 for(j = 0; j < i; ++j)
206                         if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
207                                 free(neighbours);
208                                 return 0;
209                         }
210         }
211
212
213         free(neighbours);
214         return 1;
215 }
216
217 void be_ifg_check(const be_ifg_t *ifg)
218 {
219         void *iter1 = be_ifg_nodes_iter_alloca(ifg);
220         void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
221
222         ir_node *n, *m;
223         int node_count = 0;
224         int neighbours_count = 0;
225         int degree = 0;
226
227         /* count all nodes */
228         ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
229         be_ifg_foreach_node(ifg,iter1,n)
230         {
231                 node_count++;
232                 degree = be_ifg_degree(ifg, n);
233                 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
234         }
235
236         ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
237
238         /* Check, if all neighbours are indeed connected to the node. */
239         be_ifg_foreach_node(ifg, iter1, n)
240         {
241                 ir_printf("\n%+F; ", n);
242                 be_ifg_foreach_neighbour(ifg, iter2, n, m)
243                 {
244                         ir_printf("%+F; ", m);
245                         neighbours_count++;
246                         if(!be_ifg_connected(ifg, n, m))
247                                 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
248                 }
249         }
250         ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
251 }
252
253 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
254 {
255         void *iter = be_ifg_nodes_iter_alloca(ifg);
256         int node_count = 0;
257         ir_node *n;
258
259         be_ifg_foreach_node(ifg, iter, n)
260         {
261                 node_count++;
262         }
263
264         return node_count;
265 }
266
267 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
268 {
269         const ir_node *node_a = *(ir_node **)a;
270         const ir_node *node_b = *(ir_node **)b;
271
272         int nr_a = node_a->node_nr;
273         int nr_b = node_b->node_nr;
274
275         return QSORT_CMP(nr_a, nr_b);
276 }
277
278 void be_ifg_check_sorted(const be_ifg_t *ifg)
279 {
280         void *iter1 = be_ifg_nodes_iter_alloca(ifg);
281         void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
282
283         ir_node *n, *m;
284         const int node_count = be_ifg_check_get_node_count(ifg);
285         int i = 0;
286
287         ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
288
289         be_ifg_foreach_node(ifg, iter1, n)
290         {
291                 if(!node_is_in_irgs_storage(ifg->env->irg, n))
292                 {
293                         ir_printf("+%F is in ifg but not in the current irg!", n);
294                         assert (node_is_in_irgs_storage(ifg->env->irg, n));
295                 }
296
297                 all_nodes[i] = n;
298                 i++;
299         }
300
301         qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
302
303         for (i = 0; i < node_count; i++)
304         {
305                 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
306                 int j = 0;
307                 int k = 0;
308                 int degree = 0;
309
310                 degree = be_ifg_degree(ifg, all_nodes[i]);
311
312                 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
313                 {
314                         neighbours[j] = m;
315                         j++;
316                 }
317
318                 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
319
320                 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
321
322                 for(k = 0; k < j; k++)
323                 {
324                         ir_printf("%+F, ", neighbours[k]);
325                 }
326
327                 ir_printf("\n");
328
329                 free(neighbours);
330         }
331
332         free(all_nodes);
333
334 }
335
336 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
337 {
338         void *iter1 = be_ifg_nodes_iter_alloca(ifg);
339         void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
340
341         ir_node *n, *m;
342         const int node_count = be_ifg_check_get_node_count(ifg);
343         int i = 0;
344
345         ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
346
347         be_ifg_foreach_node(ifg, iter1, n)
348         {
349                 if(!node_is_in_irgs_storage(ifg->env->irg, n))
350                 {
351                         ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
352                         assert (node_is_in_irgs_storage(ifg->env->irg, n));
353                 }
354
355                 all_nodes[i] = n;
356                 i++;
357         }
358
359         qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
360
361         for (i = 0; i < node_count; i++)
362         {
363                 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
364                 int j = 0;
365                 int k = 0;
366                 int degree = 0;
367
368                 degree = be_ifg_degree(ifg, all_nodes[i]);
369
370                 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
371                 {
372                         neighbours[j] = m;
373                         j++;
374                 }
375
376                 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
377
378                 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
379
380                 for(k = 0; k < j; k++)
381                 {
382                         ir_fprintf (f,"%+F, ", neighbours[k]);
383                 }
384
385                 ir_fprintf (f,"\n");
386
387                 free(neighbours);
388         }
389
390         free(all_nodes);
391
392 }
393
394 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
395 {
396 #ifdef WITH_LIBCORE
397         int tests = BE_CH_PERFORMANCETEST_COUNT;
398         coloring_t coloring;
399
400         int used_memory;
401
402         int i = 0;
403         int rt;
404         copy_opt_t *co;
405         be_ifg_t *old_if = chordal_env->ifg;
406
407         lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
408         unsigned long elapsed_usec = 0;
409
410         if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
411         {
412                 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
413                 coloring_save(&coloring);
414
415                 lc_timer_reset(timer);
416
417                 for (i = 0; i<tests; i++) /* performance test with std */
418                 {
419
420                         used_memory = lc_get_heap_used_bytes();
421
422                         rt = lc_timer_enter_high_priority();
423                         lc_timer_start(timer);
424
425                         chordal_env->ifg = be_ifg_std_new(chordal_env);
426
427                         lc_timer_stop(timer);
428                         rt = lc_timer_leave_high_priority();
429
430                         used_memory = lc_get_heap_used_bytes() - used_memory;
431
432                         coloring_restore(&coloring);
433
434                         co = NULL;
435                         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
436                         co_build_ou_structure(co);
437                         co_build_graph_structure(co);
438
439                         rt = lc_timer_enter_high_priority();
440                         lc_timer_start(timer);
441
442                         co_solve_heuristic_new(co);
443
444                         lc_timer_stop(timer);
445                         rt = lc_timer_leave_high_priority();
446
447                         co_free_graph_structure(co);
448                         co_free_ou_structure(co);
449                         free_copy_opt(co);
450                         be_ifg_free(chordal_env->ifg);
451
452                 }
453
454                 elapsed_usec = lc_timer_elapsed_usec(timer);
455                 /* calculating average */
456                 elapsed_usec = elapsed_usec / tests;
457
458                 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
459
460                 used_memory=0;
461                 elapsed_usec=0;
462
463                 for (i = 0; i<tests; i++)  /* performance test with clique */
464                 {
465                         used_memory = lc_get_heap_used_bytes();
466
467                         rt = lc_timer_enter_high_priority();
468                         lc_timer_start(timer);
469
470                         chordal_env->ifg = be_ifg_clique_new(chordal_env);
471
472                         lc_timer_stop(timer);
473                         rt = lc_timer_leave_high_priority();
474
475                         used_memory = lc_get_heap_used_bytes() - used_memory;
476
477                         coloring_restore(&coloring);
478
479                         co = NULL;
480                         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
481                         co_build_ou_structure(co);
482                         co_build_graph_structure(co);
483
484                         rt = lc_timer_enter_high_priority();
485                         lc_timer_start(timer);
486
487                         co_solve_heuristic_new(co);
488
489                         lc_timer_stop(timer);
490                         rt = lc_timer_leave_high_priority();
491
492                         co_free_graph_structure(co);
493                         co_free_ou_structure(co);
494                         free_copy_opt(co);
495                         be_ifg_free(chordal_env->ifg);
496
497                 }
498
499                 elapsed_usec = lc_timer_elapsed_usec(timer);
500                 /* calculating average */
501                 elapsed_usec = elapsed_usec / tests;
502
503                 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
504
505                 used_memory=0;
506                 elapsed_usec=0;
507
508                 for (i = 0; i<tests; i++)  /* performance test with list */
509                 {
510                         used_memory = lc_get_heap_used_bytes();
511
512                         rt = lc_timer_enter_high_priority();
513                         lc_timer_start(timer);
514
515                         chordal_env->ifg = be_ifg_list_new(chordal_env);
516
517                         lc_timer_stop(timer);
518                         rt = lc_timer_leave_high_priority();
519
520                         used_memory = lc_get_heap_used_bytes() - used_memory;
521
522                         coloring_restore(&coloring);
523
524                         co = NULL;
525                         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
526                         co_build_ou_structure(co);
527                         co_build_graph_structure(co);
528
529                         rt = lc_timer_enter_high_priority();
530                         lc_timer_start(timer);
531
532                         co_solve_heuristic_new(co);
533
534                         lc_timer_stop(timer);
535                         rt = lc_timer_leave_high_priority();
536
537                         co_free_graph_structure(co);
538                         co_free_ou_structure(co);
539                         free_copy_opt(co);
540                         be_ifg_free(chordal_env->ifg);
541
542                 }
543
544                 elapsed_usec = lc_timer_elapsed_usec(timer);
545                 /* calculating average */
546                 elapsed_usec = elapsed_usec / tests;
547
548                 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
549
550                 used_memory=0;
551                 elapsed_usec=0;
552
553                 for (i = 0; i<tests; i++)  /* performance test with pointer */
554                 {
555                         used_memory = lc_get_heap_used_bytes();
556
557                         rt = lc_timer_enter_high_priority();
558                         lc_timer_start(timer);
559
560                         chordal_env->ifg = be_ifg_pointer_new(chordal_env);
561
562                         lc_timer_stop(timer);
563                         rt = lc_timer_leave_high_priority();
564
565                         used_memory = lc_get_heap_used_bytes() - used_memory;
566
567                         coloring_restore(&coloring);
568
569                         co = NULL;
570                         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
571                         co_build_ou_structure(co);
572                         co_build_graph_structure(co);
573
574                         rt = lc_timer_enter_high_priority();
575                         lc_timer_start(timer);
576
577                         co_solve_heuristic_new(co);
578
579                         lc_timer_stop(timer);
580                         rt = lc_timer_leave_high_priority();
581
582                         co_free_graph_structure(co);
583                         co_free_ou_structure(co);
584                         free_copy_opt(co);
585                         be_ifg_free(chordal_env->ifg);
586
587                 }
588
589                 elapsed_usec = lc_timer_elapsed_usec(timer);
590                 /* calculating average */
591                 elapsed_usec = elapsed_usec / tests;
592
593                 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
594
595                 i=0;
596                 used_memory=0;
597                 elapsed_usec=0;
598         }
599
600         chordal_env->ifg = old_if;
601 #endif /* WITH_LIBCORE */
602 }
603
604 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
605 {
606         void *nodes_it  = be_ifg_nodes_iter_alloca(ifg);
607         void *neigh_it  = be_ifg_neighbours_iter_alloca(ifg);
608         bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
609
610         ir_node *n, *m;
611
612         fprintf(file, "graph G {\n\tgraph [");
613         if(cb->graph_attr)
614                 cb->graph_attr(file, self);
615         fprintf(file, "];\n");
616
617         if(cb->at_begin)
618                 cb->at_begin(file, self);
619
620         be_ifg_foreach_node(ifg, nodes_it, n) {
621                 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
622                         int idx = get_irn_idx(n);
623                         bitset_set(nodes, idx);
624                         fprintf(file, "\tnode [");
625                         if(cb->node_attr)
626                                 cb->node_attr(file, self, n);
627                         fprintf(file, "]; n%d;\n", idx);
628                 }
629         }
630
631         /* Check, if all neighbours are indeed connected to the node. */
632         be_ifg_foreach_node(ifg, nodes_it, n) {
633                 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
634                         int n_idx = get_irn_idx(n);
635                         int m_idx = get_irn_idx(m);
636
637                         if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
638                                 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
639                                 if(cb->edge_attr)
640                                         cb->edge_attr(file, self, n, m);
641                                 fprintf(file, "];\n");
642                         }
643                 }
644         }
645
646         if(cb->at_end)
647                 cb->at_end(file, self);
648
649         fprintf(file, "}\n");
650         bitset_free(nodes);
651 }