4772c746d3ffff7dd824ebc3555119168c54933f
[libfirm] / ir / be / bepbqpcoloring.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       PBQP based register allocation.
23  * @author      Thomas Bersch
24  * @date        27.11.2009
25  * @version     $Id: bechordal.c 26750 2009-11-27 09:37:43Z bersch $
26  */
27
28 /* miscellaneous includes */
29 #include "config.h"
30
31 #include "debug.h"
32 #include "error.h"
33
34 #include "irdom.h"
35 #include "irdump.h"
36 #include "iredges_t.h"
37 #include "irprintf.h"
38 #include "irgwalk.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                 if (!bitset_is_set(allocatable_regs, idx) || !arch_reg_out_is_allocatable(irn, arch_register_for_index(cls, idx))) {
159                         /* constrained */
160                         vector_set(costs_vector, idx, INF_COSTS);
161                         cntConstrains++;
162                 }
163         }
164
165         /* add vector to pbqp node */
166         add_node_costs(pbqp_inst, get_irn_idx(irn), costs_vector);
167         pbqp_alloc_env->restr_nodes[get_irn_idx(irn)] = cntConstrains;
168 }
169
170 static void insert_ife_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node)
171 {
172         pbqp_t                      *pbqp                = pbqp_alloc_env->pbqp_inst;
173         const arch_register_class_t *cls                 = pbqp_alloc_env->cls;
174         pbqp_matrix_t               *ife_matrix_template = pbqp_alloc_env->ife_matrix_template;
175         unsigned                    *restr_nodes         = pbqp_alloc_env->restr_nodes;
176
177         if (get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
178
179                 /* increase ife edge counter */
180                 pbqp_alloc_env->ife_edge_num[get_irn_idx(src_node)]++;
181                 pbqp_alloc_env->ife_edge_num[get_irn_idx(trg_node)]++;
182
183 #if DO_USEFUL_OPT || USE_BIPARTIT_MATCHING
184                 /* do useful optimization to speed up pbqp solving (we can do this because we know our matrix) */
185                 if (get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
186                         assert(vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs) !=
187                                vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs) &&
188                                "Interfering nodes must not have the same register!");
189                         return;
190                 }
191                 if (get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
192                         if (get_free_regs(restr_nodes, cls, src_node) == 1) {
193                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
194                                 vector_set(get_node(pbqp, get_irn_idx(trg_node))->costs, idx, INF_COSTS);
195                         }
196                         else {
197                                 unsigned idx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
198                                 vector_set(get_node(pbqp, get_irn_idx(src_node))->costs, idx, INF_COSTS);
199                         }
200                         return;
201                 }
202 #endif
203                 /* insert interference edge */
204                 insert_edge(pbqp, src_node, trg_node, ife_matrix_template);
205         }
206 }
207
208 static void insert_afe_edge(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *src_node, ir_node *trg_node, int pos)
209 {
210         pbqp_t                      *pbqp        = pbqp_alloc_env->pbqp_inst;
211         const arch_register_class_t *cls         = pbqp_alloc_env->cls;
212         unsigned                    *restr_nodes = pbqp_alloc_env->restr_nodes;
213         pbqp_matrix_t               *afe_matrix  = pbqp_matrix_alloc(pbqp, arch_register_class_n_regs(cls), arch_register_class_n_regs(cls));
214         unsigned                     colors_n    = arch_register_class_n_regs(cls);
215
216         if (get_edge(pbqp, get_irn_idx(src_node), get_irn_idx(trg_node)) == NULL) {
217                 if (use_exec_freq) {
218                         /* get exec_freq for copy_block */
219                         ir_node       *root_bl   = get_nodes_block(src_node);
220                         ir_node       *copy_bl   = is_Phi(src_node) ? get_Block_cfgpred_block(root_bl, pos) : root_bl;
221                         ir_exec_freq  *exec_freq = be_get_irg_exec_freq(pbqp_alloc_env->irg);
222                         unsigned long  res       = get_block_execfreq_ulong(exec_freq, copy_bl);
223
224                         /* create afe-matrix */
225                         unsigned row, col;
226                         for (row = 0; row < colors_n; row++) {
227                                 for (col = 0; col < colors_n; col++) {
228                                         if (row != col)
229                                                 pbqp_matrix_set(afe_matrix, row, col, (num)res);
230                                 }
231                         }
232                 }
233                 else {
234                         afe_matrix = pbqp_alloc_env->aff_matrix_template;
235                 }
236 #if DO_USEFUL_OPT || USE_BIPARTIT_MATCHING
237                 /* do useful optimization to speed up pbqp solving */
238                 if (get_free_regs(restr_nodes, cls, src_node) == 1 && get_free_regs(restr_nodes, cls, trg_node) == 1) {
239                         return;
240                 }
241                 if (get_free_regs(restr_nodes, cls, src_node) == 1 || get_free_regs(restr_nodes, cls, trg_node) == 1) {
242                         if (get_free_regs(restr_nodes, cls, src_node) == 1) {
243                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(src_node))->costs);
244                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(trg_node))->costs, afe_matrix, regIdx);
245                         }
246                         else {
247                                 unsigned regIdx = vector_get_min_index(get_node(pbqp, get_irn_idx(trg_node))->costs);
248                                 vector_add_matrix_col(get_node(pbqp, get_irn_idx(src_node))->costs, afe_matrix, regIdx);
249                         }
250                         return;
251                 }
252 #endif
253                 /* insert interference edge */
254                 insert_edge(pbqp, src_node, trg_node, afe_matrix);
255         }
256 }
257
258 static void create_affinity_edges(ir_node *irn, void *env)
259 {
260         be_pbqp_alloc_env_t         *pbqp_alloc_env = (be_pbqp_alloc_env_t*)env;
261         const arch_register_class_t *cls            = pbqp_alloc_env->cls;
262         const arch_register_req_t   *req            = arch_get_register_req_out(irn);
263         unsigned                     pos;
264         unsigned                     max;
265
266         if (is_Reg_Phi(irn)) { /* Phis */
267                 for (pos = 0, max = get_irn_arity(irn); pos < max; ++pos) {
268                         ir_node *arg = get_irn_n(irn, pos);
269
270                         if (!arch_irn_consider_in_reg_alloc(cls, arg))
271                                 continue;
272
273                         /* no edges to itself */
274                         if (irn == arg) {
275                                 continue;
276                         }
277
278                         insert_afe_edge(pbqp_alloc_env, irn, arg, pos);
279                 }
280         }
281         else if (is_Perm_Proj(irn)) { /* Perms */
282                 ir_node *arg = get_Perm_src(irn);
283                 if (!arch_irn_consider_in_reg_alloc(cls, arg))
284                         return;
285
286                 insert_afe_edge(pbqp_alloc_env, irn, arg, -1);
287         }
288         else { /* 2-address code */
289                 if (is_2addr_code(req)) {
290                         const unsigned other = req->other_same;
291                         int            i;
292
293                         for (i = 0; 1U << i <= other; ++i) {
294                                 if (other & (1U << i)) {
295                                         ir_node *other = get_irn_n(skip_Proj(irn), i);
296                                         if (!arch_irn_consider_in_reg_alloc(cls, other))
297                                                 continue;
298
299                                         /* no edges to itself */
300                                         if (irn == other) {
301                                                 continue;
302                                         }
303
304                                         insert_afe_edge(pbqp_alloc_env, irn, other, i);
305                                 }
306                         }
307                 }
308         }
309 }
310
311 static void create_pbqp_coloring_instance(ir_node *block, void *data)
312 {
313         be_pbqp_alloc_env_t         *pbqp_alloc_env     = (be_pbqp_alloc_env_t*)data;
314         be_lv_t                     *lv                 = pbqp_alloc_env->lv;
315         const arch_register_class_t *cls                = pbqp_alloc_env->cls;
316         plist_t                     *rpeo               = pbqp_alloc_env->rpeo;
317         pbqp_t                      *pbqp_inst          = pbqp_alloc_env->pbqp_inst;
318         plist_t                     *temp_list          = plist_new();
319         plist_element_t             *el;
320         ir_node                     *irn;
321         ir_nodeset_t                 live_nodes;
322 #if USE_BIPARTIT_MATCHING
323         int                         *assignment         = ALLOCAN(int, cls->n_regs);
324 #else
325         unsigned                    *restr_nodes        = pbqp_alloc_env->restr_nodes;
326         pqueue_t                    *restr_nodes_queue  = new_pqueue();
327         pqueue_t                    *queue              = new_pqueue();
328         plist_t                     *sorted_list        = plist_new();
329         ir_node                     *last_element       = NULL;
330 #endif
331
332         /* first, determine the pressure */
333         /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
334         create_borders(block, pbqp_alloc_env->env);
335
336         /* calculate living nodes for the first step */
337         ir_nodeset_init(&live_nodes);
338         be_liveness_end_of_block(lv, cls, block, &live_nodes);
339
340         /* create pbqp nodes, interference edges and reverse perfect elimination order */
341         sched_foreach_reverse(block, irn) {
342                 ir_node               *live;
343                 ir_nodeset_iterator_t  iter;
344
345                 if (get_irn_mode(irn) == mode_T) {
346                         const ir_edge_t *edge;
347                         foreach_out_edge(irn, edge) {
348                                 ir_node *proj = get_edge_src_irn(edge);
349                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
350                                         continue;
351
352                                 /* create pbqp source node if it dosn't exist */
353                                 if (get_node(pbqp_inst, get_irn_idx(proj)) == NULL) {
354                                         create_pbqp_node(pbqp_alloc_env, proj);
355                                 }
356
357                                 /* create nodes and interference edges */
358                                 foreach_ir_nodeset(&live_nodes, live, iter) {
359                                         /* create pbqp source node if it dosn't exist */
360                                         if (get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
361                                                 create_pbqp_node(pbqp_alloc_env, live);
362                                         }
363
364                                         /* no edges to itself */
365                                         if (proj == live) {
366                                                 continue;
367                                         }
368
369                                         insert_ife_edge(pbqp_alloc_env, proj, live);
370                                 }
371                         }
372                 }
373                 else {
374                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
375                                 /* create pbqp source node if it dosn't exist */
376                                 if (get_node(pbqp_inst, get_irn_idx(irn)) == NULL) {
377                                         create_pbqp_node(pbqp_alloc_env, irn);
378                                 }
379
380                                 /* create nodes and interference edges */
381                                 foreach_ir_nodeset(&live_nodes, live, iter) {
382                                         /* create pbqp source node if it dosn't exist */
383                                         if (get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
384                                                 create_pbqp_node(pbqp_alloc_env, live);
385                                         }
386
387                                         /* no edges to itself */
388                                         if (irn == live) {
389                                                 continue;
390                                         }
391
392                                         /* insert interference edge */
393                                         insert_ife_edge(pbqp_alloc_env, irn, live);
394                                 }
395                         }
396                 }
397
398                 /* get living nodes for next step */
399                 if (!is_Phi(irn)) {
400                         be_liveness_transfer(cls, irn, &live_nodes);
401                 }
402
403 #if USE_BIPARTIT_MATCHING
404                 if (get_irn_mode(irn) == mode_T) {
405                         unsigned     clique_size         = 0;
406                         unsigned     n_alloc             = 0;
407                         pbqp_node   *clique[cls->n_regs];
408                         bipartite_t *bp                  = bipartite_new(cls->n_regs, cls->n_regs);
409
410                         /* add all proj after a perm to clique */
411                         const ir_edge_t *edge;
412                         foreach_out_edge(irn, edge) {
413                                 ir_node *proj = get_edge_src_irn(edge);
414
415                                 /* ignore node if it is not necessary for register allocation */
416                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
417                                         continue;
418
419                                 /* insert pbqp node into temp rpeo list of this block */
420                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(proj)));
421
422                                 if(is_Perm_Proj(proj)) {
423                                         /* add proj to clique */
424                                         pbqp_node *clique_member = get_node(pbqp_inst,proj->node_idx);
425                                         vector    *costs         = clique_member->costs;
426                                         unsigned   idx           = 0;
427
428                                         clique[clique_size] = clique_member;
429
430                                         for(idx = 0; idx < costs->len; idx++) {
431                                                 if(costs->entries[idx].data != INF_COSTS) {
432                                                         bipartite_add(bp, clique_size, idx);
433                                                 }
434                                         }
435
436                                         /* increase node counter */
437                                         clique_size++;
438                                         n_alloc++;
439                                 }
440                         }
441
442                         if(clique_size > 0) {
443                                 plist_element_t *listElement;
444                                 foreach_plist(temp_list, listElement) {
445                                         pbqp_node *clique_candidate  = listElement->data;
446                                         unsigned   idx               = 0;
447                                         bool       isMember          = true;
448
449                                         /* clique size not bigger then register class size */
450                                         if(clique_size >= cls->n_regs) break;
451
452                                         for(idx = 0; idx < clique_size; idx++) {
453                                                 pbqp_node *member = clique[idx];
454
455                                                 if(member == clique_candidate) {
456                                                         isMember = false;
457                                                         break;
458                                                 }
459
460                                                 if(get_edge(pbqp_inst, member->index, clique_candidate->index) == NULL && get_edge(pbqp_inst, clique_candidate->index, member->index) == NULL) {
461                                                         isMember = false;
462                                                         break;
463                                                 }
464                                         }
465
466                                         /* goto next list element if current node is not a member of the clique */
467                                         if(!isMember) { continue; }
468
469                                         /* add candidate to clique */
470                                         clique[clique_size] = clique_candidate;
471
472                                         vector *costs = clique_candidate->costs;
473                                         for(idx = 0; idx < costs->len; idx++) {
474                                                 if(costs->entries[idx].data != INF_COSTS) {
475                                                         bipartite_add(bp, clique_size, idx);
476                                                 }
477                                         }
478
479                                         /* increase node counter */
480                                         clique_size++;
481                                 }
482                         }
483
484                         /* solve bipartite matching */
485                         bipartite_matching(bp, assignment);
486
487                         /* assign colors */
488                         unsigned nodeIdx = 0;
489                         for(nodeIdx = 0; nodeIdx < clique_size; nodeIdx++) {
490                                 vector *costs = clique[nodeIdx]->costs;
491                                 int     idx;
492                                 for(idx = 0; idx < (int)costs->len; idx++) {
493                                         if(assignment[nodeIdx] != idx) {
494                                                 costs->entries[idx].data = INF_COSTS;
495                                         }
496                                 }
497                                 assert(assignment[nodeIdx] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
498                         }
499
500                         /* free memory */
501                         bipartite_free(bp);
502                 }
503                 else {
504                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
505                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
506                         }
507                 }
508 #else
509                 /* order nodes for perfect elimination order */
510                 if (get_irn_mode(irn) == mode_T) {
511                         bool             allHaveIFEdges = true;
512                         const ir_edge_t *edge;
513
514                         foreach_out_edge(irn, edge) {
515                                 ir_node *proj = get_edge_src_irn(edge);
516                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
517                                         continue;
518
519                                 /* insert proj node into priority queue (descending by the number of interference edges) */
520                                 if (get_free_regs(restr_nodes, cls, proj) <= 4) {
521                                         pqueue_put(restr_nodes_queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
522                                 }
523                                 else {
524                                         pqueue_put(queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
525                                 }
526
527                                 /* skip last step if there is no last_element */
528                                 if(last_element == NULL)
529                                         continue;
530
531                                 /* check if proj has an if edge to last_element (at this time pbqp contains only if edges) */
532                                 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) {
533                                         allHaveIFEdges = false; /* there is no if edge between proj and last_element */
534                                 }
535                         }
536
537                         if(last_element != NULL && allHaveIFEdges) {
538                                 if (get_free_regs(restr_nodes, cls, last_element) <= 4) {
539                                         pqueue_put(restr_nodes_queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
540                                 }
541                                 else {
542                                         pqueue_put(queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
543                                 }
544                                 plist_erase(temp_list, plist_find_value(temp_list, get_node(pbqp_inst, last_element->node_idx)));
545                                 last_element = NULL;
546                         }
547
548                         /* first insert all restricted proj nodes */
549                         while (!pqueue_empty(restr_nodes_queue)) {
550                                 ir_node *node = (ir_node*)pqueue_pop_front(restr_nodes_queue);
551                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(node)));
552                         }
553
554                         /* insert proj nodes descending by their number of interference edges */
555                         while (!pqueue_empty(queue)) {
556                                 ir_node *node = (ir_node*)pqueue_pop_front(queue);
557                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(node)));
558                         }
559
560                         /* invert sorted list */
561                         foreach_plist(sorted_list, el) {
562                                 plist_insert_front(temp_list, el->data);
563                         }
564
565                         plist_clear(sorted_list);
566
567                 }
568                 else {
569                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
570                                 // remember last colorable node
571                                 last_element = irn;
572                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
573                         }
574                         else {
575                                 // node not colorable, so ignore it
576                                 last_element = NULL;
577                         }
578                 }
579 #endif
580         }
581
582         /* add the temp rpeo list of this block to the global reverse perfect elimination order list*/
583         foreach_plist(temp_list, el) {
584                 plist_insert_back(rpeo, el->data);
585         }
586
587         /* free reserved memory */
588         ir_nodeset_destroy(&live_nodes);
589         plist_free(temp_list);
590 #if USE_BIPARTIT_MATCHING
591 #else
592         plist_free(sorted_list);
593         del_pqueue(queue);
594         del_pqueue(restr_nodes_queue);
595 #endif
596 }
597
598 static void insert_perms(ir_node *block, void *data)
599 {
600         be_chordal_env_t *env    = (be_chordal_env_t*)data;
601         ir_node          *irn;
602
603         for (irn = sched_first(block); !sched_is_end(irn);) {
604                 be_insn_t *insn = chordal_scan_insn(env, irn);
605                 irn             = insn->next_insn;
606
607                 if (!insn->has_constraints)
608                         continue;
609
610                 pre_process_constraints(env, &insn);
611         }
612 }
613
614 static void be_pbqp_coloring(be_chordal_env_t *env)
615 {
616         ir_graph                    *irg            = env->irg;
617         const arch_register_class_t *cls            = env->cls;
618         be_lv_t                     *lv             = NULL;
619         plist_element_t             *element        = NULL;
620         unsigned                     colors_n       = arch_register_class_n_regs(cls);
621         be_pbqp_alloc_env_t          pbqp_alloc_env;
622         unsigned                     col;
623         unsigned                     row;
624         pbqp_matrix_t               *ife_matrix;
625         num                          solution;
626 #if KAPS_DUMP
627         FILE                        *file_before;
628 #endif
629 #if TIMER
630         ir_timer_t *t_ra_pbqp_alloc_create     = ir_timer_new();
631         ir_timer_t *t_ra_pbqp_alloc_solve      = ir_timer_new();
632         ir_timer_t *t_ra_pbqp_alloc_create_aff = ir_timer_new();
633
634         printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
635 #endif
636         lv = be_assure_liveness(irg);
637         be_liveness_assure_sets(lv);
638         be_liveness_assure_chk(lv);
639
640         /* insert perms */
641         assure_doms(irg);
642         dom_tree_walk_irg(irg, insert_perms, NULL, env);
643
644         /* dump graph after inserting perms */
645         if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
646                 char buf[256];
647                 snprintf(buf, sizeof(buf), "-%s-constr", cls->name);
648                 dump_ir_graph(irg, buf);
649         }
650
651
652         /* initialize pbqp allocation data structure */
653         pbqp_alloc_env.pbqp_inst        = alloc_pbqp(get_irg_last_idx(irg));  /* initialize pbqp instance */
654         pbqp_alloc_env.cls              = cls;
655         pbqp_alloc_env.irg              = irg;
656         pbqp_alloc_env.lv               = lv;
657         pbqp_alloc_env.allocatable_regs = bitset_malloc(colors_n);
658         pbqp_alloc_env.rpeo             = plist_new();
659         pbqp_alloc_env.restr_nodes      = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
660         pbqp_alloc_env.ife_edge_num     = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
661         pbqp_alloc_env.env              = env;
662         be_put_allocatable_regs(irg, cls, pbqp_alloc_env.allocatable_regs);
663
664
665         /* create costs matrix template for interference edges */
666         ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
667         /* set costs */
668         for (row = 0, col = 0; row < colors_n; row++, col++)
669                 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
670
671         pbqp_alloc_env.ife_matrix_template = ife_matrix;
672
673
674         if (!use_exec_freq) {
675                 /* create costs matrix template for affinity edges */
676                 pbqp_matrix_t *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
677                 /* set costs */
678                 for (row = 0; row < colors_n; row++) {
679                         for (col = 0; col < colors_n; col++) {
680                                 if (row != col)
681                                         pbqp_matrix_set(afe_matrix, row, col, 2);
682                         }
683                 }
684                 pbqp_alloc_env.aff_matrix_template = afe_matrix;
685         }
686
687
688         /* create pbqp instance */
689 #if TIMER
690         ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
691 #endif
692         assure_doms(irg);
693         dom_tree_walk_irg(irg, create_pbqp_coloring_instance , NULL, &pbqp_alloc_env);
694 #if TIMER
695         ir_timer_stop(t_ra_pbqp_alloc_create);
696 #endif
697
698
699         /* set up affinity edges */
700 #if TIMER
701         ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
702 #endif
703         foreach_plist(pbqp_alloc_env.rpeo, element) {
704                 pbqp_node_t *node = (pbqp_node_t*)element->data;
705                 ir_node     *irn  = get_idx_irn(irg, node->index);
706
707                 create_affinity_edges(irn, &pbqp_alloc_env);
708         }
709 #if TIMER
710         ir_timer_stop(t_ra_pbqp_alloc_create_aff);
711 #endif
712
713
714 #if KAPS_DUMP
715         // dump graph before solving pbqp
716         file_before = my_open(env, "", "-pbqp_coloring.html");
717         set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
718 #endif
719
720         /* print out reverse perfect elimination order */
721 #if PRINT_RPEO
722         {
723                 plist_element_t *elements;
724                 foreach_plist(pbqp_alloc_env.rpeo, elements) {
725                         pbqp_node_t *node = elements->data;
726                         printf(" %d(%ld);", node->index, get_idx_irn(irg, node->index)->node_nr);
727                 }
728                 printf("\n");
729         }
730 #endif
731
732         /* solve pbqp instance */
733 #if TIMER
734         ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
735 #endif
736         if(use_late_decision) {
737                 solve_pbqp_heuristical_co_ld(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
738         }
739         else {
740                 solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
741         }
742 #if TIMER
743         ir_timer_stop(t_ra_pbqp_alloc_solve);
744 #endif
745
746
747         solution = get_solution(pbqp_alloc_env.pbqp_inst);
748         if (solution == INF_COSTS)
749                 panic("No PBQP solution found");
750
751
752         /* assign colors */
753         foreach_plist(pbqp_alloc_env.rpeo, element) {
754                 pbqp_node_t           *node  = (pbqp_node_t*)element->data;
755                 ir_node               *irn   = get_idx_irn(irg, node->index);
756                 num                    color = get_node_solution(pbqp_alloc_env.pbqp_inst, node->index);
757                 const arch_register_t *reg   = arch_register_for_index(cls, color);
758
759                 arch_set_irn_register(irn, reg);
760         }
761
762
763 #if TIMER
764         printf("PBQP alloc create:     %10.3lf msec\n",
765                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
766         printf("PBQP alloc solve:      %10.3lf msec\n",
767                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
768         printf("PBQP alloc create aff: %10.3lf msec\n",
769                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
770 #endif
771
772
773         /* free reserved memory */
774 #if KAPS_DUMP
775         fclose(file_before);
776 #endif
777         bitset_free(pbqp_alloc_env.allocatable_regs);
778         free_pbqp(pbqp_alloc_env.pbqp_inst);
779         plist_free(pbqp_alloc_env.rpeo);
780         xfree(pbqp_alloc_env.restr_nodes);
781         xfree(pbqp_alloc_env.ife_edge_num);
782 }
783
784
785 /**
786  * Initializes this module.
787  */
788 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_pbqp_coloring)
789 void be_init_pbqp_coloring(void)
790 {
791         lc_opt_entry_t *be_grp       = lc_opt_get_grp(firm_opt_get_root(), "be");
792         lc_opt_entry_t *ra_grp       = lc_opt_get_grp(be_grp, "ra");
793         lc_opt_entry_t *chordal_grp  = lc_opt_get_grp(ra_grp, "chordal");
794         lc_opt_entry_t *coloring_grp = lc_opt_get_grp(chordal_grp, "coloring");
795         lc_opt_entry_t *pbqp_grp     = lc_opt_get_grp(coloring_grp, "pbqp");
796
797         static be_ra_chordal_coloring_t coloring = {
798                 be_pbqp_coloring
799         };
800
801         lc_opt_add_table(pbqp_grp, options);
802         be_register_chordal_coloring("pbqp", &coloring);
803 }