2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Interface for interference graphs.
23 * @author Sebastian Hack
33 #include <libcore/lc_opts.h>
34 #include <libcore/lc_opts_enum.h>
35 #include <libcore/lc_timing.h>
44 #include "beifg_impl.h"
45 #include "irphase_t.h"
49 #include "becopystat.h"
50 #include "becopyopt.h"
54 /** Defines values for the ifg performance test */
55 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
56 #define BE_CH_PERFORMANCETEST_COUNT (500)
58 typedef struct _coloring_t coloring_t;
62 const arch_env_t *arch_env;
66 size_t (be_ifg_nodes_iter_size)(const be_ifg_t *ifg)
68 return ifg->impl->nodes_iter_size;
71 size_t (be_ifg_neighbours_iter_size)(const be_ifg_t *ifg)
73 return ifg->impl->neighbours_iter_size;
76 size_t (be_ifg_cliques_iter_size)(const be_ifg_t *ifg)
78 return ifg->impl->cliques_iter_size;
81 static void *regs_irn_data_init(ir_phase *ph, ir_node *irn, void *data)
83 coloring_t *coloring = (coloring_t *) ph;
86 return (void *) arch_get_irn_register(coloring->arch_env, irn);
89 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
91 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init, NULL);
97 static void get_irn_color(ir_node *irn, void *c)
99 coloring_t *coloring = c;
100 phase_get_or_set_irn_data(&coloring->ph, irn);
103 static void restore_irn_color(ir_node *irn, void *c)
105 coloring_t *coloring = c;
106 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
108 arch_set_irn_register(coloring->arch_env, irn, reg);
111 void coloring_save(coloring_t *c)
113 irg_walk_graph(c->irg, NULL, get_irn_color, c);
116 void coloring_restore(coloring_t *c)
118 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
121 void (be_ifg_free)(be_ifg_t *ifg)
123 ifg->impl->free(ifg);
126 int (be_ifg_connected)(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
128 return ifg->impl->connected(ifg, a, b);
131 ir_node *(be_ifg_neighbours_begin)(const be_ifg_t *ifg, void *iter, const ir_node *irn)
133 return ifg->impl->neighbours_begin(ifg, iter, irn);
136 ir_node *(be_ifg_neighbours_next)(const be_ifg_t *ifg, void *iter)
138 return ifg->impl->neighbours_next(ifg, iter);
141 void (be_ifg_neighbours_break)(const be_ifg_t *ifg, void *iter)
143 ifg->impl->neighbours_break(ifg, iter);
146 ir_node *(be_ifg_nodes_begin)(const be_ifg_t *ifg, void *iter)
148 return ifg->impl->nodes_begin(ifg, iter);
151 ir_node *(be_ifg_nodes_next)(const be_ifg_t *ifg, void *iter)
153 return ifg->impl->nodes_next(ifg, iter);
156 void (be_ifg_nodes_break)(const be_ifg_t *ifg, void *iter)
158 ifg->impl->nodes_break(ifg, iter);
161 int (be_ifg_cliques_begin)(const be_ifg_t *ifg, void *iter, ir_node **buf)
163 return ifg->impl->cliques_begin(ifg, iter, buf);
166 int (be_ifg_cliques_next)(const be_ifg_t *ifg, void *iter)
168 return ifg->impl->cliques_next(ifg, iter);
171 void (be_ifg_cliques_break)(const be_ifg_t *ifg, void *iter)
173 ifg->impl->cliques_break(ifg, iter);
176 int (be_ifg_degree)(const be_ifg_t *ifg, const ir_node *irn)
178 return ifg->impl->degree(ifg, irn);
182 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
184 int degree = be_ifg_degree(ifg, irn);
185 void *iter = be_ifg_neighbours_iter_alloca(ifg);
187 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
193 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
194 neighbours[i++] = curr;
196 for(i = 0; i < degree; ++i) {
197 for(j = 0; j < i; ++j)
198 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
209 void be_ifg_check(const be_ifg_t *ifg)
211 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
212 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
216 int neighbours_count = 0;
219 /* count all nodes */
220 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
221 be_ifg_foreach_node(ifg,iter1,n)
224 degree = be_ifg_degree(ifg, n);
225 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
228 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
230 /* Check, if all neighbours are indeed connected to the node. */
231 be_ifg_foreach_node(ifg, iter1, n)
233 ir_printf("\n%+F; ", n);
234 be_ifg_foreach_neighbour(ifg, iter2, n, m)
236 ir_printf("%+F; ", m);
238 if(!be_ifg_connected(ifg, n, m))
239 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
242 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
245 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
247 void *iter = be_ifg_nodes_iter_alloca(ifg);
251 be_ifg_foreach_node(ifg, iter, n)
259 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
261 const ir_node *node_a = *(ir_node **)a;
262 const ir_node *node_b = *(ir_node **)b;
264 long nr_a = get_irn_node_nr(node_a);
265 long nr_b = get_irn_node_nr(node_b);
267 return QSORT_CMP(nr_a, nr_b);
270 void be_ifg_check_sorted(const be_ifg_t *ifg)
272 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
273 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
276 const int node_count = be_ifg_check_get_node_count(ifg);
279 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
281 be_ifg_foreach_node(ifg, iter1, n)
283 if(!node_is_in_irgs_storage(ifg->env->irg, n))
285 ir_printf("+%F is in ifg but not in the current irg!", n);
286 assert (node_is_in_irgs_storage(ifg->env->irg, n));
293 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
295 for (i = 0; i < node_count; i++)
297 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
302 degree = be_ifg_degree(ifg, all_nodes[i]);
304 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
310 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
312 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
314 for(k = 0; k < j; k++)
316 ir_printf("%+F, ", neighbours[k]);
328 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
330 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
331 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
334 const int node_count = be_ifg_check_get_node_count(ifg);
337 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
339 be_ifg_foreach_node(ifg, iter1, n)
341 if(!node_is_in_irgs_storage(ifg->env->irg, n))
343 ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
344 assert (node_is_in_irgs_storage(ifg->env->irg, n));
351 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
353 for (i = 0; i < node_count; i++)
355 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
360 degree = be_ifg_degree(ifg, all_nodes[i]);
362 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
368 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
370 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
372 for(k = 0; k < j; k++)
374 ir_fprintf (f,"%+F, ", neighbours[k]);
386 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
388 int tests = BE_CH_PERFORMANCETEST_COUNT;
396 be_ifg_t *old_if = chordal_env->ifg;
398 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
399 unsigned long elapsed_usec = 0;
401 if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
403 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
404 coloring_save(&coloring);
406 lc_timer_reset(timer);
408 for (i = 0; i<tests; i++) /* performance test with std */
411 used_memory = lc_get_heap_used_bytes();
413 rt = lc_timer_enter_high_priority();
414 lc_timer_start(timer);
416 chordal_env->ifg = be_ifg_std_new(chordal_env);
418 lc_timer_stop(timer);
419 rt = lc_timer_leave_high_priority();
421 used_memory = lc_get_heap_used_bytes() - used_memory;
423 coloring_restore(&coloring);
426 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
427 co_build_ou_structure(co);
428 co_build_graph_structure(co);
430 rt = lc_timer_enter_high_priority();
431 lc_timer_start(timer);
433 co_solve_heuristic_new(co);
435 lc_timer_stop(timer);
436 rt = lc_timer_leave_high_priority();
438 co_free_graph_structure(co);
439 co_free_ou_structure(co);
441 be_ifg_free(chordal_env->ifg);
445 elapsed_usec = lc_timer_elapsed_usec(timer);
446 /* calculating average */
447 elapsed_usec = elapsed_usec / tests;
449 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
454 for (i = 0; i<tests; i++) /* performance test with clique */
456 used_memory = lc_get_heap_used_bytes();
458 rt = lc_timer_enter_high_priority();
459 lc_timer_start(timer);
461 chordal_env->ifg = be_ifg_clique_new(chordal_env);
463 lc_timer_stop(timer);
464 rt = lc_timer_leave_high_priority();
466 used_memory = lc_get_heap_used_bytes() - used_memory;
468 coloring_restore(&coloring);
471 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
472 co_build_ou_structure(co);
473 co_build_graph_structure(co);
475 rt = lc_timer_enter_high_priority();
476 lc_timer_start(timer);
478 co_solve_heuristic_new(co);
480 lc_timer_stop(timer);
481 rt = lc_timer_leave_high_priority();
483 co_free_graph_structure(co);
484 co_free_ou_structure(co);
486 be_ifg_free(chordal_env->ifg);
490 elapsed_usec = lc_timer_elapsed_usec(timer);
491 /* calculating average */
492 elapsed_usec = elapsed_usec / tests;
494 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
499 for (i = 0; i<tests; i++) /* performance test with list */
501 used_memory = lc_get_heap_used_bytes();
503 rt = lc_timer_enter_high_priority();
504 lc_timer_start(timer);
506 chordal_env->ifg = be_ifg_list_new(chordal_env);
508 lc_timer_stop(timer);
509 rt = lc_timer_leave_high_priority();
511 used_memory = lc_get_heap_used_bytes() - used_memory;
513 coloring_restore(&coloring);
516 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
517 co_build_ou_structure(co);
518 co_build_graph_structure(co);
520 rt = lc_timer_enter_high_priority();
521 lc_timer_start(timer);
523 co_solve_heuristic_new(co);
525 lc_timer_stop(timer);
526 rt = lc_timer_leave_high_priority();
528 co_free_graph_structure(co);
529 co_free_ou_structure(co);
531 be_ifg_free(chordal_env->ifg);
535 elapsed_usec = lc_timer_elapsed_usec(timer);
536 /* calculating average */
537 elapsed_usec = elapsed_usec / tests;
539 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
544 for (i = 0; i<tests; i++) /* performance test with pointer */
546 used_memory = lc_get_heap_used_bytes();
548 rt = lc_timer_enter_high_priority();
549 lc_timer_start(timer);
551 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
553 lc_timer_stop(timer);
554 rt = lc_timer_leave_high_priority();
556 used_memory = lc_get_heap_used_bytes() - used_memory;
558 coloring_restore(&coloring);
561 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
562 co_build_ou_structure(co);
563 co_build_graph_structure(co);
565 rt = lc_timer_enter_high_priority();
566 lc_timer_start(timer);
568 co_solve_heuristic_new(co);
570 lc_timer_stop(timer);
571 rt = lc_timer_leave_high_priority();
573 co_free_graph_structure(co);
574 co_free_ou_structure(co);
576 be_ifg_free(chordal_env->ifg);
580 elapsed_usec = lc_timer_elapsed_usec(timer);
581 /* calculating average */
582 elapsed_usec = elapsed_usec / tests;
584 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
591 chordal_env->ifg = old_if;
594 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
596 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
597 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
598 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
602 fprintf(file, "graph G {\n\tgraph [");
604 cb->graph_attr(file, self);
605 fprintf(file, "];\n");
608 cb->at_begin(file, self);
610 be_ifg_foreach_node(ifg, nodes_it, n) {
611 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
612 int idx = get_irn_idx(n);
613 bitset_set(nodes, idx);
614 fprintf(file, "\tnode [");
616 cb->node_attr(file, self, n);
617 fprintf(file, "]; n%d;\n", idx);
621 /* Check, if all neighbours are indeed connected to the node. */
622 be_ifg_foreach_node(ifg, nodes_it, n) {
623 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
624 int n_idx = get_irn_idx(n);
625 int m_idx = get_irn_idx(m);
627 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
628 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
630 cb->edge_attr(file, self, n, m);
631 fprintf(file, "];\n");
637 cb->at_end(file, self);
639 fprintf(file, "}\n");
643 static void int_comp_rec(be_irg_t *birg, be_ifg_t *ifg, ir_node *n, bitset_t *seen)
645 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
648 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
649 if(!bitset_contains_irn(seen, m) && !arch_irn_is(birg->main_env->arch_env, m, ignore)) {
650 bitset_add_irn(seen, m);
651 int_comp_rec(birg, ifg, m, seen);
657 static int int_component_stat(be_irg_t *birg, be_ifg_t *ifg)
660 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
661 bitset_t *seen = bitset_irg_malloc(birg->irg);
665 be_ifg_foreach_node(ifg, nodes_it, n) {
666 if (! bitset_contains_irn(seen, n) && ! arch_irn_is(birg->main_env->arch_env, n, ignore)) {
668 bitset_add_irn(seen, n);
669 int_comp_rec(birg, ifg, n, seen);
677 void be_ifg_stat(be_irg_t *birg, be_ifg_t *ifg, be_ifg_stat_t *stat)
679 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
680 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
681 bitset_t *nodes = bitset_irg_malloc(birg->irg);
684 memset(stat, 0, sizeof(stat[0]));
686 be_ifg_foreach_node(ifg, nodes_it, n) {
688 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
689 bitset_add_irn(nodes, n);
690 stat->n_edges += !bitset_contains_irn(nodes, m);
694 stat->n_comps = int_component_stat(birg, ifg);
707 static int ifg_flavor = BE_IFG_STD;
709 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
710 { "std", BE_IFG_STD },
711 { "fast", BE_IFG_FAST },
712 { "clique", BE_IFG_CLIQUE },
713 { "pointer", BE_IFG_POINTER },
714 { "list", BE_IFG_LIST },
715 { "check", BE_IFG_CHECK },
719 static lc_opt_enum_int_var_t ifg_flavor_var = {
720 &ifg_flavor, ifg_flavor_items
723 static const lc_opt_table_entry_t be_ifg_options[] = {
724 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour", &ifg_flavor_var),
728 void be_init_ifg(void)
730 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
731 lc_opt_entry_t *ifg_grp = lc_opt_get_grp(be_grp, "ifg");
733 lc_opt_add_table(ifg_grp, be_ifg_options);
736 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ifg);
738 static FILE *be_ifg_open(const be_chordal_env_t *env, const char *prefix)
743 ir_snprintf(buf, sizeof(buf), "%s%F_%s.log", prefix, env->irg, env->cls->name);
744 result = fopen(buf, "wt");
746 panic("Couldn't open '%s' for writing.", buf);
752 static void check_ifg_implementations(const be_chordal_env_t *chordal_env)
757 f = be_ifg_open(chordal_env, "std");
758 ifg = be_ifg_std_new(chordal_env);
759 be_ifg_check_sorted_to_file(ifg, f);
763 f = be_ifg_open(chordal_env, "list");
764 ifg = be_ifg_list_new(chordal_env);
765 be_ifg_check_sorted_to_file(ifg, f);
769 f = be_ifg_open(chordal_env, "clique");
770 ifg = be_ifg_clique_new(chordal_env);
771 be_ifg_check_sorted_to_file(ifg, f);
775 f = be_ifg_open(chordal_env, "pointer");
776 ifg = be_ifg_pointer_new(chordal_env);
777 be_ifg_check_sorted_to_file(ifg, f);
782 be_ifg_t *be_create_ifg(const be_chordal_env_t *chordal_env)
784 be_ifg_t *ifg = NULL;
786 switch (ifg_flavor) {
789 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
792 ifg = be_ifg_std_new(chordal_env);
795 ifg = be_ifg_clique_new(chordal_env);
798 ifg = be_ifg_pointer_new(chordal_env);
801 ifg = be_ifg_list_new(chordal_env);
804 check_ifg_implementations(chordal_env);
805 /* Build the interference graph. */
806 ifg = be_ifg_std_new(chordal_env);