4 * Created on: Aug 28, 2009
11 #include "becopypbqp.h"
17 #include "html_dumper.h"
18 #include "heuristical.h"
19 #include "pbqp_node_t.h"
21 #include "becopyopt_t.h"
25 #include "irprintf_t.h"
39 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
46 n = strlen(env->birg->main_env->cup_name);
47 tu_name = XMALLOCN(char, n + 1);
48 strcpy(tu_name, env->birg->main_env->cup_name);
49 for (i = 0; i < n; ++i)
50 if (tu_name[i] == '.')
53 ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
55 result = fopen(buf, "wt");
57 panic("Couldn't open '%s' for writing.", buf);
64 static void insert_into_reverse_peo(ir_node *block, void *data) {
65 pqueue_t *queue = new_pqueue();
66 pbqp_co_t *pbqp_co = data;
69 sched_foreach(block, irn) {
72 if (get_irn_mode(irn) == mode_T) {
73 const ir_edge_t *edge;
75 foreach_out_edge(irn, edge) {
76 ir_node *proj = get_edge_src_irn(edge);
77 if (!arch_irn_consider_in_reg_alloc(pbqp_co->cls, proj))
80 // get related pbqp_node and insert into reverse peo
81 assert(node && "No corresponding PBQP-Node found!");
83 // insert proj node into priority queue (descending by their degree in ifg)
84 pqueue_put(queue,proj, be_ifg_degree(pbqp_co->ifg,proj));
87 /* insert proj nodes into reverse perfect elimination order (descending by their degree in ifg) */
88 while(!pqueue_empty(queue)) {
90 plist_insert_back(pbqp_co->rpeo, get_node(pbqp_co->pbqp, get_irn_idx(pqueue_pop_front(queue))));
93 if (!arch_irn_consider_in_reg_alloc(pbqp_co->cls, irn))
96 /* get related pbqp_node and insert into reverse peo */
97 assert(node && "No corresponding PBQP-Node found!");
98 plist_insert_back(pbqp_co->rpeo, get_node(pbqp_co->pbqp, get_irn_idx(irn)));
102 /* free priority queue */
106 static int co_solve_heuristic_pbqp(copy_opt_t *co) {
107 void *nodes_it = be_ifg_nodes_iter_alloca(co->cenv->ifg);
108 void *neigh_it = be_ifg_neighbours_iter_alloca(co->cenv->ifg);
110 ir_node *if_neighb_node;
114 unsigned number_registers = co->cls->n_regs;
115 unsigned number_nodes = get_irg_last_idx(co->irg);
117 /* create and initialize data structure for pbqp copy minimization optimization */
118 pbqp_co.cls = co->cls;
119 pbqp_co.rpeo = plist_new();;
120 pbqp_co.map = pmap_create_ex(number_nodes);
121 pbqp_co.pbqp = alloc_pbqp(number_nodes);
122 pbqp_co.ignore_reg = bitset_malloc(number_registers);
123 pbqp_co.ifg = co->cenv->ifg;
125 /* get ignored registers */
126 be_put_ignore_regs(co->cenv->birg, co->cls, pbqp_co.ignore_reg);
128 /* add costs vector to nodes */
129 be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
130 /* create costs vector */
131 struct vector *costs_vector = vector_alloc(pbqp_co.pbqp, number_registers);
135 for(cnt = 0; cnt < costs_vector->len; cnt++) {
136 if (bitset_is_set(pbqp_co.ignore_reg,cnt)) {
137 vector_set(costs_vector, cnt, INF_COSTS);
140 if (!arch_reg_out_is_allocatable(ifg_node, arch_register_for_index(co->cls, cnt)))
142 vector_set(costs_vector, cnt, INF_COSTS);
145 vector_set(costs_vector, cnt, 0);
148 #if KAPS_ENABLE_VECTOR_NAMES
149 /* add description */
150 vector_set_description(costs_vector, cnt, arch_register_for_index(co->cls, cnt)->name);
154 /* add costs vector to node */
155 add_node_costs(pbqp_co.pbqp, get_irn_idx(ifg_node), costs_vector);
157 /* insert ir_node and pbqp_node into map */
158 pmap_insert(pbqp_co.map, ifg_node, get_node(pbqp_co.pbqp, get_irn_idx(ifg_node)));
161 /* add pbqp edges and cost matrix */
162 be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
163 /* add costs matrix between nodes (interference edge) */
164 be_ifg_foreach_neighbour(co->cenv->ifg, neigh_it, ifg_node, if_neighb_node) {
165 if(get_edge(pbqp_co.pbqp,get_irn_idx(ifg_node), get_irn_idx(if_neighb_node)) == NULL) {
166 /* create costs matrix */
167 struct pbqp_matrix *matrix = pbqp_matrix_alloc(pbqp_co.pbqp, number_registers, number_registers);
171 for(row = 0; row < number_registers; row++) {
172 for(col = 0; col < number_registers; col++) {
174 pbqp_matrix_set(matrix, row, col, INF_COSTS);
177 pbqp_matrix_set(matrix, row, col, 0);
182 /* add costs matrix to interference edge */
183 add_edge_costs(pbqp_co.pbqp, get_irn_idx(ifg_node), get_irn_idx(if_neighb_node) , matrix);
187 /* add costs matrix between nodes (affinity edge) */
188 affinity_node_t *aff_node = get_affinity_info(co, ifg_node);
189 neighb_t *aff_neighb_node;
190 if(aff_node != NULL) {
191 co_gs_foreach_neighb(aff_node, aff_neighb_node) {
192 pmap_entry *ptr_pbqp_node = pmap_find(pbqp_co.map,aff_neighb_node->irn);
194 /* ignore Unknowns */
195 if(ptr_pbqp_node == NULL) {
199 if(get_edge(pbqp_co.pbqp, get_irn_idx(aff_node->irn), get_irn_idx(aff_neighb_node->irn)) == NULL) {
200 /* create costs matrix */
201 struct pbqp_matrix *matrix = pbqp_matrix_alloc(pbqp_co.pbqp, number_registers, number_registers);
205 for(row = 0; row < number_registers; row++) {
206 for(col = 0; col < number_registers; col++) {
208 pbqp_matrix_set(matrix, row, col, 0);
211 pbqp_matrix_set(matrix, row, col, 2);
216 /* add costs matrix to affinity edge */
217 add_edge_costs(pbqp_co.pbqp, get_irn_idx(aff_node->irn), get_irn_idx(aff_neighb_node->irn) , matrix);
223 /* create reverse perfect elimination order */
224 assure_doms(co->irg);
225 dom_tree_walk_irg(co->irg, insert_into_reverse_peo, NULL, &pbqp_co);
228 // dump graph before solving pbqp
229 FILE *file_before = my_open(co->cenv, "", "-before.html");
230 set_dumpfile(pbqp_co.pbqp, file_before);
231 pbqp_dump_input(pbqp_co.pbqp);
235 /* solve pbqp problem using a reverse perfect elimination order */
236 solve_pbqp_heuristical_co(pbqp_co.pbqp, pbqp_co.rpeo);
237 num solution = get_solution(pbqp_co.pbqp);
239 assert(solution != INF_COSTS && "No PBQP solution found");
243 /* dump graph after solving pbqp */
244 FILE *file_after = my_open(co->cenv, "", "-after.html");
245 set_dumpfile(pbqp_co.pbqp, file_after);
246 pbqp_dump_input(pbqp_co.pbqp);
250 be_ifg_foreach_node(co->cenv->ifg, nodes_it, ifg_node) {
251 num index = get_node_solution(pbqp_co.pbqp, get_irn_idx(ifg_node));
252 const arch_register_t *reg = arch_register_for_index(co->cls, index);
253 arch_set_irn_register(ifg_node, reg);
256 /* free pbqp resources */
261 bitset_free(pbqp_co.ignore_reg);
262 pmap_destroy(pbqp_co.map);
263 plist_free(pbqp_co.rpeo);
264 free_pbqp(pbqp_co.pbqp);
269 void be_init_copypbqp(void)
271 static co_algo_info copyheur = {
272 co_solve_heuristic_pbqp, 0
275 be_register_copyopt("pbqp", ©heur);
278 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copypbqp);