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