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)
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)
288 if(!node_is_in_irgs_storage(ifg->env->irg, n))
290 printf ("+%F is in ifg but not in the current irg!",n);
291 assert (node_is_in_irgs_storage(ifg->env->irg, n));
298 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
300 for (i = 0; i < node_count; i++)
302 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
307 degree = be_ifg_degree(ifg, all_nodes[i]);
309 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
315 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
317 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
319 for(k = 0; k < j; k++)
321 ir_printf("%+F, ", neighbours[k]);
333 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
335 int tests = BE_CH_PERFORMANCETEST_COUNT;
339 struct mallinfo minfo;
341 #endif /* __linux__ */
346 be_ifg_t *old_if = chordal_env->ifg;
348 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
349 unsigned long elapsed_usec = 0;
351 if ((int) get_irg_estimated_node_cnt >= BE_CH_PERFORMANCETEST_MIN_NODES)
354 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
355 coloring_save(&coloring);
357 lc_timer_reset(timer);
359 for (i = 0; i<tests; i++) /* performance test with std */
363 used_memory = minfo.uordblks;
364 #endif /* __linux__ */
366 rt = lc_timer_enter_high_priority();
367 lc_timer_start(timer);
369 chordal_env->ifg = be_ifg_std_new(chordal_env);
371 lc_timer_stop(timer);
372 rt = lc_timer_leave_high_priority();
376 used_memory = minfo.uordblks - used_memory;
377 #endif /* __linux__ */
379 coloring_restore(&coloring);
382 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
383 co_build_ou_structure(co);
384 co_build_graph_structure(co);
386 rt = lc_timer_enter_high_priority();
387 lc_timer_start(timer);
389 co_solve_heuristic_new(co);
391 lc_timer_stop(timer);
392 rt = lc_timer_leave_high_priority();
394 co_free_graph_structure(co);
395 co_free_ou_structure(co);
397 be_ifg_free(chordal_env->ifg);
401 elapsed_usec = lc_timer_elapsed_usec(timer);
402 /* calculating average */
403 elapsed_usec = elapsed_usec / tests;
405 ir_printf("\nstd:; %+F; ",current_ir_graph);
407 ir_printf("%u; ", used_memory);
408 #endif /* __linux__ */
409 ir_printf("%u; ", elapsed_usec);
414 #endif /* __linux__ */
417 for (i = 0; i<tests; i++) /* performance test with clique */
421 used_memory = minfo.uordblks;
422 #endif /* __linux__ */
424 rt = lc_timer_enter_high_priority();
425 lc_timer_start(timer);
427 chordal_env->ifg = be_ifg_clique_new(chordal_env);
429 lc_timer_stop(timer);
430 rt = lc_timer_leave_high_priority();
434 used_memory = minfo.uordblks - used_memory;
435 #endif /* __linux__ */
437 coloring_restore(&coloring);
440 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
441 co_build_ou_structure(co);
442 co_build_graph_structure(co);
444 rt = lc_timer_enter_high_priority();
445 lc_timer_start(timer);
447 co_solve_heuristic_new(co);
449 lc_timer_stop(timer);
450 rt = lc_timer_leave_high_priority();
452 co_free_graph_structure(co);
453 co_free_ou_structure(co);
455 be_ifg_free(chordal_env->ifg);
459 elapsed_usec = lc_timer_elapsed_usec(timer);
460 /* calculating average */
461 elapsed_usec = elapsed_usec / tests;
463 ir_printf("\nclique:; %+F; ",current_ir_graph);
465 ir_printf("%u; ", used_memory);
466 #endif /* __linux__ */
467 ir_printf("%u; ", elapsed_usec);
472 #endif /* __linux__ */
475 for (i = 0; i<tests; i++) /* performance test with list */
479 used_memory = minfo.uordblks;
480 #endif /* __linux__ */
482 rt = lc_timer_enter_high_priority();
483 lc_timer_start(timer);
485 chordal_env->ifg = be_ifg_list_new(chordal_env);
487 lc_timer_stop(timer);
488 rt = lc_timer_leave_high_priority();
492 used_memory = minfo.uordblks - used_memory;
493 #endif /* __linux__ */
495 coloring_restore(&coloring);
498 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
499 co_build_ou_structure(co);
500 co_build_graph_structure(co);
502 rt = lc_timer_enter_high_priority();
503 lc_timer_start(timer);
505 co_solve_heuristic_new(co);
507 lc_timer_stop(timer);
508 rt = lc_timer_leave_high_priority();
510 co_free_graph_structure(co);
511 co_free_ou_structure(co);
513 be_ifg_free(chordal_env->ifg);
517 elapsed_usec = lc_timer_elapsed_usec(timer);
518 /* calculating average */
519 elapsed_usec = elapsed_usec / tests;
521 ir_printf("\nlist:; %+F; ",current_ir_graph);
523 ir_printf("%u; ", used_memory);
524 #endif /* __linux__ */
525 ir_printf("%u; ", elapsed_usec);
530 #endif /* __linux__ */
533 for (i = 0; i<tests; i++) /* performance test with pointer */
537 used_memory = minfo.uordblks;
538 #endif /* __linux__ */
540 rt = lc_timer_enter_high_priority();
541 lc_timer_start(timer);
543 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
545 lc_timer_stop(timer);
546 rt = lc_timer_leave_high_priority();
550 used_memory = minfo.uordblks - used_memory;
551 #endif /* __linux__ */
553 coloring_restore(&coloring);
556 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
557 co_build_ou_structure(co);
558 co_build_graph_structure(co);
560 rt = lc_timer_enter_high_priority();
561 lc_timer_start(timer);
563 co_solve_heuristic_new(co);
565 lc_timer_stop(timer);
566 rt = lc_timer_leave_high_priority();
568 co_free_graph_structure(co);
569 co_free_ou_structure(co);
571 be_ifg_free(chordal_env->ifg);
575 elapsed_usec = lc_timer_elapsed_usec(timer);
576 /* calculating average */
577 elapsed_usec = elapsed_usec / tests;
579 ir_printf("\npointer:; %+F; ",current_ir_graph);
581 ir_printf("%u; ", used_memory);
582 #endif /* __linux__ */
583 ir_printf("%u; ", elapsed_usec);
588 #endif /* __linux__ */
592 chordal_env->ifg = old_if;
595 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
597 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
598 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
599 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
603 fprintf(file, "graph G {\n\tgraph [");
605 cb->graph_attr(file, self);
606 fprintf(file, "];\n");
609 cb->at_begin(file, self);
611 be_ifg_foreach_node(ifg, nodes_it, n) {
612 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
613 int idx = get_irn_idx(n);
614 bitset_set(nodes, idx);
615 fprintf(file, "\tnode [");
617 cb->node_attr(file, self, n);
618 fprintf(file, "]; n%d;\n", idx);
622 /* Check, if all neighbours are indeed connected to the node. */
623 be_ifg_foreach_node(ifg, nodes_it, n) {
624 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
625 int n_idx = get_irn_idx(n);
626 int m_idx = get_irn_idx(m);
628 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
629 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
631 cb->edge_attr(file, self, n, m);
632 fprintf(file, "];\n");
638 cb->at_end(file, self);
640 fprintf(file, "}\n");