2 * Copyright (C) 1995-2008 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
32 #include "lc_opts_enum.h"
42 #include "beifg_impl.h"
43 #include "irphase_t.h"
47 #include "becopystat.h"
48 #include "becopyopt.h"
52 /** Defines values for the ifg performance test */
53 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
54 #define BE_CH_PERFORMANCETEST_COUNT (500)
56 typedef struct _coloring_t coloring_t;
63 size_t (be_ifg_nodes_iter_size)(const be_ifg_t *ifg)
65 return ifg->impl->nodes_iter_size;
68 size_t (be_ifg_neighbours_iter_size)(const be_ifg_t *ifg)
70 return ifg->impl->neighbours_iter_size;
73 size_t (be_ifg_cliques_iter_size)(const be_ifg_t *ifg)
75 return ifg->impl->cliques_iter_size;
78 static void *regs_irn_data_init(ir_phase *ph, const ir_node *irn, void *data)
83 return (void*)arch_get_irn_register(irn);
86 static coloring_t *coloring_init(coloring_t *c, ir_graph *irg)
88 phase_init(&c->ph, irg, 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(irn, reg);
107 static void coloring_save(coloring_t *c)
109 irg_walk_graph(c->irg, NULL, get_irn_color, c);
112 static void coloring_restore(coloring_t *c)
114 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
117 void (be_ifg_free)(be_ifg_t *ifg)
119 ifg->impl->free(ifg);
122 int (be_ifg_connected)(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
124 return ifg->impl->connected(ifg, a, b);
127 ir_node *(be_ifg_neighbours_begin)(const be_ifg_t *ifg, void *iter, const ir_node *irn)
129 return ifg->impl->neighbours_begin(ifg, iter, irn);
132 ir_node *(be_ifg_neighbours_next)(const be_ifg_t *ifg, void *iter)
134 return ifg->impl->neighbours_next(ifg, iter);
137 void (be_ifg_neighbours_break)(const be_ifg_t *ifg, void *iter)
139 ifg->impl->neighbours_break(ifg, iter);
142 ir_node *(be_ifg_nodes_begin)(const be_ifg_t *ifg, void *iter)
144 return ifg->impl->nodes_begin(ifg, iter);
147 ir_node *(be_ifg_nodes_next)(const be_ifg_t *ifg, void *iter)
149 return ifg->impl->nodes_next(ifg, iter);
152 void (be_ifg_nodes_break)(const be_ifg_t *ifg, void *iter)
154 ifg->impl->nodes_break(ifg, iter);
157 int (be_ifg_cliques_begin)(const be_ifg_t *ifg, void *iter, ir_node **buf)
159 return ifg->impl->cliques_begin(ifg, iter, buf);
162 int (be_ifg_cliques_next)(const be_ifg_t *ifg, void *iter)
164 return ifg->impl->cliques_next(ifg, iter);
167 void (be_ifg_cliques_break)(const be_ifg_t *ifg, void *iter)
169 ifg->impl->cliques_break(ifg, iter);
172 int (be_ifg_degree)(const be_ifg_t *ifg, const ir_node *irn)
174 return ifg->impl->degree(ifg, irn);
178 void be_ifg_check(const be_ifg_t *ifg)
180 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
181 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
185 int neighbours_count = 0;
188 /* count all nodes */
189 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
190 be_ifg_foreach_node(ifg,iter1,n)
193 degree = be_ifg_degree(ifg, n);
194 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
197 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
199 /* Check, if all neighbours are indeed connected to the node. */
200 be_ifg_foreach_node(ifg, iter1, n)
202 ir_printf("\n%+F; ", n);
203 be_ifg_foreach_neighbour(ifg, iter2, n, m)
205 ir_printf("%+F; ", m);
207 if (!be_ifg_connected(ifg, n, m))
208 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
211 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
214 static int be_ifg_check_get_node_count(const be_ifg_t *ifg)
216 void *iter = be_ifg_nodes_iter_alloca(ifg);
220 be_ifg_foreach_node(ifg, iter, n)
228 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
230 const ir_node *node_a = *(ir_node **)a;
231 const ir_node *node_b = *(ir_node **)b;
233 long nr_a = get_irn_idx(node_a);
234 long nr_b = get_irn_idx(node_b);
236 return QSORT_CMP(nr_a, nr_b);
239 void be_ifg_check_sorted(const be_ifg_t *ifg)
241 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
242 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
245 const int node_count = be_ifg_check_get_node_count(ifg);
248 ir_node **all_nodes = XMALLOCN(ir_node*, node_count);
250 be_ifg_foreach_node(ifg, iter1, n)
252 if (!node_is_in_irgs_storage(ifg->env->irg, n))
254 ir_printf("+%F is in ifg but not in the current irg!", n);
255 assert (node_is_in_irgs_storage(ifg->env->irg, n));
262 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
264 for (i = 0; i < node_count; i++)
266 ir_node **neighbours = XMALLOCN(ir_node*, node_count);
271 degree = be_ifg_degree(ifg, all_nodes[i]);
273 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
279 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
281 ir_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
283 for (k = 0; k < j; k++)
285 ir_printf("%+F, ", neighbours[k]);
297 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
299 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
300 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
303 const int node_count = be_ifg_check_get_node_count(ifg);
306 ir_node **all_nodes = XMALLOCN(ir_node*, node_count);
308 be_ifg_foreach_node(ifg, iter1, n)
310 if (!node_is_in_irgs_storage(ifg->env->irg, n))
312 ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
313 assert (node_is_in_irgs_storage(ifg->env->irg, n));
320 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
322 for (i = 0; i < node_count; i++)
324 ir_node **neighbours = XMALLOCN(ir_node*, node_count);
329 degree = be_ifg_degree(ifg, all_nodes[i]);
331 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
337 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
339 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
341 for (k = 0; k < j; k++)
343 ir_fprintf (f,"%+F, ", neighbours[k]);
355 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
357 int tests = BE_CH_PERFORMANCETEST_COUNT;
365 be_ifg_t *old_if = chordal_env->ifg;
367 ir_timer_t *timer = ir_timer_new();
368 unsigned long elapsed_usec = 0;
370 if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
372 coloring_init(&coloring, chordal_env->irg);
373 coloring_save(&coloring);
375 ir_timer_reset(timer);
377 for (i = 0; i<tests; i++) /* performance test with std */
380 used_memory = ir_get_heap_used_bytes();
382 rt = ir_timer_enter_high_priority();
383 ir_timer_start(timer);
385 chordal_env->ifg = be_ifg_std_new(chordal_env);
387 ir_timer_stop(timer);
388 rt = ir_timer_leave_high_priority();
390 used_memory = ir_get_heap_used_bytes() - used_memory;
392 coloring_restore(&coloring);
395 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
396 co_build_ou_structure(co);
397 co_build_graph_structure(co);
399 rt = ir_timer_enter_high_priority();
400 ir_timer_start(timer);
402 co_solve_heuristic_new(co);
404 ir_timer_stop(timer);
405 rt = ir_timer_leave_high_priority();
407 co_free_graph_structure(co);
408 co_free_ou_structure(co);
410 be_ifg_free(chordal_env->ifg);
414 elapsed_usec = ir_timer_elapsed_usec(timer);
415 /* calculating average */
416 elapsed_usec = elapsed_usec / tests;
418 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
423 for (i = 0; i<tests; i++) /* performance test with clique */
425 used_memory = ir_get_heap_used_bytes();
427 rt = ir_timer_enter_high_priority();
428 ir_timer_start(timer);
430 chordal_env->ifg = be_ifg_clique_new(chordal_env);
432 ir_timer_stop(timer);
433 rt = ir_timer_leave_high_priority();
435 used_memory = ir_get_heap_used_bytes() - used_memory;
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 = ir_timer_enter_high_priority();
445 ir_timer_start(timer);
447 co_solve_heuristic_new(co);
449 ir_timer_stop(timer);
450 rt = ir_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 = ir_timer_elapsed_usec(timer);
460 /* calculating average */
461 elapsed_usec = elapsed_usec / tests;
463 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
468 for (i = 0; i<tests; i++) /* performance test with list */
470 used_memory = ir_get_heap_used_bytes();
472 rt = ir_timer_enter_high_priority();
473 ir_timer_start(timer);
475 chordal_env->ifg = be_ifg_list_new(chordal_env);
477 ir_timer_stop(timer);
478 rt = ir_timer_leave_high_priority();
480 used_memory = ir_get_heap_used_bytes() - used_memory;
482 coloring_restore(&coloring);
485 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
486 co_build_ou_structure(co);
487 co_build_graph_structure(co);
489 rt = ir_timer_enter_high_priority();
490 ir_timer_start(timer);
492 co_solve_heuristic_new(co);
494 ir_timer_stop(timer);
495 rt = ir_timer_leave_high_priority();
497 co_free_graph_structure(co);
498 co_free_ou_structure(co);
500 be_ifg_free(chordal_env->ifg);
504 elapsed_usec = ir_timer_elapsed_usec(timer);
505 /* calculating average */
506 elapsed_usec = elapsed_usec / tests;
508 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
513 for (i = 0; i<tests; i++) /* performance test with pointer */
515 used_memory = ir_get_heap_used_bytes();
517 rt = ir_timer_enter_high_priority();
518 ir_timer_start(timer);
520 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
522 ir_timer_stop(timer);
523 rt = ir_timer_leave_high_priority();
525 used_memory = ir_get_heap_used_bytes() - used_memory;
527 coloring_restore(&coloring);
530 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
531 co_build_ou_structure(co);
532 co_build_graph_structure(co);
534 rt = ir_timer_enter_high_priority();
535 ir_timer_start(timer);
537 co_solve_heuristic_new(co);
539 ir_timer_stop(timer);
540 rt = ir_timer_leave_high_priority();
542 co_free_graph_structure(co);
543 co_free_ou_structure(co);
545 be_ifg_free(chordal_env->ifg);
549 elapsed_usec = ir_timer_elapsed_usec(timer);
550 /* calculating average */
551 elapsed_usec = elapsed_usec / tests;
553 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
560 chordal_env->ifg = old_if;
562 ir_timer_free(timer);
565 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
567 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
568 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
569 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
573 fprintf(file, "graph G {\n\tgraph [");
575 cb->graph_attr(file, self);
576 fprintf(file, "];\n");
579 cb->at_begin(file, self);
581 be_ifg_foreach_node(ifg, nodes_it, n) {
582 if (cb->is_dump_node && cb->is_dump_node(self, n)) {
583 int idx = get_irn_idx(n);
584 bitset_set(nodes, idx);
585 fprintf(file, "\tnode [");
587 cb->node_attr(file, self, n);
588 fprintf(file, "]; n%d;\n", idx);
592 /* Check, if all neighbours are indeed connected to the node. */
593 be_ifg_foreach_node(ifg, nodes_it, n) {
594 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
595 int n_idx = get_irn_idx(n);
596 int m_idx = get_irn_idx(m);
598 if (n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
599 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
601 cb->edge_attr(file, self, n, m);
602 fprintf(file, "];\n");
608 cb->at_end(file, self);
610 fprintf(file, "}\n");
614 static void int_comp_rec(be_ifg_t *ifg, ir_node *n, bitset_t *seen)
616 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
619 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
620 if (bitset_contains_irn(seen, m))
623 if (arch_get_register_req_out(m)->type & arch_register_req_type_ignore)
626 bitset_add_irn(seen, m);
627 int_comp_rec(ifg, m, seen);
632 static int int_component_stat(be_irg_t *birg, be_ifg_t *ifg)
635 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
636 bitset_t *seen = bitset_irg_malloc(birg->irg);
640 be_ifg_foreach_node(ifg, nodes_it, n) {
641 if (bitset_contains_irn(seen, n))
644 if (arch_get_register_req_out(n)->type & arch_register_req_type_ignore)
648 bitset_add_irn(seen, n);
649 int_comp_rec(ifg, n, seen);
656 void be_ifg_stat(be_irg_t *birg, be_ifg_t *ifg, be_ifg_stat_t *stat)
658 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
659 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
660 bitset_t *nodes = bitset_irg_malloc(birg->irg);
663 memset(stat, 0, sizeof(stat[0]));
665 be_ifg_foreach_node(ifg, nodes_it, n) {
667 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
668 bitset_add_irn(nodes, n);
669 stat->n_edges += !bitset_contains_irn(nodes, m);
673 stat->n_comps = int_component_stat(birg, ifg);
686 static int ifg_flavor = BE_IFG_STD;
688 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
689 { "std", BE_IFG_STD },
690 { "fast", BE_IFG_FAST },
691 { "clique", BE_IFG_CLIQUE },
692 { "pointer", BE_IFG_POINTER },
693 { "list", BE_IFG_LIST },
694 { "check", BE_IFG_CHECK },
698 static lc_opt_enum_int_var_t ifg_flavor_var = {
699 &ifg_flavor, ifg_flavor_items
702 static const lc_opt_table_entry_t be_ifg_options[] = {
703 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour", &ifg_flavor_var),
707 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ifg);
708 void be_init_ifg(void)
710 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
711 lc_opt_entry_t *ifg_grp = lc_opt_get_grp(be_grp, "ifg");
713 lc_opt_add_table(ifg_grp, be_ifg_options);
717 static FILE *be_ifg_open(const be_chordal_env_t *env, const char *prefix)
722 ir_snprintf(buf, sizeof(buf), "%s%F_%s.log", prefix, env->irg, env->cls->name);
723 result = fopen(buf, "wt");
724 if (result == NULL) {
725 panic("Couldn't open '%s' for writing.", buf);
731 static void check_ifg_implementations(const be_chordal_env_t *chordal_env)
736 f = be_ifg_open(chordal_env, "std");
737 ifg = be_ifg_std_new(chordal_env);
738 be_ifg_check_sorted_to_file(ifg, f);
742 f = be_ifg_open(chordal_env, "list");
743 ifg = be_ifg_list_new(chordal_env);
744 be_ifg_check_sorted_to_file(ifg, f);
748 f = be_ifg_open(chordal_env, "clique");
749 ifg = be_ifg_clique_new(chordal_env);
750 be_ifg_check_sorted_to_file(ifg, f);
754 f = be_ifg_open(chordal_env, "pointer");
755 ifg = be_ifg_pointer_new(chordal_env);
756 be_ifg_check_sorted_to_file(ifg, f);
761 be_ifg_t *be_create_ifg(const be_chordal_env_t *chordal_env)
763 be_ifg_t *ifg = NULL;
765 switch (ifg_flavor) {
767 panic("invalid ifg flavour selected");
770 ifg = be_ifg_std_new(chordal_env);
773 ifg = be_ifg_clique_new(chordal_env);
776 ifg = be_ifg_pointer_new(chordal_env);
779 ifg = be_ifg_list_new(chordal_env);
782 check_ifg_implementations(chordal_env);
783 /* Build the interference graph. */
784 ifg = be_ifg_std_new(chordal_env);