becopyopt: Replace is_Reg_Phi() by just is_Phi().
[libfirm] / ir / be / bepbqpcoloring.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       PBQP based register allocation.
9  * @author      Thomas Bersch
10  * @date        27.11.2009
11  */
12
13 /* miscellaneous includes */
14 #include "config.h"
15
16 #include "debug.h"
17 #include "error.h"
18
19 #include "irdom.h"
20 #include "irdump.h"
21 #include "iredges_t.h"
22 #include "irprintf.h"
23 #include "irgwalk.h"
24 #include "irtools.h"
25 #include "time.h"
26 #include "execfreq_t.h"
27 #include "bipartite.h"
28
29 /* libfirm/ir/be includes */
30 #include "bearch.h"
31 #include "beirg.h"
32 #include "besched.h"
33 #include "bemodule.h"
34 #include "bechordal_common.h"
35 #include "bechordal.h"
36 #include "bechordal_t.h"
37 #include "becopyopt_t.h"
38 #include "beinsn_t.h"
39 #include "benode.h"
40 #include "belive.h"
41 #include "belive_t.h"
42 #include "beutil.h"
43 #include "plist.h"
44 #include "pqueue.h"
45
46 /* pbqp includes */
47 #include "kaps.h"
48 #include "matrix.h"
49 #include "vector.h"
50 #include "vector_t.h"
51 #include "heuristical_co.h"
52 #include "heuristical_co_ld.h"
53 #include "pbqp_t.h"
54 #include "html_dumper.h"
55 #include "pbqp_node_t.h"
56 #include "pbqp_node.h"
57 #include "pbqp_edge_t.h"
58
59 #define TIMER                 0
60 #define PRINT_RPEO            0
61 #define USE_BIPARTIT_MATCHING 0
62 #define DO_USEFUL_OPT         1
63
64
65 static int use_exec_freq     = true;
66 static int use_late_decision = false;
67
68 typedef struct be_pbqp_alloc_env_t {
69         pbqp_t                      *pbqp_inst;         /**< PBQP instance for register allocation */
70         ir_graph                    *irg;               /**< The graph under examination. */
71         const arch_register_class_t *cls;               /**< Current processed register class */
72         be_lv_t                     *lv;
73         bitset_t              const *allocatable_regs;
74         pbqp_matrix_t               *ife_matrix_template;
75         pbqp_matrix_t               *aff_matrix_template;
76         plist_t                     *rpeo;
77         unsigned                    *restr_nodes;
78         unsigned                    *ife_edge_num;
79         ir_execfreq_int_factors      execfreq_factors;
80         be_chordal_env_t            *env;
81 } be_pbqp_alloc_env_t;
82
83
84 #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)))
85 #define get_free_regs(restr_nodes, cls, irn)                   (arch_register_class_n_regs(cls) - restr_nodes[get_irn_idx(irn)])
86
87 static const lc_opt_table_entry_t options[] = {
88         LC_OPT_ENT_BOOL("exec_freq", "use exec_freq",  &use_exec_freq),
89         LC_OPT_ENT_BOOL("late_decision", "use late decision for register allocation",  &use_late_decision),
90         LC_OPT_LAST
91 };
92
93 #if KAPS_DUMP
94 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
95 {
96         FILE       *result;
97         char        buf[1024];
98         size_t      i;
99         size_t      n;
100         char       *tu_name;
101         const char *cup_name = be_get_irg_main_env(env->irg)->cup_name;
102
103         n = strlen(cup_name);
104         tu_name = XMALLOCN(char, n + 1);
105         strcpy(tu_name, cup_name);
106         for (i = 0; i < n; ++i)
107                 if (tu_name[i] == '.')
108                         tu_name[i] = '_';
109
110         ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
111         xfree(tu_name);
112         result = fopen(buf, "wt");
113         if (result == NULL) {
114                 panic("Couldn't open '%s' for writing.", buf);
115         }
116
117         return result;
118 }
119 #endif
120
121
122 static void create_pbqp_node(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *irn)
123 {
124         arch_register_class_t const *const cls              = pbqp_alloc_env->cls;
125         pbqp_t                      *const pbqp_inst        = pbqp_alloc_env->pbqp_inst;
126         bitset_t              const *const allocatable_regs = pbqp_alloc_env->allocatable_regs;
127         unsigned                     const colors_n         = arch_register_class_n_regs(cls);
128         unsigned                           cntConstrains    = 0;
129
130         /* create costs vector depending on register constrains */
131         vector_t *costs_vector = vector_alloc(pbqp_inst, colors_n);
132
133         /* set costs depending on register constrains */
134         unsigned idx;
135         for (idx = 0; idx < colors_n; idx++) {
136                 const arch_register_req_t *req = arch_get_irn_register_req(irn);
137                 const arch_register_t     *reg = arch_register_for_index(cls, idx);
138                 if (!bitset_is_set(allocatable_regs, idx)
139                     || !arch_reg_is_allocatable(req, reg)) {
140                         /* constrained */
141                         vector_set(costs_vector, idx, INF_COSTS);
142                         cntConstrains++;
143                 }
144         }
145
146         /* add vector to pbqp node */
147         add_node_costs(pbqp_inst, get_irn_idx(irn), costs_vector);
148         pbqp_alloc_env->restr_nodes[get_irn_idx(irn)] = cntConstrains;
149 }
150
151 static void insert_ife_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node)
152 {
153         pbqp_t                      *pbqp                = pbqp_alloc_env->pbqp_inst;
154         const arch_register_class_t *cls                 = pbqp_alloc_env->cls;
155         pbqp_matrix_t               *ife_matrix_template = pbqp_alloc_env->ife_matrix_template;
156         unsigned                    *restr_nodes         = pbqp_alloc_env->restr_nodes;
157
158         if (get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
159
160                 /* increase ife edge counter */
161                 pbqp_alloc_env->ife_edge_num[get_irn_idx(src_node)]++;
162                 pbqp_alloc_env->ife_edge_num[get_irn_idx(trg_node)]++;
163
164 #if DO_USEFUL_OPT || USE_BIPARTIT_MATCHING
165                 /* do useful optimization to speed up pbqp solving (we can do this because we know our matrix) */
166                 if (get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
167                         assert(vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs) !=
168                                vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs) &&
169                                "Interfering nodes must not have the same register!");
170                         return;
171                 }
172                 if (get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
173                         if (get_free_regs(restr_nodes, cls, src_node) == 1) {
174                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
175                                 vector_set(get_node(pbqp, get_irn_idx(trg_node))->costs, idx, INF_COSTS);
176                         }
177                         else {
178                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
179                                 vector_set(get_node(pbqp, get_irn_idx(src_node))->costs, idx, INF_COSTS);
180                         }
181                         return;
182                 }
183 #endif
184                 /* insert interference edge */
185                 insert_edge(pbqp, src_node, trg_node, ife_matrix_template);
186         }
187 }
188
189 static void insert_afe_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node, int pos)
190 {
191         pbqp_t                      *pbqp        = pbqp_alloc_env->pbqp_inst;
192         const arch_register_class_t *cls         = pbqp_alloc_env->cls;
193         unsigned                    *restr_nodes = pbqp_alloc_env->restr_nodes;
194         pbqp_matrix_t               *afe_matrix  = pbqp_matrix_alloc(pbqp, arch_register_class_n_regs(cls), arch_register_class_n_regs(cls));
195         unsigned                     colors_n    = arch_register_class_n_regs(cls);
196
197         if (get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
198                 if (use_exec_freq) {
199                         /* get exec_freq for copy_block */
200                         ir_node *root_bl = get_nodes_block(src_node);
201                         ir_node *copy_bl = is_Phi(src_node) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
202                         int      res     = get_block_execfreq_int(&pbqp_alloc_env->execfreq_factors, copy_bl);
203
204                         /* create afe-matrix */
205                         unsigned row, col;
206                         for (row = 0; row < colors_n; row++) {
207                                 for (col = 0; col < colors_n; col++) {
208                                         if (row != col)
209                                                 pbqp_matrix_set(afe_matrix, row, col, (num)res);
210                                 }
211                         }
212                 }
213                 else {
214                         afe_matrix = pbqp_alloc_env->aff_matrix_template;
215                 }
216 #if DO_USEFUL_OPT || USE_BIPARTIT_MATCHING
217                 /* do useful optimization to speed up pbqp solving */
218                 if (get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
219                         return;
220                 }
221                 if (get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
222                         if (get_free_regs(restr_nodes, cls, src_node) == 1) {
223                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
224                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(trg_node))->costs, afe_matrix, regIdx);
225                         }
226                         else {
227                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
228                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(src_node))->costs, afe_matrix, regIdx);
229                         }
230                         return;
231                 }
232 #endif
233                 /* insert interference edge */
234                 insert_edge(pbqp, src_node, trg_node, afe_matrix);
235         }
236 }
237
238 static void create_affinity_edges(ir_node *irn, void *env)
239 {
240         be_pbqp_alloc_env_t         *pbqp_alloc_env = (be_pbqp_alloc_env_t*)env;
241         const arch_register_class_t *cls            = pbqp_alloc_env->cls;
242         const arch_register_req_t   *req            = arch_get_irn_register_req(irn);
243         unsigned                     pos;
244         unsigned                     max;
245
246         if (is_Phi(irn)) { /* Phis */
247                 for (pos = 0, max = get_irn_arity(irn); pos < max; ++pos) {
248                         ir_node *arg = get_irn_n(irn, pos);
249
250                         if (!arch_irn_consider_in_reg_alloc(cls, arg))
251                                 continue;
252
253                         /* no edges to itself */
254                         if (irn == arg) {
255                                 continue;
256                         }
257
258                         insert_afe_edge(pbqp_alloc_env, irn, arg, pos);
259                 }
260         }
261         else if (is_Perm_Proj(irn)) { /* Perms */
262                 ir_node *arg = get_Perm_src(irn);
263                 if (!arch_irn_consider_in_reg_alloc(cls, arg))
264                         return;
265
266                 insert_afe_edge(pbqp_alloc_env, irn, arg, -1);
267         } else if (arch_register_req_is(req, should_be_same)) {
268                 const unsigned other = req->other_same;
269                 int            i;
270
271                 for (i = 0; 1U << i <= other; ++i) {
272                         if (other & (1U << i)) {
273                                 ir_node *other = get_irn_n(skip_Proj(irn), i);
274                                 if (!arch_irn_consider_in_reg_alloc(cls, other))
275                                         continue;
276
277                                 /* no edges to itself */
278                                 if (irn == other) {
279                                         continue;
280                                 }
281
282                                 insert_afe_edge(pbqp_alloc_env, irn, other, i);
283                         }
284                 }
285         }
286 }
287
288 static void create_pbqp_coloring_instance(ir_node *block, void *data)
289 {
290         be_pbqp_alloc_env_t         *pbqp_alloc_env     = (be_pbqp_alloc_env_t*)data;
291         be_lv_t                     *lv                 = pbqp_alloc_env->lv;
292         const arch_register_class_t *cls                = pbqp_alloc_env->cls;
293         plist_t                     *rpeo               = pbqp_alloc_env->rpeo;
294         pbqp_t                      *pbqp_inst          = pbqp_alloc_env->pbqp_inst;
295         plist_t                     *temp_list          = plist_new();
296         plist_element_t             *el;
297         ir_nodeset_t                 live_nodes;
298 #if USE_BIPARTIT_MATCHING
299         int                         *assignment         = ALLOCAN(int, cls->n_regs);
300 #else
301         unsigned                    *restr_nodes        = pbqp_alloc_env->restr_nodes;
302         pqueue_t                    *restr_nodes_queue  = new_pqueue();
303         pqueue_t                    *queue              = new_pqueue();
304         plist_t                     *sorted_list        = plist_new();
305         ir_node                     *last_element       = NULL;
306 #endif
307
308         /* first, determine the pressure */
309         /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
310         create_borders(block, pbqp_alloc_env->env);
311
312         /* calculate living nodes for the first step */
313         ir_nodeset_init(&live_nodes);
314         be_liveness_end_of_block(lv, cls, block, &live_nodes);
315
316         /* create pbqp nodes, interference edges and reverse perfect elimination order */
317         sched_foreach_reverse(block, irn) {
318                 be_foreach_value(irn, value,
319                         if (!arch_irn_consider_in_reg_alloc(cls, value))
320                                 continue;
321
322                         /* create pbqp source node if it dosn't exist */
323                         if (!get_node(pbqp_inst, get_irn_idx(value)))
324                                 create_pbqp_node(pbqp_alloc_env, value);
325
326                         /* create nodes and interference edges */
327                         foreach_ir_nodeset(&live_nodes, live, iter) {
328                                 /* create pbqp source node if it dosn't exist */
329                                 if (!get_node(pbqp_inst, get_irn_idx(live)))
330                                         create_pbqp_node(pbqp_alloc_env, live);
331
332                                 /* no edges to itself */
333                                 if (value == live)
334                                         continue;
335
336                                 insert_ife_edge(pbqp_alloc_env, value, live);
337                         }
338                 );
339
340                 /* get living nodes for next step */
341                 if (!is_Phi(irn)) {
342                         be_liveness_transfer(cls, irn, &live_nodes);
343                 }
344
345 #if USE_BIPARTIT_MATCHING
346                 if (get_irn_mode(irn) == mode_T) {
347                         unsigned     clique_size         = 0;
348                         unsigned     n_alloc             = 0;
349                         pbqp_node   *clique[cls->n_regs];
350                         bipartite_t *bp                  = bipartite_new(cls->n_regs, cls->n_regs);
351
352                         /* add all proj after a perm to clique */
353                         foreach_out_edge(irn, edge) {
354                                 ir_node *proj = get_edge_src_irn(edge);
355
356                                 /* ignore node if it is not necessary for register allocation */
357                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
358                                         continue;
359
360                                 /* insert pbqp node into temp rpeo list of this block */
361                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(proj)));
362
363                                 if(is_Perm_Proj(proj)) {
364                                         /* add proj to clique */
365                                         pbqp_node *clique_member = get_node(pbqp_inst,proj->node_idx);
366                                         vector    *costs         = clique_member->costs;
367                                         unsigned   idx           = 0;
368
369                                         clique[clique_size] = clique_member;
370
371                                         for(idx = 0; idx < costs->len; idx++) {
372                                                 if(costs->entries[idx].data != INF_COSTS) {
373                                                         bipartite_add(bp, clique_size, idx);
374                                                 }
375                                         }
376
377                                         /* increase node counter */
378                                         clique_size++;
379                                         n_alloc++;
380                                 }
381                         }
382
383                         if(clique_size > 0) {
384                                 plist_element_t *listElement;
385                                 foreach_plist(temp_list, listElement) {
386                                         pbqp_node *clique_candidate  = listElement->data;
387                                         unsigned   idx               = 0;
388                                         bool       isMember          = true;
389
390                                         /* clique size not bigger then register class size */
391                                         if(clique_size >= cls->n_regs) break;
392
393                                         for(idx = 0; idx < clique_size; idx++) {
394                                                 pbqp_node *member = clique[idx];
395
396                                                 if(member == clique_candidate) {
397                                                         isMember = false;
398                                                         break;
399                                                 }
400
401                                                 if(get_edge(pbqp_inst, member->index, clique_candidate->index) == NULL && get_edge(pbqp_inst, clique_candidate->index, member->index) == NULL) {
402                                                         isMember = false;
403                                                         break;
404                                                 }
405                                         }
406
407                                         /* goto next list element if current node is not a member of the clique */
408                                         if(!isMember) { continue; }
409
410                                         /* add candidate to clique */
411                                         clique[clique_size] = clique_candidate;
412
413                                         vector *costs = clique_candidate->costs;
414                                         for(idx = 0; idx < costs->len; idx++) {
415                                                 if(costs->entries[idx].data != INF_COSTS) {
416                                                         bipartite_add(bp, clique_size, idx);
417                                                 }
418                                         }
419
420                                         /* increase node counter */
421                                         clique_size++;
422                                 }
423                         }
424
425                         /* solve bipartite matching */
426                         bipartite_matching(bp, assignment);
427
428                         /* assign colors */
429                         unsigned nodeIdx = 0;
430                         for(nodeIdx = 0; nodeIdx < clique_size; nodeIdx++) {
431                                 vector *costs = clique[nodeIdx]->costs;
432                                 int     idx;
433                                 for(idx = 0; idx < (int)costs->len; idx++) {
434                                         if(assignment[nodeIdx] != idx) {
435                                                 costs->entries[idx].data = INF_COSTS;
436                                         }
437                                 }
438                                 assert(assignment[nodeIdx] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
439                         }
440
441                         /* free memory */
442                         bipartite_free(bp);
443                 }
444                 else {
445                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
446                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
447                         }
448                 }
449 #else
450                 /* order nodes for perfect elimination order */
451                 if (get_irn_mode(irn) == mode_T) {
452                         bool allHaveIFEdges = true;
453                         foreach_out_edge(irn, edge) {
454                                 ir_node *proj = get_edge_src_irn(edge);
455                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
456                                         continue;
457
458                                 /* insert proj node into priority queue (descending by the number of interference edges) */
459                                 if (get_free_regs(restr_nodes, cls, proj) <= 4) {
460                                         pqueue_put(restr_nodes_queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
461                                 }
462                                 else {
463                                         pqueue_put(queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
464                                 }
465
466                                 /* skip last step if there is no last_element */
467                                 if(last_element == NULL)
468                                         continue;
469
470                                 /* check if proj has an if edge to last_element (at this time pbqp contains only if edges) */
471                                 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) {
472                                         allHaveIFEdges = false; /* there is no if edge between proj and last_element */
473                                 }
474                         }
475
476                         if(last_element != NULL && allHaveIFEdges) {
477                                 if (get_free_regs(restr_nodes, cls, last_element) <= 4) {
478                                         pqueue_put(restr_nodes_queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
479                                 }
480                                 else {
481                                         pqueue_put(queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
482                                 }
483                                 plist_erase(temp_list, plist_find_value(temp_list, get_node(pbqp_inst, last_element->node_idx)));
484                                 last_element = NULL;
485                         }
486
487                         /* first insert all restricted proj nodes */
488                         while (!pqueue_empty(restr_nodes_queue)) {
489                                 ir_node *node = (ir_node*)pqueue_pop_front(restr_nodes_queue);
490                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(node)));
491                         }
492
493                         /* insert proj nodes descending by their number of interference edges */
494                         while (!pqueue_empty(queue)) {
495                                 ir_node *node = (ir_node*)pqueue_pop_front(queue);
496                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(node)));
497                         }
498
499                         /* invert sorted list */
500                         foreach_plist(sorted_list, el) {
501                                 plist_insert_front(temp_list, el->data);
502                         }
503
504                         plist_clear(sorted_list);
505
506                 }
507                 else {
508                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
509                                 // remember last colorable node
510                                 last_element = irn;
511                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
512                         }
513                         else {
514                                 // node not colorable, so ignore it
515                                 last_element = NULL;
516                         }
517                 }
518 #endif
519         }
520
521         /* add the temp rpeo list of this block to the global reverse perfect elimination order list*/
522         foreach_plist(temp_list, el) {
523                 plist_insert_back(rpeo, el->data);
524         }
525
526         /* free reserved memory */
527         ir_nodeset_destroy(&live_nodes);
528         plist_free(temp_list);
529 #if USE_BIPARTIT_MATCHING
530 #else
531         plist_free(sorted_list);
532         del_pqueue(queue);
533         del_pqueue(restr_nodes_queue);
534 #endif
535 }
536
537 static void insert_perms(ir_node *block, void *data)
538 {
539         be_chordal_env_t *env    = (be_chordal_env_t*)data;
540
541         sched_foreach_safe(block, irn) {
542                 be_insn_t *insn = be_scan_insn(env, irn);
543                 if (insn)
544                         pre_process_constraints(env, &insn);
545         }
546 }
547
548 static void be_pbqp_coloring(be_chordal_env_t *env)
549 {
550         ir_graph                    *irg            = env->irg;
551         const arch_register_class_t *cls            = env->cls;
552         be_lv_t                     *lv             = NULL;
553         plist_element_t             *element        = NULL;
554         unsigned                     colors_n       = arch_register_class_n_regs(cls);
555         be_pbqp_alloc_env_t          pbqp_alloc_env;
556         unsigned                     col;
557         unsigned                     row;
558         pbqp_matrix_t               *ife_matrix;
559         num                          solution;
560 #if KAPS_DUMP
561         FILE                        *file_before;
562 #endif
563 #if TIMER
564         ir_timer_t *t_ra_pbqp_alloc_create     = ir_timer_new();
565         ir_timer_t *t_ra_pbqp_alloc_solve      = ir_timer_new();
566         ir_timer_t *t_ra_pbqp_alloc_create_aff = ir_timer_new();
567
568         printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
569 #endif
570         be_assure_live_sets(irg);
571         lv = be_get_irg_liveness(irg);
572
573         /* insert perms */
574         assure_doms(irg);
575         dom_tree_walk_irg(irg, insert_perms, NULL, env);
576
577         /* dump graph after inserting perms */
578         if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
579                 char buf[256];
580                 snprintf(buf, sizeof(buf), "%s-constr", cls->name);
581                 dump_ir_graph(irg, buf);
582         }
583
584         ir_calculate_execfreq_int_factors(&pbqp_alloc_env.execfreq_factors, irg);
585
586         /* initialize pbqp allocation data structure */
587         pbqp_alloc_env.pbqp_inst        = alloc_pbqp(get_irg_last_idx(irg));  /* initialize pbqp instance */
588         pbqp_alloc_env.cls              = cls;
589         pbqp_alloc_env.irg              = irg;
590         pbqp_alloc_env.lv               = lv;
591         pbqp_alloc_env.allocatable_regs = env->allocatable_regs;
592         pbqp_alloc_env.rpeo             = plist_new();
593         pbqp_alloc_env.restr_nodes      = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
594         pbqp_alloc_env.ife_edge_num     = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
595         pbqp_alloc_env.env              = env;
596
597         /* create costs matrix template for interference edges */
598         ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
599         /* set costs */
600         for (row = 0, col = 0; row < colors_n; row++, col++)
601                 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
602
603         pbqp_alloc_env.ife_matrix_template = ife_matrix;
604
605
606         if (!use_exec_freq) {
607                 /* create costs matrix template for affinity edges */
608                 pbqp_matrix_t *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
609                 /* set costs */
610                 for (row = 0; row < colors_n; row++) {
611                         for (col = 0; col < colors_n; col++) {
612                                 if (row != col)
613                                         pbqp_matrix_set(afe_matrix, row, col, 2);
614                         }
615                 }
616                 pbqp_alloc_env.aff_matrix_template = afe_matrix;
617         }
618
619
620         /* create pbqp instance */
621 #if TIMER
622         ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
623 #endif
624         assure_doms(irg);
625         dom_tree_walk_irg(irg, create_pbqp_coloring_instance , NULL, &pbqp_alloc_env);
626 #if TIMER
627         ir_timer_stop(t_ra_pbqp_alloc_create);
628 #endif
629
630
631         /* set up affinity edges */
632 #if TIMER
633         ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
634 #endif
635         foreach_plist(pbqp_alloc_env.rpeo, element) {
636                 pbqp_node_t *node = (pbqp_node_t*)element->data;
637                 ir_node     *irn  = get_idx_irn(irg, node->index);
638
639                 create_affinity_edges(irn, &pbqp_alloc_env);
640         }
641 #if TIMER
642         ir_timer_stop(t_ra_pbqp_alloc_create_aff);
643 #endif
644
645
646 #if KAPS_DUMP
647         // dump graph before solving pbqp
648         file_before = my_open(env, "", "-pbqp_coloring.html");
649         set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
650 #endif
651
652         /* print out reverse perfect elimination order */
653 #if PRINT_RPEO
654         {
655                 plist_element_t *elements;
656                 foreach_plist(pbqp_alloc_env.rpeo, elements) {
657                         pbqp_node_t *node = elements->data;
658                         printf(" %d(%ld);", node->index, get_idx_irn(irg, node->index)->node_nr);
659                 }
660                 printf("\n");
661         }
662 #endif
663
664         /* solve pbqp instance */
665 #if TIMER
666         ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
667 #endif
668         if(use_late_decision) {
669                 solve_pbqp_heuristical_co_ld(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
670         }
671         else {
672                 solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
673         }
674 #if TIMER
675         ir_timer_stop(t_ra_pbqp_alloc_solve);
676 #endif
677
678
679         solution = get_solution(pbqp_alloc_env.pbqp_inst);
680         if (solution == INF_COSTS)
681                 panic("No PBQP solution found");
682
683
684         /* assign colors */
685         foreach_plist(pbqp_alloc_env.rpeo, element) {
686                 pbqp_node_t           *node  = (pbqp_node_t*)element->data;
687                 ir_node               *irn   = get_idx_irn(irg, node->index);
688                 num                    color = get_node_solution(pbqp_alloc_env.pbqp_inst, node->index);
689                 const arch_register_t *reg   = arch_register_for_index(cls, color);
690
691                 arch_set_irn_register(irn, reg);
692         }
693
694
695 #if TIMER
696         printf("PBQP alloc create:     %10.3lf msec\n",
697                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
698         printf("PBQP alloc solve:      %10.3lf msec\n",
699                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
700         printf("PBQP alloc create aff: %10.3lf msec\n",
701                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
702 #endif
703
704
705         /* free reserved memory */
706 #if KAPS_DUMP
707         fclose(file_before);
708 #endif
709         free_pbqp(pbqp_alloc_env.pbqp_inst);
710         plist_free(pbqp_alloc_env.rpeo);
711         xfree(pbqp_alloc_env.restr_nodes);
712         xfree(pbqp_alloc_env.ife_edge_num);
713 }
714
715
716 /**
717  * Initializes this module.
718  */
719 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_pbqp_coloring)
720 void be_init_pbqp_coloring(void)
721 {
722         lc_opt_entry_t *be_grp       = lc_opt_get_grp(firm_opt_get_root(), "be");
723         lc_opt_entry_t *ra_grp       = lc_opt_get_grp(be_grp, "ra");
724         lc_opt_entry_t *chordal_grp  = lc_opt_get_grp(ra_grp, "chordal");
725         lc_opt_entry_t *coloring_grp = lc_opt_get_grp(chordal_grp, "coloring");
726         lc_opt_entry_t *pbqp_grp     = lc_opt_get_grp(coloring_grp, "pbqp");
727
728         static be_ra_chordal_coloring_t coloring = {
729                 be_pbqp_coloring
730         };
731
732         lc_opt_add_table(pbqp_grp, options);
733         be_register_chordal_coloring("pbqp", &coloring);
734 }