183bc49633590d91d6bcce506d33fa05bbdc6c1f
[libfirm] / ir / be / becopypbqp.c
1 /*
2  * becopypbqp.c
3  *
4  *  Created on: Aug 28, 2009
5  *      Author: bersch
6  */
7 #ifdef FIRM_KAPS
8
9 #include "kaps.h"
10 #include "pbqp_t.h"
11 #include "vector.h"
12 #include "matrix.h"
13 #include "html_dumper.h"
14 #include "heuristical.h"
15 #include "pbqp_node_t.h"
16
17 #include "becopyopt_t.h"
18 #include "beifg.h"
19 #include "beifg_t.h"
20 #include "irprintf_t.h"
21
22 #include "error.h"
23 #include "bitset.h"
24 #include "pmap.h"
25
26 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
27 {
28         FILE *result;
29         char buf[1024];
30         size_t i, n;
31         char *tu_name;
32
33         n = strlen(env->birg->main_env->cup_name);
34         tu_name = XMALLOCN(char, n + 1);
35         strcpy(tu_name, env->birg->main_env->cup_name);
36         for (i = 0; i < n; ++i)
37                 if (tu_name[i] == '.')
38                         tu_name[i] = '_';
39
40         ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
41         xfree(tu_name);
42         result = fopen(buf, "wt");
43         if(result == NULL) {
44                 panic("Couldn't open '%s' for writing.", buf);
45         }
46
47         return result;
48 }
49
50 int co_solve_heuristic_pbqp(copy_opt_t *co) {
51         void *nodes_it  = be_ifg_nodes_iter_alloca(co->cenv->ifg);
52         void *neigh_it  = be_ifg_neighbours_iter_alloca(co->cenv->ifg);
53         ir_node *ifg_node, *if_neighb_node;
54
55         pbqp *pbqp_problem;
56         unsigned number_registers = co->cls->n_regs;
57         unsigned number_nodes = 0;
58         unsigned int nodeIdx = 0;
59
60         bitset_t *ignore_registers;
61         pmap *map;
62
63         // count nodes in ifg
64         be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
65                 number_nodes++;
66         }
67
68         // create empty PBQP instance
69         pbqp_problem = alloc_pbqp(number_nodes);
70
71         //
72         ignore_registers = bitset_malloc(number_registers);
73         be_put_ignore_regs(co->cenv->birg, co->cls, ignore_registers);
74
75         // create map
76         map = pmap_create_ex(number_nodes);
77
78         // add costs vector to nodes
79         nodeIdx = 0;
80         be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
81                 // create costs vector
82                 struct vector *costs_vector = vector_alloc(pbqp_problem, number_registers);
83
84                 // set costs
85                 unsigned int cnt;
86                 for(cnt = 0; cnt < costs_vector->len; cnt++) {
87                         if (bitset_is_set(ignore_registers,cnt)) {
88                                 vector_set(costs_vector, cnt, INF_COSTS);
89                         }
90                         else {
91                                 if (!arch_reg_out_is_allocatable(ifg_node, arch_register_for_index(co->cls, cnt)))
92                                 {
93                                         vector_set(costs_vector, cnt, INF_COSTS);
94                                 }
95                                 else {
96                                         vector_set(costs_vector, cnt, 0);
97                                 }
98                         }
99
100                         // add description
101                         vector_set_description(costs_vector, cnt, arch_register_for_index(co->cls, cnt)->name);
102                 }
103
104                 // add costs vector to node
105                 add_node_costs(pbqp_problem, nodeIdx, costs_vector);
106
107                 // insert ir_node and pbqp_node into map
108                 pmap_insert(map, ifg_node, get_node(pbqp_problem,nodeIdx));
109
110                 // increment nodeIndex
111                 nodeIdx++;
112         }
113
114         be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
115                 pbqp_node *src_node = pmap_find(map,ifg_node)->value;
116                 unsigned int srcNodeIdx = src_node->index;
117
118                 // add costs matrix between nodes (interference edge)
119                 be_ifg_foreach_neighbour(co->cenv->ifg, neigh_it, ifg_node, if_neighb_node) {
120                         pbqp_node *trg_node = pmap_find(map,if_neighb_node)->value;
121                         if(get_edge(pbqp_problem, srcNodeIdx, trg_node->index) == NULL) {
122                                 // create costs matrix
123                                 struct pbqp_matrix *matrix = pbqp_matrix_alloc(pbqp_problem, number_registers, number_registers);
124
125                                 // set costs
126                                 unsigned row, col;
127                                 for(row = 0; row < number_registers; row++) {
128                                         for(col = 0; col < number_registers; col++) {
129                                                 if(row == col) {
130                                                         pbqp_matrix_set(matrix, row, col, INF_COSTS);
131                                                 }
132                                                 else {
133                                                         pbqp_matrix_set(matrix, row, col, 2);
134                                                 }
135                                         }
136                                 }
137
138                                 // add costs matrix to interference edge
139                                 add_edge_costs(pbqp_problem, srcNodeIdx, trg_node->index , matrix);
140                         }
141                 }
142
143                 // add costs matrix between nodes (affinety edge)
144                 affinity_node_t *aff_node = get_affinity_info(co, ifg_node);
145                 neighb_t *aff_neighb_node;
146                 if(aff_node != NULL) {
147                         co_gs_foreach_neighb(aff_node, aff_neighb_node) {
148                                 pbqp_node *trg_node = pmap_find(map,aff_neighb_node->irn)->value;
149                                 if(get_edge(pbqp_problem, srcNodeIdx, trg_node->index) == NULL) {
150                                         // create costs matrix
151                                         struct pbqp_matrix *matrix = pbqp_matrix_alloc(pbqp_problem, number_registers, number_registers);
152
153                                         // set costs
154                                         unsigned row, col;
155                                         for(row = 0; row < number_registers; row++) {
156                                                 for(col = 0; col < number_registers; col++) {
157                                                         if(row == col) {
158                                                                 pbqp_matrix_set(matrix, row, col, 0);
159                                                         }
160                                                         else {
161                                                                 pbqp_matrix_set(matrix, row, col, 2);
162                                                         }
163                                                 }
164                                         }
165
166                                         // add costs matrix to interference edge
167                                         add_edge_costs(pbqp_problem, srcNodeIdx, trg_node->index , matrix);
168                                 }
169                         }
170                 }
171         }
172
173 #if KAPS_DUMP
174         // dump graph before solving pbqp
175         FILE *file_before = my_open(co->cenv, "", "-before.html");
176         set_dumpfile(pbqp_problem, file_before);
177         pbqp_dump_input(pbqp_problem);
178 #endif
179
180
181         // solve pbqp problem
182         solve_pbqp_heuristical(pbqp_problem);
183         //solve_pbqp_brute_force(pbqp_problem);
184         num solution = get_solution(pbqp_problem);
185
186         assert(solution != INF_COSTS && "No PBQP solution found");
187
188         // printf("Solution (%s): %d\n",co->name, (int)solution);
189
190         // coloring ifg
191         be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
192                 pbqp_node *node = pmap_find(map,ifg_node)->value;
193                 num index = get_node_solution(pbqp_problem, node->index);
194                 const arch_register_t *reg = arch_register_for_index(co->cls, index);
195                 arch_set_irn_register(ifg_node, reg);
196         }
197
198 #if KAPS_DUMP
199         // dump graph after solving pbqp
200         FILE *file_after = my_open(co->cenv, "", "-after.html");
201         set_dumpfile(pbqp_problem, file_after);
202         pbqp_dump_input(pbqp_problem);
203 #endif
204
205
206         // free pbqp resources
207 #if KAPS_DUMP
208         fclose(file_before);
209         fclose(file_after);
210 #endif
211         bitset_free(ignore_registers);
212         pmap_destroy(map);
213         free_pbqp(pbqp_problem);
214
215         return 0;
216 }
217
218 #endif