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;
84 return (void *) arch_get_irn_register(coloring->arch_env, irn);
87 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
89 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init, NULL);
95 static void get_irn_color(ir_node *irn, void *c)
97 coloring_t *coloring = c;
98 phase_get_or_set_irn_data(&coloring->ph, irn);
101 static void restore_irn_color(ir_node *irn, void *c)
103 coloring_t *coloring = c;
104 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
106 arch_set_irn_register(coloring->arch_env, irn, reg);
109 void coloring_save(coloring_t *c)
111 irg_walk_graph(c->irg, NULL, get_irn_color, c);
114 void coloring_restore(coloring_t *c)
116 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
119 void (be_ifg_free)(be_ifg_t *ifg)
121 ifg->impl->free(ifg);
124 int (be_ifg_connected)(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
126 return ifg->impl->connected(ifg, a, b);
129 ir_node *(be_ifg_neighbours_begin)(const be_ifg_t *ifg, void *iter, const ir_node *irn)
131 return ifg->impl->neighbours_begin(ifg, iter, irn);
134 ir_node *(be_ifg_neighbours_next)(const be_ifg_t *ifg, void *iter)
136 return ifg->impl->neighbours_next(ifg, iter);
139 void (be_ifg_neighbours_break)(const be_ifg_t *ifg, void *iter)
141 ifg->impl->neighbours_break(ifg, iter);
144 ir_node *(be_ifg_nodes_begin)(const be_ifg_t *ifg, void *iter)
146 return ifg->impl->nodes_begin(ifg, iter);
149 ir_node *(be_ifg_nodes_next)(const be_ifg_t *ifg, void *iter)
151 return ifg->impl->nodes_next(ifg, iter);
154 void (be_ifg_nodes_break)(const be_ifg_t *ifg, void *iter)
156 ifg->impl->nodes_break(ifg, iter);
159 int (be_ifg_cliques_begin)(const be_ifg_t *ifg, void *iter, ir_node **buf)
161 return ifg->impl->cliques_begin(ifg, iter, buf);
164 int (be_ifg_cliques_next)(const be_ifg_t *ifg, void *iter)
166 return ifg->impl->cliques_next(ifg, iter);
169 void (be_ifg_cliques_break)(const be_ifg_t *ifg, void *iter)
171 ifg->impl->cliques_break(ifg, iter);
174 int (be_ifg_degree)(const be_ifg_t *ifg, const ir_node *irn)
176 return ifg->impl->degree(ifg, irn);
180 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
182 int degree = be_ifg_degree(ifg, irn);
183 void *iter = be_ifg_neighbours_iter_alloca(ifg);
185 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
191 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
192 neighbours[i++] = curr;
194 for(i = 0; i < degree; ++i) {
195 for(j = 0; j < i; ++j)
196 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
207 void be_ifg_check(const be_ifg_t *ifg)
209 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
210 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
214 int neighbours_count = 0;
217 /* count all nodes */
218 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
219 be_ifg_foreach_node(ifg,iter1,n)
222 degree = be_ifg_degree(ifg, n);
223 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
226 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
228 /* Check, if all neighbours are indeed connected to the node. */
229 be_ifg_foreach_node(ifg, iter1, n)
231 ir_printf("\n%+F; ", n);
232 be_ifg_foreach_neighbour(ifg, iter2, n, m)
234 ir_printf("%+F; ", m);
236 if(!be_ifg_connected(ifg, n, m))
237 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
240 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
243 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
245 void *iter = be_ifg_nodes_iter_alloca(ifg);
249 be_ifg_foreach_node(ifg, iter, n)
257 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
259 const ir_node *node_a = *(ir_node **)a;
260 const ir_node *node_b = *(ir_node **)b;
262 long nr_a = get_irn_node_nr(node_a);
263 long nr_b = get_irn_node_nr(node_b);
265 return QSORT_CMP(nr_a, nr_b);
268 void be_ifg_check_sorted(const be_ifg_t *ifg)
270 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
271 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
274 const int node_count = be_ifg_check_get_node_count(ifg);
277 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
279 be_ifg_foreach_node(ifg, iter1, n)
281 if(!node_is_in_irgs_storage(ifg->env->irg, n))
283 ir_printf("+%F is in ifg but not in the current irg!", n);
284 assert (node_is_in_irgs_storage(ifg->env->irg, n));
291 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
293 for (i = 0; i < node_count; i++)
295 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
300 degree = be_ifg_degree(ifg, all_nodes[i]);
302 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
308 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
310 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
312 for(k = 0; k < j; k++)
314 ir_printf("%+F, ", neighbours[k]);
326 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
328 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
329 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
332 const int node_count = be_ifg_check_get_node_count(ifg);
335 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
337 be_ifg_foreach_node(ifg, iter1, n)
339 if(!node_is_in_irgs_storage(ifg->env->irg, n))
341 ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
342 assert (node_is_in_irgs_storage(ifg->env->irg, n));
349 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
351 for (i = 0; i < node_count; i++)
353 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
358 degree = be_ifg_degree(ifg, all_nodes[i]);
360 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
366 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
368 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
370 for(k = 0; k < j; k++)
372 ir_fprintf (f,"%+F, ", neighbours[k]);
384 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
386 int tests = BE_CH_PERFORMANCETEST_COUNT;
394 be_ifg_t *old_if = chordal_env->ifg;
396 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
397 unsigned long elapsed_usec = 0;
399 if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
401 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
402 coloring_save(&coloring);
404 lc_timer_reset(timer);
406 for (i = 0; i<tests; i++) /* performance test with std */
409 used_memory = lc_get_heap_used_bytes();
411 rt = lc_timer_enter_high_priority();
412 lc_timer_start(timer);
414 chordal_env->ifg = be_ifg_std_new(chordal_env);
416 lc_timer_stop(timer);
417 rt = lc_timer_leave_high_priority();
419 used_memory = lc_get_heap_used_bytes() - used_memory;
421 coloring_restore(&coloring);
424 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
425 co_build_ou_structure(co);
426 co_build_graph_structure(co);
428 rt = lc_timer_enter_high_priority();
429 lc_timer_start(timer);
431 co_solve_heuristic_new(co);
433 lc_timer_stop(timer);
434 rt = lc_timer_leave_high_priority();
436 co_free_graph_structure(co);
437 co_free_ou_structure(co);
439 be_ifg_free(chordal_env->ifg);
443 elapsed_usec = lc_timer_elapsed_usec(timer);
444 /* calculating average */
445 elapsed_usec = elapsed_usec / tests;
447 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
452 for (i = 0; i<tests; i++) /* performance test with clique */
454 used_memory = lc_get_heap_used_bytes();
456 rt = lc_timer_enter_high_priority();
457 lc_timer_start(timer);
459 chordal_env->ifg = be_ifg_clique_new(chordal_env);
461 lc_timer_stop(timer);
462 rt = lc_timer_leave_high_priority();
464 used_memory = lc_get_heap_used_bytes() - used_memory;
466 coloring_restore(&coloring);
469 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
470 co_build_ou_structure(co);
471 co_build_graph_structure(co);
473 rt = lc_timer_enter_high_priority();
474 lc_timer_start(timer);
476 co_solve_heuristic_new(co);
478 lc_timer_stop(timer);
479 rt = lc_timer_leave_high_priority();
481 co_free_graph_structure(co);
482 co_free_ou_structure(co);
484 be_ifg_free(chordal_env->ifg);
488 elapsed_usec = lc_timer_elapsed_usec(timer);
489 /* calculating average */
490 elapsed_usec = elapsed_usec / tests;
492 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
497 for (i = 0; i<tests; i++) /* performance test with list */
499 used_memory = lc_get_heap_used_bytes();
501 rt = lc_timer_enter_high_priority();
502 lc_timer_start(timer);
504 chordal_env->ifg = be_ifg_list_new(chordal_env);
506 lc_timer_stop(timer);
507 rt = lc_timer_leave_high_priority();
509 used_memory = lc_get_heap_used_bytes() - used_memory;
511 coloring_restore(&coloring);
514 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
515 co_build_ou_structure(co);
516 co_build_graph_structure(co);
518 rt = lc_timer_enter_high_priority();
519 lc_timer_start(timer);
521 co_solve_heuristic_new(co);
523 lc_timer_stop(timer);
524 rt = lc_timer_leave_high_priority();
526 co_free_graph_structure(co);
527 co_free_ou_structure(co);
529 be_ifg_free(chordal_env->ifg);
533 elapsed_usec = lc_timer_elapsed_usec(timer);
534 /* calculating average */
535 elapsed_usec = elapsed_usec / tests;
537 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
542 for (i = 0; i<tests; i++) /* performance test with pointer */
544 used_memory = lc_get_heap_used_bytes();
546 rt = lc_timer_enter_high_priority();
547 lc_timer_start(timer);
549 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
551 lc_timer_stop(timer);
552 rt = lc_timer_leave_high_priority();
554 used_memory = lc_get_heap_used_bytes() - used_memory;
556 coloring_restore(&coloring);
559 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
560 co_build_ou_structure(co);
561 co_build_graph_structure(co);
563 rt = lc_timer_enter_high_priority();
564 lc_timer_start(timer);
566 co_solve_heuristic_new(co);
568 lc_timer_stop(timer);
569 rt = lc_timer_leave_high_priority();
571 co_free_graph_structure(co);
572 co_free_ou_structure(co);
574 be_ifg_free(chordal_env->ifg);
578 elapsed_usec = lc_timer_elapsed_usec(timer);
579 /* calculating average */
580 elapsed_usec = elapsed_usec / tests;
582 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
589 chordal_env->ifg = old_if;
592 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
594 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
595 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
596 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
600 fprintf(file, "graph G {\n\tgraph [");
602 cb->graph_attr(file, self);
603 fprintf(file, "];\n");
606 cb->at_begin(file, self);
608 be_ifg_foreach_node(ifg, nodes_it, n) {
609 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
610 int idx = get_irn_idx(n);
611 bitset_set(nodes, idx);
612 fprintf(file, "\tnode [");
614 cb->node_attr(file, self, n);
615 fprintf(file, "]; n%d;\n", idx);
619 /* Check, if all neighbours are indeed connected to the node. */
620 be_ifg_foreach_node(ifg, nodes_it, n) {
621 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
622 int n_idx = get_irn_idx(n);
623 int m_idx = get_irn_idx(m);
625 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
626 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
628 cb->edge_attr(file, self, n, m);
629 fprintf(file, "];\n");
635 cb->at_end(file, self);
637 fprintf(file, "}\n");
641 static void int_comp_rec(be_irg_t *birg, be_ifg_t *ifg, ir_node *n, bitset_t *seen)
643 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
646 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
647 if(!bitset_contains_irn(seen, m) && !arch_irn_is(birg->main_env->arch_env, m, ignore)) {
648 bitset_add_irn(seen, m);
649 int_comp_rec(birg, ifg, m, seen);
655 static int int_component_stat(be_irg_t *birg, be_ifg_t *ifg)
658 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
659 bitset_t *seen = bitset_irg_malloc(birg->irg);
663 be_ifg_foreach_node(ifg, nodes_it, n) {
664 if (! bitset_contains_irn(seen, n) && ! arch_irn_is(birg->main_env->arch_env, n, ignore)) {
666 bitset_add_irn(seen, n);
667 int_comp_rec(birg, ifg, n, seen);
675 void be_ifg_stat(be_irg_t *birg, be_ifg_t *ifg, be_ifg_stat_t *stat)
677 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
678 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
679 bitset_t *nodes = bitset_irg_malloc(birg->irg);
682 memset(stat, 0, sizeof(stat[0]));
684 be_ifg_foreach_node(ifg, nodes_it, n) {
686 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
687 bitset_add_irn(nodes, n);
688 stat->n_edges += !bitset_contains_irn(nodes, m);
692 stat->n_comps = int_component_stat(birg, ifg);
705 static int ifg_flavor = BE_IFG_STD;
707 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
708 { "std", BE_IFG_STD },
709 { "fast", BE_IFG_FAST },
710 { "clique", BE_IFG_CLIQUE },
711 { "pointer", BE_IFG_POINTER },
712 { "list", BE_IFG_LIST },
713 { "check", BE_IFG_CHECK },
717 static lc_opt_enum_int_var_t ifg_flavor_var = {
718 &ifg_flavor, ifg_flavor_items
721 static const lc_opt_table_entry_t be_ifg_options[] = {
722 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour", &ifg_flavor_var),
726 void be_init_ifg(void)
728 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
729 lc_opt_entry_t *ifg_grp = lc_opt_get_grp(be_grp, "ifg");
731 lc_opt_add_table(ifg_grp, be_ifg_options);
734 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ifg);
736 static FILE *be_ifg_open(const be_chordal_env_t *env, const char *prefix)
741 ir_snprintf(buf, sizeof(buf), "%s%F_%s.log", prefix, env->irg, env->cls->name);
742 result = fopen(buf, "wt");
744 panic("Couldn't open '%s' for writing.", buf);
750 static void check_ifg_implementations(const be_chordal_env_t *chordal_env)
755 f = be_ifg_open(chordal_env, "std");
756 ifg = be_ifg_std_new(chordal_env);
757 be_ifg_check_sorted_to_file(ifg, f);
761 f = be_ifg_open(chordal_env, "list");
762 ifg = be_ifg_list_new(chordal_env);
763 be_ifg_check_sorted_to_file(ifg, f);
767 f = be_ifg_open(chordal_env, "clique");
768 ifg = be_ifg_clique_new(chordal_env);
769 be_ifg_check_sorted_to_file(ifg, f);
773 f = be_ifg_open(chordal_env, "pointer");
774 ifg = be_ifg_pointer_new(chordal_env);
775 be_ifg_check_sorted_to_file(ifg, f);
780 be_ifg_t *be_create_ifg(const be_chordal_env_t *chordal_env)
782 be_ifg_t *ifg = NULL;
784 switch (ifg_flavor) {
787 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
790 ifg = be_ifg_std_new(chordal_env);
793 ifg = be_ifg_clique_new(chordal_env);
796 ifg = be_ifg_pointer_new(chordal_env);
799 ifg = be_ifg_list_new(chordal_env);
802 check_ifg_implementations(chordal_env);
803 /* Build the interference graph. */
804 ifg = be_ifg_std_new(chordal_env);