2a578d4fa4fd4778245e39f5bcf6b023bd2b8cf8
[libfirm] / ir / be / bepbqpcoloring.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       PBQP based register allocation.
23  * @author      Thomas Bersch
24  * @date        27.11.2009
25  * @version     $Id: bechordal.c 26750 2009-11-27 09:37:43Z bersch $
26  */
27
28 /*      miscellaneous includes */
29 #include "config.h"
30
31 #ifdef FIRM_KAPS
32
33 #include "debug.h"
34 #include "error.h"
35
36 #include "irdom.h"
37 #include "iredges_t.h"
38 #include "irprintf.h"
39 #include "irgwalk.h"
40 #include "time.h"
41
42 /* libfirm/ir/be includes */
43 #include "bearch.h"
44 #include "beirg.h"
45 #include "besched.h"
46 #include "bemodule.h"
47 #include "bechordal_common.h"
48 #include "bechordal.h"
49 #include "bechordal_t.h"
50 #include "beinsn_t.h"
51 #include "benode.h"
52 #include "belive.h"
53 #include "belive_t.h"
54 #include "beutil.h"
55 #include "plist.h"
56 #include "pqueue.h"
57 #include "becopyopt.h"
58
59 /* pbqp includes */
60 #include "kaps.h"
61 #include "matrix.h"
62 #include "vector.h"
63 #include "vector_t.h"
64 #include "heuristical.h"
65 #include "pbqp_t.h"
66 #include "html_dumper.h"
67 #include "pbqp_node_t.h"
68 #include "pbqp_node.h"
69
70 #define TIMER 0
71
72
73 static bool use_exec_freq = true;
74
75 typedef struct _be_pbqp_alloc_env_t {
76         pbqp                                            *pbqp_inst;                     /**< PBQP instance for register allocation */
77         be_irg_t                        *birg;                  /**< Back-end IRG session. */
78         ir_graph                        *irg;                   /**< The graph under examination. */
79         const arch_register_class_t *cls;                               /**< Current processed register class */
80         be_lv_t                     *lv;
81         bitset_t                    *ignored_regs;
82         pbqp_matrix                                     *ife_matrix_template;
83         pbqp_matrix                                     *aff_matrix_template;
84         plist_t                                         *rpeo;
85         unsigned                                        *restr_nodes;
86         unsigned                                        *ife_edge_num;
87         be_chordal_env_t                        *env;
88 } be_pbqp_alloc_env_t;
89
90
91 #define is_Reg_Phi(irn)                                                                                 (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
92 #define get_Perm_src(irn)                                                                               (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
93 #define is_Perm_Proj(irn)                                                                               (is_Proj(irn) && be_is_Perm(get_Proj_pred(irn)))
94 #define insert_edge(pbqp, src_node, trg_node, template_matrix)  (add_edge_costs(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node), pbqp_matrix_copy(pbqp, template_matrix)))
95 #define get_free_regs(restr_nodes, cls, irn)                                    (arch_register_class_n_regs(cls) - restr_nodes[get_irn_idx(irn)])
96
97 static inline int is_2addr_code(const arch_register_req_t *req)
98 {
99         return (req->type & arch_register_req_type_should_be_same) != 0;
100 }
101
102 static const lc_opt_table_entry_t options[] = {
103         LC_OPT_ENT_BOOL      ("exec_freq", "use exec_freq",  &use_exec_freq),
104         LC_OPT_LAST
105 };
106
107 #if KAPS_DUMP
108 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
109 {
110         FILE *result;
111         char buf[1024];
112         size_t i, n;
113         char *tu_name;
114
115         n = strlen(env->birg->main_env->cup_name);
116         tu_name = XMALLOCN(char, n + 1);
117         strcpy(tu_name, env->birg->main_env->cup_name);
118         for (i = 0; i < n; ++i)
119                 if (tu_name[i] == '.')
120                         tu_name[i] = '_';
121
122         ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
123         xfree(tu_name);
124         result = fopen(buf, "wt");
125         if(result == NULL) {
126                 panic("Couldn't open '%s' for writing.", buf);
127         }
128
129         return result;
130 }
131 #endif
132
133
134 static void create_pbqp_node(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *irn) {
135         const arch_register_class_t *cls = pbqp_alloc_env->cls;
136         pbqp     *pbqp_inst              = pbqp_alloc_env->pbqp_inst;
137         bitset_t *ignored_regs           = pbqp_alloc_env->ignored_regs;
138         unsigned  colors_n               = arch_register_class_n_regs(cls);
139         unsigned  cntConstrains          = 0;
140
141         /* create costs vector depending on register constrains */
142         struct vector *costs_vector = vector_alloc(pbqp_inst, colors_n);
143
144         /* set costs depending on register constrains */
145         unsigned idx;
146         for(idx = 0; idx < colors_n; idx++) {
147                 if(bitset_is_set(ignored_regs, idx) || !arch_reg_out_is_allocatable(irn, arch_register_for_index(cls, idx))) {
148                         vector_set(costs_vector, idx, INF_COSTS);
149                         cntConstrains++;
150                 }
151         }
152
153         /* add vector to pbqp node */
154         add_node_costs(pbqp_inst, get_irn_idx(irn), costs_vector);
155         pbqp_alloc_env->restr_nodes[get_irn_idx(irn)] = cntConstrains;
156 }
157
158 static void insert_ife_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node) {
159         pbqp                                            *pbqp                = pbqp_alloc_env->pbqp_inst;
160         const arch_register_class_t *cls                 = pbqp_alloc_env->cls;
161         pbqp_matrix                             *ife_matrix_template = pbqp_alloc_env->ife_matrix_template;
162         unsigned                                        *restr_nodes         = pbqp_alloc_env->restr_nodes;
163
164         if(get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
165
166                 /* increase ife edge counter */
167                 pbqp_alloc_env->ife_edge_num[get_irn_idx(src_node)]++;
168                 pbqp_alloc_env->ife_edge_num[get_irn_idx(trg_node)]++;
169
170                 /* do useful optimization to speed up pbqp solving (we can do this because we know our matrix) */
171                 if(get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
172                         unsigned src_idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
173                         unsigned trg_idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
174                         assert(src_idx != trg_idx && "Interfering nodes could not have the same register!");
175                         return;
176                 }
177                 if(get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
178                         if(get_free_regs(restr_nodes, cls, src_node) == 1) {
179                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
180                                 vector_set(get_node(pbqp, get_irn_idx(trg_node))->costs, idx, INF_COSTS);
181                         }
182                         else {
183                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
184                                 vector_set(get_node(pbqp, get_irn_idx(src_node))->costs, idx, INF_COSTS);
185                         }
186                         return;
187                 }
188
189                 /* insert interference edge */
190                 insert_edge(pbqp, src_node, trg_node, ife_matrix_template);
191         }
192 }
193
194 static void inser_afe_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node, int pos) {
195         pbqp                                            *pbqp             = pbqp_alloc_env->pbqp_inst;
196         const arch_register_class_t *cls              = pbqp_alloc_env->cls;
197         unsigned                                        *restr_nodes      = pbqp_alloc_env->restr_nodes;
198         pbqp_matrix                                     *afe_matrix       = pbqp_matrix_alloc(pbqp, arch_register_class_n_regs(cls), arch_register_class_n_regs(cls));
199         unsigned                                         colors_n                 = arch_register_class_n_regs(cls);
200
201         if(get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
202                 if(use_exec_freq) {
203                         /* get exec_freq for copy_block */
204                         ir_node *root_bl = get_nodes_block(src_node);
205                         ir_node *copy_bl = is_Phi(src_node) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
206                         unsigned long res = get_block_execfreq_ulong(pbqp_alloc_env->birg->exec_freq, copy_bl);
207
208                         /* create afe-matrix */
209                         unsigned row, col;
210                         for(row = 0; row < colors_n; row++) {
211                                 for(col = 0; col < colors_n; col++) {
212                                         if(row != col)
213                                                 pbqp_matrix_set(afe_matrix, row, col, (num)res);
214                                 }
215                         }
216                 }
217                 else {
218                         afe_matrix = pbqp_alloc_env->aff_matrix_template;
219                 }
220
221                 /* do useful optimization to speed up pbqp solving */
222                 if(get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
223                         return;
224                 }
225                 if(get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
226                         if(get_free_regs(restr_nodes, cls, src_node) == 1) {
227                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
228                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(trg_node))->costs, afe_matrix, regIdx);
229                         }
230                         else {
231                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
232                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(src_node))->costs, afe_matrix, regIdx);
233                         }
234                         return;
235                 }
236
237                 /* insert interference edge */
238                 insert_edge(pbqp, src_node, trg_node, afe_matrix);
239         }
240 }
241
242 static void create_affinity_edges(ir_node *irn, void *env) {
243         be_pbqp_alloc_env_t         *pbqp_alloc_env   = env;
244         const arch_register_class_t *cls              = pbqp_alloc_env->cls;
245         const arch_register_req_t   *req              = arch_get_register_req_out(irn);
246         unsigned pos, max;
247
248         if (is_Reg_Phi(irn)) { /* Phis */
249                 for (pos=0, max=get_irn_arity(irn); pos<max; ++pos) {
250                         ir_node *arg = get_irn_n(irn, pos);
251
252                         if (!arch_irn_consider_in_reg_alloc(cls, arg))
253                                 continue;
254
255                         /* no edges to itself */
256                         if(irn == arg) {
257                                 continue;
258                         }
259
260                         inser_afe_edge(pbqp_alloc_env, irn, arg, pos);
261                 }
262         }
263         else if (is_Perm_Proj(irn)) { /* Perms */
264                 ir_node *arg = get_Perm_src(irn);
265                 if (!arch_irn_consider_in_reg_alloc(cls, arg))
266                         return;
267
268                 inser_afe_edge(pbqp_alloc_env, irn, arg, -1);
269         }
270         else { /* 2-address code */
271                 if (is_2addr_code(req)) {
272                         const unsigned other = req->other_same;
273                         int i;
274
275                         for (i = 0; 1U << i <= other; ++i) {
276                                 if (other & (1U << i)) {
277                                         ir_node *other = get_irn_n(skip_Proj(irn), i);
278                                         if (!arch_irn_consider_in_reg_alloc(cls, other))
279                                                 continue;
280
281                                         /* no edges to itself */
282                                         if(irn == other) {
283                                                 continue;
284                                         }
285
286                                         inser_afe_edge(pbqp_alloc_env, irn, other, i);
287                                 }
288                         }
289                 }
290         }
291 }
292
293 static void create_pbqp_coloring_instance(ir_node *block, void *data) {
294         be_pbqp_alloc_env_t         *pbqp_alloc_env     = data;
295         be_lv_t                     *lv                 = pbqp_alloc_env->lv;
296         const arch_register_class_t *cls                = pbqp_alloc_env->cls;
297         plist_t                                         *rpeo                           = pbqp_alloc_env->rpeo;
298         pbqp                                            *pbqp_inst                      = pbqp_alloc_env->pbqp_inst;
299         unsigned                                        *restr_nodes            = pbqp_alloc_env->restr_nodes;
300         pqueue_t                                        *queue                  = new_pqueue();
301         pqueue_t                                        *restr_nodes_queue      = new_pqueue();
302         plist_t                                         *temp_list              = plist_new();
303         ir_node                     *irn;
304         ir_nodeset_t                 live_nodes;
305
306         /* first, determine the pressure */
307         /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
308         create_borders(block, pbqp_alloc_env->env);
309
310         /* calculate living nodes for the first step */
311         ir_nodeset_init(&live_nodes);
312         be_liveness_end_of_block(lv, cls, block, &live_nodes);
313
314         /* create pbqp nodes, interference edges and reverse perfect elimination order */
315         sched_foreach_reverse(block, irn) {
316                 ir_node *live;
317                 ir_nodeset_iterator_t iter;
318
319                 if (get_irn_mode(irn) == mode_T) {
320                         const ir_edge_t *edge;
321                         foreach_out_edge(irn, edge) {
322                                 ir_node *proj = get_edge_src_irn(edge);
323                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
324                                         continue;
325
326                                 /* create pbqp source node if it dosn't exist */
327                                 if(get_node(pbqp_inst, get_irn_idx(proj)) == NULL) {
328                                         create_pbqp_node(pbqp_alloc_env, proj);
329                                 }
330
331                                 /* create nodes and interference edges */
332                                 foreach_ir_nodeset(&live_nodes, live, iter) {
333                                         /* create pbqp source node if it dosn't exist */
334                                         if(get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
335                                                 create_pbqp_node(pbqp_alloc_env, live);
336                                         }
337
338                                         /* no edges to itself */
339                                         if(proj == live) {
340                                                 continue;
341                                         }
342
343                                         insert_ife_edge(pbqp_alloc_env, proj, live);
344                                 }
345                         }
346                 }
347                 else {
348                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
349                                 /* create pbqp source node if it dosn't exist */
350                                 if(get_node(pbqp_inst, get_irn_idx(irn)) == NULL) {
351                                         create_pbqp_node(pbqp_alloc_env, irn);
352                                 }
353
354                                 /* create nodes and interference edges */
355                                 foreach_ir_nodeset(&live_nodes, live, iter) {
356                                         /* create pbqp source node if it dosn't exist */
357                                         if(get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
358                                                 create_pbqp_node(pbqp_alloc_env, live);
359                                         }
360
361                                         /* no edges to itself */
362                                         if(irn == live) {
363                                                 continue;
364                                         }
365
366                                         /* insert interference edge */
367                                         insert_ife_edge(pbqp_alloc_env, irn, live);
368                                 }
369                         }
370                 }
371
372                 /* get living nodes for next step */
373                 if (!is_Phi(irn)) {
374                         be_liveness_transfer(cls, irn, &live_nodes);
375                 }
376
377                 /* order nodes for perfect elimination order */
378                 if (get_irn_mode(irn) == mode_T) {
379                         plist_element_t *first = plist_first(temp_list);
380                         const ir_edge_t *edge;
381
382                         foreach_out_edge(irn, edge) {
383                                 ir_node *proj = get_edge_src_irn(edge);
384                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
385                                         continue;
386
387                                 // insert proj node into priority queue (descending by the number of interference edges)
388                                 if(get_free_regs(restr_nodes, cls, proj) <= 4/*bitset_is_set(restr_nodes, get_irn_idx(proj))*/) {
389                                         pqueue_put(restr_nodes_queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
390                                 }
391                                 else {
392                                         pqueue_put(queue,proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
393                                 }
394                         }
395
396                         /* first insert all restricted nodes */
397                         while(!pqueue_empty(restr_nodes_queue)) {
398                                 if(first == NULL) {
399                                         plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
400                                         first = plist_first(temp_list);
401                                 } else {
402                                         plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
403                                 }
404                         }
405
406                         /* insert proj nodes descending by their number of interference edges */
407                         while(!pqueue_empty(queue)) {
408                                 if(first == NULL) {
409                                         plist_insert_back(temp_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
410                                         first = plist_first(temp_list);
411                                 } else {
412                                         plist_insert_before(temp_list, first, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
413                                 }
414                         }
415                 }
416                 else {
417                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
418                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
419                         }
420                 }
421         }
422
423         /* insert nodes into reverse perfect elimination order */
424         plist_element_t *el;
425         foreach_plist(temp_list, el) {
426                 plist_insert_back(rpeo, el->data);
427         }
428
429         /* free reserved memory */
430         ir_nodeset_destroy(&live_nodes);
431         plist_free(temp_list);
432         del_pqueue(queue);
433         del_pqueue(restr_nodes_queue);
434 }
435
436 static void insert_perms(ir_node *block, void *data) {
437         /*
438          * Start silent in the start block.
439          * The silence remains until the first barrier is seen.
440          * Each other block is begun loud.
441          */
442         be_chordal_env_t *env    = data;
443         ir_node          *irn;
444         int               silent = block == get_irg_start_block(get_irn_irg(block));
445
446         /*
447          * If the block is the start block search the barrier and
448          * start handling constraints from there.
449          */
450         for (irn = sched_first(block); !sched_is_end(irn);) {
451                 int silent_old = silent;        /* store old silent value */
452                 if (be_is_Barrier(irn))
453                         silent = !silent;               /* toggle silent flag */
454
455                 be_insn_t *insn         = chordal_scan_insn(env, irn);
456                 irn                                     = insn->next_insn;
457
458                 if (silent_old)
459                         continue;
460
461                 if (!insn->has_constraints)
462                         continue;
463
464                 pre_process_constraints(env, &insn);
465         }
466 }
467
468 void be_pbqp_coloring(be_chordal_env_t *env) {
469         ir_graph                      *irg  = env->irg;
470         be_irg_t                      *birg = env->birg;
471         const arch_register_class_t   *cls  = env->cls;
472         unsigned colors_n                                   = arch_register_class_n_regs(cls);
473         be_pbqp_alloc_env_t pbqp_alloc_env;
474         unsigned idx, row, col;
475         be_lv_t *lv;
476
477 #if TIMER
478         ir_timer_t *t_ra_pbqp_alloc_create    = ir_timer_register("be_pbqp_alloc_create", "pbqp alloc create");
479         ir_timer_t *t_ra_pbqp_alloc_solve     = ir_timer_register("be_pbqp_alloc_solve", "pbqp alloc solve");
480         ir_timer_t *t_ra_pbqp_alloc_create_aff  = ir_timer_register("be_pbqp_alloc_create_aff", "pbqp alloc create aff");
481
482         printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
483 #endif
484         lv = be_assure_liveness(birg);
485         be_liveness_assure_sets(lv);
486         be_liveness_assure_chk(lv);
487
488         /* insert perms */
489         assure_doms(irg);
490         dom_tree_walk_irg(irg, insert_perms, NULL, env);
491
492         /* dump graph after inserting perms */
493         if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
494                 char buf[256];
495                 snprintf(buf, sizeof(buf), "-%s-constr", cls->name);
496                 be_dump(irg, buf, dump_ir_block_graph_sched);
497         }
498
499
500         /* initialize pbqp allocation data structure */
501         pbqp_alloc_env.pbqp_inst    = alloc_pbqp(get_irg_last_idx(irg));                /* initialize pbqp instance */
502         pbqp_alloc_env.birg         = birg;
503         pbqp_alloc_env.cls          = cls;
504         pbqp_alloc_env.irg          = irg;
505         pbqp_alloc_env.lv           = lv;
506         pbqp_alloc_env.ignored_regs = bitset_malloc(colors_n);
507         pbqp_alloc_env.rpeo                     = plist_new();
508         pbqp_alloc_env.restr_nodes  = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
509         pbqp_alloc_env.ife_edge_num = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
510         pbqp_alloc_env.env                      = env;
511         be_put_ignore_regs(birg, cls, pbqp_alloc_env.ignored_regs);                             /* get ignored registers */
512
513
514         /* create costs matrix template for interference edges */
515         struct pbqp_matrix *ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
516         /* set costs */
517         for(row = 0, col=0; row < colors_n; row++, col++)
518                 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
519
520         pbqp_alloc_env.ife_matrix_template = ife_matrix;
521
522
523         if(!use_exec_freq) {
524                 /* create costs matrix template for affinity edges */
525                 struct pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
526                 /* set costs */
527                 for(row = 0; row < colors_n; row++) {
528                         for(col = 0; col < colors_n; col++) {
529                                 if(row != col)
530                                         pbqp_matrix_set(afe_matrix, row, col, 2);
531                         }
532                 }
533                 pbqp_alloc_env.aff_matrix_template = afe_matrix;
534         }
535
536
537         /* create pbqp instance */
538 #if TIMER
539         ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
540 #endif
541         assure_doms(irg);
542         dom_tree_walk_irg(irg, create_pbqp_coloring_instance , NULL, &pbqp_alloc_env);
543 #if TIMER
544         ir_timer_stop(t_ra_pbqp_alloc_create);
545 #endif
546
547
548         /* set up affinity edges */
549 #if TIMER
550         ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
551 #endif
552         plist_element_t *el;
553         foreach_plist(pbqp_alloc_env.rpeo, el) {
554                 pbqp_node *node                    = el->data;
555                 idx                                    = node->index;
556                 ir_node *irn               = get_idx_irn(irg, idx);
557                 create_affinity_edges(irn, &pbqp_alloc_env);
558         }
559 #if TIMER
560         ir_timer_stop(t_ra_pbqp_alloc_create_aff);
561 #endif
562
563
564 #if KAPS_DUMP
565         // dump graph before solving pbqp
566         FILE *file_before = my_open(env, "", "-pbqp_coloring.html");
567         set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
568 #endif
569
570
571         /* solve pbqp instance */
572 #if TIMER
573         ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
574 #endif
575         solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
576 #if TIMER
577         ir_timer_stop(t_ra_pbqp_alloc_solve);
578 #endif
579         num solution = get_solution(pbqp_alloc_env.pbqp_inst);
580         assert(solution != INF_COSTS && "No PBQP solution found");
581
582
583         /* assign colors */
584         plist_element_t *element;
585         foreach_plist(pbqp_alloc_env.rpeo, element) {
586                 pbqp_node *node                    = element->data;
587                 idx                                    = node->index;
588                 ir_node *irn               = get_idx_irn(irg, idx);
589                 num color                  = get_node_solution(pbqp_alloc_env.pbqp_inst, idx);
590                 const arch_register_t *reg = arch_register_for_index(cls, color);
591
592                 arch_set_irn_register(irn, reg);
593         }
594
595
596 #if TIMER
597         printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_create), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
598         printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_solve), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
599         printf("%-20s: %8.3lf msec\n" , ir_timer_get_description(t_ra_pbqp_alloc_create_aff), (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
600 #endif
601
602
603         /* free reserved memory */
604 #if KAPS_DUMP
605         fclose(file_before);
606 #endif
607         bitset_free(pbqp_alloc_env.ignored_regs);
608         free_pbqp(pbqp_alloc_env.pbqp_inst);
609         plist_free(pbqp_alloc_env.rpeo);
610         xfree(pbqp_alloc_env.restr_nodes);
611         xfree(pbqp_alloc_env.ife_edge_num);
612 }
613
614
615 /**
616  * Initializes this module.
617  */
618 void be_init_pbqp_coloring(void) {
619         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
620         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
621         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
622         lc_opt_entry_t *coloring_grp = lc_opt_get_grp(chordal_grp, "coloring");
623         lc_opt_entry_t *pbqp_grp = lc_opt_get_grp(coloring_grp, "pbqp");
624
625         static be_ra_chordal_coloring_t coloring = {
626                 be_pbqp_coloring
627         };
628
629         lc_opt_add_table(pbqp_grp, options);
630         be_register_chordal_coloring("pbqp", &coloring);
631 }
632
633 BE_REGISTER_MODULE_CONSTRUCTOR(be_pbqp_alloc);
634
635 #endif