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