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 */
41 #include "beifg_impl.h"
43 #include "irphase_t.h"
44 #include "bechordal.h"
46 #include "becopystat.h"
47 #include "becopyopt.h"
49 /** Defines values for the ifg performance test */
50 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
51 #define BE_CH_PERFORMANCETEST_COUNT (500)
53 typedef struct _coloring_t coloring_t;
57 const arch_env_t *arch_env;
61 size_t (be_ifg_nodes_iter_size)(const void *self)
63 const be_ifg_t *ifg = self;
64 return ifg->impl->nodes_iter_size;
67 size_t (be_ifg_neighbours_iter_size)(const void *self)
69 const be_ifg_t *ifg = self;
70 return ifg->impl->neighbours_iter_size;
73 size_t (be_ifg_cliques_iter_size)(const void *self)
75 const be_ifg_t *ifg = self;
76 return ifg->impl->cliques_iter_size;
79 static void *regs_irn_data_init(phase_t *ph, ir_node *irn, void *data)
81 coloring_t *coloring = (coloring_t *) ph;
82 return (void *) arch_get_irn_register(coloring->arch_env, irn);
85 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
87 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init);
93 static void get_irn_color(ir_node *irn, void *c)
95 coloring_t *coloring = c;
96 phase_get_or_set_irn_data(&coloring->ph, irn);
99 static void restore_irn_color(ir_node *irn, void *c)
101 coloring_t *coloring = c;
102 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
104 arch_set_irn_register(coloring->arch_env, irn, reg);
107 void coloring_save(coloring_t *c)
109 irg_walk_graph(c->irg, NULL, get_irn_color, c);
112 void coloring_restore(coloring_t *c)
114 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
117 void (be_ifg_free)(void *self)
119 be_ifg_t *ifg = self;
120 ifg->impl->free(self);
123 int (be_ifg_connected)(const void *self, const ir_node *a, const ir_node *b)
125 const be_ifg_t *ifg = self;
126 return ifg->impl->connected(self, a, b);
129 ir_node *(be_ifg_neighbours_begin)(const void *self, void *iter, const ir_node *irn)
131 const be_ifg_t *ifg = self;
132 return ifg->impl->neighbours_begin(self, iter, irn);
135 ir_node *(be_ifg_neighbours_next)(const void *self, void *iter)
137 const be_ifg_t *ifg = self;
138 return ifg->impl->neighbours_next(self, iter);
141 void (be_ifg_neighbours_break)(const void *self, void *iter)
143 const be_ifg_t *ifg = self;
144 ifg->impl->neighbours_break(self, iter);
147 ir_node *(be_ifg_nodes_begin)(const void *self, void *iter)
149 const be_ifg_t *ifg = self;
150 return ifg->impl->nodes_begin(self, iter);
153 ir_node *(be_ifg_nodes_next)(const void *self, void *iter)
155 const be_ifg_t *ifg = self;
156 return ifg->impl->nodes_next(self, iter);
159 void (be_ifg_nodes_break)(const void *self, void *iter)
161 const be_ifg_t *ifg = self;
162 ifg->impl->nodes_break(self, iter);
165 int (be_ifg_cliques_begin)(const void *self, void *iter, ir_node **buf)
167 const be_ifg_t *ifg = self;
168 return ifg->impl->cliques_begin(self, iter, buf);
171 int (be_ifg_cliques_next)(const void *self, void *iter)
173 const be_ifg_t *ifg = self;
174 return ifg->impl->cliques_next(self, iter);
177 void (be_ifg_cliques_break)(const void *self, void *iter)
179 const be_ifg_t *ifg = self;
180 ifg->impl->cliques_break(self, iter);
183 int (be_ifg_degree)(const void *self, const ir_node *irn)
185 const be_ifg_t *ifg = self;
186 return ifg->impl->degree(self, irn);
190 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
192 int degree = be_ifg_degree(ifg, irn);
193 void *iter = be_ifg_neighbours_iter_alloca(ifg);
195 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
201 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
202 neighbours[i++] = curr;
204 for(i = 0; i < degree; ++i) {
205 for(j = 0; j < i; ++j)
206 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
217 void be_ifg_check(const be_ifg_t *ifg)
219 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
220 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
224 int neighbours_count = 0;
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)
232 degree = be_ifg_degree(ifg, n);
233 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
236 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
238 /* Check, if all neighbours are indeed connected to the node. */
239 be_ifg_foreach_node(ifg, iter1, n)
241 ir_printf("\n%+F; ", n);
242 be_ifg_foreach_neighbour(ifg, iter2, n, m)
244 ir_printf("%+F; ", m);
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);
250 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
253 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
255 void *iter = be_ifg_nodes_iter_alloca(ifg);
259 be_ifg_foreach_node(ifg, iter, n)
267 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
269 const ir_node *node_a = *(ir_node **)a;
270 const ir_node *node_b = *(ir_node **)b;
272 int nr_a = node_a->node_nr;
273 int nr_b = node_b->node_nr;
275 return QSORT_CMP(nr_a, nr_b);
278 void be_ifg_check_sorted(const be_ifg_t *ifg)
280 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
281 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
284 const int node_count = be_ifg_check_get_node_count(ifg);
287 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
289 be_ifg_foreach_node(ifg, iter1, n)
291 if(!node_is_in_irgs_storage(ifg->env->irg, n))
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));
301 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
303 for (i = 0; i < node_count; i++)
305 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
310 degree = be_ifg_degree(ifg, all_nodes[i]);
312 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
318 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
320 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
322 for(k = 0; k < j; k++)
324 ir_printf("%+F, ", neighbours[k]);
336 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
338 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
339 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
342 const int node_count = be_ifg_check_get_node_count(ifg);
345 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
347 be_ifg_foreach_node(ifg, iter1, n)
349 if(!node_is_in_irgs_storage(ifg->env->irg, n))
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));
359 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
361 for (i = 0; i < node_count; i++)
363 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
368 degree = be_ifg_degree(ifg, all_nodes[i]);
370 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
376 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
378 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
380 for(k = 0; k < j; k++)
382 ir_fprintf (f,"%+F, ", neighbours[k]);
394 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
396 int tests = BE_CH_PERFORMANCETEST_COUNT;
404 be_ifg_t *old_if = chordal_env->ifg;
406 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
407 unsigned long elapsed_usec = 0;
409 if ((int) get_irg_estimated_node_cnt >= BE_CH_PERFORMANCETEST_MIN_NODES)
411 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
412 coloring_save(&coloring);
414 lc_timer_reset(timer);
416 for (i = 0; i<tests; i++) /* performance test with std */
419 used_memory = lc_get_heap_used_bytes();
421 rt = lc_timer_enter_high_priority();
422 lc_timer_start(timer);
424 chordal_env->ifg = be_ifg_std_new(chordal_env);
426 lc_timer_stop(timer);
427 rt = lc_timer_leave_high_priority();
429 used_memory = lc_get_heap_used_bytes() - used_memory;
431 coloring_restore(&coloring);
434 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
435 co_build_ou_structure(co);
436 co_build_graph_structure(co);
438 rt = lc_timer_enter_high_priority();
439 lc_timer_start(timer);
441 co_solve_heuristic_new(co);
443 lc_timer_stop(timer);
444 rt = lc_timer_leave_high_priority();
446 co_free_graph_structure(co);
447 co_free_ou_structure(co);
449 be_ifg_free(chordal_env->ifg);
453 elapsed_usec = lc_timer_elapsed_usec(timer);
454 /* calculating average */
455 elapsed_usec = elapsed_usec / tests;
457 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
463 for (i = 0; i<tests; i++) /* performance test with clique */
465 used_memory = lc_get_heap_used_bytes();
467 rt = lc_timer_enter_high_priority();
468 lc_timer_start(timer);
470 chordal_env->ifg = be_ifg_clique_new(chordal_env);
472 lc_timer_stop(timer);
473 rt = lc_timer_leave_high_priority();
475 used_memory = lc_get_heap_used_bytes() - used_memory;
477 coloring_restore(&coloring);
480 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
481 co_build_ou_structure(co);
482 co_build_graph_structure(co);
484 rt = lc_timer_enter_high_priority();
485 lc_timer_start(timer);
487 co_solve_heuristic_new(co);
489 lc_timer_stop(timer);
490 rt = lc_timer_leave_high_priority();
492 co_free_graph_structure(co);
493 co_free_ou_structure(co);
495 be_ifg_free(chordal_env->ifg);
499 elapsed_usec = lc_timer_elapsed_usec(timer);
500 /* calculating average */
501 elapsed_usec = elapsed_usec / tests;
503 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);
555 for (i = 0; i<tests; i++) /* performance test with pointer */
557 used_memory = lc_get_heap_used_bytes();
559 rt = lc_timer_enter_high_priority();
560 lc_timer_start(timer);
562 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
564 lc_timer_stop(timer);
565 rt = lc_timer_leave_high_priority();
567 used_memory = lc_get_heap_used_bytes() - used_memory;
569 coloring_restore(&coloring);
572 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
573 co_build_ou_structure(co);
574 co_build_graph_structure(co);
576 rt = lc_timer_enter_high_priority();
577 lc_timer_start(timer);
579 co_solve_heuristic_new(co);
581 lc_timer_stop(timer);
582 rt = lc_timer_leave_high_priority();
584 co_free_graph_structure(co);
585 co_free_ou_structure(co);
587 be_ifg_free(chordal_env->ifg);
591 elapsed_usec = lc_timer_elapsed_usec(timer);
592 /* calculating average */
593 elapsed_usec = elapsed_usec / tests;
595 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
602 chordal_env->ifg = old_if;
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");