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