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
23 * @author Sebastian Hack
25 * Copyright (C) 2005 Universitaet Karlsruhe
26 * Released under the GPL
34 #include <libcore/lc_opts.h>
35 #include <libcore/lc_opts_enum.h>
36 #include <libcore/lc_timing.h>
45 #include "beifg_impl.h"
46 #include "irphase_t.h"
50 #include "becopystat.h"
51 #include "becopyopt.h"
55 /** Defines values for the ifg performance test */
56 #define BE_CH_PERFORMANCETEST_MIN_NODES (50)
57 #define BE_CH_PERFORMANCETEST_COUNT (500)
59 typedef struct _coloring_t coloring_t;
63 const arch_env_t *arch_env;
67 size_t (be_ifg_nodes_iter_size)(const be_ifg_t *ifg)
69 return ifg->impl->nodes_iter_size;
72 size_t (be_ifg_neighbours_iter_size)(const be_ifg_t *ifg)
74 return ifg->impl->neighbours_iter_size;
77 size_t (be_ifg_cliques_iter_size)(const be_ifg_t *ifg)
79 return ifg->impl->cliques_iter_size;
82 static void *regs_irn_data_init(ir_phase *ph, ir_node *irn, void *data)
84 coloring_t *coloring = (coloring_t *) ph;
85 return (void *) arch_get_irn_register(coloring->arch_env, irn);
88 coloring_t *coloring_init(coloring_t *c, ir_graph *irg, const arch_env_t *aenv)
90 phase_init(&c->ph, "regs_map", irg, PHASE_DEFAULT_GROWTH, regs_irn_data_init, NULL);
96 static void get_irn_color(ir_node *irn, void *c)
98 coloring_t *coloring = c;
99 phase_get_or_set_irn_data(&coloring->ph, irn);
102 static void restore_irn_color(ir_node *irn, void *c)
104 coloring_t *coloring = c;
105 const arch_register_t *reg = phase_get_irn_data(&coloring->ph, irn);
107 arch_set_irn_register(coloring->arch_env, irn, reg);
110 void coloring_save(coloring_t *c)
112 irg_walk_graph(c->irg, NULL, get_irn_color, c);
115 void coloring_restore(coloring_t *c)
117 irg_walk_graph(c->irg, NULL, restore_irn_color, c);
120 void (be_ifg_free)(be_ifg_t *ifg)
122 ifg->impl->free(ifg);
125 int (be_ifg_connected)(const be_ifg_t *ifg, const ir_node *a, const ir_node *b)
127 return ifg->impl->connected(ifg, a, b);
130 ir_node *(be_ifg_neighbours_begin)(const be_ifg_t *ifg, void *iter, const ir_node *irn)
132 return ifg->impl->neighbours_begin(ifg, iter, irn);
135 ir_node *(be_ifg_neighbours_next)(const be_ifg_t *ifg, void *iter)
137 return ifg->impl->neighbours_next(ifg, iter);
140 void (be_ifg_neighbours_break)(const be_ifg_t *ifg, void *iter)
142 ifg->impl->neighbours_break(ifg, iter);
145 ir_node *(be_ifg_nodes_begin)(const be_ifg_t *ifg, void *iter)
147 return ifg->impl->nodes_begin(ifg, iter);
150 ir_node *(be_ifg_nodes_next)(const be_ifg_t *ifg, void *iter)
152 return ifg->impl->nodes_next(ifg, iter);
155 void (be_ifg_nodes_break)(const be_ifg_t *ifg, void *iter)
157 ifg->impl->nodes_break(ifg, iter);
160 int (be_ifg_cliques_begin)(const be_ifg_t *ifg, void *iter, ir_node **buf)
162 return ifg->impl->cliques_begin(ifg, iter, buf);
165 int (be_ifg_cliques_next)(const be_ifg_t *ifg, void *iter)
167 return ifg->impl->cliques_next(ifg, iter);
170 void (be_ifg_cliques_break)(const be_ifg_t *ifg, void *iter)
172 ifg->impl->cliques_break(ifg, iter);
175 int (be_ifg_degree)(const be_ifg_t *ifg, const ir_node *irn)
177 return ifg->impl->degree(ifg, irn);
181 int be_ifg_is_simplicial(const be_ifg_t *ifg, const ir_node *irn)
183 int degree = be_ifg_degree(ifg, irn);
184 void *iter = be_ifg_neighbours_iter_alloca(ifg);
186 ir_node **neighbours = xmalloc(degree * sizeof(neighbours[0]));
192 be_ifg_foreach_neighbour(ifg, iter, irn, curr)
193 neighbours[i++] = curr;
195 for(i = 0; i < degree; ++i) {
196 for(j = 0; j < i; ++j)
197 if(!be_ifg_connected(ifg, neighbours[i], neighbours[j])) {
208 void be_ifg_check(const be_ifg_t *ifg)
210 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
211 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
215 int neighbours_count = 0;
218 /* count all nodes */
219 ir_printf("\n\nFound the following nodes in the graph %+F:\n\n", current_ir_graph);
220 be_ifg_foreach_node(ifg,iter1,n)
223 degree = be_ifg_degree(ifg, n);
224 ir_printf("%d. %+F with degree: %d\n", node_count, n, degree);
227 ir_printf("\n\nNumber of nodes: %d\n\n", node_count);
229 /* Check, if all neighbours are indeed connected to the node. */
230 be_ifg_foreach_node(ifg, iter1, n)
232 ir_printf("\n%+F; ", n);
233 be_ifg_foreach_neighbour(ifg, iter2, n, m)
235 ir_printf("%+F; ", m);
237 if(!be_ifg_connected(ifg, n, m))
238 ir_fprintf(stderr, "%+F is a neighbour of %+F but they are not connected!\n", n, m);
241 ir_printf("\n\nFound %d nodes in the 'check neighbour section'\n", neighbours_count);
244 int be_ifg_check_get_node_count(const be_ifg_t *ifg)
246 void *iter = be_ifg_nodes_iter_alloca(ifg);
250 be_ifg_foreach_node(ifg, iter, n)
258 static int be_ifg_check_cmp_nodes(const void *a, const void *b)
260 const ir_node *node_a = *(ir_node **)a;
261 const ir_node *node_b = *(ir_node **)b;
263 long nr_a = get_irn_node_nr(node_a);
264 long nr_b = get_irn_node_nr(node_b);
266 return QSORT_CMP(nr_a, nr_b);
269 void be_ifg_check_sorted(const be_ifg_t *ifg)
271 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
272 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
275 const int node_count = be_ifg_check_get_node_count(ifg);
278 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
280 be_ifg_foreach_node(ifg, iter1, n)
282 if(!node_is_in_irgs_storage(ifg->env->irg, n))
284 ir_printf("+%F is in ifg but not in the current irg!", n);
285 assert (node_is_in_irgs_storage(ifg->env->irg, 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_printf("%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
313 for(k = 0; k < j; k++)
315 ir_printf("%+F, ", neighbours[k]);
327 void be_ifg_check_sorted_to_file(const be_ifg_t *ifg, FILE *f)
329 void *iter1 = be_ifg_nodes_iter_alloca(ifg);
330 void *iter2 = be_ifg_neighbours_iter_alloca(ifg);
333 const int node_count = be_ifg_check_get_node_count(ifg);
336 ir_node **all_nodes = xmalloc(node_count * sizeof(all_nodes[0]));
338 be_ifg_foreach_node(ifg, iter1, n)
340 if(!node_is_in_irgs_storage(ifg->env->irg, n))
342 ir_fprintf (f,"+%F is in ifg but not in the current irg!",n);
343 assert (node_is_in_irgs_storage(ifg->env->irg, n));
350 qsort(all_nodes, node_count, sizeof(all_nodes[0]), be_ifg_check_cmp_nodes);
352 for (i = 0; i < node_count; i++)
354 ir_node **neighbours = xmalloc(node_count * sizeof(neighbours[0]));
359 degree = be_ifg_degree(ifg, all_nodes[i]);
361 be_ifg_foreach_neighbour(ifg, iter2, all_nodes[i], m)
367 qsort(neighbours, j, sizeof(neighbours[0]), be_ifg_check_cmp_nodes);
369 ir_fprintf (f,"%d. %+F's neighbours(%d): ", i+1, all_nodes[i], degree);
371 for(k = 0; k < j; k++)
373 ir_fprintf (f,"%+F, ", neighbours[k]);
385 void be_ifg_check_performance(be_chordal_env_t *chordal_env)
387 int tests = BE_CH_PERFORMANCETEST_COUNT;
395 be_ifg_t *old_if = chordal_env->ifg;
397 lc_timer_t *timer = lc_timer_register("getTime","get Time of copy minimization using the ifg");
398 unsigned long elapsed_usec = 0;
400 if (get_irg_estimated_node_cnt(chordal_env->irg) >= BE_CH_PERFORMANCETEST_MIN_NODES)
402 coloring_init(&coloring, chordal_env->irg, chordal_env->birg->main_env->arch_env);
403 coloring_save(&coloring);
405 lc_timer_reset(timer);
407 for (i = 0; i<tests; i++) /* performance test with std */
410 used_memory = lc_get_heap_used_bytes();
412 rt = lc_timer_enter_high_priority();
413 lc_timer_start(timer);
415 chordal_env->ifg = be_ifg_std_new(chordal_env);
417 lc_timer_stop(timer);
418 rt = lc_timer_leave_high_priority();
420 used_memory = lc_get_heap_used_bytes() - used_memory;
422 coloring_restore(&coloring);
425 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
426 co_build_ou_structure(co);
427 co_build_graph_structure(co);
429 rt = lc_timer_enter_high_priority();
430 lc_timer_start(timer);
432 co_solve_heuristic_new(co);
434 lc_timer_stop(timer);
435 rt = lc_timer_leave_high_priority();
437 co_free_graph_structure(co);
438 co_free_ou_structure(co);
440 be_ifg_free(chordal_env->ifg);
444 elapsed_usec = lc_timer_elapsed_usec(timer);
445 /* calculating average */
446 elapsed_usec = elapsed_usec / tests;
448 ir_printf("\nstd:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
453 for (i = 0; i<tests; i++) /* performance test with clique */
455 used_memory = lc_get_heap_used_bytes();
457 rt = lc_timer_enter_high_priority();
458 lc_timer_start(timer);
460 chordal_env->ifg = be_ifg_clique_new(chordal_env);
462 lc_timer_stop(timer);
463 rt = lc_timer_leave_high_priority();
465 used_memory = lc_get_heap_used_bytes() - used_memory;
467 coloring_restore(&coloring);
470 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
471 co_build_ou_structure(co);
472 co_build_graph_structure(co);
474 rt = lc_timer_enter_high_priority();
475 lc_timer_start(timer);
477 co_solve_heuristic_new(co);
479 lc_timer_stop(timer);
480 rt = lc_timer_leave_high_priority();
482 co_free_graph_structure(co);
483 co_free_ou_structure(co);
485 be_ifg_free(chordal_env->ifg);
489 elapsed_usec = lc_timer_elapsed_usec(timer);
490 /* calculating average */
491 elapsed_usec = elapsed_usec / tests;
493 ir_printf("\nclique:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
498 for (i = 0; i<tests; i++) /* performance test with list */
500 used_memory = lc_get_heap_used_bytes();
502 rt = lc_timer_enter_high_priority();
503 lc_timer_start(timer);
505 chordal_env->ifg = be_ifg_list_new(chordal_env);
507 lc_timer_stop(timer);
508 rt = lc_timer_leave_high_priority();
510 used_memory = lc_get_heap_used_bytes() - used_memory;
512 coloring_restore(&coloring);
515 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
516 co_build_ou_structure(co);
517 co_build_graph_structure(co);
519 rt = lc_timer_enter_high_priority();
520 lc_timer_start(timer);
522 co_solve_heuristic_new(co);
524 lc_timer_stop(timer);
525 rt = lc_timer_leave_high_priority();
527 co_free_graph_structure(co);
528 co_free_ou_structure(co);
530 be_ifg_free(chordal_env->ifg);
534 elapsed_usec = lc_timer_elapsed_usec(timer);
535 /* calculating average */
536 elapsed_usec = elapsed_usec / tests;
538 ir_printf("\nlist:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
543 for (i = 0; i<tests; i++) /* performance test with pointer */
545 used_memory = lc_get_heap_used_bytes();
547 rt = lc_timer_enter_high_priority();
548 lc_timer_start(timer);
550 chordal_env->ifg = be_ifg_pointer_new(chordal_env);
552 lc_timer_stop(timer);
553 rt = lc_timer_leave_high_priority();
555 used_memory = lc_get_heap_used_bytes() - used_memory;
557 coloring_restore(&coloring);
560 co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
561 co_build_ou_structure(co);
562 co_build_graph_structure(co);
564 rt = lc_timer_enter_high_priority();
565 lc_timer_start(timer);
567 co_solve_heuristic_new(co);
569 lc_timer_stop(timer);
570 rt = lc_timer_leave_high_priority();
572 co_free_graph_structure(co);
573 co_free_ou_structure(co);
575 be_ifg_free(chordal_env->ifg);
579 elapsed_usec = lc_timer_elapsed_usec(timer);
580 /* calculating average */
581 elapsed_usec = elapsed_usec / tests;
583 ir_printf("\npointer:; %+F; %u; %u ",current_ir_graph, used_memory, elapsed_usec);
590 chordal_env->ifg = old_if;
593 void be_ifg_dump_dot(be_ifg_t *ifg, ir_graph *irg, FILE *file, const be_ifg_dump_dot_cb_t *cb, void *self)
595 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
596 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
597 bitset_t *nodes = bitset_malloc(get_irg_last_idx(irg));
601 fprintf(file, "graph G {\n\tgraph [");
603 cb->graph_attr(file, self);
604 fprintf(file, "];\n");
607 cb->at_begin(file, self);
609 be_ifg_foreach_node(ifg, nodes_it, n) {
610 if(cb->is_dump_node && cb->is_dump_node(self, n)) {
611 int idx = get_irn_idx(n);
612 bitset_set(nodes, idx);
613 fprintf(file, "\tnode [");
615 cb->node_attr(file, self, n);
616 fprintf(file, "]; n%d;\n", idx);
620 /* Check, if all neighbours are indeed connected to the node. */
621 be_ifg_foreach_node(ifg, nodes_it, n) {
622 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
623 int n_idx = get_irn_idx(n);
624 int m_idx = get_irn_idx(m);
626 if(n_idx < m_idx && bitset_is_set(nodes, n_idx) && bitset_is_set(nodes, m_idx)) {
627 fprintf(file, "\tn%d -- n%d [", n_idx, m_idx);
629 cb->edge_attr(file, self, n, m);
630 fprintf(file, "];\n");
636 cb->at_end(file, self);
638 fprintf(file, "}\n");
642 static void int_comp_rec(be_irg_t *birg, be_ifg_t *ifg, ir_node *n, bitset_t *seen)
644 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
647 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
648 if(!bitset_contains_irn(seen, m) && !arch_irn_is(birg->main_env->arch_env, m, ignore)) {
649 bitset_add_irn(seen, m);
650 int_comp_rec(birg, ifg, m, seen);
656 static int int_component_stat(be_irg_t *birg, be_ifg_t *ifg)
659 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
660 bitset_t *seen = bitset_irg_malloc(birg->irg);
664 be_ifg_foreach_node(ifg, nodes_it, n) {
665 if (! bitset_contains_irn(seen, n) && ! arch_irn_is(birg->main_env->arch_env, n, ignore)) {
667 bitset_add_irn(seen, n);
668 int_comp_rec(birg, ifg, n, seen);
676 void be_ifg_stat(be_irg_t *birg, be_ifg_t *ifg, be_ifg_stat_t *stat)
678 void *nodes_it = be_ifg_nodes_iter_alloca(ifg);
679 void *neigh_it = be_ifg_neighbours_iter_alloca(ifg);
680 bitset_t *nodes = bitset_irg_malloc(birg->irg);
683 memset(stat, 0, sizeof(stat[0]));
685 be_ifg_foreach_node(ifg, nodes_it, n) {
687 be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
688 bitset_add_irn(nodes, n);
689 stat->n_edges += !bitset_contains_irn(nodes, m);
693 stat->n_comps = int_component_stat(birg, ifg);
706 static int ifg_flavor = BE_IFG_STD;
708 static const lc_opt_enum_int_items_t ifg_flavor_items[] = {
709 { "std", BE_IFG_STD },
710 { "fast", BE_IFG_FAST },
711 { "clique", BE_IFG_CLIQUE },
712 { "pointer", BE_IFG_POINTER },
713 { "list", BE_IFG_LIST },
714 { "check", BE_IFG_CHECK },
718 static lc_opt_enum_int_var_t ifg_flavor_var = {
719 &ifg_flavor, ifg_flavor_items
722 static const lc_opt_table_entry_t be_ifg_options[] = {
723 LC_OPT_ENT_ENUM_PTR ("ifg", "interference graph flavour", &ifg_flavor_var),
727 void be_init_ifg(void)
729 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
730 lc_opt_entry_t *ifg_grp = lc_opt_get_grp(be_grp, "ifg");
732 lc_opt_add_table(ifg_grp, be_ifg_options);
735 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ifg);
737 static FILE *be_ifg_open(const be_chordal_env_t *env, const char *prefix)
742 ir_snprintf(buf, sizeof(buf), "%s%F_%s.log", prefix, env->irg, env->cls->name);
743 result = fopen(buf, "wt");
745 panic("Couldn't open '%s' for writing.", buf);
751 static void check_ifg_implementations(const be_chordal_env_t *chordal_env)
756 f = be_ifg_open(chordal_env, "std");
757 ifg = be_ifg_std_new(chordal_env);
758 be_ifg_check_sorted_to_file(ifg, f);
762 f = be_ifg_open(chordal_env, "list");
763 ifg = be_ifg_list_new(chordal_env);
764 be_ifg_check_sorted_to_file(ifg, f);
768 f = be_ifg_open(chordal_env, "clique");
769 ifg = be_ifg_clique_new(chordal_env);
770 be_ifg_check_sorted_to_file(ifg, f);
774 f = be_ifg_open(chordal_env, "pointer");
775 ifg = be_ifg_pointer_new(chordal_env);
776 be_ifg_check_sorted_to_file(ifg, f);
781 be_ifg_t *be_create_ifg(const be_chordal_env_t *chordal_env)
783 be_ifg_t *ifg = NULL;
785 switch (ifg_flavor) {
788 fprintf(stderr, "no valid ifg flavour selected. falling back to std\n");
791 ifg = be_ifg_std_new(chordal_env);
794 ifg = be_ifg_clique_new(chordal_env);
797 ifg = be_ifg_pointer_new(chordal_env);
800 ifg = be_ifg_list_new(chordal_env);
803 check_ifg_implementations(chordal_env);
804 /* Build the interference graph. */
805 ifg = be_ifg_std_new(chordal_env);