array: Add and use NEW_ARR_DZ().
[libfirm] / ir / be / becopyheur4.c
1 /*
2  * Copyright (C) 1995-2011 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  *
26  * This is the C implementation of the mst algorithm
27  * originally written in Java by Sebastian Hack.
28  * (also known as "heur3" :)
29  * Performs simple copy minimization.
30  */
31 #include "config.h"
32
33 #define DISABLE_STATEV
34
35 #include <float.h>
36
37 #include "array.h"
38 #include "irnode_t.h"
39 #include "bitset.h"
40 #include "raw_bitset.h"
41 #include "irnodemap.h"
42 #include "pqueue.h"
43 #include "xmalloc.h"
44 #include "pdeq.h"
45 #include "irprintf.h"
46 #include "util.h"
47 #include "irtools.h"
48 #include "error.h"
49 #include "list.h"
50 #include "statev_t.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 #ifdef DEBUG_libfirm
60
61 #define DBG_AFF_CHUNK(env, level, chunk) do { if (firm_dbg_get_mask(dbg) & (level)) dbg_aff_chunk((env), (chunk)); } while (0)
62 #define DBG_COL_COST(env, level, cost)   do { if (firm_dbg_get_mask(dbg) & (level)) dbg_col_cost((env), (cost)); } while (0)
63
64 static firm_dbg_module_t *dbg = NULL;
65
66 #else
67
68 #define DBG_AFF_CHUNK(env, level, chunk)
69 #define DBG_COL_COST(env, level, cost)
70
71 #endif
72
73 typedef float real_t;
74 #define REAL(C)   (C ## f)
75
76 static unsigned last_chunk_id   = 0;
77 static int recolor_limit        = 7;
78 static double dislike_influence = REAL(0.1);
79
80 typedef struct col_cost_t {
81         int     col;
82         real_t  cost;
83 } col_cost_t;
84
85 /**
86  * An affinity chunk.
87  */
88 typedef struct aff_chunk_t {
89         const ir_node  **n;                     /**< An ARR_F containing all nodes of the chunk. */
90         const ir_node  **interfere;             /**< An ARR_F containing all inference. */
91         int              weight;                /**< Weight of this chunk */
92         unsigned         weight_consistent : 1; /**< Set if the weight is consistent. */
93         unsigned         deleted           : 1; /**< For debugging: Set if the was deleted. */
94         unsigned         id;                    /**< An id of this chunk. */
95         unsigned         visited;
96         list_head        list;
97         col_cost_t       color_affinity[1];
98 } aff_chunk_t;
99
100 /**
101  * An affinity edge.
102  */
103 typedef struct aff_edge_t {
104         const ir_node *src;                   /**< Source node. */
105         const ir_node *tgt;                   /**< Target node. */
106         int           weight;                 /**< The weight of this edge. */
107 } aff_edge_t;
108
109 /* main coalescing environment */
110 typedef struct co_mst_env_t {
111         int              n_regs;         /**< number of regs in class */
112         bitset_t         *allocatable_regs; /**< set containing all global ignore registers */
113         ir_nodemap        map;           /**< phase object holding data for nodes */
114         struct obstack    obst;
115         pqueue_t         *chunks;        /**< priority queue for chunks */
116         list_head         chunklist;     /**< list holding all chunks */
117         be_ifg_t         *ifg;           /**< the interference graph */
118         copy_opt_t       *co;            /**< the copy opt object */
119         unsigned         chunk_visited;
120         col_cost_t      **single_cols;
121 } co_mst_env_t;
122
123 /* stores coalescing related information for a node */
124 typedef struct co_mst_irn_t {
125         const ir_node    *irn;              /**< the irn this information belongs to */
126         aff_chunk_t      *chunk;            /**< the chunk this irn belongs to */
127         bitset_t         *adm_colors;       /**< set of admissible colors for this irn */
128         ir_node          **int_neighs;      /**< array of all interfering neighbours (cached for speed reasons) */
129         int              n_neighs;          /**< length of the interfering neighbours array. */
130         int              int_aff_neigh;     /**< number of interfering affinity neighbours */
131         int              col;               /**< color currently assigned */
132         int              init_col;          /**< the initial color */
133         int              tmp_col;           /**< a temporary assigned color */
134         unsigned         fixed     : 1;     /**< the color is fixed */
135         struct list_head list;              /**< Queue for coloring undo. */
136         real_t           constr_factor;
137 } co_mst_irn_t;
138
139 /**
140  * In case there is no phase information for irn, initialize it.
141  */
142 static co_mst_irn_t *co_mst_irn_init(co_mst_env_t *env, const ir_node *irn)
143 {
144         co_mst_irn_t *res = OALLOC(&env->obst, co_mst_irn_t);
145
146         const arch_register_req_t *req;
147         neighbours_iter_t nodes_it;
148         ir_node  *neigh;
149         unsigned len;
150
151         res->irn           = irn;
152         res->chunk         = NULL;
153         res->fixed         = 0;
154         res->tmp_col       = -1;
155         res->int_neighs    = NULL;
156         res->int_aff_neigh = 0;
157         res->col           = arch_get_irn_register(irn)->index;
158         res->init_col      = res->col;
159         INIT_LIST_HEAD(&res->list);
160
161         DB((dbg, LEVEL_4, "Creating phase info for %+F\n", irn));
162
163         /* set admissible registers */
164         res->adm_colors = bitset_obstack_alloc(&env->obst, env->n_regs);
165
166         /* Exclude colors not assignable to the irn */
167         req = arch_get_irn_register_req(irn);
168         if (arch_register_req_is(req, limited)) {
169                 rbitset_copy_to_bitset(req->limited, res->adm_colors);
170                 /* exclude global ignore registers as well */
171                 bitset_and(res->adm_colors, env->allocatable_regs);
172         } else {
173                 bitset_copy(res->adm_colors, env->allocatable_regs);
174         }
175
176         /* compute the constraint factor */
177         res->constr_factor = (real_t) (1 + env->n_regs - bitset_popcount(res->adm_colors)) / env->n_regs;
178
179         /* set the number of interfering affinity neighbours to -1, they are calculated later */
180         res->int_aff_neigh = -1;
181
182         /* build list of interfering neighbours */
183         len = 0;
184         be_ifg_foreach_neighbour(env->ifg, &nodes_it, irn, neigh) {
185                 if (!arch_irn_is_ignore(neigh)) {
186                         obstack_ptr_grow(&env->obst, neigh);
187                         ++len;
188                 }
189         }
190         res->int_neighs = (ir_node**)obstack_finish(&env->obst);
191         res->n_neighs   = len;
192         return res;
193 }
194
195 static co_mst_irn_t *get_co_mst_irn(co_mst_env_t *env, const ir_node *node)
196 {
197         co_mst_irn_t *res = ir_nodemap_get(co_mst_irn_t, &env->map, node);
198         if (res == NULL) {
199                 res = co_mst_irn_init(env, node);
200                 ir_nodemap_insert(&env->map, node, res);
201         }
202         return res;
203 }
204
205 typedef int decide_func_t(const co_mst_irn_t *node, int col);
206
207 #ifdef DEBUG_libfirm
208
209 /**
210  * Write a chunk to stderr for debugging.
211  */
212 static void dbg_aff_chunk(const co_mst_env_t *env, const aff_chunk_t *c)
213 {
214         int i, l;
215         (void) env;
216         if (c->weight_consistent)
217                 ir_fprintf(stderr, " $%d ", c->weight);
218         ir_fprintf(stderr, "{");
219         for (i = 0, l = ARR_LEN(c->n); i < l; ++i) {
220                 const ir_node *n = c->n[i];
221                 ir_fprintf(stderr, " %+F,", n);
222         }
223         ir_fprintf(stderr, "}");
224 }
225
226 /**
227  * Dump all admissible colors to stderr.
228  */
229 static void dbg_admissible_colors(const co_mst_env_t *env, const co_mst_irn_t *node)
230 {
231         (void) env;
232
233         if (bitset_popcount(node->adm_colors) < 1)
234                 fprintf(stderr, "no admissible colors?!?");
235         else {
236                 bitset_foreach(node->adm_colors, idx) {
237                         ir_fprintf(stderr, " %zu", idx);
238                 }
239         }
240 }
241
242 /**
243  * Dump color-cost pairs to stderr.
244  */
245 static void dbg_col_cost(const co_mst_env_t *env, const col_cost_t *cost)
246 {
247         int i;
248         for (i = 0; i < env->n_regs; ++i)
249                 fprintf(stderr, " (%d, %.4f)", cost[i].col, cost[i].cost);
250 }
251
252 #endif /* DEBUG_libfirm */
253
254 static inline int get_mst_irn_col(const co_mst_irn_t *node)
255 {
256         return node->tmp_col >= 0 ? node->tmp_col : node->col;
257 }
258
259 /**
260  * @return 1 if node @p node has color @p col, 0 otherwise.
261  */
262 static int decider_has_color(const co_mst_irn_t *node, int col)
263 {
264         return get_mst_irn_col(node) == col;
265 }
266
267 /**
268  * @return 1 if node @p node has not color @p col, 0 otherwise.
269  */
270 static int decider_hasnot_color(const co_mst_irn_t *node, int col)
271 {
272         return get_mst_irn_col(node) != col;
273 }
274
275 /**
276  * Always returns true.
277  */
278 static int decider_always_yes(const co_mst_irn_t *node, int col)
279 {
280         (void) node;
281         (void) col;
282         return 1;
283 }
284
285 /** compares two affinity edges by its weight */
286 static int cmp_aff_edge(const void *a, const void *b)
287 {
288         const aff_edge_t *e1 = (const aff_edge_t*)a;
289         const aff_edge_t *e2 = (const aff_edge_t*)b;
290
291         if (e2->weight == e1->weight) {
292                 if (e2->src->node_idx == e1->src->node_idx)
293                         return QSORT_CMP(e2->tgt->node_idx, e1->tgt->node_idx);
294                 else
295                         return QSORT_CMP(e2->src->node_idx, e1->src->node_idx);
296         }
297         /* sort in descending order */
298         return QSORT_CMP(e2->weight, e1->weight);
299 }
300
301 /** compares to color-cost pairs */
302 static __attribute__((unused)) int cmp_col_cost_lt(const void *a, const void *b)
303 {
304         const col_cost_t *c1 = (const col_cost_t*)a;
305         const col_cost_t *c2 = (const col_cost_t*)b;
306         real_t diff = c1->cost - c2->cost;
307
308         if (diff < 0)
309                 return 1;
310         if (diff > 0)
311                 return -1;
312
313         return QSORT_CMP(c1->col, c2->col);
314 }
315
316 static int cmp_col_cost_gt(const void *a, const void *b)
317 {
318         const col_cost_t *c1 = (const col_cost_t*)a;
319         const col_cost_t *c2 = (const col_cost_t*)b;
320         real_t diff = c2->cost - c1->cost;
321
322         if (diff > 0)
323                 return 1;
324         if (diff < 0)
325                 return -1;
326
327         return QSORT_CMP(c1->col, c2->col);
328 }
329
330 /**
331  * Creates a new affinity chunk
332  */
333 static inline aff_chunk_t *new_aff_chunk(co_mst_env_t *env)
334 {
335         aff_chunk_t *c = XMALLOCF(aff_chunk_t, color_affinity, env->n_regs);
336         c->n                 = NEW_ARR_F(const ir_node *, 0);
337         c->interfere         = NEW_ARR_F(const ir_node *, 0);
338         c->weight            = -1;
339         c->weight_consistent = 0;
340         c->deleted           = 0;
341         c->id                = ++last_chunk_id;
342         c->visited           = 0;
343         list_add(&c->list, &env->chunklist);
344         return c;
345 }
346
347 /**
348  * Frees all memory allocated by an affinity chunk.
349  */
350 static inline void delete_aff_chunk(aff_chunk_t *c)
351 {
352         list_del(&c->list);
353         DEL_ARR_F(c->interfere);
354         DEL_ARR_F(c->n);
355         c->deleted = 1;
356         free(c);
357 }
358
359 /**
360  * binary search of sorted nodes.
361  *
362  * @return the position where n is found in the array arr or ~pos
363  * if the nodes is not here.
364  */
365 static inline int nodes_bsearch(const ir_node **arr, const ir_node *n)
366 {
367         int hi = ARR_LEN(arr);
368         int lo = 0;
369
370         while (lo < hi) {
371                 int md = lo + ((hi - lo) >> 1);
372
373                 if (arr[md] == n)
374                         return md;
375                 if (arr[md] < n)
376                         lo = md + 1;
377                 else
378                         hi = md;
379         }
380
381         return ~lo;
382 }
383
384 /** Check if a node n can be found inside arr. */
385 static int node_contains(const ir_node **arr, const ir_node *n)
386 {
387         int i = nodes_bsearch(arr, n);
388         return i >= 0;
389 }
390
391 /**
392  * Insert a node into the sorted nodes list.
393  *
394  * @return 1 if the node was inserted, 0 else
395  */
396 static int nodes_insert(const ir_node ***arr, const ir_node *irn)
397 {
398         int idx = nodes_bsearch(*arr, irn);
399
400         if (idx < 0) {
401                 int i, n = ARR_LEN(*arr);
402                 const ir_node **l;
403
404                 ARR_APP1(const ir_node *, *arr, irn);
405
406                 /* move it */
407                 idx = ~idx;
408                 l = *arr;
409                 for (i = n - 1; i >= idx; --i)
410                         l[i + 1] = l[i];
411                 l[idx] = irn;
412                 return 1;
413         }
414         return 0;
415 }
416
417 /**
418  * Adds a node to an affinity chunk
419  */
420 static inline void aff_chunk_add_node(aff_chunk_t *c, co_mst_irn_t *node)
421 {
422         int i;
423
424         if (! nodes_insert(&c->n, node->irn))
425                 return;
426
427         c->weight_consistent = 0;
428         node->chunk          = c;
429
430         for (i = node->n_neighs - 1; i >= 0; --i) {
431                 ir_node *neigh = node->int_neighs[i];
432                 nodes_insert(&c->interfere, neigh);
433         }
434 }
435
436 /**
437  * Check if affinity chunk @p chunk interferes with node @p irn.
438  */
439 static inline int aff_chunk_interferes(const aff_chunk_t *chunk, const ir_node *irn)
440 {
441         return node_contains(chunk->interfere, irn);
442 }
443
444 /**
445  * Check if there are interference edges from c1 to c2.
446  * @param c1    A chunk
447  * @param c2    Another chunk
448  * @return 1 if there are interferences between nodes of c1 and c2, 0 otherwise.
449  */
450 static inline int aff_chunks_interfere(const aff_chunk_t *c1, const aff_chunk_t *c2)
451 {
452         int i;
453
454         if (c1 == c2)
455                 return 0;
456
457         /* check if there is a node in c2 having an interfering neighbor in c1 */
458         for (i = ARR_LEN(c2->n) - 1; i >= 0; --i) {
459                 const ir_node *irn = c2->n[i];
460
461                 if (node_contains(c1->interfere, irn))
462                         return 1;
463         }
464         return 0;
465 }
466
467 /**
468  * Returns the affinity chunk of @p irn or creates a new
469  * one with @p irn as element if there is none assigned.
470  */
471 static inline aff_chunk_t *get_aff_chunk(co_mst_env_t *env, const ir_node *irn)
472 {
473         co_mst_irn_t *node = get_co_mst_irn(env, irn);
474         return node->chunk;
475 }
476
477 /**
478  * Let chunk(src) absorb the nodes of chunk(tgt) (only possible when there
479  * are no interference edges from chunk(src) to chunk(tgt)).
480  * @return 1 if successful, 0 if not possible
481  */
482 static int aff_chunk_absorb(co_mst_env_t *env, const ir_node *src, const ir_node *tgt)
483 {
484         aff_chunk_t *c1 = get_aff_chunk(env, src);
485         aff_chunk_t *c2 = get_aff_chunk(env, tgt);
486
487 #ifdef DEBUG_libfirm
488                 DB((dbg, LEVEL_4, "Attempt to let c1 (id %u): ", c1 ? c1->id : 0));
489                 if (c1) {
490                         DBG_AFF_CHUNK(env, LEVEL_4, c1);
491                 } else {
492                         DB((dbg, LEVEL_4, "{%+F}", src));
493                 }
494                 DB((dbg, LEVEL_4, "\n\tabsorb c2 (id %u): ", c2 ? c2->id : 0));
495                 if (c2) {
496                         DBG_AFF_CHUNK(env, LEVEL_4, c2);
497                 } else {
498                         DB((dbg, LEVEL_4, "{%+F}", tgt));
499                 }
500                 DB((dbg, LEVEL_4, "\n"));
501 #endif
502
503         if (c1 == NULL) {
504                 if (c2 == NULL) {
505                         /* no chunk exists */
506                         co_mst_irn_t *mirn = get_co_mst_irn(env, src);
507                         int i;
508
509                         for (i = mirn->n_neighs - 1; i >= 0; --i) {
510                                 if (mirn->int_neighs[i] == tgt)
511                                         break;
512                         }
513                         if (i < 0) {
514                                 /* create one containing both nodes */
515                                 c1 = new_aff_chunk(env);
516                                 aff_chunk_add_node(c1, get_co_mst_irn(env, src));
517                                 aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
518                                 goto absorbed;
519                         }
520                 } else {
521                         /* c2 already exists */
522                         if (! aff_chunk_interferes(c2, src)) {
523                                 aff_chunk_add_node(c2, get_co_mst_irn(env, src));
524                                 goto absorbed;
525                         }
526                 }
527         } else if (c2 == NULL) {
528                 /* c1 already exists */
529                 if (! aff_chunk_interferes(c1, tgt)) {
530                         aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
531                         goto absorbed;
532                 }
533         } else if (c1 != c2 && ! aff_chunks_interfere(c1, c2)) {
534                 int idx, len;
535
536                 for (idx = 0, len = ARR_LEN(c2->n); idx < len; ++idx)
537                         aff_chunk_add_node(c1, get_co_mst_irn(env, c2->n[idx]));
538
539                 for (idx = 0, len = ARR_LEN(c2->interfere); idx < len; ++idx) {
540                         const ir_node *irn = c2->interfere[idx];
541                         nodes_insert(&c1->interfere, irn);
542                 }
543
544                 c1->weight_consistent = 0;
545
546                 delete_aff_chunk(c2);
547                 goto absorbed;
548         }
549         DB((dbg, LEVEL_4, " ... c1 interferes with c2, skipped\n"));
550         return 0;
551
552 absorbed:
553         DB((dbg, LEVEL_4, " ... absorbed\n"));
554         return 1;
555 }
556
557 /**
558  * Assures that the weight of the given chunk is consistent.
559  */
560 static void aff_chunk_assure_weight(co_mst_env_t *env, aff_chunk_t *c)
561 {
562         if (! c->weight_consistent) {
563                 int w = 0;
564                 int idx, len, i;
565
566                 for (i = 0; i < env->n_regs; ++i) {
567                         c->color_affinity[i].col = i;
568                         c->color_affinity[i].cost = REAL(0.0);
569                 }
570
571                 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
572                         const ir_node         *n       = c->n[idx];
573                         const affinity_node_t *an      = get_affinity_info(env->co, n);
574                         co_mst_irn_t          *node    = get_co_mst_irn(env, n);
575
576                         node->chunk = c;
577                         if (node->constr_factor > REAL(0.0)) {
578                                 bitset_foreach (node->adm_colors, col)
579                                         c->color_affinity[col].cost += node->constr_factor;
580                         }
581
582                         if (an != NULL) {
583                                 co_gs_foreach_neighb(an, neigh) {
584                                         const ir_node *m = neigh->irn;
585
586                                         if (arch_irn_is_ignore(m))
587                                                 continue;
588
589                                         w += node_contains(c->n, m) ? neigh->costs : 0;
590                                 }
591                         }
592                 }
593
594                 for (i = 0; i < env->n_regs; ++i)
595                         c->color_affinity[i].cost *= (REAL(1.0) / ARR_LEN(c->n));
596
597                 c->weight            = w;
598                 // c->weight            = bitset_popcount(c->nodes);
599                 c->weight_consistent = 1;
600         }
601 }
602
603 /**
604  * Count the number of interfering affinity neighbours
605  */
606 static int count_interfering_aff_neighs(co_mst_env_t *env, const affinity_node_t *an)
607 {
608         const ir_node      *irn  = an->irn;
609         const co_mst_irn_t *node = get_co_mst_irn(env, irn);
610         int                res   = 0;
611
612         co_gs_foreach_neighb(an, neigh) {
613                 const ir_node *n = neigh->irn;
614                 int           i;
615
616                 if (arch_irn_is_ignore(n))
617                         continue;
618
619                 /* check if the affinity neighbour interfere */
620                 for (i = 0; i < node->n_neighs; ++i) {
621                         if (node->int_neighs[i] == n) {
622                                 ++res;
623                                 break;
624                         }
625                 }
626         }
627         return res;
628 }
629
630
631 /**
632  * Build chunks of nodes connected by affinity edges.
633  * We start at the heaviest affinity edge.
634  * The chunks of the two edge-defining nodes will be
635  * merged if there are no interference edges from one
636  * chunk to the other.
637  */
638 static void build_affinity_chunks(co_mst_env_t *env)
639 {
640         nodes_iter_t nodes_it;
641         aff_edge_t  *edges    = NEW_ARR_F(aff_edge_t, 0);
642         ir_node     *n;
643         int         i, len;
644         size_t      pn;
645
646         /* at first we create the affinity edge objects */
647         be_ifg_foreach_node(env->ifg, &nodes_it, n) {
648                 int             n_idx = get_irn_idx(n);
649                 co_mst_irn_t    *n1;
650                 affinity_node_t *an;
651
652                 if (arch_irn_is_ignore(n))
653                         continue;
654
655                 n1 = get_co_mst_irn(env, n);
656                 an = get_affinity_info(env->co, n);
657
658                 if (an != NULL) {
659                         if (n1->int_aff_neigh < 0)
660                                 n1->int_aff_neigh = count_interfering_aff_neighs(env, an);
661
662                         /* build the affinity edges */
663                         co_gs_foreach_neighb(an, neigh) {
664                                 const ir_node *m     = neigh->irn;
665                                 int            m_idx = get_irn_idx(m);
666
667                                 /* record the edge in only one direction */
668                                 if (n_idx < m_idx) {
669                                         co_mst_irn_t *n2;
670                                         aff_edge_t   edge;
671
672                                         /* skip ignore nodes */
673                                         if (arch_irn_is_ignore(m))
674                                                 continue;
675
676                                         edge.src = n;
677                                         edge.tgt = m;
678
679                                         n2 = get_co_mst_irn(env, m);
680                                         if (n2->int_aff_neigh < 0) {
681                                                 affinity_node_t *am = get_affinity_info(env->co, m);
682                                                 n2->int_aff_neigh = count_interfering_aff_neighs(env, am);
683                                         }
684                                         /*
685                                          * these weights are pure hackery ;-).
686                                          * It's not chriswue's fault but mine.
687                                          */
688                                         edge.weight = neigh->costs;
689                                         ARR_APP1(aff_edge_t, edges, edge);
690                                 }
691                         }
692                 }
693         }
694
695         /* now: sort edges and build the affinity chunks */
696         len = ARR_LEN(edges);
697         qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
698         for (i = 0; i < len; ++i) {
699                 DBG((dbg, LEVEL_1, "edge (%u,%u) %f\n", edges[i].src->node_idx, edges[i].tgt->node_idx, edges[i].weight));
700
701                 (void)aff_chunk_absorb(env, edges[i].src, edges[i].tgt);
702         }
703
704         /* now insert all chunks into a priority queue */
705         list_for_each_entry(aff_chunk_t, curr_chunk, &env->chunklist, list) {
706                 aff_chunk_assure_weight(env, curr_chunk);
707
708                 DBG((dbg, LEVEL_1, "entry #%u", curr_chunk->id));
709                 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
710                 DBG((dbg, LEVEL_1, "\n"));
711
712                 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
713         }
714
715         for (pn = 0; pn < ARR_LEN(env->map.data); ++pn) {
716                 co_mst_irn_t *mirn = (co_mst_irn_t*)env->map.data[pn];
717                 if (mirn == NULL)
718                         continue;
719                 if (mirn->chunk != NULL)
720                         continue;
721
722                 /* no chunk is allocated so far, do it now */
723                 aff_chunk_t *curr_chunk = new_aff_chunk(env);
724                 aff_chunk_add_node(curr_chunk, mirn);
725
726                 aff_chunk_assure_weight(env, curr_chunk);
727
728                 DBG((dbg, LEVEL_1, "entry #%u", curr_chunk->id));
729                 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
730                 DBG((dbg, LEVEL_1, "\n"));
731
732                 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
733         }
734
735         DEL_ARR_F(edges);
736 }
737
738 static __attribute__((unused)) void chunk_order_nodes(co_mst_env_t *env, aff_chunk_t *chunk)
739 {
740         pqueue_t      *grow       = new_pqueue();
741         ir_node const *max_node   = NULL;
742         int            max_weight = 0;
743         size_t         i;
744
745         for (i = ARR_LEN(chunk->n); i != 0;) {
746                 const ir_node   *irn = chunk->n[--i];
747                 affinity_node_t *an  = get_affinity_info(env->co, irn);
748                 int w = 0;
749
750                 if (arch_irn_is_ignore(irn))
751                         continue;
752
753                 if (an) {
754                         co_gs_foreach_neighb(an, neigh)
755                                 w += neigh->costs;
756
757                         if (w > max_weight) {
758                                 max_weight = w;
759                                 max_node   = irn;
760                         }
761                 }
762         }
763
764         if (max_node) {
765                 bitset_t *visited = bitset_malloc(get_irg_last_idx(env->co->irg));
766
767                 for (i = ARR_LEN(chunk->n); i != 0;)
768                         bitset_set(visited, get_irn_idx(chunk->n[--i]));
769
770                 pqueue_put(grow, (void *) max_node, max_weight);
771                 bitset_clear(visited, get_irn_idx(max_node));
772                 i = 0;
773                 while (!pqueue_empty(grow)) {
774                         ir_node *irn = (ir_node*)pqueue_pop_front(grow);
775                         affinity_node_t *an = get_affinity_info(env->co, irn);
776
777                         if (arch_irn_is_ignore(irn))
778                                 continue;
779
780                         assert(i <= ARR_LEN(chunk->n));
781                         chunk->n[i++] = irn;
782
783                         assert(an);
784
785                         /* build the affinity edges */
786                         co_gs_foreach_neighb(an, neigh) {
787                                 co_mst_irn_t *node = get_co_mst_irn(env, neigh->irn);
788
789                                 if (bitset_is_set(visited, get_irn_idx(node->irn))) {
790                                         pqueue_put(grow, (void *) neigh->irn, neigh->costs);
791                                         bitset_clear(visited, get_irn_idx(node->irn));
792                                 }
793                         }
794                 }
795
796                 del_pqueue(grow);
797                 bitset_free(visited);
798         }
799 }
800
801 /**
802  * Greedy collect affinity neighbours into thew new chunk @p chunk starting at node @p node.
803  */
804 static void expand_chunk_from(co_mst_env_t *env, co_mst_irn_t *node, bitset_t *visited,
805         aff_chunk_t *chunk, aff_chunk_t *orig_chunk, decide_func_t *decider, int col)
806 {
807         waitq *nodes = new_waitq();
808
809         DBG((dbg, LEVEL_1, "\n\tExpanding new chunk (#%u) from %+F, color %d:", chunk->id, node->irn, col));
810
811         /* init queue and chunk */
812         waitq_put(nodes, node);
813         bitset_set(visited, get_irn_idx(node->irn));
814         aff_chunk_add_node(chunk, node);
815         DB((dbg, LEVEL_1, " %+F", node->irn));
816
817         /* as long as there are nodes in the queue */
818         while (! waitq_empty(nodes)) {
819                 co_mst_irn_t    *n  = (co_mst_irn_t*)waitq_get(nodes);
820                 affinity_node_t *an = get_affinity_info(env->co, n->irn);
821
822                 /* check all affinity neighbors */
823                 if (an != NULL) {
824                         co_gs_foreach_neighb(an, neigh) {
825                                 const ir_node *m    = neigh->irn;
826                                 int            m_idx = get_irn_idx(m);
827                                 co_mst_irn_t *n2;
828
829                                 if (arch_irn_is_ignore(m))
830                                         continue;
831
832                                 n2 = get_co_mst_irn(env, m);
833
834                                 if (! bitset_is_set(visited, m_idx)  &&
835                                         decider(n2, col)                 &&
836                                         ! n2->fixed                      &&
837                                         ! aff_chunk_interferes(chunk, m) &&
838                                         node_contains(orig_chunk->n, m))
839                                 {
840                                         /*
841                                                 following conditions are met:
842                                                 - neighbour is not visited
843                                                 - neighbour likes the color
844                                                 - neighbour has not yet a fixed color
845                                                 - the new chunk doesn't interfere with the neighbour
846                                                 - neighbour belongs or belonged once to the original chunk
847                                         */
848                                         bitset_set(visited, m_idx);
849                                         aff_chunk_add_node(chunk, n2);
850                                         DB((dbg, LEVEL_1, " %+F", n2->irn));
851                                         /* enqueue for further search */
852                                         waitq_put(nodes, n2);
853                                 }
854                         }
855                 }
856         }
857
858         DB((dbg, LEVEL_1, "\n"));
859
860         del_waitq(nodes);
861 }
862
863 /**
864  * Fragment the given chunk into chunks having given color and not having given color.
865  */
866 static aff_chunk_t *fragment_chunk(co_mst_env_t *env, int col, aff_chunk_t *c, waitq *tmp)
867 {
868         bitset_t    *visited = bitset_malloc(get_irg_last_idx(env->co->irg));
869         int         idx, len;
870         aff_chunk_t *best = NULL;
871
872         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
873                 const ir_node *irn;
874                 co_mst_irn_t  *node;
875                 aff_chunk_t   *tmp_chunk;
876                 decide_func_t *decider;
877                 int           check_for_best;
878
879                 irn = c->n[idx];
880                 if (bitset_is_set(visited, get_irn_idx(irn)))
881                         continue;
882
883                 node = get_co_mst_irn(env, irn);
884
885                 if (get_mst_irn_col(node) == col) {
886                         decider        = decider_has_color;
887                         check_for_best = 1;
888                         DBG((dbg, LEVEL_4, "\tcolor %d wanted\n", col));
889                 }
890                 else {
891                         decider        = decider_hasnot_color;
892                         check_for_best = 0;
893                         DBG((dbg, LEVEL_4, "\tcolor %d forbidden\n", col));
894                 }
895
896                 /* create a new chunk starting at current node */
897                 tmp_chunk = new_aff_chunk(env);
898                 waitq_put(tmp, tmp_chunk);
899                 expand_chunk_from(env, node, visited, tmp_chunk, c, decider, col);
900                 assert(ARR_LEN(tmp_chunk->n) > 0 && "No nodes added to chunk");
901
902                 /* remember the local best */
903                 aff_chunk_assure_weight(env, tmp_chunk);
904                 if (check_for_best && (! best || best->weight < tmp_chunk->weight))
905                         best = tmp_chunk;
906         }
907
908         assert(best && "No chunk found?");
909         bitset_free(visited);
910         return best;
911 }
912
913 /**
914  * Resets the temporary fixed color of all nodes within wait queue @p nodes.
915  * ATTENTION: the queue is empty after calling this function!
916  */
917 static inline void reject_coloring(struct list_head *nodes)
918 {
919         DB((dbg, LEVEL_4, "\treject coloring for"));
920         list_for_each_entry_safe(co_mst_irn_t, n, temp, nodes, list) {
921                 DB((dbg, LEVEL_4, " %+F", n->irn));
922                 assert(n->tmp_col >= 0);
923                 n->tmp_col = -1;
924                 list_del_init(&n->list);
925         }
926         DB((dbg, LEVEL_4, "\n"));
927 }
928
929 static inline void materialize_coloring(struct list_head *nodes)
930 {
931         list_for_each_entry_safe(co_mst_irn_t, n, temp, nodes, list) {
932                 assert(n->tmp_col >= 0);
933                 n->col     = n->tmp_col;
934                 n->tmp_col = -1;
935                 list_del_init(&n->list);
936         }
937 }
938
939 static inline void set_temp_color(co_mst_irn_t *node, int col, struct list_head *changed)
940 {
941         assert(col >= 0);
942         assert(!node->fixed);
943         assert(node->tmp_col < 0);
944         assert(node->list.next == &node->list && node->list.prev == &node->list);
945         assert(bitset_is_set(node->adm_colors, col));
946
947         list_add_tail(&node->list, changed);
948         node->tmp_col = col;
949 }
950
951 static inline int is_loose(co_mst_irn_t *node)
952 {
953         return !node->fixed && node->tmp_col < 0;
954 }
955
956 /**
957  * Determines the costs for each color if it would be assigned to node @p node.
958  */
959 static void determine_color_costs(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs)
960 {
961         int   *neigh_cols = ALLOCAN(int, env->n_regs);
962         int    n_loose    = 0;
963         real_t coeff;
964         int    i;
965
966         for (i = 0; i < env->n_regs; ++i) {
967                 neigh_cols[i] = 0;
968                 costs[i].col = i;
969                 costs[i].cost = bitset_is_set(node->adm_colors, i) ? node->constr_factor : REAL(0.0);
970         }
971
972         for (i = 0; i < node->n_neighs; ++i) {
973                 co_mst_irn_t *n = get_co_mst_irn(env, node->int_neighs[i]);
974                 int col = get_mst_irn_col(n);
975                 if (is_loose(n)) {
976                         ++n_loose;
977                         ++neigh_cols[col];
978                 } else
979                         costs[col].cost = REAL(0.0);
980         }
981
982         if (n_loose > 0) {
983                 coeff = REAL(1.0) / n_loose;
984                 for (i = 0; i < env->n_regs; ++i)
985                         costs[i].cost *= REAL(1.0) - coeff * neigh_cols[i];
986         }
987 }
988
989 /* need forward declaration due to recursive call */
990 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, struct list_head *changed_ones, int depth, int *max_depth, int *trip);
991
992 /**
993  * Tries to change node to a color but @p explude_col.
994  * @return 1 if succeeded, 0 otherwise.
995  */
996 static int change_node_color_excluded(co_mst_env_t *env, co_mst_irn_t *node, int exclude_col, struct list_head *changed, int depth, int *max_depth, int *trip)
997 {
998         int col = get_mst_irn_col(node);
999         int res = 0;
1000
1001         /* neighbours has already a different color -> good, temporary fix it */
1002         if (col != exclude_col) {
1003                 if (is_loose(node))
1004                         set_temp_color(node, col, changed);
1005                 return 1;
1006         }
1007
1008         /* The node has the color it should not have _and_ has not been visited yet. */
1009         if (is_loose(node)) {
1010                 col_cost_t *costs = ALLOCAN(col_cost_t, env->n_regs);
1011
1012                 /* Get the costs for giving the node a specific color. */
1013                 determine_color_costs(env, node, costs);
1014
1015                 /* Since the node must not have the not_col, set the costs for that color to "infinity" */
1016                 costs[exclude_col].cost = REAL(0.0);
1017
1018                 /* sort the colors according costs, cheapest first. */
1019                 qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost_gt);
1020
1021                 /* Try recoloring the node using the color list. */
1022                 res = recolor_nodes(env, node, costs, changed, depth + 1, max_depth, trip);
1023         }
1024
1025         return res;
1026 }
1027
1028 /**
1029  * Tries to bring node @p node to cheapest color and color all interfering neighbours with other colors.
1030  * ATTENTION: Expect @p costs already sorted by increasing costs.
1031  * @return 1 if coloring could be applied, 0 otherwise.
1032  */
1033 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, struct list_head *changed, int depth, int *max_depth, int *trip)
1034 {
1035         int   i;
1036         struct list_head local_changed;
1037
1038         ++*trip;
1039         if (depth > *max_depth)
1040                 *max_depth = depth;
1041
1042         DBG((dbg, LEVEL_4, "\tRecoloring %+F with color-costs", node->irn));
1043         DBG_COL_COST(env, LEVEL_4, costs);
1044         DB((dbg, LEVEL_4, "\n"));
1045
1046         if (depth >= recolor_limit) {
1047                 DBG((dbg, LEVEL_4, "\tHit recolor limit\n"));
1048                 return 0;
1049         }
1050
1051         for (i = 0; i < env->n_regs; ++i) {
1052                 int tgt_col  = costs[i].col;
1053                 int neigh_ok = 1;
1054                 int j;
1055
1056                 /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
1057                 if (costs[i].cost == REAL(0.0)) {
1058                         DBG((dbg, LEVEL_4, "\tAll further colors forbidden\n"));
1059                         return 0;
1060                 }
1061
1062                 /* Set the new color of the node and mark the node as temporarily fixed. */
1063                 assert(node->tmp_col < 0 && "Node must not have been temporary fixed.");
1064                 INIT_LIST_HEAD(&local_changed);
1065                 set_temp_color(node, tgt_col, &local_changed);
1066                 DBG((dbg, LEVEL_4, "\tTemporary setting %+F to color %d\n", node->irn, tgt_col));
1067
1068                 /* try to color all interfering neighbours with current color forbidden */
1069                 for (j = 0; j < node->n_neighs; ++j) {
1070                         co_mst_irn_t *nn;
1071                         ir_node      *neigh;
1072
1073                         neigh = node->int_neighs[j];
1074
1075                         if (arch_irn_is_ignore(neigh))
1076                                 continue;
1077
1078                         nn = get_co_mst_irn(env, neigh);
1079                         DB((dbg, LEVEL_4, "\tHandling neighbour %+F, at position %d (fixed: %d, tmp_col: %d, col: %d)\n",
1080                                 neigh, j, nn->fixed, nn->tmp_col, nn->col));
1081
1082                         /*
1083                                 Try to change the color of the neighbor and record all nodes which
1084                                 get changed in the tmp list. Add this list to the "changed" list for
1085                                 that color. If we did not succeed to change the color of the neighbor,
1086                                 we bail out and try the next color.
1087                         */
1088                         if (get_mst_irn_col(nn) == tgt_col) {
1089                                 /* try to color neighbour with tgt_col forbidden */
1090                                 neigh_ok = change_node_color_excluded(env, nn, tgt_col, &local_changed, depth + 1, max_depth, trip);
1091
1092                                 if (!neigh_ok)
1093                                         break;
1094                         }
1095                 }
1096
1097                 /*
1098                         We managed to assign the target color to all neighbors, so from the perspective
1099                         of the current node, every thing was ok and we can return safely.
1100                 */
1101                 if (neigh_ok) {
1102                         /* append the local_changed ones to global ones */
1103                         list_splice(&local_changed, changed);
1104                         return 1;
1105                 }
1106                 else {
1107                         /* coloring of neighbours failed, so we try next color */
1108                         reject_coloring(&local_changed);
1109                 }
1110         }
1111
1112         DBG((dbg, LEVEL_4, "\tAll colors failed\n"));
1113         return 0;
1114 }
1115
1116 /**
1117  * Tries to bring node @p node and all its neighbours to color @p tgt_col.
1118  * @return 1 if color @p col could be applied, 0 otherwise
1119  */
1120 static int change_node_color(co_mst_env_t *env, co_mst_irn_t *node, int tgt_col, struct list_head *changed)
1121 {
1122         int col = get_mst_irn_col(node);
1123
1124         /* if node already has the target color -> good, temporary fix it */
1125         if (col == tgt_col) {
1126                 DBG((dbg, LEVEL_4, "\t\tCNC: %+F has already color %d, fix temporary\n", node->irn, tgt_col));
1127                 if (is_loose(node))
1128                         set_temp_color(node, tgt_col, changed);
1129                 return 1;
1130         }
1131
1132         /*
1133                 Node has not yet a fixed color and target color is admissible
1134                 -> try to recolor node and its affinity neighbours
1135         */
1136         if (is_loose(node) && bitset_is_set(node->adm_colors, tgt_col)) {
1137                 col_cost_t *costs = env->single_cols[tgt_col];
1138                 int res, max_depth, trip;
1139
1140                 max_depth = 0;
1141                 trip      = 0;
1142
1143                 DBG((dbg, LEVEL_4, "\t\tCNC: Attempt to recolor %+F ===>>\n", node->irn));
1144                 res = recolor_nodes(env, node, costs, changed, 0, &max_depth, &trip);
1145                 DBG((dbg, LEVEL_4, "\t\tCNC: <<=== Recoloring of %+F %s\n", node->irn, res ? "succeeded" : "failed"));
1146                 stat_ev_int("heur4_recolor_depth_max", max_depth);
1147                 stat_ev_int("heur4_recolor_trip", trip);
1148
1149
1150                 return res;
1151         }
1152
1153 #ifdef DEBUG_libfirm
1154                 if (firm_dbg_get_mask(dbg) & LEVEL_4) {
1155                         if (!is_loose(node))
1156                                 DB((dbg, LEVEL_4, "\t\tCNC: %+F has already fixed color %d\n", node->irn, col));
1157                         else {
1158                                 DB((dbg, LEVEL_4, "\t\tCNC: color %d not admissible for %+F (", tgt_col, node->irn));
1159                                 dbg_admissible_colors(env, node);
1160                                 DB((dbg, LEVEL_4, ")\n"));
1161                         }
1162                 }
1163 #endif
1164
1165         return 0;
1166 }
1167
1168 /**
1169  * Tries to color an affinity chunk (or at least a part of it).
1170  * Inserts uncolored parts of the chunk as a new chunk into the priority queue.
1171  */
1172 static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c)
1173 {
1174         aff_chunk_t *best_chunk   = NULL;
1175         int         n_nodes       = ARR_LEN(c->n);
1176         int         best_color    = -1;
1177         int         n_int_chunks  = 0;
1178         waitq       *tmp_chunks   = new_waitq();
1179         waitq       *best_starts  = NULL;
1180         col_cost_t  *order        = ALLOCANZ(col_cost_t, env->n_regs);
1181         bitset_t    *visited;
1182         int         i;
1183         size_t      idx;
1184         size_t      len;
1185         size_t      nidx;
1186         size_t      pos;
1187         struct list_head changed;
1188
1189         DB((dbg, LEVEL_2, "fragmentizing chunk #%u", c->id));
1190         DBG_AFF_CHUNK(env, LEVEL_2, c);
1191         DB((dbg, LEVEL_2, "\n"));
1192
1193         stat_ev_ctx_push_fmt("heur4_color_chunk", "%u", c->id);
1194
1195         ++env->chunk_visited;
1196
1197         /* compute color preference */
1198         for (pos = 0, len = ARR_LEN(c->interfere); pos < len; ++pos) {
1199                 const ir_node *n = c->interfere[pos];
1200                 co_mst_irn_t *node = get_co_mst_irn(env, n);
1201                 aff_chunk_t *chunk = node->chunk;
1202
1203                 if (is_loose(node) && chunk && chunk->visited < env->chunk_visited) {
1204                         assert(!chunk->deleted);
1205                         chunk->visited = env->chunk_visited;
1206                         ++n_int_chunks;
1207
1208                         aff_chunk_assure_weight(env, chunk);
1209                         for (i = 0; i < env->n_regs; ++i)
1210                                 order[i].cost += chunk->color_affinity[i].cost;
1211                 }
1212         }
1213
1214         for (i = 0; i < env->n_regs; ++i) {
1215                 real_t dislike = n_int_chunks > 0 ? REAL(1.0) - order[i].cost / n_int_chunks : REAL(0.0);
1216                 order[i].col  = i;
1217                 order[i].cost = (REAL(1.0) - dislike_influence) * c->color_affinity[i].cost + dislike_influence * dislike;
1218         }
1219
1220         qsort(order, env->n_regs, sizeof(order[0]), cmp_col_cost_gt);
1221
1222         DBG_COL_COST(env, LEVEL_2, order);
1223         DB((dbg, LEVEL_2, "\n"));
1224
1225         /* check which color is the "best" for the given chunk.
1226          * if we found a color which was ok for all nodes, we take it
1227          * and do not look further. (see did_all flag usage below.)
1228          * If we have many colors which fit all nodes it is hard to decide
1229          * which one to take anyway.
1230          * TODO Sebastian: Perhaps we should at all nodes and figure out
1231          * a suitable color using costs as done above (determine_color_costs).
1232          */
1233         for (i = 0; i < env->n_regs; ++i) {
1234                 int         col = order[i].col;
1235                 waitq       *good_starts;
1236                 aff_chunk_t *local_best;
1237                 int          n_succeeded;
1238
1239                 /* skip ignore colors */
1240                 if (!bitset_is_set(env->allocatable_regs, col))
1241                         continue;
1242
1243                 DB((dbg, LEVEL_2, "\ttrying color %d\n", col));
1244
1245                 n_succeeded = 0;
1246                 good_starts = new_waitq();
1247
1248                 /* try to bring all nodes of given chunk to the current color. */
1249                 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1250                         const ir_node   *irn  = c->n[idx];
1251                         co_mst_irn_t    *node = get_co_mst_irn(env, irn);
1252                         int              good;
1253
1254                         assert(! node->fixed && "Node must not have a fixed color.");
1255                         DB((dbg, LEVEL_4, "\t\tBringing %+F from color %d to color %d ...\n", irn, node->col, col));
1256
1257                         /*
1258                                 The order of the colored nodes is important, so we record the successfully
1259                                 colored ones in the order they appeared.
1260                         */
1261                         INIT_LIST_HEAD(&changed);
1262                         stat_ev_tim_push();
1263                         good = change_node_color(env, node, col, &changed);
1264                         stat_ev_tim_pop("heur4_recolor");
1265                         if (good) {
1266                                 waitq_put(good_starts, node);
1267                                 materialize_coloring(&changed);
1268                                 node->fixed = 1;
1269                         }
1270
1271                         else
1272                                 reject_coloring(&changed);
1273
1274                         n_succeeded += good;
1275                         DB((dbg, LEVEL_4, "\t\t... %+F attempt from %d to %d %s\n", irn, node->col, col, good ? "succeeded" : "failed"));
1276                 }
1277
1278                 /* unfix all nodes */
1279                 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1280                         co_mst_irn_t *node = get_co_mst_irn(env, c->n[idx]);
1281                         node->fixed = 0;
1282                 }
1283
1284                 /* try next color when failed */
1285                 if (n_succeeded == 0) {
1286                         del_waitq(good_starts);
1287                         continue;
1288                 }
1289
1290                 /* fragment the chunk according to the coloring */
1291                 local_best = fragment_chunk(env, col, c, tmp_chunks);
1292
1293                 /* search the best of the good list
1294                    and make it the new best if it is better than the current */
1295                 if (local_best) {
1296                         aff_chunk_assure_weight(env, local_best);
1297
1298                         DB((dbg, LEVEL_3, "\t\tlocal best chunk (id %u) for color %d: ", local_best->id, col));
1299                         DBG_AFF_CHUNK(env, LEVEL_3, local_best);
1300
1301                         if (! best_chunk || best_chunk->weight < local_best->weight) {
1302                                 best_chunk = local_best;
1303                                 best_color = col;
1304                                 if (best_starts)
1305                                         del_waitq(best_starts);
1306                                 best_starts = good_starts;
1307                                 DB((dbg, LEVEL_3, "\n\t\t... setting global best chunk (id %u), color %d\n", best_chunk->id, best_color));
1308                         } else {
1309                                 DB((dbg, LEVEL_3, "\n\t\t... omitting, global best is better\n"));
1310                                 del_waitq(good_starts);
1311                         }
1312                 }
1313                 else {
1314                         del_waitq(good_starts);
1315                 }
1316
1317                 /* if all nodes were recolored, bail out */
1318                 if (n_succeeded == n_nodes)
1319                         break;
1320         }
1321
1322         stat_ev_int("heur4_colors_tried", i);
1323
1324         /* free all intermediate created chunks except best one */
1325         while (! waitq_empty(tmp_chunks)) {
1326                 aff_chunk_t *tmp = (aff_chunk_t*)waitq_get(tmp_chunks);
1327                 if (tmp != best_chunk)
1328                         delete_aff_chunk(tmp);
1329         }
1330         del_waitq(tmp_chunks);
1331
1332         /* return if coloring failed */
1333         if (! best_chunk) {
1334                 if (best_starts)
1335                         del_waitq(best_starts);
1336                 return;
1337         }
1338
1339         DB((dbg, LEVEL_2, "\tbest chunk #%u ", best_chunk->id));
1340         DBG_AFF_CHUNK(env, LEVEL_2, best_chunk);
1341         DB((dbg, LEVEL_2, "using color %d\n", best_color));
1342
1343         for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx) {
1344                 const ir_node *irn  = best_chunk->n[idx];
1345                 co_mst_irn_t  *node = get_co_mst_irn(env, irn);
1346                 int res;
1347
1348                 /* bring the node to the color. */
1349                 DB((dbg, LEVEL_4, "\tManifesting color %d for %+F, chunk #%u\n", best_color, node->irn, best_chunk->id));
1350                 INIT_LIST_HEAD(&changed);
1351                 stat_ev_tim_push();
1352                 res = change_node_color(env, node, best_color, &changed);
1353                 stat_ev_tim_pop("heur4_recolor");
1354                 if (res) {
1355                         materialize_coloring(&changed);
1356                         node->fixed = 1;
1357                 }
1358                 assert(list_empty(&changed));
1359         }
1360
1361         /* remove the nodes in best chunk from original chunk */
1362         len = ARR_LEN(best_chunk->n);
1363         for (idx = 0; idx < len; ++idx) {
1364                 const ir_node *irn = best_chunk->n[idx];
1365                 int pos = nodes_bsearch(c->n, irn);
1366
1367                 if (pos > 0)
1368                         c->n[pos] = NULL;
1369         }
1370         len = ARR_LEN(c->n);
1371         for (idx = nidx = 0; idx < len; ++idx) {
1372                 const ir_node *irn = c->n[idx];
1373
1374                 if (irn != NULL) {
1375                         c->n[nidx++] = irn;
1376                 }
1377         }
1378         ARR_SHRINKLEN(c->n, nidx);
1379
1380
1381         /* we have to get the nodes back into the original chunk because they are scattered over temporary chunks */
1382         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1383                 const ir_node *n  = c->n[idx];
1384                 co_mst_irn_t  *nn = get_co_mst_irn(env, n);
1385                 nn->chunk = c;
1386         }
1387
1388         /* fragment the remaining chunk */
1389         visited = bitset_malloc(get_irg_last_idx(env->co->irg));
1390         for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx)
1391                 bitset_set(visited, get_irn_idx(best_chunk->n[idx]));
1392
1393         for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1394                 const ir_node *irn = c->n[idx];
1395                 if (! bitset_is_set(visited, get_irn_idx(irn))) {
1396                         aff_chunk_t  *new_chunk = new_aff_chunk(env);
1397                         co_mst_irn_t *node      = get_co_mst_irn(env, irn);
1398
1399                         expand_chunk_from(env, node, visited, new_chunk, c, decider_always_yes, 0);
1400                         aff_chunk_assure_weight(env, new_chunk);
1401                         pqueue_put(env->chunks, new_chunk, new_chunk->weight);
1402                 }
1403         }
1404
1405         for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx) {
1406                 const ir_node *n  = best_chunk->n[idx];
1407                 co_mst_irn_t  *nn = get_co_mst_irn(env, n);
1408                 nn->chunk = NULL;
1409         }
1410
1411         /* clear obsolete chunks and free some memory */
1412         delete_aff_chunk(best_chunk);
1413         bitset_free(visited);
1414         if (best_starts)
1415                 del_waitq(best_starts);
1416
1417         stat_ev_ctx_pop("heur4_color_chunk");
1418 }
1419
1420 /**
1421  * Main driver for mst safe coalescing algorithm.
1422  */
1423 static int co_solve_heuristic_mst(copy_opt_t *co)
1424 {
1425         unsigned     n_regs            = co->cls->n_regs;
1426         bitset_t     *allocatable_regs = bitset_alloca(n_regs);
1427         unsigned     i, j;
1428         size_t       pn;
1429         ir_node      *irn;
1430         co_mst_env_t mst_env;
1431
1432         last_chunk_id = 0;
1433
1434         stat_ev_tim_push();
1435
1436         /* init phase */
1437         ir_nodemap_init(&mst_env.map, co->irg);
1438         obstack_init(&mst_env.obst);
1439
1440         be_put_allocatable_regs(co->cenv->irg, co->cls, allocatable_regs);
1441
1442         mst_env.n_regs           = n_regs;
1443         mst_env.chunks           = new_pqueue();
1444         mst_env.co               = co;
1445         mst_env.allocatable_regs = allocatable_regs;
1446         mst_env.ifg              = co->cenv->ifg;
1447         INIT_LIST_HEAD(&mst_env.chunklist);
1448         mst_env.chunk_visited    = 0;
1449         mst_env.single_cols      = OALLOCN(&mst_env.obst, col_cost_t*, n_regs);
1450
1451         for (i = 0; i < n_regs; ++i) {
1452                 col_cost_t *vec = OALLOCN(&mst_env.obst, col_cost_t, n_regs);
1453
1454                 mst_env.single_cols[i] = vec;
1455                 for (j = 0; j < n_regs; ++j) {
1456                         vec[j].col  = j;
1457                         vec[j].cost = REAL(0.0);
1458                 }
1459                 vec[i].col  = 0;
1460                 vec[0].col  = i;
1461                 vec[0].cost = REAL(1.0);
1462         }
1463
1464         DBG((dbg, LEVEL_1, "==== Coloring %+F, class %s ====\n", co->irg, co->cls->name));
1465
1466         /* build affinity chunks */
1467         stat_ev_tim_push();
1468         build_affinity_chunks(&mst_env);
1469         stat_ev_tim_pop("heur4_initial_chunk");
1470
1471         /* color chunks as long as there are some */
1472         while (! pqueue_empty(mst_env.chunks)) {
1473                 aff_chunk_t *chunk = (aff_chunk_t*)pqueue_pop_front(mst_env.chunks);
1474
1475                 color_aff_chunk(&mst_env, chunk);
1476                 DB((dbg, LEVEL_4, "<<<====== Coloring chunk (%u) done\n", chunk->id));
1477                 delete_aff_chunk(chunk);
1478         }
1479
1480         /* apply coloring */
1481         for (pn = 0; pn < ARR_LEN(mst_env.map.data); ++pn) {
1482                 co_mst_irn_t *mirn = (co_mst_irn_t*)mst_env.map.data[pn];
1483                 const arch_register_t *reg;
1484                 if (mirn == NULL)
1485                         continue;
1486                 irn = get_idx_irn(co->irg, pn);
1487                 if (arch_irn_is_ignore(irn))
1488                         continue;
1489
1490                 /* skip nodes where color hasn't changed */
1491                 if (mirn->init_col == mirn->col)
1492                         continue;
1493
1494                 reg = arch_register_for_index(co->cls, mirn->col);
1495                 arch_set_irn_register(irn, reg);
1496                 DB((dbg, LEVEL_1, "%+F set color from %d to %d\n", irn, mirn->init_col, mirn->col));
1497         }
1498
1499         /* free allocated memory */
1500         del_pqueue(mst_env.chunks);
1501         obstack_free(&mst_env.obst, NULL);
1502         ir_nodemap_destroy(&mst_env.map);
1503
1504         stat_ev_tim_pop("heur4_total");
1505
1506         return 0;
1507 }
1508
1509 static const lc_opt_table_entry_t options[] = {
1510         LC_OPT_ENT_INT      ("limit", "limit recoloring",  &recolor_limit),
1511         LC_OPT_ENT_DBL      ("di",    "dislike influence", &dislike_influence),
1512         LC_OPT_LAST
1513 };
1514
1515 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyheur4)
1516 void be_init_copyheur4(void)
1517 {
1518         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1519         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
1520         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
1521         lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
1522         lc_opt_entry_t *heur4_grp = lc_opt_get_grp(co_grp, "heur4");
1523
1524         static co_algo_info copyheur = {
1525                 co_solve_heuristic_mst, 0
1526         };
1527
1528         lc_opt_add_table(heur4_grp, options);
1529         be_register_copyopt("heur4", &copyheur);
1530
1531         FIRM_DBG_REGISTER(dbg, "firm.be.co.heur4");
1532 }