4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
22 #endif /* __linux__ */
29 #include <libcore/lc_opts.h>
30 #include <libcore/lc_opts_enum.h>
31 #include <libcore/lc_timing.h>
32 #endif /* WITH_LIBCORE */
42 #include "beifg_impl.h"
44 #include "irphase_t.h"
45 #include "bechordal.h"
47 #include "becopystat.h"
48 #include "becopyopt.h"
50 /** Defines values for the ifg performance test */
51 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
52 #define BE_CH_PERFORMANCETEST_COUNT (500)
54 typedef struct _coloring_t coloring_t;
58 const arch_env_t *arch_env;
62 size_t (be_ifg_nodes_iter_size)(const void *self)
64 const be_ifg_t *ifg = self;
65 return ifg->impl->nodes_iter_size;
68 size_t (be_ifg_neighbours_iter_size)(const void *self)
70 const be_ifg_t *ifg = self;
71 return ifg->impl->neighbours_iter_size;
74 size_t (be_ifg_cliques_iter_size)(const void *self)
76 const be_ifg_t *ifg = self;
77 return ifg->impl->cliques_iter_size;
80 static void *regs_irn_data_init(phase_t *ph, ir_node *irn, void *data)
82 coloring_t *coloring = (coloring_t *) ph;
83 return (void *) arch_get_irn_register(coloring->arch_env, irn);
86 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
88 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init);
94 static void get_irn_color(ir_node *irn, void *c)
96 coloring_t *coloring = c;
97 phase_get_or_set_irn_data(&coloring->ph, irn);
100 static void restore_irn_color(ir_node *irn, void *c)
102 coloring_t *coloring = c;
103 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
105 arch_set_irn_register(coloring->arch_env, irn, reg);
108 void coloring_save(coloring_t *c)
110 irg_walk_graph(c->irg, NULL, get_irn_color, c);
113 void coloring_restore(coloring_t *c)
115 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
118 void (be_ifg_free)(void *self)
120 be_ifg_t *ifg = self;
121 ifg->impl->free(self);
124 int (be_ifg_connected)(const void *self, const ir_node *a, const ir_node *b)
126 const be_ifg_t *ifg = self;
127 return ifg->impl->connected(self, a, b);
130 ir_node *(be_ifg_neighbours_begin)(const void *self, void *iter, const ir_node *irn)
132 const be_ifg_t *ifg = self;
133 return ifg->impl->neighbours_begin(self, iter, irn);
136 ir_node *(be_ifg_neighbours_next)(const void *self, void *iter)
138 const be_ifg_t *ifg = self;
139 return ifg->impl->neighbours_next(self, iter);
142 void (be_ifg_neighbours_break)(const void *self, void *iter)
144 const be_ifg_t *ifg = self;
145 ifg->impl->neighbours_break(self, iter);
148 ir_node *(be_ifg_nodes_begin)(const void *self, void *iter)
150 const be_ifg_t *ifg = self;
151 return ifg->impl->nodes_begin(self, iter);
154 ir_node *(be_ifg_nodes_next)(const void *self, void *iter)
156 const be_ifg_t *ifg = self;
157 return ifg->impl->nodes_next(self, iter);
160 void (be_ifg_nodes_break)(const void *self, void *iter)
162 const be_ifg_t *ifg = self;
163 ifg->impl->nodes_break(self, iter);
166 int (be_ifg_cliques_begin)(const void *self, void *iter, ir_node **buf)
168 const be_ifg_t *ifg = self;
169 return ifg->impl->cliques_begin(self, iter, buf);
172 int (be_ifg_cliques_next)(const void *self, void *iter)
174 const be_ifg_t *ifg = self;
175 return ifg->impl->cliques_next(self, iter);
178 void (be_ifg_cliques_break)(const void *self, void *iter)
180 const be_ifg_t *ifg = self;
181 ifg->impl->cliques_break(self, iter);
184 int (be_ifg_degree)(const void *self, const ir_node *irn)
186 const be_ifg_t *ifg = self;
187 return ifg->impl->degree(self, irn);
191 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
193 int degree = be_ifg_degree(ifg, irn);
194 void *iter = be_ifg_neighbours_iter_alloca(ifg);
196 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
202 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
203 neighbours[i++] = curr;
205 for(i = 0; i < degree; ++i) {
206 for(j = 0; j < i; ++j)
207 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
218 void be_ifg_check(const be_ifg_t *ifg)
220 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
221 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
225 int neighbours_count = 0;
228 /* count all nodes */
229 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
230 be_ifg_foreach_node(ifg,iter1,n)
233 degree = be_ifg_degree(ifg, n);
234 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
237 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
239 /* Check, if all neighbours are indeed connected to the node. */
240 be_ifg_foreach_node(ifg, iter1, n)
242 ir_printf("\n%+F; ", n);
243 be_ifg_foreach_neighbour(ifg, iter2, n, m)
245 ir_printf("%+F; ", m);
247 if(!be_ifg_connected(ifg, n, m))
248 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
251 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
254 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
256 void *iter = be_ifg_nodes_iter_alloca(ifg);
260 be_ifg_foreach_node(ifg, iter, n)
268 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
270 const ir_node *node_a = *(ir_node **)a;
271 const ir_node *node_b = *(ir_node **)b;
273 int nr_a = node_a->node_nr;
274 int nr_b = node_b->node_nr;
276 return QSORT_CMP(nr_a, nr_b);
279 void be_ifg_check_sorted(const be_ifg_t *ifg)
281 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
282 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
285 const int node_count = be_ifg_check_get_node_count(ifg);
288 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
290 be_ifg_foreach_node(ifg, iter1, n)
292 if(!node_is_in_irgs_storage(ifg->env->irg, n))
294 ir_printf("+%F is in ifg but not in the current irg!", n);
295 assert (node_is_in_irgs_storage(ifg->env->irg, n));
302 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
304 for (i = 0; i < node_count; i++)
306 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
311 degree = be_ifg_degree(ifg, all_nodes[i]);
313 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
319 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
321 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
323 for(k = 0; k < j; k++)
325 ir_printf("%+F, ", neighbours[k]);
337 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
339 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
340 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
343 const int node_count = be_ifg_check_get_node_count(ifg);
346 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
348 be_ifg_foreach_node(ifg, iter1, n)
350 if(!node_is_in_irgs_storage(ifg->env->irg, n))
352 ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
353 assert (node_is_in_irgs_storage(ifg->env->irg, n));
360 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
362 for (i = 0; i < node_count; i++)
364 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
369 degree = be_ifg_degree(ifg, all_nodes[i]);
371 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
377 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
379 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
381 for(k = 0; k < j; k++)
383 ir_fprintf (f,"%+F, ", neighbours[k]);
395 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
398 int tests = BE_CH_PERFORMANCETEST_COUNT;
406 be_ifg_t *old_if = chordal_env->ifg;
408 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
409 unsigned long elapsed_usec = 0;
411 if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
413 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
414 coloring_save(&coloring);
416 lc_timer_reset(timer);
418 for (i = 0; i<tests; i++) /* performance test with std */
421 used_memory = lc_get_heap_used_bytes();
423 rt = lc_timer_enter_high_priority();
424 lc_timer_start(timer);
426 chordal_env->ifg = be_ifg_std_new(chordal_env);
428 lc_timer_stop(timer);
429 rt = lc_timer_leave_high_priority();
431 used_memory = lc_get_heap_used_bytes() - used_memory;
433 coloring_restore(&coloring);
436 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
437 co_build_ou_structure(co);
438 co_build_graph_structure(co);
440 rt = lc_timer_enter_high_priority();
441 lc_timer_start(timer);
443 co_solve_heuristic_new(co);
445 lc_timer_stop(timer);
446 rt = lc_timer_leave_high_priority();
448 co_free_graph_structure(co);
449 co_free_ou_structure(co);
451 be_ifg_free(chordal_env->ifg);
455 elapsed_usec = lc_timer_elapsed_usec(timer);
456 /* calculating average */
457 elapsed_usec = elapsed_usec / tests;
459 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
464 for (i = 0; i<tests; i++) /* performance test with clique */
466 used_memory = lc_get_heap_used_bytes();
468 rt = lc_timer_enter_high_priority();
469 lc_timer_start(timer);
471 chordal_env->ifg = be_ifg_clique_new(chordal_env);
473 lc_timer_stop(timer);
474 rt = lc_timer_leave_high_priority();
476 used_memory = lc_get_heap_used_bytes() - used_memory;
478 coloring_restore(&coloring);
481 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
482 co_build_ou_structure(co);
483 co_build_graph_structure(co);
485 rt = lc_timer_enter_high_priority();
486 lc_timer_start(timer);
488 co_solve_heuristic_new(co);
490 lc_timer_stop(timer);
491 rt = lc_timer_leave_high_priority();
493 co_free_graph_structure(co);
494 co_free_ou_structure(co);
496 be_ifg_free(chordal_env->ifg);
500 elapsed_usec = lc_timer_elapsed_usec(timer);
501 /* calculating average */
502 elapsed_usec = elapsed_usec / tests;
504 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
509 for (i = 0; i<tests; i++) /* performance test with list */
511 used_memory = lc_get_heap_used_bytes();
513 rt = lc_timer_enter_high_priority();
514 lc_timer_start(timer);
516 chordal_env->ifg = be_ifg_list_new(chordal_env);
518 lc_timer_stop(timer);
519 rt = lc_timer_leave_high_priority();
521 used_memory = lc_get_heap_used_bytes() - used_memory;
523 coloring_restore(&coloring);
526 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
527 co_build_ou_structure(co);
528 co_build_graph_structure(co);
530 rt = lc_timer_enter_high_priority();
531 lc_timer_start(timer);
533 co_solve_heuristic_new(co);
535 lc_timer_stop(timer);
536 rt = lc_timer_leave_high_priority();
538 co_free_graph_structure(co);
539 co_free_ou_structure(co);
541 be_ifg_free(chordal_env->ifg);
545 elapsed_usec = lc_timer_elapsed_usec(timer);
546 /* calculating average */
547 elapsed_usec = elapsed_usec / tests;
549 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
554 for (i = 0; i<tests; i++) /* performance test with pointer */
556 used_memory = lc_get_heap_used_bytes();
558 rt = lc_timer_enter_high_priority();
559 lc_timer_start(timer);
561 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
563 lc_timer_stop(timer);
564 rt = lc_timer_leave_high_priority();
566 used_memory = lc_get_heap_used_bytes() - used_memory;
568 coloring_restore(&coloring);
571 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
572 co_build_ou_structure(co);
573 co_build_graph_structure(co);
575 rt = lc_timer_enter_high_priority();
576 lc_timer_start(timer);
578 co_solve_heuristic_new(co);
580 lc_timer_stop(timer);
581 rt = lc_timer_leave_high_priority();
583 co_free_graph_structure(co);
584 co_free_ou_structure(co);
586 be_ifg_free(chordal_env->ifg);
590 elapsed_usec = lc_timer_elapsed_usec(timer);
591 /* calculating average */
592 elapsed_usec = elapsed_usec / tests;
594 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
601 chordal_env->ifg = old_if;
602 #endif /* WITH_LIBCORE */
605 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
607 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
608 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
609 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
613 fprintf(file, "graph G {\n\tgraph [");
615 cb->graph_attr(file, self);
616 fprintf(file, "];\n");
619 cb->at_begin(file, self);
621 be_ifg_foreach_node(ifg, nodes_it, n) {
622 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
623 int idx = get_irn_idx(n);
624 bitset_set(nodes, idx);
625 fprintf(file, "\tnode [");
627 cb->node_attr(file, self, n);
628 fprintf(file, "]; n%d;\n", idx);
632 /* Check, if all neighbours are indeed connected to the node. */
633 be_ifg_foreach_node(ifg, nodes_it, n) {
634 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
635 int n_idx = get_irn_idx(n);
636 int m_idx = get_irn_idx(m);
638 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
639 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
641 cb->edge_attr(file, self, n, m);
642 fprintf(file, "];\n");
648 cb->at_end(file, self);
650 fprintf(file, "}\n");
654 static void int_comp_rec(const be_chordal_env_t *cenv, ir_node *n, bitset_t *seen)
656 void *neigh_it = be_ifg_neighbours_iter_alloca(cenv->ifg);
659 be_ifg_foreach_neighbour(cenv->ifg, neigh_it, n, m) {
660 if(!bitset_contains_irn(seen, m) && !arch_irn_is(cenv->birg->main_env->arch_env, m, ignore)) {
661 bitset_add_irn(seen, m);
662 int_comp_rec(cenv, m, seen);
668 static int int_component_stat(const be_chordal_env_t *cenv)
671 void *nodes_it = be_ifg_nodes_iter_alloca(cenv->ifg);
672 bitset_t *seen = bitset_irg_malloc(cenv->irg);
676 be_ifg_foreach_node(cenv->ifg, nodes_it, n) {
677 if(!bitset_contains_irn(seen, n) && !arch_irn_is(cenv->birg->main_env->arch_env, n, ignore)) {
679 bitset_add_irn(seen, n);
680 int_comp_rec(cenv, n, seen);
688 void be_ifg_stat(const be_chordal_env_t *cenv, be_ifg_stat_t *stat)
690 void *nodes_it = be_ifg_nodes_iter_alloca(cenv->ifg);
691 void *neigh_it = be_ifg_neighbours_iter_alloca(cenv->ifg);
692 bitset_t *nodes = bitset_irg_malloc(cenv->irg);
696 memset(stat, 0, sizeof(stat[0]));
697 be_ifg_foreach_node(cenv->ifg, nodes_it, n) {
699 be_ifg_foreach_neighbour(cenv->ifg, neigh_it, n, m) {
700 bitset_add_irn(nodes, n);
701 stat->n_edges += !bitset_contains_irn(nodes, m);
705 stat->n_comps = int_component_stat(cenv);