Adapt to coding conventions.
[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 #ifdef FIRM_KAPS
32
33 #include "debug.h"
34 #include "error.h"
35
36 #include "irdom.h"
37 #include "irdump.h"
38 #include "iredges_t.h"
39 #include "irprintf.h"
40 #include "irgwalk.h"
41 #include "time.h"
42
43 /* libfirm/ir/adt includes */
44 #include "bipartite.h"
45
46 /* libfirm/ir/be includes */
47 #include "bearch.h"
48 #include "beirg.h"
49 #include "besched.h"
50 #include "bemodule.h"
51 #include "bechordal_common.h"
52 #include "bechordal.h"
53 #include "bechordal_t.h"
54 #include "beinsn_t.h"
55 #include "benode.h"
56 #include "belive.h"
57 #include "belive_t.h"
58 #include "beutil.h"
59 #include "plist.h"
60 #include "pqueue.h"
61 #include "becopyopt.h"
62
63 /* pbqp includes */
64 #include "kaps.h"
65 #include "matrix.h"
66 #include "vector.h"
67 #include "vector_t.h"
68 #include "heuristical_co.h"
69 #include "heuristical_co_ld.h"
70 #include "pbqp_t.h"
71 #include "html_dumper.h"
72 #include "pbqp_node_t.h"
73 #include "pbqp_node.h"
74 #include "pbqp_edge_t.h"
75
76 #define TIMER                 0
77 #define PRINT_RPEO            0
78 #define USE_BIPARTIT_MATCHING 0
79 #define DO_USEFUL_OPT         1
80
81
82 static int use_exec_freq     = true;
83 static int use_late_decision = false;
84
85 typedef struct _be_pbqp_alloc_env_t {
86         pbqp                        *pbqp_inst;                 /**< PBQP instance for register allocation */
87         ir_graph                    *irg;                       /**< The graph under examination. */
88         const arch_register_class_t *cls;                               /**< Current processed register class */
89         be_lv_t                     *lv;
90         bitset_t                    *ignored_regs;
91         pbqp_matrix                 *ife_matrix_template;
92         pbqp_matrix                 *aff_matrix_template;
93         plist_t                     *rpeo;
94         unsigned                    *restr_nodes;
95         unsigned                    *ife_edge_num;
96         be_chordal_env_t            *env;
97 } be_pbqp_alloc_env_t;
98
99
100 #define is_Reg_Phi(irn)                                        (is_Phi(irn) && mode_is_data(get_irn_mode(irn)))
101 #define get_Perm_src(irn)                                      (get_irn_n(get_Proj_pred(irn), get_Proj_proj(irn)))
102 #define is_Perm_Proj(irn)                                      (is_Proj(irn) && be_is_Perm(get_Proj_pred(irn)))
103 #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)))
104 #define get_free_regs(restr_nodes, cls, irn)                   (arch_register_class_n_regs(cls) - restr_nodes[get_irn_idx(irn)])
105
106 static inline int is_2addr_code(const arch_register_req_t *req)
107 {
108         return (req->type & arch_register_req_type_should_be_same) != 0;
109 }
110
111 static const lc_opt_table_entry_t options[] = {
112         LC_OPT_ENT_BOOL("exec_freq", "use exec_freq",  &use_exec_freq),
113         LC_OPT_ENT_BOOL("late_decision", "use late decision for register allocation",  &use_late_decision),
114         LC_OPT_LAST
115 };
116
117 #if KAPS_DUMP
118 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
119 {
120         FILE       *result;
121         char        buf[1024];
122         size_t      i;
123         size_t      n;
124         char       *tu_name;
125         const char *cup_name = be_get_irg_main_env(env->irg)->cup_name;
126
127         n = strlen(cup_name);
128         tu_name = XMALLOCN(char, n + 1);
129         strcpy(tu_name, cup_name);
130         for (i = 0; i < n; ++i)
131                 if (tu_name[i] == '.')
132                         tu_name[i] = '_';
133
134         ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
135         xfree(tu_name);
136         result = fopen(buf, "wt");
137         if (result == NULL) {
138                 panic("Couldn't open '%s' for writing.", buf);
139         }
140
141         return result;
142 }
143 #endif
144
145
146 static void create_pbqp_node(be_pbqp_alloc_env_t *pbqp_alloc_env, ir_node *irn)
147 {
148         const arch_register_class_t *cls = pbqp_alloc_env->cls;
149         pbqp     *pbqp_inst              = pbqp_alloc_env->pbqp_inst;
150         bitset_t *ignored_regs           = pbqp_alloc_env->ignored_regs;
151         unsigned  colors_n               = arch_register_class_n_regs(cls);
152         unsigned  cntConstrains          = 0;
153
154         /* create costs vector depending on register constrains */
155         struct vector *costs_vector = vector_alloc(pbqp_inst, colors_n);
156
157         /* set costs depending on register constrains */
158         unsigned idx;
159         for (idx = 0; idx < colors_n; idx++) {
160                 if (bitset_is_set(ignored_regs, idx) || !arch_reg_out_is_allocatable(irn, arch_register_for_index(cls, idx))) {
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                        *pbqp                = pbqp_alloc_env->pbqp_inst;
175         const arch_register_class_t *cls                 = pbqp_alloc_env->cls;
176         pbqp_matrix                 *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                        *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                 *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 = env;
263         const arch_register_class_t *cls            = pbqp_alloc_env->cls;
264         const arch_register_req_t   *req            = arch_get_register_req_out(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     = 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                        *pbqp_inst          = pbqp_alloc_env->pbqp_inst;
320         plist_t                     *temp_list          = plist_new();
321         plist_element_t             *el;
322         ir_node                     *irn;
323         ir_nodeset_t                 live_nodes;
324 #if USE_BIPARTIT_MATCHING
325         int                         *assignment         = ALLOCAN(int, cls->n_regs);
326 #else
327         unsigned                    *restr_nodes        = pbqp_alloc_env->restr_nodes;
328         pqueue_t                    *restr_nodes_queue  = new_pqueue();
329         pqueue_t                    *queue              = new_pqueue();
330         plist_t                     *sorted_list        = plist_new();
331         ir_node                     *last_element       = NULL;
332 #endif
333
334         /* first, determine the pressure */
335         /* (this is only for compatibility with copymin optimization, it's not needed for pbqp coloring) */
336         create_borders(block, pbqp_alloc_env->env);
337
338         /* calculate living nodes for the first step */
339         ir_nodeset_init(&live_nodes);
340         be_liveness_end_of_block(lv, cls, block, &live_nodes);
341
342         /* create pbqp nodes, interference edges and reverse perfect elimination order */
343         sched_foreach_reverse(block, irn) {
344                 ir_node               *live;
345                 ir_nodeset_iterator_t  iter;
346
347                 if (get_irn_mode(irn) == mode_T) {
348                         const ir_edge_t *edge;
349                         foreach_out_edge(irn, edge) {
350                                 ir_node *proj = get_edge_src_irn(edge);
351                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
352                                         continue;
353
354                                 /* create pbqp source node if it dosn't exist */
355                                 if (get_node(pbqp_inst, get_irn_idx(proj)) == NULL) {
356                                         create_pbqp_node(pbqp_alloc_env, proj);
357                                 }
358
359                                 /* create nodes and interference edges */
360                                 foreach_ir_nodeset(&live_nodes, live, iter) {
361                                         /* create pbqp source node if it dosn't exist */
362                                         if (get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
363                                                 create_pbqp_node(pbqp_alloc_env, live);
364                                         }
365
366                                         /* no edges to itself */
367                                         if (proj == live) {
368                                                 continue;
369                                         }
370
371                                         insert_ife_edge(pbqp_alloc_env, proj, live);
372                                 }
373                         }
374                 }
375                 else {
376                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
377                                 /* create pbqp source node if it dosn't exist */
378                                 if (get_node(pbqp_inst, get_irn_idx(irn)) == NULL) {
379                                         create_pbqp_node(pbqp_alloc_env, irn);
380                                 }
381
382                                 /* create nodes and interference edges */
383                                 foreach_ir_nodeset(&live_nodes, live, iter) {
384                                         /* create pbqp source node if it dosn't exist */
385                                         if (get_node(pbqp_inst, get_irn_idx(live)) == NULL) {
386                                                 create_pbqp_node(pbqp_alloc_env, live);
387                                         }
388
389                                         /* no edges to itself */
390                                         if (irn == live) {
391                                                 continue;
392                                         }
393
394                                         /* insert interference edge */
395                                         insert_ife_edge(pbqp_alloc_env, irn, live);
396                                 }
397                         }
398                 }
399
400                 /* get living nodes for next step */
401                 if (!is_Phi(irn)) {
402                         be_liveness_transfer(cls, irn, &live_nodes);
403                 }
404
405 #if USE_BIPARTIT_MATCHING
406                 if (get_irn_mode(irn) == mode_T) {
407                         unsigned     clique_size         = 0;
408                         unsigned     n_alloc             = 0;
409                         pbqp_node   *clique[cls->n_regs];
410                         bipartite_t *bp                  = bipartite_new(cls->n_regs, cls->n_regs);
411
412                         /* add all proj after a perm to clique */
413                         const ir_edge_t *edge;
414                         foreach_out_edge(irn, edge) {
415                                 ir_node *proj = get_edge_src_irn(edge);
416
417                                 /* ignore node if it is not necessary for register allocation */
418                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
419                                         continue;
420
421                                 /* insert pbqp node into temp rpeo list of this block */
422                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(proj)));
423
424                                 if(is_Perm_Proj(proj)) {
425                                         /* add proj to clique */
426                                         pbqp_node *clique_member = get_node(pbqp_inst,proj->node_idx);
427                                         vector    *costs         = clique_member->costs;
428                                         unsigned   idx           = 0;
429
430                                         clique[clique_size] = clique_member;
431
432                                         for(idx = 0; idx < costs->len; idx++) {
433                                                 if(costs->entries[idx].data != INF_COSTS) {
434                                                         bipartite_add(bp, clique_size, idx);
435                                                 }
436                                         }
437
438                                         /* increase node counter */
439                                         clique_size++;
440                                         n_alloc++;
441                                 }
442                         }
443
444                         if(clique_size > 0) {
445                                 plist_element_t *listElement;
446                                 foreach_plist(temp_list, listElement) {
447                                         pbqp_node *clique_candidate  = listElement->data;
448                                         unsigned   idx               = 0;
449                                         bool       isMember          = true;
450
451                                         /* clique size not bigger then register class size */
452                                         if(clique_size >= cls->n_regs) break;
453
454                                         for(idx = 0; idx < clique_size; idx++) {
455                                                 pbqp_node *member = clique[idx];
456
457                                                 if(member == clique_candidate) {
458                                                         isMember = false;
459                                                         break;
460                                                 }
461
462                                                 if(get_edge(pbqp_inst, member->index, clique_candidate->index) == NULL && get_edge(pbqp_inst, clique_candidate->index, member->index) == NULL) {
463                                                         isMember = false;
464                                                         break;
465                                                 }
466                                         }
467
468                                         /* goto next list element if current node is not a member of the clique */
469                                         if(!isMember) { continue; }
470
471                                         /* add candidate to clique */
472                                         clique[clique_size] = clique_candidate;
473
474                                         vector *costs = clique_candidate->costs;
475                                         for(idx = 0; idx < costs->len; idx++) {
476                                                 if(costs->entries[idx].data != INF_COSTS) {
477                                                         bipartite_add(bp, clique_size, idx);
478                                                 }
479                                         }
480
481                                         /* increase node counter */
482                                         clique_size++;
483                                 }
484                         }
485
486                         /* solve bipartite matching */
487                         bipartite_matching(bp, assignment);
488
489                         /* assign colors */
490                         unsigned nodeIdx = 0;
491                         for(nodeIdx = 0; nodeIdx < clique_size; nodeIdx++) {
492                                 vector *costs = clique[nodeIdx]->costs;
493                                 int     idx;
494                                 for(idx = 0; idx < (int)costs->len; idx++) {
495                                         if(assignment[nodeIdx] != idx) {
496                                                 costs->entries[idx].data = INF_COSTS;
497                                         }
498                                 }
499                                 assert(assignment[nodeIdx] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
500                         }
501
502                         /* free memory */
503                         bipartite_free(bp);
504                 }
505                 else {
506                         if (arch_irn_consider_in_reg_alloc(cls, irn)) {
507                                 plist_insert_front(temp_list, get_node(pbqp_inst, get_irn_idx(irn)));
508                         }
509                 }
510 #else
511                 /* order nodes for perfect elimination order */
512                 if (get_irn_mode(irn) == mode_T) {
513                         bool             allHaveIFEdges = true;
514                         const ir_edge_t *edge;
515
516                         foreach_out_edge(irn, edge) {
517                                 ir_node *proj = get_edge_src_irn(edge);
518                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
519                                         continue;
520
521                                 /* insert proj node into priority queue (descending by the number of interference edges) */
522                                 if (get_free_regs(restr_nodes, cls, proj) <= 4) {
523                                         pqueue_put(restr_nodes_queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
524                                 }
525                                 else {
526                                         pqueue_put(queue, proj, pbqp_alloc_env->ife_edge_num[get_irn_idx(proj)]);
527                                 }
528
529                                 /* skip last step if there is no last_element */
530                                 if(last_element == NULL)
531                                         continue;
532
533                                 /* check if proj has an if edge to last_element (at this time pbqp contains only if edges) */
534                                 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) {
535                                         allHaveIFEdges = false; /* there is no if edge between proj and last_element */
536                                 }
537                         }
538
539                         if(last_element != NULL && allHaveIFEdges) {
540                                 if (get_free_regs(restr_nodes, cls, last_element) <= 4) {
541                                         pqueue_put(restr_nodes_queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
542                                 }
543                                 else {
544                                         pqueue_put(queue, last_element, pbqp_alloc_env->ife_edge_num[get_irn_idx(last_element)]);
545                                 }
546                                 plist_erase(temp_list, plist_find_value(temp_list, get_node(pbqp_inst, last_element->node_idx)));
547                                 last_element = NULL;
548                         }
549
550                         /* first insert all restricted proj nodes */
551                         while (!pqueue_empty(restr_nodes_queue)) {
552                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(restr_nodes_queue))));
553                         }
554
555                         /* insert proj nodes descending by their number of interference edges */
556                         while (!pqueue_empty(queue)) {
557                                 plist_insert_front(sorted_list, get_node(pbqp_inst, get_irn_idx(pqueue_pop_front(queue))));
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         /*
601          * Start silent in the start block.
602          * The silence remains until the first barrier is seen.
603          * Each other block is begun loud.
604          */
605         be_chordal_env_t *env    = data;
606         ir_node          *irn;
607         int               silent = block == get_irg_start_block(get_irn_irg(block));
608
609         /*
610          * If the block is the start block search the barrier and
611          * start handling constraints from there.
612          */
613         for (irn = sched_first(block); !sched_is_end(irn);) {
614                 int silent_old = silent;        /* store old silent value */
615                 if (be_is_Barrier(irn))
616                         silent = !silent;               /* toggle silent flag */
617
618                 be_insn_t *insn = chordal_scan_insn(env, irn);
619                 irn             = insn->next_insn;
620
621                 if (silent_old)
622                         continue;
623
624                 if (!insn->has_constraints)
625                         continue;
626
627                 pre_process_constraints(env, &insn);
628         }
629 }
630
631 static void be_pbqp_coloring(be_chordal_env_t *env)
632 {
633         ir_graph                    *irg            = env->irg;
634         const arch_register_class_t *cls            = env->cls;
635         be_lv_t                     *lv             = NULL;
636         plist_element_t             *element        = NULL;
637         unsigned                     colors_n       = arch_register_class_n_regs(cls);
638         be_pbqp_alloc_env_t          pbqp_alloc_env;
639         unsigned                     col;
640         unsigned                     row;
641
642 #if TIMER
643         ir_timer_t *t_ra_pbqp_alloc_create     = ir_timer_new();
644         ir_timer_t *t_ra_pbqp_alloc_solve      = ir_timer_new();
645         ir_timer_t *t_ra_pbqp_alloc_create_aff = ir_timer_new();
646
647         printf("#### ----- === Allocating registers of %s (%s) ===\n", cls->name, get_entity_name(get_irg_entity(irg)));
648 #endif
649         lv = be_assure_liveness(irg);
650         be_liveness_assure_sets(lv);
651         be_liveness_assure_chk(lv);
652
653         /* insert perms */
654         assure_doms(irg);
655         dom_tree_walk_irg(irg, insert_perms, NULL, env);
656
657         /* dump graph after inserting perms */
658         if (env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
659                 char buf[256];
660                 snprintf(buf, sizeof(buf), "-%s-constr", cls->name);
661                 dump_ir_graph(irg, buf);
662         }
663
664
665         /* initialize pbqp allocation data structure */
666         pbqp_alloc_env.pbqp_inst    = alloc_pbqp(get_irg_last_idx(irg));                /* initialize pbqp instance */
667         pbqp_alloc_env.cls          = cls;
668         pbqp_alloc_env.irg          = irg;
669         pbqp_alloc_env.lv           = lv;
670         pbqp_alloc_env.ignored_regs = bitset_malloc(colors_n);
671         pbqp_alloc_env.rpeo         = plist_new();
672         pbqp_alloc_env.restr_nodes  = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
673         pbqp_alloc_env.ife_edge_num = XMALLOCNZ(unsigned, get_irg_last_idx(irg));
674         pbqp_alloc_env.env          = env;
675         be_put_ignore_regs(irg, cls, pbqp_alloc_env.ignored_regs);                              /* get ignored registers */
676
677
678         /* create costs matrix template for interference edges */
679         struct pbqp_matrix *ife_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
680         /* set costs */
681         for (row = 0, col = 0; row < colors_n; row++, col++)
682                 pbqp_matrix_set(ife_matrix, row, col, INF_COSTS);
683
684         pbqp_alloc_env.ife_matrix_template = ife_matrix;
685
686
687         if (!use_exec_freq) {
688                 /* create costs matrix template for affinity edges */
689                 struct pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp_alloc_env.pbqp_inst, colors_n, colors_n);
690                 /* set costs */
691                 for (row = 0; row < colors_n; row++) {
692                         for (col = 0; col < colors_n; col++) {
693                                 if (row != col)
694                                         pbqp_matrix_set(afe_matrix, row, col, 2);
695                         }
696                 }
697                 pbqp_alloc_env.aff_matrix_template = afe_matrix;
698         }
699
700
701         /* create pbqp instance */
702 #if TIMER
703         ir_timer_reset_and_start(t_ra_pbqp_alloc_create);
704 #endif
705         assure_doms(irg);
706         dom_tree_walk_irg(irg, create_pbqp_coloring_instance , NULL, &pbqp_alloc_env);
707 #if TIMER
708         ir_timer_stop(t_ra_pbqp_alloc_create);
709 #endif
710
711
712         /* set up affinity edges */
713 #if TIMER
714         ir_timer_reset_and_start(t_ra_pbqp_alloc_create_aff);
715 #endif
716         foreach_plist(pbqp_alloc_env.rpeo, element) {
717                 pbqp_node *node = element->data;
718                 ir_node   *irn  = get_idx_irn(irg, node->index);
719
720                 create_affinity_edges(irn, &pbqp_alloc_env);
721         }
722 #if TIMER
723         ir_timer_stop(t_ra_pbqp_alloc_create_aff);
724 #endif
725
726
727 #if KAPS_DUMP
728         // dump graph before solving pbqp
729         FILE *file_before = my_open(env, "", "-pbqp_coloring.html");
730         set_dumpfile(pbqp_alloc_env.pbqp_inst, file_before);
731 #endif
732
733         /* print out reverse perfect eleminiation order */
734 #if PRINT_RPEO
735         plist_element_t *elements;
736         foreach_plist(pbqp_alloc_env.rpeo, elements) {
737                 pbqp_node *node                    = elements->data;
738                 printf(" %d(%lu);", node->index, get_idx_irn(irg, node->index)->node_nr);
739         }
740         printf("\n");
741 #endif
742
743         /* solve pbqp instance */
744 #if TIMER
745         ir_timer_reset_and_start(t_ra_pbqp_alloc_solve);
746 #endif
747         if(use_late_decision) {
748                 solve_pbqp_heuristical_co_ld(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
749         }
750         else {
751                 solve_pbqp_heuristical_co(pbqp_alloc_env.pbqp_inst,pbqp_alloc_env.rpeo);
752         }
753 #if TIMER
754         ir_timer_stop(t_ra_pbqp_alloc_solve);
755 #endif
756
757
758         num solution = get_solution(pbqp_alloc_env.pbqp_inst);
759         if (solution == INF_COSTS)
760                 panic("No PBQP solution found");
761
762
763         /* assign colors */
764         foreach_plist(pbqp_alloc_env.rpeo, element) {
765                 pbqp_node             *node  = element->data;
766                 ir_node               *irn   = get_idx_irn(irg, node->index);
767                 num                    color = get_node_solution(pbqp_alloc_env.pbqp_inst, node->index);
768                 const arch_register_t *reg   = arch_register_for_index(cls, color);
769
770                 arch_set_irn_register(irn, reg);
771         }
772
773
774 #if TIMER
775         printf("PBQP alloc create:     %10.3lf msec\n",
776                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create) / 1000.0);
777         printf("PBQP alloc solve:      %10.3lf msec\n",
778                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_solve) / 1000.0);
779         printf("PBQP alloc create aff: %10.3lf msec\n",
780                (double)ir_timer_elapsed_usec(t_ra_pbqp_alloc_create_aff) / 1000.0);
781 #endif
782
783
784         /* free reserved memory */
785 #if KAPS_DUMP
786         fclose(file_before);
787 #endif
788         bitset_free(pbqp_alloc_env.ignored_regs);
789         free_pbqp(pbqp_alloc_env.pbqp_inst);
790         plist_free(pbqp_alloc_env.rpeo);
791         xfree(pbqp_alloc_env.restr_nodes);
792         xfree(pbqp_alloc_env.ife_edge_num);
793 }
794
795
796 /**
797  * Initializes this module.
798  */
799 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_pbqp_coloring);
800 void be_init_pbqp_coloring(void)
801 {
802         lc_opt_entry_t *be_grp       = lc_opt_get_grp(firm_opt_get_root(), "be");
803         lc_opt_entry_t *ra_grp       = lc_opt_get_grp(be_grp, "ra");
804         lc_opt_entry_t *chordal_grp  = lc_opt_get_grp(ra_grp, "chordal");
805         lc_opt_entry_t *coloring_grp = lc_opt_get_grp(chordal_grp, "coloring");
806         lc_opt_entry_t *pbqp_grp     = lc_opt_get_grp(coloring_grp, "pbqp");
807
808         static be_ra_chordal_coloring_t coloring = {
809                 be_pbqp_coloring
810         };
811
812         lc_opt_add_table(pbqp_grp, options);
813         be_register_chordal_coloring("pbqp", &coloring);
814 }
815
816 #endif