4 * @author Sebastian Hack
6 * Copyright (C) 2005 Universitaet Karlsruhe
7 * Released under the GPL
25 #include <libcore/lc_opts.h>
26 #include <libcore/lc_opts_enum.h>
27 #include <libcore/lc_timing.h>
28 #endif /* WITH_LIBCORE */
37 #include "beifg_impl.h"
39 #include "irphase_t.h"
40 #include "bechordal.h"
42 #include "becopystat.h"
43 #include "becopyopt.h"
45 /** Defines values for the ifg performance test */
46 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
47 #define BE_CH_PERFORMANCETEST_COUNT (10)
49 typedef struct _coloring_t coloring_t;
53 const arch_env_t *arch_env;
57 size_t (be_ifg_nodes_iter_size)(const void *self)
59 const be_ifg_t *ifg = self;
60 return ifg->impl->nodes_iter_size;
63 size_t (be_ifg_neighbours_iter_size)(const void *self)
65 const be_ifg_t *ifg = self;
66 return ifg->impl->neighbours_iter_size;
69 size_t (be_ifg_cliques_iter_size)(const void *self)
71 const be_ifg_t *ifg = self;
72 return ifg->impl->cliques_iter_size;
75 static void *regs_irn_data_init(phase_t *ph, ir_node *irn, void *data)
77 coloring_t *coloring = (coloring_t *) ph;
78 return (void *) arch_get_irn_register(coloring->arch_env, irn);
81 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
83 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init);
89 static void get_irn_color(ir_node *irn, void *c)
91 coloring_t *coloring = c;
92 phase_get_or_set_irn_data(&coloring->ph, irn);
95 static void restore_irn_color(ir_node *irn, void *c)
97 coloring_t *coloring = c;
98 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
100 arch_set_irn_register(coloring->arch_env, irn, reg);
103 void coloring_save(coloring_t *c)
105 irg_walk_graph(c->irg, NULL, get_irn_color, c);
108 void coloring_restore(coloring_t *c)
110 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
113 void (be_ifg_free)(void *self)
115 be_ifg_t *ifg = self;
116 ifg->impl->free(self);
119 int (be_ifg_connected)(const void *self, const ir_node *a, const ir_node *b)
121 const be_ifg_t *ifg = self;
122 return ifg->impl->connected(self, a, b);
125 ir_node *(be_ifg_neighbours_begin)(const void *self, void *iter, const ir_node *irn)
127 const be_ifg_t *ifg = self;
128 return ifg->impl->neighbours_begin(self, iter, irn);
131 ir_node *(be_ifg_neighbours_next)(const void *self, void *iter)
133 const be_ifg_t *ifg = self;
134 return ifg->impl->neighbours_next(self, iter);
137 void (be_ifg_neighbours_break)(const void *self, void *iter)
139 const be_ifg_t *ifg = self;
140 ifg->impl->neighbours_break(self, iter);
143 ir_node *(be_ifg_nodes_begin)(const void *self, void *iter)
145 const be_ifg_t *ifg = self;
146 return ifg->impl->nodes_begin(self, iter);
149 ir_node *(be_ifg_nodes_next)(const void *self, void *iter)
151 const be_ifg_t *ifg = self;
152 return ifg->impl->nodes_next(self, iter);
155 void (be_ifg_nodes_break)(const void *self, void *iter)
157 const be_ifg_t *ifg = self;
158 ifg->impl->nodes_break(self, iter);
161 int (be_ifg_cliques_begin)(const void *self, void *iter, ir_node **buf)
163 const be_ifg_t *ifg = self;
164 return ifg->impl->cliques_begin(self, iter, buf);
167 int (be_ifg_cliques_next)(const void *self, void *iter)
169 const be_ifg_t *ifg = self;
170 return ifg->impl->cliques_next(self, iter);
173 void (be_ifg_cliques_break)(const void *self, void *iter)
175 const be_ifg_t *ifg = self;
176 ifg->impl->cliques_break(self, iter);
179 int (be_ifg_degree)(const void *self, const ir_node *irn)
181 const be_ifg_t *ifg = self;
182 return ifg->impl->degree(self, irn);
186 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
188 int degree = be_ifg_degree(ifg, irn);
189 void *iter = be_ifg_neighbours_iter_alloca(ifg);
191 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
197 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
198 neighbours[i++] = curr;
200 for(i = 0; i < degree; ++i) {
201 for(j = 0; j < i; ++j)
202 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
213 void be_ifg_check(const be_ifg_t *ifg)
215 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
216 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
220 int neighbours_count = 0;
223 /* count all nodes */
224 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
225 be_ifg_foreach_node(ifg,iter1,n)
228 degree = be_ifg_degree(ifg, n);
229 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
232 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
234 /* Check, if all neighbours are indeed connected to the node. */
235 be_ifg_foreach_node(ifg, iter1, n)
237 ir_printf("\n%+F; ", n);
238 be_ifg_foreach_neighbour(ifg, iter2, n, m)
240 ir_printf("%+F; ", m);
242 if(!be_ifg_connected(ifg, n, m))
243 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
246 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
249 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
251 void *iter = be_ifg_nodes_iter_alloca(ifg);
255 be_ifg_foreach_node(ifg, iter, n)
263 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
265 const ir_node *node_a = *(ir_node **)a;
266 const ir_node *node_b = *(ir_node **)b;
268 int nr_a = node_a->node_nr;
269 int nr_b = node_b->node_nr;
271 return QSORT_CMP(nr_a, nr_b);
274 void be_ifg_check_sorted(const be_ifg_t *ifg, FILE *f)
276 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
277 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
280 const int node_count = be_ifg_check_get_node_count(ifg);
281 int neighbours_count = 0;
284 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
286 be_ifg_foreach_node(ifg, iter1, n)
292 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
294 for (i = 0; i < node_count; i++)
296 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
301 degree = be_ifg_degree(ifg, all_nodes[i]);
303 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
309 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
311 ir_fprintf(f, "%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
313 for(k = 0; k < j; k++)
315 ir_fprintf(f, "%+F, ", neighbours[k]);
327 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
329 int tests = BE_CH_PERFORMANCETEST_COUNT;
333 struct mallinfo minfo;
340 be_ifg_t *old_if = chordal_env->ifg;
342 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
343 unsigned long elapsed_usec = 0;
345 if ((int) get_irg_estimated_node_cnt >= BE_CH_PERFORMANCETEST_MIN_NODES)
348 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
349 coloring_save(&coloring);
351 lc_timer_reset(timer);
353 for (i = 0; i<tests; i++) /* performance test with std */
357 used_memory = minfo.uordblks;
360 rt = lc_timer_enter_high_priority();
361 lc_timer_start(timer);
363 chordal_env->ifg = be_ifg_std_new(chordal_env);
365 lc_timer_stop(timer);
366 rt = lc_timer_leave_high_priority();
370 used_memory = minfo.uordblks - used_memory;
373 coloring_restore(&coloring);
376 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
377 co_build_ou_structure(co);
378 co_build_graph_structure(co);
380 rt = lc_timer_enter_high_priority();
381 lc_timer_start(timer);
383 co_solve_heuristic_new(co);
385 lc_timer_stop(timer);
386 rt = lc_timer_leave_high_priority();
388 co_free_graph_structure(co);
389 co_free_ou_structure(co);
391 be_ifg_free(chordal_env->ifg);
395 elapsed_usec = lc_timer_elapsed_usec(timer);
396 /* calculating average */
397 elapsed_usec = elapsed_usec / tests;
399 ir_printf("\nstd:; %+F; ",current_ir_graph);
401 ir_printf("%u; ", used_memory);
403 ir_printf("%u; ", elapsed_usec);
411 for (i = 0; i<tests; i++) /* performance test with clique */
415 used_memory = minfo.uordblks;
418 rt = lc_timer_enter_high_priority();
419 lc_timer_start(timer);
421 chordal_env->ifg = be_ifg_clique_new(chordal_env);
423 lc_timer_stop(timer);
424 rt = lc_timer_leave_high_priority();
428 used_memory = minfo.uordblks - 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("\nclique:; %+F; ",current_ir_graph);
459 ir_printf("%u; ", used_memory);
461 ir_printf("%u; ", elapsed_usec);
469 for (i = 0; i<tests; i++) /* performance test with list */
473 used_memory = minfo.uordblks;
476 rt = lc_timer_enter_high_priority();
477 lc_timer_start(timer);
479 chordal_env->ifg = be_ifg_list_new(chordal_env);
481 lc_timer_stop(timer);
482 rt = lc_timer_leave_high_priority();
486 used_memory = minfo.uordblks - used_memory;
489 coloring_restore(&coloring);
492 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
493 co_build_ou_structure(co);
494 co_build_graph_structure(co);
496 rt = lc_timer_enter_high_priority();
497 lc_timer_start(timer);
499 co_solve_heuristic_new(co);
501 lc_timer_stop(timer);
502 rt = lc_timer_leave_high_priority();
504 co_free_graph_structure(co);
505 co_free_ou_structure(co);
507 be_ifg_free(chordal_env->ifg);
511 elapsed_usec = lc_timer_elapsed_usec(timer);
512 /* calculating average */
513 elapsed_usec = elapsed_usec / tests;
515 ir_printf("\nlist:; %+F; ",current_ir_graph);
517 ir_printf("%u; ", used_memory);
519 ir_printf("%u; ", elapsed_usec);
527 for (i = 0; i<tests; i++) /* performance test with pointer */
531 used_memory = minfo.uordblks;
534 rt = lc_timer_enter_high_priority();
535 lc_timer_start(timer);
537 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
539 lc_timer_stop(timer);
540 rt = lc_timer_leave_high_priority();
544 used_memory = minfo.uordblks - used_memory;
547 coloring_restore(&coloring);
550 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
551 co_build_ou_structure(co);
552 co_build_graph_structure(co);
554 rt = lc_timer_enter_high_priority();
555 lc_timer_start(timer);
557 co_solve_heuristic_new(co);
559 lc_timer_stop(timer);
560 rt = lc_timer_leave_high_priority();
562 co_free_graph_structure(co);
563 co_free_ou_structure(co);
565 be_ifg_free(chordal_env->ifg);
569 elapsed_usec = lc_timer_elapsed_usec(timer);
570 /* calculating average */
571 elapsed_usec = elapsed_usec / tests;
573 ir_printf("\npointer:; %+F; ",current_ir_graph);
575 ir_printf("%u; ", used_memory);
577 ir_printf("%u; ", elapsed_usec);
586 chordal_env->ifg = old_if;
589 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
591 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
592 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
593 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
597 fprintf(file, "graph G {\n\tgraph [");
599 cb->graph_attr(file, self);
600 fprintf(file, "];\n");
603 cb->at_begin(file, self);
605 be_ifg_foreach_node(ifg, nodes_it, n) {
606 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
607 int idx = get_irn_idx(n);
608 bitset_set(nodes, idx);
609 fprintf(file, "\tnode [");
611 cb->node_attr(file, self, n);
612 fprintf(file, "]; n%d;\n", idx);
616 /* Check, if all neighbours are indeed connected to the node. */
617 be_ifg_foreach_node(ifg, nodes_it, n) {
618 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
619 int n_idx = get_irn_idx(n);
620 int m_idx = get_irn_idx(m);
622 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
623 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
625 cb->edge_attr(file, self, n, m);
626 fprintf(file, "];\n");
632 cb->at_end(file, self);
634 fprintf(file, "}\n");