ee2926d99485331f23878514438746148528c070
[libfirm] / ir / be / becopyheur4.c
1 /*
2  * Copyright (C) 1995-2007 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       Simple copy minimization heuristics.
23  * @author      Christian Wuerdig
24  * @date        27.04.2007
25  * @version     $Id$
26  *
27  * This is the C implementation of the mst algorithm
28  * originally written in Java by Sebastian Hack.
29  * (also known as "heur3" :)
30  * Performs simple copy minimization.
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif /* HAVE_CONFIG_H */
35
36 #include <float.h>
37
38 #include "array.h"
39 #include "irnode_t.h"
40 #include "bitset.h"
41 #include "raw_bitset.h"
42 #include "irphase_t.h"
43 #include "pqueue.h"
44 #include "xmalloc.h"
45 #include "pdeq.h"
46 #include "pset.h"
47 #include "irprintf.h"
48 #include "irbitset.h"
49 #include "error.h"
50
51 #include "bearch.h"
52 #include "beifg.h"
53 #include "be_t.h"
54 #include "becopyopt_t.h"
55 #include "bemodule.h"
56
57
58 #define COL_COST_INFEASIBLE       DBL_MAX
59 #define AFF_NEIGHBOUR_FIX_BENEFIT 128.0
60 #define NEIGHBOUR_CONSTR_COSTS    64.0
61
62 #ifdef NDEBUG
63
64 #define DBG_AFF_CHUNK(env, level, chunk)
65 #define DBG_COL_COST(env, level, cost)
66
67 #else
68
69 static firm_dbg_module_t *dbg = NULL;
70 #define DBG_AFF_CHUNK(env, level, chunk) do { if (firm_dbg_get_mask(dbg) & (level)) dbg_aff_chunk((env), (chunk)); } while(0)
71 #define DBG_COL_COST(env, level, cost)   do { if (firm_dbg_get_mask(dbg) & (level)) dbg_col_cost((env), (cost)); } while(0)
72
73 #endif
74
75 static int last_chunk_id = 0;
76
77 typedef struct _col_cost_t {
78         int    col;
79         double cost;
80 } col_cost_t;
81
82 /**
83  * An affinity chunk.
84  */
85 typedef struct _aff_chunk_t {
86         ir_node  **n;                   /**< An ARR_F containing all nodes of the chunk. */
87         bitset_t *nodes;                /**< A bitset containing all nodes inside this chunk. */
88         bitset_t *interfere;            /**< A bitset containing all interfering neighbours of the nodes in this chunk. */
89         int      weight;                /**< Weight of this chunk */
90         unsigned weight_consistent : 1; /**< Set if the weight is consistent. */
91         unsigned deleted           : 1; /**< Set if the was deleted. */
92         int      id;                    /**< For debugging: An id of this chunk. */
93 } aff_chunk_t;
94
95 /**
96  * An affinity edge.
97  */
98 typedef struct _aff_edge_t {
99         ir_node *src;                   /**< Source node. */
100         ir_node *tgt;                   /**< Target node. */
101         double  weight;                 /**< The weight of this edge. */
102 } aff_edge_t;
103
104 /* main coalescing environment */
105 typedef struct _co_mst_env_t {
106         int              n_regs;         /**< number of regs in class */
107         int              k;              /**< number of non-ignore registers in class */
108         bitset_t         *ignore_regs;   /**< set containing all global ignore registers */
109         ir_phase         ph;             /**< phase object holding data for nodes */
110         pqueue           *chunks;        /**< priority queue for chunks */
111         pset             *chunkset;      /**< set holding all chunks */
112         be_ifg_t         *ifg;           /**< the interference graph */
113         const arch_env_t *aenv;          /**< the arch environment */
114         copy_opt_t       *co;            /**< the copy opt object */
115 } co_mst_env_t;
116
117 /* stores coalescing related information for a node */
118 typedef struct _co_mst_irn_t {
119         ir_node     *irn;              /**< the irn this information belongs to */
120         aff_chunk_t *chunk;            /**< the chunk this irn belongs to */
121         bitset_t    *adm_colors;       /**< set of admissible colors for this irn */
122         ir_node     **int_neighs;      /**< array of all interfering neighbours (cached for speed reasons) */
123         int         n_neighs;          /**< length of the interfering neighbours array. */
124         int         int_aff_neigh;     /**< number of interfering affinity neighbours */
125         int         col;               /**< color currently assigned */
126         int         init_col;          /**< the initial color */
127         int         tmp_col;           /**< a temporary assigned color */
128         unsigned    fixed     : 1;     /**< the color is fixed */
129         unsigned    tmp_fixed : 1;     /**< the color is temporary fixed */
130 } co_mst_irn_t;
131
132 #define get_co_mst_irn(mst_env, irn) (phase_get_or_set_irn_data(&(mst_env)->ph, (irn)))
133
134 typedef int decide_func_t(const co_mst_irn_t *node, int col);
135
136 #ifdef DEBUG_libfirm
137
138 /**
139  * Write a chunk to stderr for debugging.
140  */
141 static void dbg_aff_chunk(const co_mst_env_t *env, const aff_chunk_t *c) {
142         bitset_pos_t idx;
143         if (c->weight_consistent)
144                 ir_fprintf(stderr, " $%d ", c->weight);
145         ir_fprintf(stderr, "{");
146         bitset_foreach(c->nodes, idx) {
147                 ir_node *n = get_idx_irn(env->co->irg, idx);
148                 ir_fprintf(stderr, " %+F,", n);
149         }
150         ir_fprintf(stderr, "}");
151 }
152
153 /**
154  * Dump all admissible colors to stderr.
155  */
156 static void dbg_admissible_colors(const co_mst_env_t *env, const co_mst_irn_t *node) {
157         bitset_pos_t idx;
158         (void) env;
159
160         if (bitset_popcnt(node->adm_colors) < 1)
161                 fprintf(stderr, "no admissible colors?!?");
162         else {
163                 bitset_foreach(node->adm_colors, idx)
164                         fprintf(stderr, " %d", idx);
165         }
166 }
167
168 /**
169  * Dump color-cost pairs to stderr.
170  */
171 static void dbg_col_cost(const co_mst_env_t *env, const col_cost_t *cost) {
172         int i;
173         for (i = 0; i < env->n_regs; ++i) {
174                 if (cost[i].cost == COL_COST_INFEASIBLE)
175                         fprintf(stderr, " (%d, INF)", cost[i].col);
176                 else
177                         fprintf(stderr, " (%d, %.1f)", cost[i].col, cost[i].cost);
178         }
179 }
180
181 #endif /* DEBUG_libfirm */
182
183 static INLINE int get_mst_irn_col(const co_mst_irn_t *node) {
184         return node->tmp_fixed ? node->tmp_col : node->col;
185 }
186
187 /**
188  * @return 1 if node @p node has color @p col, 0 otherwise.
189  */
190 static int decider_has_color(const co_mst_irn_t *node, int col) {
191         return get_mst_irn_col(node) == col;
192 }
193
194 /**
195  * @return 1 if node @p node has not color @p col, 0 otherwise.
196  */
197 static int decider_hasnot_color(const co_mst_irn_t *node, int col) {
198         return get_mst_irn_col(node) != col;
199 }
200
201 /**
202  * Always returns true.
203  */
204 static int decider_always_yes(const co_mst_irn_t *node, int col) {
205         (void) node;
206         (void) col;
207         return 1;
208 }
209
210 /** compares two affinity edges by its weight */
211 static int cmp_aff_edge(const void *a, const void *b) {
212         const aff_edge_t *e1 = a;
213         const aff_edge_t *e2 = b;
214
215         if (e2->weight == e1->weight) {
216                 if (e2->src->node_idx == e1->src->node_idx)
217                         return QSORT_CMP(e2->tgt->node_idx, e1->tgt->node_idx);
218                 else
219                         return QSORT_CMP(e2->src->node_idx, e1->src->node_idx);
220         }
221         /* sort in descending order */
222         return QSORT_CMP(e2->weight, e1->weight);
223 }
224
225 /** compares to color-cost pairs */
226 static int cmp_col_cost(const void *a, const void *b) {
227         const col_cost_t *c1 = a;
228         const col_cost_t *c2 = b;
229
230         return c1->cost < c2->cost ? -1 : 1;
231 }
232
233 /**
234  * Creates a new affinity chunk
235  */
236 static INLINE aff_chunk_t *new_aff_chunk(co_mst_env_t *env) {
237         aff_chunk_t *c = xmalloc(sizeof(*c));
238         c->weight            = -1;
239         c->weight_consistent = 0;
240         c->n                 = NEW_ARR_F(ir_node *, 0);
241         c->nodes             = bitset_irg_malloc(env->co->irg);
242         c->interfere         = bitset_irg_malloc(env->co->irg);
243         c->id                = last_chunk_id++;
244         pset_insert(env->chunkset, c, c->id);
245         return c;
246 }
247
248 /**
249  * Frees all memory allocated by an affinity chunk.
250  */
251 static INLINE void delete_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
252         pset_remove(env->chunkset, c, c->id);
253         bitset_free(c->nodes);
254         bitset_free(c->interfere);
255         DEL_ARR_F(c->n);
256         c->deleted = 1;
257         free(c);
258 }
259
260 /**
261  * Adds a node to an affinity chunk
262  */
263 static INLINE void aff_chunk_add_node(aff_chunk_t *c, co_mst_irn_t *node) {
264         int i;
265
266         if (bitset_is_set(c->nodes, get_irn_idx(node->irn)))
267                 return;
268
269         c->weight_consistent = 0;
270         node->chunk          = c;
271         bitset_set(c->nodes, get_irn_idx(node->irn));
272
273         ARR_APP1(ir_node *, c->n, node->irn);
274
275         for (i = node->n_neighs - 1; i >= 0; --i) {
276                 ir_node *neigh = node->int_neighs[i];
277                 bitset_set(c->interfere, get_irn_idx(neigh));
278         }
279 }
280
281 /**
282  * In case there is no phase information for irn, initialize it.
283  */
284 static void *co_mst_irn_init(ir_phase *ph, ir_node *irn, void *old) {
285         co_mst_irn_t *res = old ? old : phase_alloc(ph, sizeof(res[0]));
286         co_mst_env_t *env = ph->priv;
287
288         if (res != old) {
289                 const arch_register_req_t *req;
290                 void     *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
291                 ir_node  *neigh;
292                 unsigned len;
293
294                 res->irn           = irn;
295                 res->chunk         = NULL;
296                 res->fixed         = 0;
297                 res->tmp_fixed     = 0;
298                 res->tmp_col       = -1;
299                 res->int_neighs    = NULL;
300                 res->int_aff_neigh = 0;
301                 res->col           = arch_register_get_index(arch_get_irn_register(env->aenv, irn));
302                 res->init_col      = res->col;
303
304                 DB((dbg, LEVEL_4, "Creating phase info for %+F\n", irn));
305
306                 /* set admissible registers */
307                 res->adm_colors = bitset_obstack_alloc(phase_obst(ph), env->n_regs);
308
309                 /* Exclude colors not assignable to the irn */
310                 req = arch_get_register_req(env->aenv, irn, -1);
311                 if (arch_register_req_is(req, limited))
312                         rbitset_copy_to_bitset(req->limited, res->adm_colors);
313                 else
314                         bitset_set_all(res->adm_colors);
315
316                 /* exclude global ignore registers as well */
317                 bitset_andnot(res->adm_colors, env->ignore_regs);
318
319                 /* set the number of interfering affinity neighbours to -1, they are calculated later */
320                 res->int_aff_neigh = -1;
321
322                 /* build list of interfering neighbours */
323                 len = 0;
324                 be_ifg_foreach_neighbour(env->ifg, nodes_it, irn, neigh) {
325                         if (! arch_irn_is(env->aenv, neigh, ignore)) {
326                                 obstack_ptr_grow(phase_obst(ph), neigh);
327                                 ++len;
328                         }
329                 }
330                 res->int_neighs = obstack_finish(phase_obst(ph));
331                 res->n_neighs   = len;
332         }
333         return res;
334 }
335
336 /**
337  * Check if affinity chunk @p chunk interferes with node @p irn.
338  */
339 static INLINE int aff_chunk_interferes(co_mst_env_t *env, const aff_chunk_t *chunk, ir_node *irn) {
340         (void) env;
341         return bitset_is_set(chunk->interfere, get_irn_idx(irn));
342 }
343
344 /**
345  * Check if there are interference edges from c1 to c2.
346  * @param env   The global co_mst environment
347  * @param c1    A chunk
348  * @param c2    Another chunk
349  * @return 1 if there are interferences between nodes of c1 and c2, 0 otherwise.
350  */
351 static INLINE int aff_chunks_interfere(co_mst_env_t *env, const aff_chunk_t *c1, const aff_chunk_t *c2) {
352         bitset_t *tmp;
353
354         if (c1 == c2)
355                 return 0;
356
357         /* check if there is a node in c2 having an interfering neighbor in c1 */
358         tmp = bitset_alloca(get_irg_last_idx(env->co->irg));
359         tmp = bitset_copy(tmp, c1->interfere);
360         tmp = bitset_and(tmp, c2->nodes);
361
362         return bitset_popcnt(tmp) > 0;
363 }
364
365 /**
366  * Returns the affinity chunk of @p irn or creates a new
367  * one with @p irn as element if there is none assigned.
368  */
369 static INLINE aff_chunk_t *get_aff_chunk(co_mst_env_t *env, ir_node *irn) {
370         co_mst_irn_t *node = get_co_mst_irn(env, irn);
371         return node->chunk;
372 }
373
374 /**
375  * Let chunk(src) absorb the nodes of chunk(tgt) (only possible when there
376  * are no interference edges from chunk(src) to chunk(tgt)).
377  * @return 1 if successful, 0 if not possible
378  */
379 static int aff_chunk_absorb(co_mst_env_t *env, ir_node *src, ir_node *tgt) {
380         aff_chunk_t *c1 = get_aff_chunk(env, src);
381         aff_chunk_t *c2 = get_aff_chunk(env, tgt);
382
383 #ifndef NDEBUG
384                 DB((dbg, LEVEL_4, "Attempt to let c1 (id %d): ", c1 ? c1->id : -1));
385                 if (c1) {
386                         DBG_AFF_CHUNK(env, LEVEL_4, c1);
387                 } else {
388                         DB((dbg, LEVEL_4, "{%+F}", src));
389                 }
390                 DB((dbg, LEVEL_4, "\n\tabsorb c2 (id %d): ", c2 ? c2->id : -1));
391                 if (c2) {
392                         DBG_AFF_CHUNK(env, LEVEL_4, c2);
393                 } else {
394                         DB((dbg, LEVEL_4, "{%+F}", tgt));
395                 }
396                 DB((dbg, LEVEL_4, "\n"));
397 #endif
398
399         if (c1 == NULL) {
400                 if (c2 == NULL) {
401                         /* no chunk exists */
402                         co_mst_irn_t *mirn = get_co_mst_irn(env, src);
403                         int i;
404
405                         for (i = mirn->n_neighs - 1; i >= 0; --i) {
406                                 if (mirn->int_neighs[i] == tgt)
407                                         break;
408                         }
409                         if (i < 0) {
410                                 /* create one containing both nodes */
411                                 c1 = new_aff_chunk(env);
412                                 aff_chunk_add_node(c1, get_co_mst_irn(env, src));
413                                 aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
414                                 goto absorbed;
415                         }
416                 } else {
417                         /* c2 already exists */
418                         if (! aff_chunk_interferes(env, c2, src)) {
419                                 aff_chunk_add_node(c2, get_co_mst_irn(env, src));
420                                 goto absorbed;
421                         }
422                 }
423         } else if (c2 == NULL) {
424                 /* c1 already exists */
425                 if (! aff_chunk_interferes(env, c1, tgt)) {
426                         aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
427                         goto absorbed;
428                 }
429         } else if (c1 != c2 && ! aff_chunks_interfere(env, c1, c2)) {
430                 int idx, len;
431
432                 for (idx = 0, len = ARR_LEN(c2->n); idx < len; ++idx) {
433                         ir_node      *n  = c2->n[idx];
434                         co_mst_irn_t *mn = get_co_mst_irn(env, n);
435
436                         mn->chunk = c1;
437
438                         if (! bitset_is_set(c1->nodes, get_irn_idx(n)))
439                                 ARR_APP1(ir_node *, c1->n, n);
440                 }
441
442                 bitset_or(c1->nodes, c2->nodes);
443                 bitset_or(c1->interfere, c2->interfere);
444                 c1->weight_consistent = 0;
445
446                 delete_aff_chunk(env, c2);
447                 goto absorbed;
448         }
449         DB((dbg, LEVEL_4, " ... c1 interferes with c2, skipped\n"));
450         return 0;
451
452 absorbed:
453         DB((dbg, LEVEL_4, " ... absorbed\n"));
454         return 1;
455 }
456
457 /**
458  * Assures that the weight of the given chunk is consistent.
459  */
460 static void aff_chunk_assure_weight(const co_mst_env_t *env, aff_chunk_t *c) {
461         if (! c->weight_consistent) {
462                 int w = 0;
463                 int idx, len;
464
465                 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
466                         ir_node               *n  = c->n[idx];
467                         const affinity_node_t *an = get_affinity_info(env->co, n);
468
469                         if (an != NULL) {
470                                 neighb_t *neigh;
471                                 co_gs_foreach_neighb(an, neigh) {
472                                         const ir_node *m    = neigh->irn;
473                                         const int     m_idx = get_irn_idx(m);
474
475                                         /* skip ignore nodes */
476                                         if (arch_irn_is(env->aenv, m, ignore))
477                                                 continue;
478
479                                         w += bitset_is_set(c->nodes, m_idx) ? neigh->costs : 0;
480                                 }
481                         }
482                 }
483
484                 c->weight            = w;
485                 c->weight_consistent = 1;
486         }
487 }
488
489 /**
490  * Count the number of interfering affinity neighbours
491  */
492 static int count_interfering_aff_neighs(co_mst_env_t *env, const affinity_node_t *an) {
493         const neighb_t     *neigh;
494         ir_node            *irn  = an->irn;
495         const co_mst_irn_t *node = get_co_mst_irn(env, irn);
496         int                res   = 0;
497
498         co_gs_foreach_neighb(an, neigh) {
499                 const ir_node *n = neigh->irn;
500                 int           i;
501
502                 /* skip ignore nodes */
503                 if (arch_irn_is(env->aenv, n, ignore))
504                         continue;
505
506                 /* check if the affinity neighbour interfere */
507                 for (i = 0; i < node->n_neighs; ++i) {
508                         if (node->int_neighs[i] == n) {
509                                 ++res;
510                                 break;
511                         }
512                 }
513         }
514         return res;
515 }
516
517
518 /**
519  * Build chunks of nodes connected by affinity edges.
520  * We start at the heaviest affinity edge.
521  * The chunks of the two edge-defining nodes will be
522  * merged if there are no interference edges from one
523  * chunk to the other.
524  */
525 static void build_affinity_chunks(co_mst_env_t *env) {
526         void        *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
527         aff_edge_t  *edges    = NEW_ARR_F(aff_edge_t, 0);
528         ir_node     *n;
529         int         i, len;
530         aff_chunk_t *curr_chunk;
531
532         /* at first we create the affinity edge objects */
533         be_ifg_foreach_node(env->ifg, nodes_it, n) {
534                 int             n_idx = get_irn_idx(n);
535                 co_mst_irn_t    *n1;
536                 affinity_node_t *an;
537
538                 /* skip ignore nodes */
539                 if (arch_irn_is(env->aenv, n, ignore))
540                         continue;
541
542                 n1 = get_co_mst_irn(env, n);
543                 an = get_affinity_info(env->co, n);
544
545                 if (an != NULL) {
546                         neighb_t *neigh;
547
548                         if (n1->int_aff_neigh < 0)
549                                 n1->int_aff_neigh = count_interfering_aff_neighs(env, an);
550
551                         /* build the affinity edges */
552                         co_gs_foreach_neighb(an, neigh) {
553                                 ir_node *m    = neigh->irn;
554                                 int     m_idx = get_irn_idx(m);
555
556                                 /* record the edge in only one direction */
557                                 if (n_idx < m_idx) {
558                                         co_mst_irn_t *n2;
559                                         aff_edge_t   edge;
560
561                                         /* skip ignore nodes */
562                                         if (arch_irn_is(env->aenv, m, ignore))
563                                                 continue;
564
565                                         edge.src = n;
566                                         edge.tgt = m;
567
568                                         n2 = get_co_mst_irn(env, m);
569                                         if (n2->int_aff_neigh < 0) {
570                                                 affinity_node_t *am = get_affinity_info(env->co, m);
571                                                 n2->int_aff_neigh = count_interfering_aff_neighs(env, am);
572                                         }
573                                         /*
574                                          * these weights are pure hackery ;-).
575                                          * It's not chriswue's fault but mine.
576                                          */
577                                         edge.weight = (double)neigh->costs / (double)(1 + n1->int_aff_neigh + n2->int_aff_neigh);
578                                         ARR_APP1(aff_edge_t, edges, edge);
579                                 }
580                         }
581                 }
582         }
583
584         /* now: sort edges and build the affinity chunks */
585         len = ARR_LEN(edges);
586         qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
587         for (i = 0; i < len; ++i) {
588                 DBG((dbg, LEVEL_1, "edge (%u,%u) %f\n", edges[i].src->node_idx, edges[i].tgt->node_idx, edges[i].weight));
589
590                 (void)aff_chunk_absorb(env, edges[i].src, edges[i].tgt);
591         }
592
593         /* now insert all chunks into a priority queue */
594         foreach_pset(env->chunkset, curr_chunk) {
595                 aff_chunk_assure_weight(env, curr_chunk);
596
597                 DBG((dbg, LEVEL_1, "entry #%d", curr_chunk->id));
598                 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
599                 DBG((dbg, LEVEL_1, "\n"));
600
601                 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
602         }
603         foreach_phase_irn(&env->ph, n) {
604                 co_mst_irn_t *mirn = get_co_mst_irn(env, n);
605
606                 if (mirn->chunk == NULL) {
607                         /* no chunk is allocated so far, do it now */
608                         aff_chunk_t *curr_chunk = new_aff_chunk(env);
609                         aff_chunk_add_node(curr_chunk, mirn);
610
611                         aff_chunk_assure_weight(env, curr_chunk);
612
613                         DBG((dbg, LEVEL_1, "entry #%d", curr_chunk->id));
614                         DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
615                         DBG((dbg, LEVEL_1, "\n"));
616
617                         pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
618                 }
619         }
620
621         DEL_ARR_F(edges);
622 }
623
624 /**
625  * Greedy collect affinity neighbours into thew new chunk @p chunk starting at node @p node.
626  */
627 static void expand_chunk_from(co_mst_env_t *env, co_mst_irn_t *node, bitset_t *visited,
628         aff_chunk_t *chunk, aff_chunk_t *orig_chunk, decide_func_t *decider, int col)
629 {
630         waitq *nodes = new_waitq();
631
632         DBG((dbg, LEVEL_1, "\n\tExpanding new chunk (#%d) from %+F, color %d:", chunk->id, node->irn, col));
633
634         /* init queue and chunk */
635         waitq_put(nodes, node);
636         bitset_set(visited, get_irn_idx(node->irn));
637         aff_chunk_add_node(chunk, node);
638         DB((dbg, LEVEL_1, " %+F", node->irn));
639
640         /* as long as there are nodes in the queue */
641         while (! waitq_empty(nodes)) {
642                 co_mst_irn_t    *n  = waitq_get(nodes);
643                 affinity_node_t *an = get_affinity_info(env->co, n->irn);
644
645                 /* check all affinity neighbors */
646                 if (an != NULL) {
647                         neighb_t *neigh;
648                         co_gs_foreach_neighb(an, neigh) {
649                                 ir_node      *m    = neigh->irn;
650                                 int          m_idx = get_irn_idx(m);
651                                 co_mst_irn_t *n2;
652
653                                 /* skip ignore nodes */
654                                 if (arch_irn_is(env->aenv, m, ignore))
655                                         continue;
656
657                                 n2 = get_co_mst_irn(env, m);
658
659                                 if (! bitset_is_set(visited, m_idx)       &&
660                                         decider(n2, col)                      &&
661                                         ! n2->fixed                           &&
662                                         ! aff_chunk_interferes(env, chunk, m) &&
663                                         bitset_is_set(orig_chunk->nodes, m_idx))
664                                 {
665                                         /*
666                                                 following conditions are met:
667                                                 - neighbour is not visited
668                                                 - neighbour likes the color
669                                                 - neighbour has not yet a fixed color
670                                                 - the new chunk doesn't interfere with the neighbour
671                                                 - neighbour belongs or belonged once to the original chunk
672                                         */
673                                         bitset_set(visited, m_idx);
674                                         aff_chunk_add_node(chunk, n2);
675                                         DB((dbg, LEVEL_1, " %+F", n2->irn));
676                                         /* enqueue for further search */
677                                         waitq_put(nodes, n2);
678                                 }
679                         }
680                 }
681         }
682
683         DB((dbg, LEVEL_1, "\n"));
684
685         del_waitq(nodes);
686 }
687
688 /**
689  * Fragment the given chunk into chunks having given color and not having given color.
690  */
691 static aff_chunk_t *fragment_chunk(co_mst_env_t *env, int col, aff_chunk_t *c, waitq *tmp) {
692         bitset_t    *visited = bitset_irg_malloc(env->co->irg);
693         int         idx, len;
694         aff_chunk_t *best = NULL;
695
696         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
697                 ir_node       *irn;
698                 co_mst_irn_t  *node;
699                 aff_chunk_t   *tmp_chunk;
700                 decide_func_t *decider;
701                 int           check_for_best;
702
703                 irn = c->n[idx];
704                 if (bitset_is_set(visited, get_irn_idx(irn)))
705                         continue;
706
707                 node = get_co_mst_irn(env, irn);
708
709                 if (get_mst_irn_col(node) == col) {
710                         decider        = decider_has_color;
711                         check_for_best = 1;
712                         DBG((dbg, LEVEL_4, "\tcolor %d wanted", col));
713                 }
714                 else {
715                         decider        = decider_hasnot_color;
716                         check_for_best = 0;
717                         DBG((dbg, LEVEL_4, "\tcolor %d forbidden", col));
718                 }
719
720                 /* create a new chunk starting at current node */
721                 tmp_chunk = new_aff_chunk(env);
722                 waitq_put(tmp, tmp_chunk);
723                 expand_chunk_from(env, node, visited, tmp_chunk, c, decider, col);
724                 assert(bitset_popcnt(tmp_chunk->nodes) > 0 && "No nodes added to chunk");
725
726                 /* remember the local best */
727                 aff_chunk_assure_weight(env, tmp_chunk);
728                 if (check_for_best && (! best || best->weight < tmp_chunk->weight))
729                         best = tmp_chunk;
730         }
731
732         assert(best && "No chunk found?");
733         bitset_free(visited);
734         return best;
735 }
736
737 /**
738  * Initializes an array of color-cost pairs.
739  * Sets forbidden colors to costs COL_COST_INFEASIBLE and all others to @p c.
740  */
741 static INLINE void col_cost_init(co_mst_env_t *env, col_cost_t *cost, double c) {
742         int i;
743
744         for (i = 0; i < env->n_regs; ++i) {
745                 cost[i].col = i;
746                 if (bitset_is_set(env->ignore_regs, i))
747                         cost[i].cost = COL_COST_INFEASIBLE;
748                 else
749                         cost[i].cost = c;
750         }
751 }
752
753 /**
754  * Initializes an array of color-cost pairs.
755  * Sets all colors except color @p col to COL_COST_INFEASIBLE and @p col to 0.0
756  */
757 static INLINE void col_cost_init_single(co_mst_env_t *env, col_cost_t *cost, int col) {
758         assert(! bitset_is_set(env->ignore_regs, col) && "Attempt to use forbidden color.");
759         col_cost_init(env, cost, COL_COST_INFEASIBLE);
760         cost[col].col = 0;
761         cost[0].col   = col;
762         cost[0].cost  = 0.0;
763 }
764
765 /**
766  * Resets the temporary fixed color of all nodes within wait queue @p nodes.
767  * ATTENTION: the queue is empty after calling this function!
768  */
769 static INLINE void reject_coloring(waitq *nodes) {
770         DB((dbg, LEVEL_4, "\treject coloring for"));
771         while (! waitq_empty(nodes)) {
772                 co_mst_irn_t *n = waitq_get(nodes);
773                 DB((dbg, LEVEL_4, " %+F", n->irn));
774                 n->tmp_fixed = 0;
775         }
776         DB((dbg, LEVEL_4, "\n"));
777 }
778
779 /**
780  * Determines the costs for each color if it would be assigned to node @p node.
781  */
782 static void determine_color_costs(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs) {
783         affinity_node_t *an = get_affinity_info(env->co, node->irn);
784         neighb_t        *aff_neigh;
785         bitset_pos_t     idx;
786         int              i;
787
788         col_cost_init(env, costs, 0.0);
789
790         /* calculate (negative) costs for affinity neighbours */
791         if (an != NULL) {
792                 co_gs_foreach_neighb(an, aff_neigh) {
793                         ir_node      *m = aff_neigh->irn;
794                         co_mst_irn_t *neigh;
795                         double       c;
796
797                         /* skip ignore nodes */
798                         if (arch_irn_is(env->aenv, m, ignore))
799                                 continue;
800
801                         neigh = get_co_mst_irn(env, m);
802                         c     = (double)aff_neigh->costs;
803
804                         /* calculate costs for fixed affinity neighbours */
805                         if (neigh->tmp_fixed || neigh->fixed) {
806                                 int col = get_mst_irn_col(neigh);
807                                 costs[col].cost -= c * AFF_NEIGHBOUR_FIX_BENEFIT;
808                         }
809                 }
810         }
811
812         /* calculate (positive) costs for interfering neighbours */
813         for (i = 0; i < node->n_neighs; ++i) {
814                 co_mst_irn_t *neigh;
815                 int          col, col_cnt;
816                 ir_node      *int_neigh;
817
818                 int_neigh = node->int_neighs[i];
819
820                 /* skip ignore nodes */
821                 if (arch_irn_is(env->aenv, int_neigh, ignore))
822                         continue;
823
824                 neigh   = get_co_mst_irn(env, int_neigh);
825                 col     = get_mst_irn_col(neigh);
826                 col_cnt = bitset_popcnt(neigh->adm_colors);
827
828                 if (neigh->tmp_fixed || neigh->fixed) {
829                         /* colors of fixed interfering neighbours are infeasible */
830                         costs[col].cost = COL_COST_INFEASIBLE;
831                 }
832                 else if (col_cnt < env->k) {
833                         /* calculate costs for constrained interfering neighbours */
834                         double ratio = 1.0 - ((double)col_cnt / (double)env->k);
835
836                         bitset_foreach_clear(neigh->adm_colors, idx) {
837                                 /* check only explicitly forbidden colors (skip global forbidden ones) */
838                                 if (! bitset_is_set(env->ignore_regs, idx)) {
839                                         costs[col].cost += ratio * NEIGHBOUR_CONSTR_COSTS;
840                                 }
841                         }
842                 }
843         }
844
845         /* set all not admissible colors to COL_COST_INFEASIBLE */
846         bitset_foreach_clear(node->adm_colors, idx)
847                 costs[idx].cost = COL_COST_INFEASIBLE;
848 }
849
850 /* need forward declaration due to recursive call */
851 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones);
852
853 /**
854  * Tries to change node to a color but @p explude_col.
855  * @return 1 if succeeded, 0 otherwise.
856  */
857 static int change_node_color_excluded(co_mst_env_t *env, co_mst_irn_t *node, int exclude_col, waitq *changed_ones) {
858         int col = get_mst_irn_col(node);
859         int res = 0;
860
861         /* neighbours has already a different color -> good, temporary fix it */
862         if (col != exclude_col) {
863                 node->tmp_fixed = 1;
864                 node->tmp_col   = col;
865                 waitq_put(changed_ones, node);
866                 return 1;
867         }
868
869         /* The node has the color it should not have _and_ has not been visited yet. */
870         if (! (node->tmp_fixed || node->fixed)) {
871                 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
872
873                 /* Get the costs for giving the node a specific color. */
874                 determine_color_costs(env, node, costs);
875
876                 /* Since the node must not have the not_col, set the costs for that color to "infinity" */
877                 costs[exclude_col].cost = COL_COST_INFEASIBLE;
878
879                 /* sort the colors according costs, cheapest first. */
880                 qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost);
881
882                 /* Try recoloring the node using the color list. */
883                 res = recolor_nodes(env, node, costs, changed_ones);
884         }
885
886         return res;
887 }
888
889 /**
890  * Tries to bring node @p node to cheapest color and color all interfering neighbours with other colors.
891  * ATTENTION: Expect @p costs already sorted by increasing costs.
892  * @return 1 if coloring could be applied, 0 otherwise.
893  */
894 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones) {
895         int   i;
896         waitq *local_changed = new_waitq();
897         waitq *tmp           = new_waitq();
898
899         DBG((dbg, LEVEL_1, "\tRecoloring %+F with color-costs", node->irn));
900         DBG_COL_COST(env, LEVEL_1, costs);
901         DB((dbg, LEVEL_1, "\n"));
902
903         for (i = 0; i < env->n_regs; ++i) {
904                 int tgt_col  = costs[i].col;
905                 int neigh_ok = 1;
906                 int j;
907
908                 /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
909                 if (costs[i].cost == COL_COST_INFEASIBLE) {
910                         node->tmp_fixed = 0;
911                         del_waitq(local_changed);
912                         del_waitq(tmp);
913                         return 0;
914                 }
915
916                 /* Set the new color of the node and mark the node as temporarily fixed. */
917                 assert(! node->tmp_fixed && "Node must not have been temporary fixed.");
918                 node->tmp_fixed = 1;
919                 node->tmp_col   = tgt_col;
920                 DBG((dbg, LEVEL_4, "\tTemporary setting %+F to color %d\n", node->irn, tgt_col));
921
922                 assert(waitq_empty(local_changed) && "Node queue should be empty here.");
923                 waitq_put(local_changed, node);
924
925                 /* try to color all interfering neighbours with current color forbidden */
926                 for (j = 0; j < node->n_neighs; ++j) {
927                         co_mst_irn_t *nn;
928                         ir_node      *neigh;
929
930                         neigh = node->int_neighs[j];
931
932                         /* skip ignore nodes */
933                         if (arch_irn_is(env->aenv, neigh, ignore))
934                                 continue;
935
936                         nn = get_co_mst_irn(env, neigh);
937                         DB((dbg, LEVEL_4, "\tHandling neighbour %+F, at position %d (fixed: %d, tmp_fixed: %d, tmp_col: %d, col: %d)\n",
938                                 neigh, j, nn->fixed, nn->tmp_fixed, nn->tmp_col, nn->col));
939
940                         /*
941                                 Try to change the color of the neighbor and record all nodes which
942                                 get changed in the tmp list. Add this list to the "changed" list for
943                                 that color. If we did not succeed to change the color of the neighbor,
944                                 we bail out and try the next color.
945                         */
946                         if (get_mst_irn_col(nn) == tgt_col) {
947                                 /* try to color neighbour with tgt_col forbidden */
948                                 neigh_ok = change_node_color_excluded(env, nn, tgt_col, tmp);
949
950                                 /* join lists of changed nodes */
951                                 while (! waitq_empty(tmp))
952                                         waitq_put(local_changed, waitq_get(tmp));
953
954                                 if (! neigh_ok)
955                                         break;
956                         }
957                 }
958
959                 /*
960                         We managed to assign the target color to all neighbors, so from the perspective
961                         of the current node, every thing was ok and we can return safely.
962                 */
963                 if (neigh_ok) {
964                         /* append the local_changed ones to global ones */
965                         while (! waitq_empty(local_changed))
966                                 waitq_put(changed_ones, waitq_get(local_changed));
967                         del_waitq(local_changed);
968                         del_waitq(tmp);
969                         return 1;
970                 }
971                 else {
972                         /* coloring of neighbours failed, so we try next color */
973                         reject_coloring(local_changed);
974                 }
975         }
976
977         del_waitq(local_changed);
978         del_waitq(tmp);
979         return 0;
980 }
981
982 /**
983  * Tries to bring node @p node and all it's neighbours to color @p tgt_col.
984  * @return 1 if color @p col could be applied, 0 otherwise
985  */
986 static int change_node_color(co_mst_env_t *env, co_mst_irn_t *node, int tgt_col, waitq *changed_ones) {
987         int col = get_mst_irn_col(node);
988
989         /* if node already has the target color -> good, temporary fix it */
990         if (col == tgt_col) {
991                 DBG((dbg, LEVEL_4, "\t\tCNC: %+F has already color %d, fix temporary\n", node->irn, tgt_col));
992                 if (! node->tmp_fixed) {
993                         node->tmp_fixed = 1;
994                         node->tmp_col   = tgt_col;
995                         waitq_put(changed_ones, node);
996                 }
997                 return 1;
998         }
999
1000         /*
1001                 Node has not yet a fixed color and target color is admissible
1002                 -> try to recolor node and it's affinity neighbours
1003         */
1004         if (! (node->fixed || node->tmp_fixed) && bitset_is_set(node->adm_colors, tgt_col)) {
1005                 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
1006                 int        res;
1007
1008                 col_cost_init_single(env, costs, tgt_col);
1009
1010                 DBG((dbg, LEVEL_4, "\t\tCNC: Attempt to recolor %+F ===>>\n", node->irn));
1011                 res = recolor_nodes(env, node, costs, changed_ones);
1012                 DBG((dbg, LEVEL_4, "\t\tCNC: <<=== Recoloring of %+F %s\n", node->irn, res ? "succeeded" : "failed"));
1013
1014                 return res;
1015         }
1016
1017 #ifndef NDEBUG
1018                 if (firm_dbg_get_mask(dbg) & LEVEL_4) {
1019                         if (node->fixed || node->tmp_fixed)
1020                                 DB((dbg, LEVEL_4, "\t\tCNC: %+F has already fixed color %d\n", node->irn, col));
1021                         else {
1022                                 DB((dbg, LEVEL_4, "\t\tCNC: color %d not admissible for %+F (", tgt_col, node->irn));
1023                                 dbg_admissible_colors(env, node);
1024                                 DB((dbg, LEVEL_4, ")\n"));
1025                         }
1026                 }
1027 #endif
1028
1029         return 0;
1030 }
1031
1032 /**
1033  * Tries to color an affinity chunk (or at least a part of it).
1034  * Inserts uncolored parts of the chunk as a new chunk into the priority queue.
1035  */
1036 static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
1037         aff_chunk_t *best_chunk   = NULL;
1038         int         best_color    = -1;
1039         int         did_all       = 0;
1040         waitq       *changed_ones = new_waitq();
1041         waitq       *tmp_chunks   = new_waitq();
1042         waitq       *best_starts  = NULL;
1043         bitset_t    *visited;
1044         int         col, idx, len;
1045
1046         DB((dbg, LEVEL_2, "fragmentizing chunk #%d", c->id));
1047         DBG_AFF_CHUNK(env, LEVEL_2, c);
1048         DB((dbg, LEVEL_2, "\n"));
1049
1050
1051         /* check which color is the "best" for the given chunk.
1052          * if we found a color which was ok for all nodes, we take it
1053          * and do not look further. (see did_all flag usage below.)
1054          * If we have many colors which fit all nodes it is hard to decide
1055          * which one to take anyway.
1056          * TODO Sebastian: Perhaps we should at all nodes and figure out
1057          * a suitable color using costs as done above (determine_color_costs).
1058          */
1059         for (col = 0; col < env->n_regs && !did_all; ++col) {
1060                 int         one_good     = 0;
1061                 waitq       *good_starts = new_waitq();
1062                 aff_chunk_t *local_best;
1063
1064                 /* skip ignore colors */
1065                 if (bitset_is_set(env->ignore_regs, col))
1066                         continue;
1067
1068                 DB((dbg, LEVEL_3, "\ttrying color %d\n", col));
1069
1070                 /* suppose we can color all nodes to the same color */
1071                 did_all = 1;
1072
1073                 /* try to bring all nodes of given chunk to the current color. */
1074                 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1075                         ir_node      *irn  = c->n[idx];
1076                         co_mst_irn_t *node = get_co_mst_irn(env, irn);
1077                         int          good  = 0;
1078
1079                         assert(! node->fixed && "Node must not have a fixed color.");
1080                         DB((dbg, LEVEL_4, "\t\tBringing %+F from color %d to color %d ...\n", irn, node->col, col));
1081
1082                         /*
1083                                 The order of the colored nodes is important, so we record the successfully
1084                                 colored ones in the order they appeared.
1085                         */
1086                         good = change_node_color(env, node, col, changed_ones);
1087                         if (good)
1088                                 waitq_put(good_starts, node);
1089                         one_good |= good;
1090                         did_all  &= good;
1091
1092                         DB((dbg, LEVEL_4, "\t\t... %+F attempt from %d to %d %s\n", irn, node->col, col, one_good ? "succeeded" : "failed"));
1093                 }
1094
1095                 /* try next color when failed */
1096                 if (! one_good)
1097                         continue;
1098
1099                 /* fragment the chunk according to the coloring */
1100                 local_best = fragment_chunk(env, col, c, tmp_chunks);
1101
1102                 /* search the best of the good list
1103                    and make it the new best if it is better than the current */
1104                 if (local_best) {
1105                         aff_chunk_assure_weight(env, local_best);
1106
1107                         DB((dbg, LEVEL_4, "\t\tlocal best chunk (id %d) for color %d: ", local_best->id, col));
1108                         DBG_AFF_CHUNK(env, LEVEL_4, local_best);
1109
1110                         if (! best_chunk || best_chunk->weight < local_best->weight) {
1111                                 best_chunk = local_best;
1112                                 best_color = col;
1113                                 if (best_starts)
1114                                         del_waitq(best_starts);
1115                                 best_starts = good_starts;
1116                                 DB((dbg, LEVEL_4, "\n\t\t... setting global best chunk (id %d), color %d\n", best_chunk->id, best_color));
1117                         } else {
1118                                 DB((dbg, LEVEL_4, "\n\t\t... omitting, global best is better\n"));
1119                                 del_waitq(good_starts);
1120                         }
1121                 }
1122                 else {
1123                         del_waitq(good_starts);
1124                 }
1125
1126                 /* reject the coloring and bring the coloring to the initial state */
1127                 reject_coloring(changed_ones);
1128         }
1129
1130         /* free all intermediate created chunks except best one */
1131         while (! waitq_empty(tmp_chunks)) {
1132                 aff_chunk_t *tmp = waitq_get(tmp_chunks);
1133                 if (tmp != best_chunk)
1134                         delete_aff_chunk(env, tmp);
1135         }
1136         del_waitq(tmp_chunks);
1137
1138         /* return if coloring failed */
1139         if (! best_chunk) {
1140                 del_waitq(changed_ones);
1141                 if (best_starts)
1142                         del_waitq(best_starts);
1143                 return;
1144         }
1145
1146         DB((dbg, LEVEL_2, "\tbest chunk #%d ", best_chunk->id));
1147         DBG_AFF_CHUNK(env, LEVEL_2, best_chunk);
1148         DB((dbg, LEVEL_2, "using color %d\n", best_color));
1149
1150         /* get the best fragment from the best list and color it */
1151         while (! waitq_empty(best_starts)) {
1152                 co_mst_irn_t *node = waitq_get(best_starts);
1153                 int          res;
1154
1155                 if (! bitset_is_set(best_chunk->nodes, get_irn_idx(node->irn)))
1156                         continue;
1157
1158                 res = change_node_color(env, node, best_color, changed_ones);
1159                 if (! res)
1160                         panic("Color manifesting failed for %+F, color %d in chunk %d\n", node->irn, best_color, best_chunk->id);
1161                 node->fixed = 1;
1162                 node->chunk = best_chunk;
1163         }
1164         /* we colored the successful start nodes, now color the rest of the chunk */
1165         for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx) {
1166                 ir_node      *irn  = best_chunk->n[idx];
1167                 co_mst_irn_t *node = get_co_mst_irn(env, irn);
1168                 int          res;
1169
1170                 res = change_node_color(env, node, best_color, changed_ones);
1171                 if (! res)
1172                         panic("Color manifesting failed for %+F, color %d in chunk %d\n", irn, best_color, best_chunk->id);
1173                 DB((dbg, LEVEL_4, "\tManifesting color %d for %+F, chunk #%d\n", best_color, irn, best_chunk->id));
1174                 node->fixed = 1;
1175                 node->chunk = best_chunk;
1176         }
1177
1178         /* materialize colors on changed nodes */
1179         while (! waitq_empty(changed_ones)) {
1180                 co_mst_irn_t *n = waitq_get(changed_ones);
1181                 n->tmp_fixed = 0;
1182                 n->col       = n->tmp_col;
1183         }
1184
1185         /* remove the nodes in best chunk from original chunk */
1186         bitset_andnot(c->nodes, best_chunk->nodes);
1187         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1188                 ir_node *irn = c->n[idx];
1189
1190                 if (bitset_is_set(best_chunk->nodes, get_irn_idx(irn))) {
1191                         int last = ARR_LEN(c->n) - 1;
1192
1193                         c->n[idx] = c->n[last];
1194                         ARR_SHRINKLEN(c->n, last);
1195                         len--;
1196                 }
1197         }
1198
1199         /* we have to get the nodes back into the original chunk because they are scattered over temporary chunks */
1200         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1201                 ir_node      *n  = c->n[idx];
1202                 co_mst_irn_t *nn = get_co_mst_irn(env, n);
1203                 nn->chunk = c;
1204         }
1205
1206         /* fragment the remaining chunk */
1207         visited = bitset_irg_malloc(env->co->irg);
1208         bitset_or(visited, best_chunk->nodes);
1209         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1210                 ir_node *irn = c->n[idx];
1211                 if (! bitset_is_set(visited, get_irn_idx(irn))) {
1212                         aff_chunk_t  *new_chunk = new_aff_chunk(env);
1213                         co_mst_irn_t *node      = get_co_mst_irn(env, irn);
1214
1215                         expand_chunk_from(env, node, visited, new_chunk, c, decider_always_yes, 0);
1216                         aff_chunk_assure_weight(env, new_chunk);
1217                         pqueue_put(env->chunks, new_chunk, new_chunk->weight);
1218                 }
1219         }
1220
1221         /* clear obsolete chunks and free some memory */
1222         delete_aff_chunk(env, best_chunk);
1223         bitset_free(visited);
1224         del_waitq(changed_ones);
1225         if (best_starts)
1226                 del_waitq(best_starts);
1227 }
1228
1229 /**
1230  * Main driver for mst safe coalescing algorithm.
1231  */
1232 int co_solve_heuristic_mst(copy_opt_t *co) {
1233         unsigned     n_regs       = co->cls->n_regs;
1234         bitset_t     *ignore_regs = bitset_alloca(n_regs);
1235         unsigned     k;
1236         ir_node      *irn;
1237         co_mst_env_t mst_env;
1238
1239         /* init phase */
1240         phase_init(&mst_env.ph, "co_mst", co->irg, PHASE_DEFAULT_GROWTH, co_mst_irn_init, &mst_env);
1241
1242         k = be_put_ignore_regs(co->cenv->birg, co->cls, ignore_regs);
1243         k = n_regs - k;
1244
1245         mst_env.n_regs      = n_regs;
1246         mst_env.k           = k;
1247         mst_env.chunks      = new_pqueue();
1248         mst_env.co          = co;
1249         mst_env.ignore_regs = ignore_regs;
1250         mst_env.ifg         = co->cenv->ifg;
1251         mst_env.aenv        = co->aenv;
1252         mst_env.chunkset    = pset_new_ptr(512);
1253
1254         DBG((dbg, LEVEL_1, "==== Coloring %+F, class %s ====\n", co->irg, co->cls->name));
1255
1256         /* build affinity chunks */
1257         build_affinity_chunks(&mst_env);
1258
1259         /* color chunks as long as there are some */
1260         while (! pqueue_empty(mst_env.chunks)) {
1261                 aff_chunk_t *chunk = pqueue_get(mst_env.chunks);
1262
1263                 color_aff_chunk(&mst_env, chunk);
1264                 DB((dbg, LEVEL_4, "<<<====== Coloring chunk (%d) done\n", chunk->id));
1265                 delete_aff_chunk(&mst_env, chunk);
1266         }
1267
1268         /* apply coloring */
1269         foreach_phase_irn(&mst_env.ph, irn) {
1270                 co_mst_irn_t *mirn = get_co_mst_irn(&mst_env, irn);
1271                 const arch_register_t *reg;
1272
1273                 if (arch_irn_is(mst_env.aenv, irn, ignore))
1274                         continue;
1275
1276                 assert(mirn->fixed && "Node should have fixed color");
1277
1278                 /* skip nodes where color hasn't changed */
1279                 if (mirn->init_col == mirn->col)
1280                         continue;
1281
1282                 reg = arch_register_for_index(co->cls, mirn->col);
1283                 arch_set_irn_register(co->aenv, irn, reg);
1284                 DB((dbg, LEVEL_1, "%+F set color from %d to %d\n", irn, mirn->init_col, mirn->col));
1285         }
1286
1287         /* free allocated memory */
1288         del_pqueue(mst_env.chunks);
1289         phase_free(&mst_env.ph);
1290         del_pset(mst_env.chunkset);
1291
1292         return 0;
1293 }
1294
1295 void be_init_copyheur4(void) {
1296         FIRM_DBG_REGISTER(dbg, "firm.be.co.heur4");
1297 }
1298
1299 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyheur4);