2 * This is the C implementation of the mst algorithm
3 * originally written in Java by Sebastian Hack.
4 * (also known as "heur3" :)
5 * Performs simple copy minimization.
7 * @author Christian Wuerdig
14 #endif /* HAVE_CONFIG_H */
21 #include "raw_bitset.h"
22 #include "irphase_t.h"
32 #include "becopyopt_t.h"
35 #define COL_COST_INFEASIBLE DBL_MAX
36 #define AFF_NEIGHBOUR_FIX_BENEFIT 128.0
37 #define NEIGHBOUR_CONSTR_COSTS 64.0
39 #define DBG_AFF_CHUNK(env, level, chunk) DEBUG_ONLY(if (firm_dbg_get_mask((env)->dbg) & (level)) dbg_aff_chunk((env), (chunk));)
40 #define DBG_COL_COST(env, level, cost) DEBUG_ONLY(if (firm_dbg_get_mask((env)->dbg) & (level)) dbg_col_cost((env), (cost));)
42 static int last_chunk_id = 0;
44 typedef struct _col_cost_t {
49 typedef struct _aff_chunk_t {
52 unsigned weight_consistent : 1;
56 typedef struct _aff_edge_t {
62 /* main coalescing environment*/
63 typedef struct _co_mst_env_t {
64 int n_regs; /**< number of regs in class */
65 int k; /**< number of non-ignore registers in class */
66 bitset_t *ignore_regs; /**< set containing all global ignore registers */
67 ir_phase ph; /**< phase object holding data for nodes */
68 pqueue *chunks; /**< priority queue for chunks */
69 pset_new_t chunkset; /**< set holding all chunks */
70 be_ifg_t *ifg; /**< the interference graph */
71 const arch_env_t *aenv; /**< the arch environment */
72 copy_opt_t *co; /**< the copy opt object */
73 DEBUG_ONLY(firm_dbg_module_t *dbg);
76 /* stores coalescing related information for a node */
77 typedef struct _co_mst_irn_t {
86 unsigned tmp_fixed : 1;
89 #define get_co_mst_irn(mst_env, irn) (phase_get_or_set_irn_data(&(mst_env)->ph, (irn)))
91 typedef int decide_func_t(co_mst_irn_t *node, int col);
95 static void dbg_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
97 bitset_foreach(c->nodes, idx) {
98 ir_node *n = get_idx_irn(env->co->irg, idx);
99 ir_fprintf(stderr, " %+F", n);
101 if (c->weight_consistent)
102 ir_fprintf(stderr, " weight %d", c->weight);
105 static void dbg_admissible_colors(co_mst_env_t *env, co_mst_irn_t *node) {
107 if (bitset_popcnt(node->adm_colors) < 1)
108 fprintf(stderr, "no admissible colors?!?");
110 bitset_foreach(node->adm_colors, idx)
111 fprintf(stderr, " %d", idx);
115 static void dbg_col_cost(co_mst_env_t *env, col_cost_t *cost) {
117 for (i = 0; i < env->n_regs; ++i) {
118 if (cost[i].cost == COL_COST_INFEASIBLE)
119 fprintf(stderr, " (%d, INF)", cost[i].col);
121 fprintf(stderr, " (%d, %.1f)", cost[i].col, cost[i].cost);
125 #endif /* DEBUG_libfirm */
127 static INLINE int get_mst_irn_col(co_mst_irn_t *node) {
128 return node->tmp_fixed ? node->tmp_col : node->col;
132 * @return 1 if node @p node has color @p col, 0 otherwise.
134 static int decider_has_color(co_mst_irn_t *node, int col) {
135 return get_mst_irn_col(node) == col;
139 * @return 1 if node @p node has not color @p col, 0 otherwise.
141 static int decider_hasnot_color(co_mst_irn_t *node, int col) {
142 return get_mst_irn_col(node) != col;
146 * Always returns true.
148 static int decider_always_yes(co_mst_irn_t *node, int col) {
152 /* compares two affinity edges by its weight */
153 static int cmp_aff_edge(const void *a, const void *b) {
154 const aff_edge_t *e1 = a;
155 const aff_edge_t *e2 = b;
157 /* sort in descending order */
158 return QSORT_CMP(e2->weight, e1->weight);
161 /* compares to color-cost pairs */
162 static int cmp_col_cost(const void *a, const void *b) {
163 const col_cost_t *c1 = a;
164 const col_cost_t *c2 = b;
166 return c1->cost < c2->cost ? -1 : 1;
170 * Creates a new affinity chunk
172 static INLINE aff_chunk_t *new_aff_chunk(co_mst_env_t *env) {
173 aff_chunk_t *c = xmalloc(sizeof(*c));
175 c->weight_consistent = 0;
176 c->nodes = bitset_irg_malloc(env->co->irg);
177 c->id = last_chunk_id++;
178 pset_new_insert(&env->chunkset, c);
183 * Frees all memory allocated by an affinity chunk.
185 static INLINE void delete_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
186 pset_new_remove(&env->chunkset, c);
187 bitset_free(c->nodes);
192 * Adds a node to an affinity chunk
194 static INLINE void aff_chunk_add_node(aff_chunk_t *c, co_mst_irn_t *node) {
195 c->weight_consistent = 0;
197 bitset_set(c->nodes, get_irn_idx(node->irn));
202 * In case there is no phase information for irn, initialize it.
204 static void *co_mst_irn_init(ir_phase *ph, ir_node *irn, void *old) {
205 co_mst_irn_t *res = old ? old : phase_alloc(ph, sizeof(res[0]));
206 co_mst_env_t *env = ph->priv;
209 void *neigh_it = be_ifg_neighbours_iter_alloca(env->ifg);
210 const arch_register_req_t *req;
214 res->chunk = new_aff_chunk(env);
219 res->col = arch_register_get_index(arch_get_irn_register(env->aenv, irn));
220 res->init_col = res->col;
222 /* add note to new chunk */
223 aff_chunk_add_node(res->chunk, res);
225 DBG((env->dbg, LEVEL_2, "Creating phase info for %+F, chunk %d\n", irn, res->chunk->id));
227 /* set admissible registers */
228 res->adm_colors = bitset_obstack_alloc(phase_obst(ph), env->n_regs);
230 /* Exclude colors not assignable to the irn */
231 req = arch_get_register_req(env->aenv, irn, -1);
232 if (arch_register_req_is(req, limited))
233 rbitset_copy_to_bitset(req->limited, res->adm_colors);
235 bitset_set_all(res->adm_colors);
237 /* exclude global ignore registers as well */
238 bitset_andnot(res->adm_colors, env->ignore_regs);
240 /* calculate the number of interfering neighbours */
241 be_ifg_foreach_neighbour(env->ifg, neigh_it, irn, m) {
242 if (! arch_irn_is(env->aenv, m, ignore))
252 * Check if affinity chunk @p chunk interferes with node @p irn.
254 static INLINE int aff_chunk_interferes(co_mst_env_t *env, aff_chunk_t *chunk, ir_node *irn) {
255 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
258 be_ifg_foreach_neighbour(env->ifg, nodes_it, irn, neigh) {
259 if (! arch_irn_is(env->aenv, neigh, ignore) && bitset_is_set(chunk->nodes, get_irn_idx(neigh)))
267 * Check if there are interference edges from c1 to c2.
268 * @param env The global co_mst environment
270 * @param c2 Another chunk
271 * @return 1 if there are interferences between nodes of c1 and c2, 0 otherwise.
273 static INLINE int aff_chunks_interfere(co_mst_env_t *env, aff_chunk_t *c1, aff_chunk_t *c2) {
279 /* check if there is a node in c2 having an interfering neighbour in c1 */
280 bitset_foreach(c2->nodes, idx) {
281 ir_node *n = get_idx_irn(env->co->irg, idx);
283 if (aff_chunk_interferes(env, c1, n))
291 * Let c1 absorb the nodes of c2 (only possible when there
292 * are no interference edges from c1 to c2).
293 * @return 1 if successful, 0 if not possible
295 static INLINE int aff_chunk_absorb(co_mst_env_t *env, aff_chunk_t *c1, aff_chunk_t *c2) {
296 DBG((env->dbg, LEVEL_1, "Attempt to let c1 (id %d): ", c1->id));
297 DBG_AFF_CHUNK(env, LEVEL_1, c1);
298 DB((env->dbg, LEVEL_1, "\n\tabsorb c2 (id %d): ", c2->id));
299 DBG_AFF_CHUNK(env, LEVEL_1, c2);
300 DB((env->dbg, LEVEL_1, "\n"));
302 if (! aff_chunks_interfere(env, c1, c2) && c1 != c2) {
305 bitset_or(c1->nodes, c2->nodes);
306 c1->weight_consistent = 0;
308 bitset_foreach(c2->nodes, idx) {
309 ir_node *n = get_idx_irn(env->co->irg, idx);
310 co_mst_irn_t *mn = get_co_mst_irn(env, n);
314 DB((env->dbg, LEVEL_1, " ... absorbed, c2 deleted\n"));
315 delete_aff_chunk(env, c2);
318 DB((env->dbg, LEVEL_1, " ... c1 interferes with c2, skipped\n"));
323 * Returns the affinity chunk of @p irn or creates a new
324 * one with @p irn as element if there is none assigned.
326 static INLINE aff_chunk_t *get_aff_chunk(co_mst_env_t *env, ir_node *irn) {
327 co_mst_irn_t *node = get_co_mst_irn(env, irn);
328 assert(node->chunk && "Node should have a chunk.");
333 * Assures that the weight of the given chunk is consistent.
335 static void aff_chunk_assure_weight(co_mst_env_t *env, aff_chunk_t *c) {
336 if (! c->weight_consistent) {
340 bitset_foreach(c->nodes, idx) {
341 ir_node *n = get_idx_irn(env->co->irg, idx);
342 affinity_node_t *an = get_affinity_info(env->co, n);
346 co_gs_foreach_neighb(an, neigh) {
347 ir_node *m = neigh->irn;
348 int m_idx = get_irn_idx(m);
350 /* skip ignore nodes */
351 if (arch_irn_is(env->aenv, m, ignore))
354 w += bitset_is_set(c->nodes, m_idx) ? neigh->costs : 0;
360 c->weight_consistent = 1;
365 * Build chunks of nodes connected by affinity edges.
366 * We start at the heaviest affinity edge.
367 * The chunks of the two edge-defining nodes will be
368 * merged if there are no interference edges from one
369 * chunk to the other.
371 static void build_affinity_chunks(co_mst_env_t *env) {
372 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
373 aff_edge_t *edges = NEW_ARR_F(aff_edge_t, 0);
376 aff_chunk_t *curr_chunk;
377 pset_new_iterator_t iter;
379 /* at first we create the affinity edge objects */
380 be_ifg_foreach_node(env->ifg, nodes_it, n) {
381 int n_idx = get_irn_idx(n);
385 /* skip ignore nodes */
386 if (arch_irn_is(env->aenv, n, ignore))
389 n1 = get_co_mst_irn(env, n);
390 an = get_affinity_info(env->co, n);
394 co_gs_foreach_neighb(an, neigh) {
395 ir_node *m = neigh->irn;
396 int m_idx = get_irn_idx(m);
398 /* record the edge in only one direction */
403 /* skip ignore nodes */
404 if (arch_irn_is(env->aenv, m, ignore))
409 n2 = get_co_mst_irn(env, m);
410 edge.weight = (double)neigh->costs / (double)(1 + n1->int_neigh + n2->int_neigh);
411 ARR_APP1(aff_edge_t, edges, edge);
417 /* now: sort edges and build the affinity chunks */
418 len = ARR_LEN(edges);
419 qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
420 for (i = 0; i < len; ++i) {
421 aff_chunk_t *c1 = get_aff_chunk(env, edges[i].src);
422 aff_chunk_t *c2 = get_aff_chunk(env, edges[i].tgt);
424 (void)aff_chunk_absorb(env, c1, c2);
427 /* now insert all chunks into a priority queue */
428 foreach_pset_new(&env->chunkset, curr_chunk, iter) {
429 aff_chunk_assure_weight(env, curr_chunk);
431 DBG((env->dbg, LEVEL_1, "Putting chunk (id %d): ", curr_chunk->id));
432 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
433 DB((env->dbg, LEVEL_1, "\n\tinto priority queue\n"));
435 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
442 * Greedy collect affinity neighbours into thew new chunk @p chunk starting at node @p node.
444 static void expand_chunk_from(co_mst_env_t *env, co_mst_irn_t *node, bitset_t *visited,
445 aff_chunk_t *chunk, aff_chunk_t *orig_chunk, decide_func_t *decider, int col)
447 waitq *nodes = new_waitq();
449 DBG((env->dbg, LEVEL_1, "\nExpanding new chunk (id %d) from %+F:", chunk->id, node->irn));
451 /* init queue and chunk */
452 waitq_put(nodes, node);
453 bitset_set(visited, get_irn_idx(node->irn));
454 aff_chunk_add_node(chunk, node);
455 DB((env->dbg, LEVEL_1, " %+F", node->irn));
457 /* as long as there are nodes in the queue */
458 while (! waitq_empty(nodes)) {
459 co_mst_irn_t *n = waitq_get(nodes);
460 affinity_node_t *an = get_affinity_info(env->co, n->irn);
462 /* check all affinity neighbors */
465 co_gs_foreach_neighb(an, neigh) {
466 ir_node *m = neigh->irn;
467 int m_idx = get_irn_idx(m);
470 /* skip ignore nodes */
471 if (arch_irn_is(env->aenv, m, ignore))
474 n2 = get_co_mst_irn(env, m);
476 if (! bitset_is_set(visited, m_idx) &&
479 ! aff_chunk_interferes(env, chunk, m) &&
480 bitset_is_set(orig_chunk->nodes, m_idx))
483 following conditions are met:
484 - neighbour is not visited
485 - neighbour likes the color
486 - neighbour has not yet a fixed color
487 - the new chunk doesn't interfere with the neighbour
488 - neighbour belongs or belonged once to the original chunk
490 bitset_set(visited, m_idx);
491 aff_chunk_add_node(chunk, n2);
492 DB((env->dbg, LEVEL_1, " %+F", n2->irn));
493 /* enqueue for further search */
494 waitq_put(nodes, n2);
500 DB((env->dbg, LEVEL_1, "\n"));
506 * Fragment the given chunk into chunks having given color and not having given color.
508 static aff_chunk_t *fragment_chunk(co_mst_env_t *env, int col, aff_chunk_t *c, waitq *tmp) {
509 bitset_t *visited = bitset_irg_malloc(env->co->irg);
511 aff_chunk_t *best = NULL;
513 bitset_foreach(c->nodes, idx) {
516 aff_chunk_t *tmp_chunk;
517 decide_func_t *decider;
520 if (bitset_is_set(visited, idx))
523 irn = get_idx_irn(env->co->irg, idx);
524 node = get_co_mst_irn(env, irn);
526 if (get_mst_irn_col(node) == col) {
527 decider = decider_has_color;
531 decider = decider_hasnot_color;
535 /* create a new chunk starting at current node */
536 tmp_chunk = new_aff_chunk(env);
537 waitq_put(tmp, tmp_chunk);
538 expand_chunk_from(env, node, visited, tmp_chunk, c, decider, col);
539 assert(bitset_popcnt(tmp_chunk->nodes) > 0 && "No nodes added to chunk");
541 /* remember the local best */
542 aff_chunk_assure_weight(env, tmp_chunk);
543 if (check_for_best && (! best || best->weight < tmp_chunk->weight))
547 assert(best && "No chunk found?");
548 bitset_free(visited);
553 * Initializes an array of color-cost pairs.
554 * Sets forbidden colors to costs COL_COST_INFEASIBLE and all others to @p c.
556 static INLINE void col_cost_init(co_mst_env_t *env, col_cost_t *cost, double c) {
559 for (i = 0; i < env->n_regs; ++i) {
561 if (bitset_is_set(env->ignore_regs, i))
562 cost[i].cost = COL_COST_INFEASIBLE;
569 * Initializes an array of color-cost pairs.
570 * Sets all colors except color @p col to COL_COST_INFEASIBLE and @p col to 0.0
572 static INLINE void col_cost_init_single(co_mst_env_t *env, col_cost_t *cost, int col) {
573 assert(! bitset_is_set(env->ignore_regs, col) && "Attempt to use forbidden color.");
574 col_cost_init(env, cost, COL_COST_INFEASIBLE);
581 * Resets the temporary fixed color of all nodes within wait queue @p nodes.
582 * ATTENTION: the queue is empty after calling this function!
584 static INLINE void reject_coloring(waitq *nodes) {
585 while (! waitq_empty(nodes)) {
586 co_mst_irn_t *n = waitq_get(nodes);
592 * Determines the costs for each color if it would be assigned to node @p node.
594 static void determine_color_costs(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs) {
595 affinity_node_t *an = get_affinity_info(env->co, node->irn);
596 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
601 col_cost_init(env, costs, 0.0);
603 /* calculate (negative) costs for affinity neighbours */
605 co_gs_foreach_neighb(an, aff_neigh) {
606 ir_node *m = aff_neigh->irn;
610 /* skip ignore nodes */
611 if (arch_irn_is(env->aenv, m, ignore))
614 neigh = get_co_mst_irn(env, m);
615 c = (double)aff_neigh->costs;
617 /* calculate costs for fixed affinity neighbours */
618 if (neigh->tmp_fixed || neigh->fixed) {
619 int col = get_mst_irn_col(neigh);
620 costs[col].cost -= c * AFF_NEIGHBOUR_FIX_BENEFIT;
625 /* calculate (positive) costs for interfering neighbours */
626 be_ifg_foreach_neighbour(env->ifg, nodes_it, node->irn, int_neigh) {
630 /* skip ignore nodes */
631 if (arch_irn_is(env->aenv, int_neigh, ignore))
634 neigh = get_co_mst_irn(env, int_neigh);
635 col = get_mst_irn_col(neigh);
636 col_cnt = bitset_popcnt(neigh->adm_colors);
638 if (neigh->tmp_fixed || neigh->fixed) {
639 /* colors of fixed interfering neighbours are infeasible */
640 costs[col].cost = COL_COST_INFEASIBLE;
642 else if (col_cnt < env->k) {
643 /* calculate costs for constrained interfering neighbours */
644 double ratio = 1.0 - ((double)col_cnt / (double)env->k);
646 bitset_foreach_clear(neigh->adm_colors, idx) {
647 /* check only explicitly forbidden colors (skip global forbidden ones) */
648 if (! bitset_is_set(env->ignore_regs, idx)) {
649 costs[col].cost += ratio * NEIGHBOUR_CONSTR_COSTS;
655 /* set all not admissible colors to COL_COST_INFEASIBLE */
656 bitset_foreach_clear(node->adm_colors, idx)
657 costs[idx].cost = COL_COST_INFEASIBLE;
660 /* need forward declaration due to recursive call */
661 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones);
664 * Tries to change node to a color but @p explude_col.
665 * @return 1 if succeeded, 0 otherwise.
667 static int change_node_color_excluded(co_mst_env_t *env, co_mst_irn_t *node, int exclude_col, waitq *changed_ones) {
668 int col = get_mst_irn_col(node);
671 /* neighbours has already a different color -> good, temporary fix it */
672 if (col != exclude_col) {
675 waitq_put(changed_ones, node);
679 /* The node has the color it should not have _and_ has not been visited yet. */
680 if (! (node->tmp_fixed || node->fixed)) {
681 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
683 /* Get the costs for giving the node a specific color. */
684 determine_color_costs(env, node, costs);
686 /* Since the node must not have the not_col, set the costs for that color to "infinity" */
687 costs[exclude_col].cost = COL_COST_INFEASIBLE;
689 /* sort the colors according costs, cheapest first. */
690 qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost);
692 /* Try recoloring the node using the color list. */
693 res = recolor_nodes(env, node, costs, changed_ones);
700 * Tries to bring node @p node to cheapest color and color all interfering neighbours with other colors.
701 * ATTENTION: Expect @p costs already sorted by increasing costs.
702 * @return 1 if coloring could be applied, 0 otherwise.
704 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones) {
706 waitq *local_changed = new_waitq();
708 DBG((env->dbg, LEVEL_1, "\tRecoloring %+F with color-costs", node->irn));
709 DBG_COL_COST(env, LEVEL_1, costs);
710 DB((env->dbg, LEVEL_1, "\n"));
712 for (i = 0; i < env->n_regs; ++i) {
713 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
714 int tgt_col = costs[i].col;
718 /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
719 if (costs[i].cost == COL_COST_INFEASIBLE) {
721 del_waitq(local_changed);
725 /* Set the new color of the node and mark the node as temporarily fixed. */
726 assert(! node->tmp_fixed && "Node must not have been temporary fixed.");
728 node->tmp_col = tgt_col;
730 assert(waitq_empty(local_changed) && "Node queue should be empty here.");
731 waitq_put(local_changed, node);
733 /* try to color all interfering neighbours with current color forbidden */
734 be_ifg_foreach_neighbour(env->ifg, nodes_it, node->irn, neigh) {
737 /* skip ignore nodes */
738 if (arch_irn_is(env->aenv, neigh, ignore))
741 nn = get_co_mst_irn(env, neigh);
744 Try to change the color of the neighbor and record all nodes which
745 get changed in the tmp list. Add this list to the "changed" list for
746 that color. If we did not succeed to change the color of the neighbor,
747 we bail out and try the next color.
749 if (get_mst_irn_col(nn) == tgt_col) {
750 waitq *tmp = new_waitq();
752 /* try to color neighbour with tgt_col forbidden */
753 neigh_ok = change_node_color_excluded(env, nn, tgt_col, tmp);
755 /* join lists of changed nodes */
756 while (! waitq_empty(tmp))
757 waitq_put(local_changed, waitq_get(tmp));
766 We managed to assign the target color to all neighbors, so from the perspective
767 of the current node, every thing was ok and we can return safely.
770 /* append the local_changed ones to global ones */
771 while (! waitq_empty(local_changed))
772 waitq_put(changed_ones, waitq_get(local_changed));
773 del_waitq(local_changed);
777 /* coloring of neighbours failed, so we try next color */
778 reject_coloring(local_changed);
782 del_waitq(local_changed);
787 * Tries to bring node @p node and all it's neighbours to color @p tgt_col.
788 * @return 1 if color @p col could be applied, 0 otherwise
790 static int change_node_color(co_mst_env_t *env, co_mst_irn_t *node, int tgt_col, waitq *changed_ones) {
791 int col = get_mst_irn_col(node);
793 /* if node already has the target color -> good, temporary fix it */
794 if (col == tgt_col) {
795 DBG((env->dbg, LEVEL_1, "\t\tCNC: %+F has already color %d, fix temporary\n", node->irn, tgt_col));
796 if (! node->tmp_fixed) {
798 node->tmp_col = tgt_col;
799 waitq_put(changed_ones, node);
805 Node has not yet a fixed color and target color is admissible
806 -> try to recolor node and it's affinity neighbours
808 if (! (node->fixed || node->tmp_fixed) && bitset_is_set(node->adm_colors, tgt_col)) {
809 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
812 col_cost_init_single(env, costs, tgt_col);
814 DBG((env->dbg, LEVEL_1, "\t\tCNC: Attempt to recolor %+F ===>>\n", node->irn));
815 res = recolor_nodes(env, node, costs, changed_ones);
816 DBG((env->dbg, LEVEL_1, "\t\tCNC: <<=== Recoloring of %+F %s\n", node->irn, res ? "succeeded" : "failed"));
822 if (firm_dbg_get_mask(env->dbg) & LEVEL_1) {
823 if (node->fixed || node->tmp_fixed)
824 DBG((env->dbg, LEVEL_1, "\t\tCNC: %+F has already fixed color %d\n", node->irn, col));
826 DBG((env->dbg, LEVEL_1, "\t\tCNC: color %d not admissible for %+F (", tgt_col, node->irn));
827 dbg_admissible_colors(env, node);
828 DB((env->dbg, LEVEL_1, ")\n"));
837 * Tries to color an affinity chunk (or at least a part of it).
838 * Inserts uncolored parts of the chunk as a new chunk into the priority queue.
840 static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
841 aff_chunk_t *best_chunk = NULL;
843 waitq *changed_ones = new_waitq();
844 waitq *tmp_chunks = new_waitq();
848 /* check which color is the "best" for the given chunk */
849 for (col = 0; col < env->k; ++col) {
851 aff_chunk_t *local_best;
853 DBG((env->dbg, LEVEL_1, "Trying color %d\n", col));
855 /* try to bring all nodes of given chunk to the current color. */
856 bitset_foreach(c->nodes, idx) {
857 ir_node *irn = get_idx_irn(env->co->irg, idx);
858 co_mst_irn_t *node = get_co_mst_irn(env, irn);
860 assert(! node->fixed && "Node must not have a fixed color.");
862 DBG((env->dbg, LEVEL_1, "\tBringing %+F from color %d to color %d ...\n", irn, node->col, col));
863 one_good = change_node_color(env, node, col, changed_ones);
864 DBG((env->dbg, LEVEL_1, "\t... %+F attempt from %d to %d %s\n", irn, node->col, col, one_good ? "succeeded" : "failed"));
870 /* try next color when failed */
874 /* fragment the chunk according to the coloring */
875 local_best = fragment_chunk(env, col, c, tmp_chunks);
877 /* check if the local best is global best */
879 aff_chunk_assure_weight(env, local_best);
881 DBG((env->dbg, LEVEL_1, "\tlocal best chunk (id %d) for color %d: ", local_best->id, col));
882 DBG_AFF_CHUNK(env, LEVEL_1, local_best);
884 if (! best_chunk || best_chunk->weight < local_best->weight) {
885 best_chunk = local_best;
887 DB((env->dbg, LEVEL_1, "\n\t... setting global best chunk (id %d), color %d\n", best_chunk->id, best_color));
890 DB((env->dbg, LEVEL_1, "\n\t... omitting, global best is better\n"));
894 /* reject the coloring and bring the coloring to the initial state */
895 reject_coloring(changed_ones);
898 /* free all intermediate created chunks except best one */
899 while (! waitq_empty(tmp_chunks)) {
900 aff_chunk_t *tmp = waitq_get(tmp_chunks);
901 if (tmp != best_chunk)
902 delete_aff_chunk(env, tmp);
904 del_waitq(tmp_chunks);
906 /* return if coloring failed */
908 delete_aff_chunk(env, c);
909 del_waitq(changed_ones);
913 DBG((env->dbg, LEVEL_1, "\tBringing best chunk (id %d) to color %d: ", best_chunk->id, best_color));
914 DBG_AFF_CHUNK(env, LEVEL_1, best_chunk);
915 DB((env->dbg, LEVEL_1, "\n"));
917 /* get the best fragment from the best list and color it */
918 bitset_foreach(best_chunk->nodes, idx) {
919 ir_node *irn = get_idx_irn(env->co->irg, idx);
920 co_mst_irn_t *node = get_co_mst_irn(env, irn);
923 res = change_node_color(env, node, best_color, changed_ones);
924 assert(res && "Coloring failed");
926 node->chunk = best_chunk;
929 /* materialize colors on changed nodes */
930 while (! waitq_empty(changed_ones)) {
931 co_mst_irn_t *n = waitq_get(changed_ones);
936 /* remove the nodes in best chunk from original chunk */
937 bitset_andnot(c->nodes, best_chunk->nodes);
939 /* we have to get the nodes back into the original chunk because they are scattered over temporary chunks */
940 bitset_foreach(c->nodes, idx) {
941 ir_node *n = get_idx_irn(env->co->irg, idx);
942 co_mst_irn_t *nn = get_co_mst_irn(env, n);
946 /* fragment the remaining chunk */
947 visited = bitset_irg_malloc(env->co->irg);
948 bitset_or(visited, best_chunk->nodes);
949 bitset_foreach(c->nodes, idx) {
950 if (! bitset_is_set(visited, idx)) {
951 aff_chunk_t *new_chunk = new_aff_chunk(env);
952 ir_node *irn = get_idx_irn(env->co->irg, idx);
953 co_mst_irn_t *node = get_co_mst_irn(env, irn);
955 expand_chunk_from(env, node, visited, new_chunk, c, decider_always_yes, 0);
956 aff_chunk_assure_weight(env, new_chunk);
957 pqueue_put(env->chunks, new_chunk, new_chunk->weight);
961 /* clear obsolete chunks and free some memory */
962 delete_aff_chunk(env, c);
963 delete_aff_chunk(env, best_chunk);
964 bitset_free(visited);
965 del_waitq(changed_ones);
969 * Main driver for mst safe coalescing algorithm.
971 int co_solve_heuristic_mst(copy_opt_t *co)
973 unsigned n_regs = co->cls->n_regs;
974 bitset_t *ignore_regs = bitset_alloca(n_regs);
977 co_mst_env_t mst_env;
980 phase_init(&mst_env.ph, "co_mst", co->irg, PHASE_DEFAULT_GROWTH, co_mst_irn_init, &mst_env);
982 k = be_put_ignore_regs(co->cenv->birg, co->cls, ignore_regs);
985 FIRM_DBG_REGISTER(mst_env.dbg, "firm.be.co.heur4");
986 mst_env.n_regs = n_regs;
988 mst_env.chunks = new_pqueue();
990 mst_env.ignore_regs = ignore_regs;
991 mst_env.ifg = co->cenv->ifg;
992 mst_env.aenv = co->aenv;
993 pset_new_init(&mst_env.chunkset);
995 DBG((mst_env.dbg, LEVEL_1, "==== Coloring %+F, class %s ====\n", co->irg, co->cls->name));
997 /* build affinity chunks */
998 build_affinity_chunks(&mst_env);
1000 /* color chunks as long as there are some */
1001 while (! pqueue_empty(mst_env.chunks)) {
1002 aff_chunk_t *chunk = pqueue_get(mst_env.chunks);
1004 DBG((mst_env.dbg, LEVEL_1, "\nColoring chunk (id %d): ", chunk->id));
1005 DBG_AFF_CHUNK(&mst_env, LEVEL_1, chunk);
1006 DB((mst_env.dbg, LEVEL_1, "\n======>>> \n"));
1008 color_aff_chunk(&mst_env, chunk);
1010 DB((mst_env.dbg, LEVEL_1, "<<<====== Coloring chunk (%d) done\n", chunk->id));
1013 /* apply coloring */
1014 foreach_phase_irn(&mst_env.ph, irn) {
1015 co_mst_irn_t *mirn = get_co_mst_irn(&mst_env, irn);
1016 const arch_register_t *reg;
1018 if (arch_irn_is(mst_env.aenv, irn, ignore))
1021 assert(mirn->fixed && "Node should have fixed color");
1023 /* skip nodes where color hasn't changed */
1024 if (mirn->init_col == mirn->col)
1027 reg = arch_register_for_index(co->cls, mirn->col);
1028 arch_set_irn_register(co->aenv, irn, reg);
1029 DBG((mst_env.dbg, LEVEL_1, "%+F set color from %d to %d\n", irn, mirn->init_col, mirn->col));
1032 /* free allocated memory */
1033 del_pqueue(mst_env.chunks);
1034 phase_free(&mst_env.ph);
1035 pset_new_destroy(&mst_env.chunkset);