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