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