3 * @brief Simple copy minimization heuristics.
8 * Copyrigth (C) 1995-2007 University of Karlsruhe. All right reserved.
10 * This file is part of libFirm.
12 * This file may be distributed and/or modified under the terms of the
13 * GNU General Public License version 2 as published by the Free Software
14 * Foundation and appearing in the file LICENSE.GPL included in the
15 * packaging of this file.
17 * Licensees holding valid libFirm Professional Edition licenses may use
18 * this file in accordance with the libFirm Commercial License.
19 * Agreement provided with the Software.
21 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
22 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
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.
35 #endif /* HAVE_CONFIG_H */
42 #include "raw_bitset.h"
43 #include "irphase_t.h"
53 #include "becopyopt_t.h"
56 #define COL_COST_INFEASIBLE DBL_MAX
57 #define AFF_NEIGHBOUR_FIX_BENEFIT 128.0
58 #define NEIGHBOUR_CONSTR_COSTS 64.0
60 #define DBG_AFF_CHUNK(env, level, chunk) DEBUG_ONLY(if (firm_dbg_get_mask((env)->dbg) & (level)) dbg_aff_chunk((env), (chunk));)
61 #define DBG_COL_COST(env, level, cost) DEBUG_ONLY(if (firm_dbg_get_mask((env)->dbg) & (level)) dbg_col_cost((env), (cost));)
63 static int last_chunk_id = 0;
65 typedef struct _col_cost_t {
70 typedef struct _aff_chunk_t {
73 unsigned weight_consistent : 1;
77 typedef struct _aff_edge_t {
83 /* main coalescing environment*/
84 typedef struct _co_mst_env_t {
85 int n_regs; /**< number of regs in class */
86 int k; /**< number of non-ignore registers in class */
87 bitset_t *ignore_regs; /**< set containing all global ignore registers */
88 ir_phase ph; /**< phase object holding data for nodes */
89 pqueue *chunks; /**< priority queue for chunks */
90 pset_new_t chunkset; /**< set holding all chunks */
91 be_ifg_t *ifg; /**< the interference graph */
92 const arch_env_t *aenv; /**< the arch environment */
93 copy_opt_t *co; /**< the copy opt object */
94 DEBUG_ONLY(firm_dbg_module_t *dbg);
97 /* stores coalescing related information for a node */
98 typedef struct _co_mst_irn_t {
101 bitset_t *adm_colors;
107 unsigned tmp_fixed : 1;
110 #define get_co_mst_irn(mst_env, irn) (phase_get_or_set_irn_data(&(mst_env)->ph, (irn)))
112 typedef int decide_func_t(co_mst_irn_t *node, int col);
117 * Write a chunk to stderr for debugging.
119 static void dbg_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
121 if (c->weight_consistent)
122 ir_fprintf(stderr, " $%d ", c->weight);
123 ir_fprintf(stderr, "{");
124 bitset_foreach(c->nodes, idx) {
125 ir_node *n = get_idx_irn(env->co->irg, idx);
126 ir_fprintf(stderr, " %+F,", n);
128 ir_fprintf(stderr, "}");
131 static void dbg_admissible_colors(co_mst_env_t *env, co_mst_irn_t *node) {
133 if (bitset_popcnt(node->adm_colors) < 1)
134 fprintf(stderr, "no admissible colors?!?");
136 bitset_foreach(node->adm_colors, idx)
137 fprintf(stderr, " %d", idx);
141 static void dbg_col_cost(co_mst_env_t *env, col_cost_t *cost) {
143 for (i = 0; i < env->n_regs; ++i) {
144 if (cost[i].cost == COL_COST_INFEASIBLE)
145 fprintf(stderr, " (%d, INF)", cost[i].col);
147 fprintf(stderr, " (%d, %.1f)", cost[i].col, cost[i].cost);
151 #endif /* DEBUG_libfirm */
153 static INLINE int get_mst_irn_col(co_mst_irn_t *node) {
154 return node->tmp_fixed ? node->tmp_col : node->col;
158 * @return 1 if node @p node has color @p col, 0 otherwise.
160 static int decider_has_color(co_mst_irn_t *node, int col) {
161 return get_mst_irn_col(node) == col;
165 * @return 1 if node @p node has not color @p col, 0 otherwise.
167 static int decider_hasnot_color(co_mst_irn_t *node, int col) {
168 return get_mst_irn_col(node) != col;
172 * Always returns true.
174 static int decider_always_yes(co_mst_irn_t *node, int col) {
178 /* > compares two affinity edges by its weight */
179 static int cmp_aff_edge(const void *a, const void *b) {
180 const aff_edge_t *e1 = a;
181 const aff_edge_t *e2 = b;
183 if (e2->weight == e1->weight) {
184 if (e2->src->node_idx == e1->src->node_idx)
185 return QSORT_CMP(e2->tgt->node_idx, e1->tgt->node_idx);
187 return QSORT_CMP(e2->src->node_idx, e1->src->node_idx);
189 /* sort in descending order */
190 return QSORT_CMP(e2->weight, e1->weight);
193 /* compares to color-cost pairs */
194 static int cmp_col_cost(const void *a, const void *b) {
195 const col_cost_t *c1 = a;
196 const col_cost_t *c2 = b;
198 return c1->cost < c2->cost ? -1 : 1;
202 * Creates a new affinity chunk
204 static INLINE aff_chunk_t *new_aff_chunk(co_mst_env_t *env) {
205 aff_chunk_t *c = xmalloc(sizeof(*c));
207 c->weight_consistent = 0;
208 c->nodes = bitset_irg_malloc(env->co->irg);
209 c->id = last_chunk_id++;
210 pset_new_insert(&env->chunkset, c);
215 * Frees all memory allocated by an affinity chunk.
217 static INLINE void delete_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
218 pset_new_remove(&env->chunkset, c);
219 bitset_free(c->nodes);
224 * Adds a node to an affinity chunk
226 static INLINE void aff_chunk_add_node(aff_chunk_t *c, co_mst_irn_t *node) {
227 c->weight_consistent = 0;
229 bitset_set(c->nodes, get_irn_idx(node->irn));
233 * In case there is no phase information for irn, initialize it.
235 static void *co_mst_irn_init(ir_phase *ph, ir_node *irn, void *old) {
236 co_mst_irn_t *res = old ? old : phase_alloc(ph, sizeof(res[0]));
237 co_mst_env_t *env = ph->priv;
240 const arch_register_req_t *req;
243 res->chunk = new_aff_chunk(env);
248 res->col = arch_register_get_index(arch_get_irn_register(env->aenv, irn));
249 res->init_col = res->col;
251 /* add note to new chunk */
252 aff_chunk_add_node(res->chunk, res);
254 DB((env->dbg, LEVEL_4, "Creating phase info for %+F, chunk %d\n", irn, res->chunk->id));
256 /* set admissible registers */
257 res->adm_colors = bitset_obstack_alloc(phase_obst(ph), env->n_regs);
259 /* Exclude colors not assignable to the irn */
260 req = arch_get_register_req(env->aenv, irn, -1);
261 if (arch_register_req_is(req, limited))
262 rbitset_copy_to_bitset(req->limited, res->adm_colors);
264 bitset_set_all(res->adm_colors);
266 /* exclude global ignore registers as well */
267 bitset_andnot(res->adm_colors, env->ignore_regs);
269 /* set the number of interfering affinity neighbours to -1, they are calculated later */
276 * Check if affinity chunk @p chunk interferes with node @p irn.
278 static INLINE int aff_chunk_interferes(co_mst_env_t *env, aff_chunk_t *chunk, ir_node *irn) {
279 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
282 be_ifg_foreach_neighbour(env->ifg, nodes_it, irn, neigh) {
283 if (! arch_irn_is(env->aenv, neigh, ignore) && bitset_is_set(chunk->nodes, get_irn_idx(neigh)))
291 * Check if there are interference edges from c1 to c2.
292 * @param env The global co_mst environment
294 * @param c2 Another chunk
295 * @return 1 if there are interferences between nodes of c1 and c2, 0 otherwise.
297 static INLINE int aff_chunks_interfere(co_mst_env_t *env, aff_chunk_t *c1, aff_chunk_t *c2) {
303 /* check if there is a node in c2 having an interfering neighbor in c1 */
304 bitset_foreach(c2->nodes, idx) {
305 ir_node *n = get_idx_irn(env->co->irg, idx);
307 if (aff_chunk_interferes(env, c1, n))
315 * Let c1 absorb the nodes of c2 (only possible when there
316 * are no interference edges from c1 to c2).
317 * @return 1 if successful, 0 if not possible
319 static int aff_chunk_absorb(co_mst_env_t *env, aff_chunk_t *c1, aff_chunk_t *c2) {
320 DB((env->dbg, LEVEL_4, "Attempt to let c1 (id %d): ", c1->id));
321 DBG_AFF_CHUNK(env, LEVEL_4, c1);
322 DB((env->dbg, LEVEL_4, "\n\tabsorb c2 (id %d): ", c2->id));
323 DBG_AFF_CHUNK(env, LEVEL_4, c2);
324 DB((env->dbg, LEVEL_4, "\n"));
326 if (c1 != c2 && ! aff_chunks_interfere(env, c1, c2)) {
329 bitset_or(c1->nodes, c2->nodes);
330 c1->weight_consistent = 0;
332 bitset_foreach(c2->nodes, idx) {
333 ir_node *n = get_idx_irn(env->co->irg, idx);
334 co_mst_irn_t *mn = get_co_mst_irn(env, n);
338 DB((env->dbg, LEVEL_4, " ... absorbed, c2 deleted\n"));
339 delete_aff_chunk(env, c2);
342 DB((env->dbg, LEVEL_4, " ... c1 interferes with c2, skipped\n"));
347 * Returns the affinity chunk of @p irn or creates a new
348 * one with @p irn as element if there is none assigned.
350 static INLINE aff_chunk_t *get_aff_chunk(co_mst_env_t *env, ir_node *irn) {
351 co_mst_irn_t *node = get_co_mst_irn(env, irn);
352 assert(node->chunk && "Node should have a chunk.");
357 * Assures that the weight of the given chunk is consistent.
359 static void aff_chunk_assure_weight(co_mst_env_t *env, aff_chunk_t *c) {
360 if (! c->weight_consistent) {
364 bitset_foreach(c->nodes, idx) {
365 ir_node *n = get_idx_irn(env->co->irg, idx);
366 affinity_node_t *an = get_affinity_info(env->co, n);
370 co_gs_foreach_neighb(an, neigh) {
371 ir_node *m = neigh->irn;
372 int m_idx = get_irn_idx(m);
374 /* skip ignore nodes */
375 if (arch_irn_is(env->aenv, m, ignore))
378 w += bitset_is_set(c->nodes, m_idx) ? neigh->costs : 0;
384 c->weight_consistent = 1;
389 * Count the number of interfering affinity neighbors
391 static int count_interfering_aff_neighs(co_mst_env_t *env, affinity_node_t *an) {
393 void *neigh_it = be_ifg_neighbours_iter_alloca(env->ifg);
394 ir_node *irn = an->irn;
397 co_gs_foreach_neighb(an, neigh) {
398 ir_node *m, *n = neigh->irn;
400 /* skip ignore nodes */
401 if (arch_irn_is(env->aenv, n, ignore))
404 /* check if the affinity neighbor interfere */
405 be_ifg_foreach_neighbour(env->ifg, neigh_it, irn, m) {
417 * Build chunks of nodes connected by affinity edges.
418 * We start at the heaviest affinity edge.
419 * The chunks of the two edge-defining nodes will be
420 * merged if there are no interference edges from one
421 * chunk to the other.
423 static void build_affinity_chunks(co_mst_env_t *env) {
424 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
425 aff_edge_t *edges = NEW_ARR_F(aff_edge_t, 0);
428 aff_chunk_t *curr_chunk;
429 pset_new_iterator_t iter;
431 /* at first we create the affinity edge objects */
432 be_ifg_foreach_node(env->ifg, nodes_it, n) {
433 int n_idx = get_irn_idx(n);
437 /* skip ignore nodes */
438 if (arch_irn_is(env->aenv, n, ignore))
441 n1 = get_co_mst_irn(env, n);
442 an = get_affinity_info(env->co, n);
447 if (n1->int_neigh < 0)
448 n1->int_neigh = count_interfering_aff_neighs(env, an);
449 co_gs_foreach_neighb(an, neigh) {
450 ir_node *m = neigh->irn;
451 int m_idx = get_irn_idx(m);
453 /* record the edge in only one direction */
458 /* skip ignore nodes */
459 if (arch_irn_is(env->aenv, m, ignore))
465 n2 = get_co_mst_irn(env, m);
466 if (n2->int_neigh < 0) {
467 affinity_node_t *am = get_affinity_info(env->co, m);
468 n2->int_neigh = count_interfering_aff_neighs(env, am);
470 edge.weight = (double)neigh->costs / (double)(1 + n1->int_neigh + n2->int_neigh);
471 ARR_APP1(aff_edge_t, edges, edge);
477 /* now: sort edges and build the affinity chunks */
478 len = ARR_LEN(edges);
479 qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
480 for (i = 0; i < len; ++i) {
481 aff_chunk_t *c1 = get_aff_chunk(env, edges[i].src);
482 aff_chunk_t *c2 = get_aff_chunk(env, edges[i].tgt);
484 DBG((env->dbg, LEVEL_1, "edge (%u,%u) %f\n", edges[i].src->node_idx, edges[i].tgt->node_idx, edges[i].weight));
486 (void)aff_chunk_absorb(env, c1, c2);
489 /* now insert all chunks into a priority queue */
490 foreach_pset_new(&env->chunkset, curr_chunk, iter) {
491 aff_chunk_assure_weight(env, curr_chunk);
493 DBG((env->dbg, LEVEL_1, "entry #%d", curr_chunk->id));
494 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
495 DBG((env->dbg, LEVEL_1, "\n"));
498 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
505 * Greedy collect affinity neighbours into thew new chunk @p chunk starting at node @p node.
507 static void expand_chunk_from(co_mst_env_t *env, co_mst_irn_t *node, bitset_t *visited,
508 aff_chunk_t *chunk, aff_chunk_t *orig_chunk, decide_func_t *decider, int col)
510 waitq *nodes = new_waitq();
512 DBG((env->dbg, LEVEL_1, "\nExpanding new chunk (id %d) from %+F:", chunk->id, node->irn));
514 /* init queue and chunk */
515 waitq_put(nodes, node);
516 bitset_set(visited, get_irn_idx(node->irn));
517 aff_chunk_add_node(chunk, node);
518 DB((env->dbg, LEVEL_1, " %+F", node->irn));
520 /* as long as there are nodes in the queue */
521 while (! waitq_empty(nodes)) {
522 co_mst_irn_t *n = waitq_get(nodes);
523 affinity_node_t *an = get_affinity_info(env->co, n->irn);
525 /* check all affinity neighbors */
528 co_gs_foreach_neighb(an, neigh) {
529 ir_node *m = neigh->irn;
530 int m_idx = get_irn_idx(m);
533 /* skip ignore nodes */
534 if (arch_irn_is(env->aenv, m, ignore))
537 n2 = get_co_mst_irn(env, m);
539 if (! bitset_is_set(visited, m_idx) &&
542 ! aff_chunk_interferes(env, chunk, m) &&
543 bitset_is_set(orig_chunk->nodes, m_idx))
546 following conditions are met:
547 - neighbour is not visited
548 - neighbour likes the color
549 - neighbour has not yet a fixed color
550 - the new chunk doesn't interfere with the neighbour
551 - neighbour belongs or belonged once to the original chunk
553 bitset_set(visited, m_idx);
554 aff_chunk_add_node(chunk, n2);
555 DB((env->dbg, LEVEL_1, " %+F", n2->irn));
556 /* enqueue for further search */
557 waitq_put(nodes, n2);
563 DB((env->dbg, LEVEL_1, "\n"));
569 * Fragment the given chunk into chunks having given color and not having given color.
571 static aff_chunk_t *fragment_chunk(co_mst_env_t *env, int col, aff_chunk_t *c, waitq *tmp) {
572 bitset_t *visited = bitset_irg_malloc(env->co->irg);
574 aff_chunk_t *best = NULL;
576 bitset_foreach(c->nodes, idx) {
579 aff_chunk_t *tmp_chunk;
580 decide_func_t *decider;
583 if (bitset_is_set(visited, idx))
586 irn = get_idx_irn(env->co->irg, idx);
587 node = get_co_mst_irn(env, irn);
589 if (get_mst_irn_col(node) == col) {
590 decider = decider_has_color;
594 decider = decider_hasnot_color;
598 /* create a new chunk starting at current node */
599 tmp_chunk = new_aff_chunk(env);
600 waitq_put(tmp, tmp_chunk);
601 expand_chunk_from(env, node, visited, tmp_chunk, c, decider, col);
602 assert(bitset_popcnt(tmp_chunk->nodes) > 0 && "No nodes added to chunk");
604 /* remember the local best */
605 aff_chunk_assure_weight(env, tmp_chunk);
606 if (check_for_best && (! best || best->weight < tmp_chunk->weight))
610 assert(best && "No chunk found?");
611 bitset_free(visited);
616 * Initializes an array of color-cost pairs.
617 * Sets forbidden colors to costs COL_COST_INFEASIBLE and all others to @p c.
619 static INLINE void col_cost_init(co_mst_env_t *env, col_cost_t *cost, double c) {
622 for (i = 0; i < env->n_regs; ++i) {
624 if (bitset_is_set(env->ignore_regs, i))
625 cost[i].cost = COL_COST_INFEASIBLE;
632 * Initializes an array of color-cost pairs.
633 * Sets all colors except color @p col to COL_COST_INFEASIBLE and @p col to 0.0
635 static INLINE void col_cost_init_single(co_mst_env_t *env, col_cost_t *cost, int col) {
636 assert(! bitset_is_set(env->ignore_regs, col) && "Attempt to use forbidden color.");
637 col_cost_init(env, cost, COL_COST_INFEASIBLE);
644 * Resets the temporary fixed color of all nodes within wait queue @p nodes.
645 * ATTENTION: the queue is empty after calling this function!
647 static INLINE void reject_coloring(waitq *nodes) {
648 while (! waitq_empty(nodes)) {
649 co_mst_irn_t *n = waitq_get(nodes);
655 * Determines the costs for each color if it would be assigned to node @p node.
657 static void determine_color_costs(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs) {
658 affinity_node_t *an = get_affinity_info(env->co, node->irn);
659 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
664 col_cost_init(env, costs, 0.0);
666 /* calculate (negative) costs for affinity neighbours */
668 co_gs_foreach_neighb(an, aff_neigh) {
669 ir_node *m = aff_neigh->irn;
673 /* skip ignore nodes */
674 if (arch_irn_is(env->aenv, m, ignore))
677 neigh = get_co_mst_irn(env, m);
678 c = (double)aff_neigh->costs;
680 /* calculate costs for fixed affinity neighbours */
681 if (neigh->tmp_fixed || neigh->fixed) {
682 int col = get_mst_irn_col(neigh);
683 costs[col].cost -= c * AFF_NEIGHBOUR_FIX_BENEFIT;
688 /* calculate (positive) costs for interfering neighbours */
689 be_ifg_foreach_neighbour(env->ifg, nodes_it, node->irn, int_neigh) {
693 /* skip ignore nodes */
694 if (arch_irn_is(env->aenv, int_neigh, ignore))
697 neigh = get_co_mst_irn(env, int_neigh);
698 col = get_mst_irn_col(neigh);
699 col_cnt = bitset_popcnt(neigh->adm_colors);
701 if (neigh->tmp_fixed || neigh->fixed) {
702 /* colors of fixed interfering neighbours are infeasible */
703 costs[col].cost = COL_COST_INFEASIBLE;
705 else if (col_cnt < env->k) {
706 /* calculate costs for constrained interfering neighbours */
707 double ratio = 1.0 - ((double)col_cnt / (double)env->k);
709 bitset_foreach_clear(neigh->adm_colors, idx) {
710 /* check only explicitly forbidden colors (skip global forbidden ones) */
711 if (! bitset_is_set(env->ignore_regs, idx)) {
712 costs[col].cost += ratio * NEIGHBOUR_CONSTR_COSTS;
718 /* set all not admissible colors to COL_COST_INFEASIBLE */
719 bitset_foreach_clear(node->adm_colors, idx)
720 costs[idx].cost = COL_COST_INFEASIBLE;
723 /* need forward declaration due to recursive call */
724 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones);
727 * Tries to change node to a color but @p explude_col.
728 * @return 1 if succeeded, 0 otherwise.
730 static int change_node_color_excluded(co_mst_env_t *env, co_mst_irn_t *node, int exclude_col, waitq *changed_ones) {
731 int col = get_mst_irn_col(node);
734 /* neighbours has already a different color -> good, temporary fix it */
735 if (col != exclude_col) {
738 waitq_put(changed_ones, node);
742 /* The node has the color it should not have _and_ has not been visited yet. */
743 if (! (node->tmp_fixed || node->fixed)) {
744 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
746 /* Get the costs for giving the node a specific color. */
747 determine_color_costs(env, node, costs);
749 /* Since the node must not have the not_col, set the costs for that color to "infinity" */
750 costs[exclude_col].cost = COL_COST_INFEASIBLE;
752 /* sort the colors according costs, cheapest first. */
753 qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost);
755 /* Try recoloring the node using the color list. */
756 res = recolor_nodes(env, node, costs, changed_ones);
763 * Tries to bring node @p node to cheapest color and color all interfering neighbours with other colors.
764 * ATTENTION: Expect @p costs already sorted by increasing costs.
765 * @return 1 if coloring could be applied, 0 otherwise.
767 static int recolor_nodes(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs, waitq *changed_ones) {
769 waitq *local_changed = new_waitq();
770 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
772 DBG((env->dbg, LEVEL_1, "\tRecoloring %+F with color-costs", node->irn));
773 DBG_COL_COST(env, LEVEL_1, costs);
774 DB((env->dbg, LEVEL_1, "\n"));
776 for (i = 0; i < env->n_regs; ++i) {
777 int tgt_col = costs[i].col;
781 /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
782 if (costs[i].cost == COL_COST_INFEASIBLE) {
784 del_waitq(local_changed);
788 /* Set the new color of the node and mark the node as temporarily fixed. */
789 assert(! node->tmp_fixed && "Node must not have been temporary fixed.");
791 node->tmp_col = tgt_col;
793 assert(waitq_empty(local_changed) && "Node queue should be empty here.");
794 waitq_put(local_changed, node);
796 /* try to color all interfering neighbours with current color forbidden */
797 be_ifg_foreach_neighbour(env->ifg, nodes_it, node->irn, neigh) {
800 /* skip ignore nodes */
801 if (arch_irn_is(env->aenv, neigh, ignore))
804 nn = get_co_mst_irn(env, neigh);
807 Try to change the color of the neighbor and record all nodes which
808 get changed in the tmp list. Add this list to the "changed" list for
809 that color. If we did not succeed to change the color of the neighbor,
810 we bail out and try the next color.
812 if (get_mst_irn_col(nn) == tgt_col) {
813 waitq *tmp = new_waitq();
815 /* try to color neighbour with tgt_col forbidden */
816 neigh_ok = change_node_color_excluded(env, nn, tgt_col, tmp);
818 /* join lists of changed nodes */
819 while (! waitq_empty(tmp))
820 waitq_put(local_changed, waitq_get(tmp));
829 We managed to assign the target color to all neighbors, so from the perspective
830 of the current node, every thing was ok and we can return safely.
833 /* append the local_changed ones to global ones */
834 while (! waitq_empty(local_changed))
835 waitq_put(changed_ones, waitq_get(local_changed));
836 del_waitq(local_changed);
840 /* coloring of neighbours failed, so we try next color */
841 reject_coloring(local_changed);
845 del_waitq(local_changed);
850 * Tries to bring node @p node and all it's neighbours to color @p tgt_col.
851 * @return 1 if color @p col could be applied, 0 otherwise
853 static int change_node_color(co_mst_env_t *env, co_mst_irn_t *node, int tgt_col, waitq *changed_ones) {
854 int col = get_mst_irn_col(node);
856 /* if node already has the target color -> good, temporary fix it */
857 if (col == tgt_col) {
858 DBG((env->dbg, LEVEL_4, "\t\tCNC: %+F has already color %d, fix temporary\n", node->irn, tgt_col));
859 if (! node->tmp_fixed) {
861 node->tmp_col = tgt_col;
862 waitq_put(changed_ones, node);
868 Node has not yet a fixed color and target color is admissible
869 -> try to recolor node and it's affinity neighbours
871 if (! (node->fixed || node->tmp_fixed) && bitset_is_set(node->adm_colors, tgt_col)) {
872 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
875 col_cost_init_single(env, costs, tgt_col);
877 DBG((env->dbg, LEVEL_4, "\t\tCNC: Attempt to recolor %+F ===>>\n", node->irn));
878 res = recolor_nodes(env, node, costs, changed_ones);
879 DBG((env->dbg, LEVEL_4, "\t\tCNC: <<=== Recoloring of %+F %s\n", node->irn, res ? "succeeded" : "failed"));
885 if (firm_dbg_get_mask(env->dbg) & LEVEL_4) {
886 if (node->fixed || node->tmp_fixed)
887 DB((env->dbg, LEVEL_4, "\t\tCNC: %+F has already fixed color %d\n", node->irn, col));
889 DB((env->dbg, LEVEL_4, "\t\tCNC: color %d not admissible for %+F (", tgt_col, node->irn));
890 dbg_admissible_colors(env, node);
891 DB((env->dbg, LEVEL_4, ")\n"));
900 * Tries to color an affinity chunk (or at least a part of it).
901 * Inserts uncolored parts of the chunk as a new chunk into the priority queue.
903 static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
904 aff_chunk_t *best_chunk = NULL;
906 waitq *changed_ones = new_waitq();
907 waitq *tmp_chunks = new_waitq();
911 DB((env->dbg, LEVEL_2, "fragmentizing chunk #%d", c->id));
912 DBG_AFF_CHUNK(env, LEVEL_2, c);
913 DB((env->dbg, LEVEL_2, "\n"));
916 /* check which color is the "best" for the given chunk */
917 for (col = 0; col < env->k; ++col) {
919 aff_chunk_t *local_best;
921 DB((env->dbg, LEVEL_3, "\ttrying color %d\n", col));
923 /* try to bring all nodes of given chunk to the current color. */
924 bitset_foreach(c->nodes, idx) {
925 ir_node *irn = get_idx_irn(env->co->irg, idx);
926 co_mst_irn_t *node = get_co_mst_irn(env, irn);
928 assert(! node->fixed && "Node must not have a fixed color.");
930 DB((env->dbg, LEVEL_4, "\t\tBringing %+F from color %d to color %d ...\n", irn, node->col, col));
931 one_good |= change_node_color(env, node, col, changed_ones);
932 DB((env->dbg, LEVEL_4, "\t\t... %+F attempt from %d to %d %s\n", irn, node->col, col, one_good ? "succeeded" : "failed"));
935 /* try next color when failed */
939 /* fragment the chunk according to the coloring */
940 local_best = fragment_chunk(env, col, c, tmp_chunks);
942 /* search the best of the good list
943 and make it the new best if it is better than the current */
945 aff_chunk_assure_weight(env, local_best);
947 DB((env->dbg, LEVEL_4, "\t\tlocal best chunk (id %d) for color %d: ", local_best->id, col));
948 DBG_AFF_CHUNK(env, LEVEL_4, local_best);
950 if (! best_chunk || best_chunk->weight < local_best->weight) {
951 best_chunk = local_best;
953 DB((env->dbg, LEVEL_4, "\n\t\t... setting global best chunk (id %d), color %d\n", best_chunk->id, best_color));
955 DB((env->dbg, LEVEL_4, "\n\t\t... omitting, global best is better\n"));
959 /* reject the coloring and bring the coloring to the initial state */
960 reject_coloring(changed_ones);
963 /* free all intermediate created chunks except best one */
964 while (! waitq_empty(tmp_chunks)) {
965 aff_chunk_t *tmp = waitq_get(tmp_chunks);
966 if (tmp != best_chunk)
967 delete_aff_chunk(env, tmp);
969 del_waitq(tmp_chunks);
971 /* return if coloring failed */
973 delete_aff_chunk(env, c);
974 del_waitq(changed_ones);
978 DB((env->dbg, LEVEL_2, "\tbest chunk #%d ", best_chunk->id));
979 DBG_AFF_CHUNK(env, LEVEL_2, best_chunk);
980 DB((env->dbg, LEVEL_2, "using color %d\n", best_color));
982 /* get the best fragment from the best list and color it */
983 bitset_foreach(best_chunk->nodes, idx) {
984 ir_node *irn = get_idx_irn(env->co->irg, idx);
985 co_mst_irn_t *node = get_co_mst_irn(env, irn);
988 res = change_node_color(env, node, best_color, changed_ones);
989 assert(res && "color manifesting failed");
991 node->chunk = best_chunk;
994 /* materialize colors on changed nodes */
995 while (! waitq_empty(changed_ones)) {
996 co_mst_irn_t *n = waitq_get(changed_ones);
1001 /* remove the nodes in best chunk from original chunk */
1002 bitset_andnot(c->nodes, best_chunk->nodes);
1004 /* we have to get the nodes back into the original chunk because they are scattered over temporary chunks */
1005 bitset_foreach(c->nodes, idx) {
1006 ir_node *n = get_idx_irn(env->co->irg, idx);
1007 co_mst_irn_t *nn = get_co_mst_irn(env, n);
1011 /* fragment the remaining chunk */
1012 visited = bitset_irg_malloc(env->co->irg);
1013 bitset_or(visited, best_chunk->nodes);
1014 bitset_foreach(c->nodes, idx) {
1015 if (! bitset_is_set(visited, idx)) {
1016 aff_chunk_t *new_chunk = new_aff_chunk(env);
1017 ir_node *irn = get_idx_irn(env->co->irg, idx);
1018 co_mst_irn_t *node = get_co_mst_irn(env, irn);
1020 expand_chunk_from(env, node, visited, new_chunk, c, decider_always_yes, 0);
1021 aff_chunk_assure_weight(env, new_chunk);
1022 pqueue_put(env->chunks, new_chunk, new_chunk->weight);
1026 /* clear obsolete chunks and free some memory */
1027 delete_aff_chunk(env, best_chunk);
1028 bitset_free(visited);
1029 del_waitq(changed_ones);
1033 * Main driver for mst safe coalescing algorithm.
1035 int co_solve_heuristic_mst(copy_opt_t *co)
1037 unsigned n_regs = co->cls->n_regs;
1038 bitset_t *ignore_regs = bitset_alloca(n_regs);
1041 co_mst_env_t mst_env;
1044 phase_init(&mst_env.ph, "co_mst", co->irg, PHASE_DEFAULT_GROWTH, co_mst_irn_init, &mst_env);
1046 k = be_put_ignore_regs(co->cenv->birg, co->cls, ignore_regs);
1049 FIRM_DBG_REGISTER(mst_env.dbg, "firm.be.co.heur4");
1050 mst_env.n_regs = n_regs;
1052 mst_env.chunks = new_pqueue();
1054 mst_env.ignore_regs = ignore_regs;
1055 mst_env.ifg = co->cenv->ifg;
1056 mst_env.aenv = co->aenv;
1057 pset_new_init(&mst_env.chunkset);
1059 DBG((mst_env.dbg, LEVEL_1, "==== Coloring %+F, class %s ====\n", co->irg, co->cls->name));
1061 /* build affinity chunks */
1062 build_affinity_chunks(&mst_env);
1064 /* color chunks as long as there are some */
1065 while (! pqueue_empty(mst_env.chunks)) {
1066 aff_chunk_t *chunk = pqueue_get(mst_env.chunks);
1068 color_aff_chunk(&mst_env, chunk);
1069 DB((mst_env.dbg, LEVEL_4, "<<<====== Coloring chunk (%d) done\n", chunk->id));
1070 delete_aff_chunk(&mst_env, chunk);
1073 /* apply coloring */
1074 foreach_phase_irn(&mst_env.ph, irn) {
1075 co_mst_irn_t *mirn = get_co_mst_irn(&mst_env, irn);
1076 const arch_register_t *reg;
1078 if (arch_irn_is(mst_env.aenv, irn, ignore))
1081 assert(mirn->fixed && "Node should have fixed color");
1083 /* skip nodes where color hasn't changed */
1084 if (mirn->init_col == mirn->col)
1087 reg = arch_register_for_index(co->cls, mirn->col);
1088 arch_set_irn_register(co->aenv, irn, reg);
1089 DB((mst_env.dbg, LEVEL_1, "%+F set color from %d to %d\n", irn, mirn->init_col, mirn->col));
1092 /* free allocated memory */
1093 del_pqueue(mst_env.chunks);
1094 phase_free(&mst_env.ph);
1095 pset_new_destroy(&mst_env.chunkset);