2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Simple copy minimization heuristics.
23 * @author Christian Wuerdig
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.
34 #endif /* HAVE_CONFIG_H */
41 #include "raw_bitset.h"
42 #include "irphase_t.h"
58 #include "becopyopt_t.h"
62 #define COL_COST_INFEASIBLE DBL_MAX
63 #define AFF_NEIGHBOUR_FIX_BENEFIT 128.0
64 #define NEIGHBOUR_CONSTR_COSTS 64.0
69 #define DBG_AFF_CHUNK(env, level, chunk) do { if (firm_dbg_get_mask(dbg) & (level)) dbg_aff_chunk((env), (chunk)); } while(0)
70 #define DBG_COL_COST(env, level, cost) do { if (firm_dbg_get_mask(dbg) & (level)) dbg_col_cost((env), (cost)); } while(0)
72 static firm_dbg_module_t *dbg = NULL;
76 #define DBG_AFF_CHUNK(env, level, chunk)
77 #define DBG_COL_COST(env, level, cost)
82 #define REAL(C) (C ## f)
84 static int last_chunk_id = 0;
85 static int recolor_limit = 4;
86 static real_t dislike_influence = REAL(0.1);
88 typedef struct _col_cost_t {
96 typedef struct _aff_chunk_t {
97 const ir_node **n; /**< An ARR_F containing all nodes of the chunk. */
98 const ir_node **interfere; /**< An ARR_F containing all inference. */
99 int weight; /**< Weight of this chunk */
100 unsigned weight_consistent : 1; /**< Set if the weight is consistent. */
101 unsigned deleted : 1; /**< For debugging: Set if the was deleted. */
102 int id; /**< An id of this chunk. */
104 col_cost_t color_affinity[1];
110 typedef struct _aff_edge_t {
111 const ir_node *src; /**< Source node. */
112 const ir_node *tgt; /**< Target node. */
113 double weight; /**< The weight of this edge. */
116 /* main coalescing environment */
117 typedef struct _co_mst_env_t {
118 int n_regs; /**< number of regs in class */
119 int k; /**< number of non-ignore registers in class */
120 bitset_t *ignore_regs; /**< set containing all global ignore registers */
121 ir_phase ph; /**< phase object holding data for nodes */
122 pqueue *chunks; /**< priority queue for chunks */
123 pset *chunkset; /**< set holding all chunks */
124 be_ifg_t *ifg; /**< the interference graph */
125 const arch_env_t *aenv; /**< the arch environment */
126 copy_opt_t *co; /**< the copy opt object */
128 col_cost_t **single_cols;
131 /* stores coalescing related information for a node */
132 typedef struct _co_mst_irn_t {
133 const ir_node *irn; /**< the irn this information belongs to */
134 aff_chunk_t *chunk; /**< the chunk this irn belongs to */
135 bitset_t *adm_colors; /**< set of admissible colors for this irn */
136 ir_node **int_neighs; /**< array of all interfering neighbours (cached for speed reasons) */
137 int n_neighs; /**< length of the interfering neighbours array. */
138 int int_aff_neigh; /**< number of interfering affinity neighbours */
139 int col; /**< color currently assigned */
140 int init_col; /**< the initial color */
141 int tmp_col; /**< a temporary assigned color */
142 unsigned fixed : 1; /**< the color is fixed */
143 struct list_head list; /**< Queue for coloring undo. */
144 real_t constr_factor;
147 #define get_co_mst_irn(mst_env, irn) (phase_get_or_set_irn_data(&(mst_env)->ph, (irn)))
149 typedef int decide_func_t(const co_mst_irn_t *node, int col);
154 * Write a chunk to stderr for debugging.
156 static void dbg_aff_chunk(const co_mst_env_t *env, const aff_chunk_t *c) {
160 if (c->weight_consistent)
161 ir_fprintf(stderr, " $%d ", c->weight);
162 ir_fprintf(stderr, "{");
163 for (i = 0, l = ARR_LEN(c->n); i < l; ++i) {
164 const ir_node *n = c->n[i];
165 ir_fprintf(stderr, " %+F,", n);
167 ir_fprintf(stderr, "}");
171 * Dump all admissible colors to stderr.
173 static void dbg_admissible_colors(const co_mst_env_t *env, const co_mst_irn_t *node) {
177 if (bitset_popcnt(node->adm_colors) < 1)
178 fprintf(stderr, "no admissible colors?!?");
180 bitset_foreach(node->adm_colors, idx) {
181 fprintf(stderr, " %d", idx);
187 * Dump color-cost pairs to stderr.
189 static void dbg_col_cost(const co_mst_env_t *env, const col_cost_t *cost) {
191 for (i = 0; i < env->n_regs; ++i)
192 fprintf(stderr, " (%d, %.4f)", cost[i].col, cost[i].cost);
195 #endif /* DEBUG_libfirm */
197 static INLINE int get_mst_irn_col(const co_mst_irn_t *node) {
198 return node->tmp_col >= 0 ? node->tmp_col : node->col;
202 * @return 1 if node @p node has color @p col, 0 otherwise.
204 static int decider_has_color(const co_mst_irn_t *node, int col) {
205 return get_mst_irn_col(node) == col;
209 * @return 1 if node @p node has not color @p col, 0 otherwise.
211 static int decider_hasnot_color(const co_mst_irn_t *node, int col) {
212 return get_mst_irn_col(node) != col;
216 * Always returns true.
218 static int decider_always_yes(const co_mst_irn_t *node, int col) {
224 /** compares two affinity edges by its weight */
225 static int cmp_aff_edge(const void *a, const void *b) {
226 const aff_edge_t *e1 = a;
227 const aff_edge_t *e2 = b;
229 if (e2->weight == e1->weight) {
230 if (e2->src->node_idx == e1->src->node_idx)
231 return QSORT_CMP(e2->tgt->node_idx, e1->tgt->node_idx);
233 return QSORT_CMP(e2->src->node_idx, e1->src->node_idx);
235 /* sort in descending order */
236 return QSORT_CMP(e2->weight, e1->weight);
239 /** compares to color-cost pairs */
240 static __attribute__((unused)) int cmp_col_cost_lt(const void *a, const void *b) {
241 const col_cost_t *c1 = a;
242 const col_cost_t *c2 = b;
243 real_t diff = c1->cost - c2->cost;
244 return (diff > 0) - (diff < 0);
247 static int cmp_col_cost_gt(const void *a, const void *b) {
248 const col_cost_t *c1 = a;
249 const col_cost_t *c2 = b;
250 real_t diff = c2->cost - c1->cost;
251 return (diff > 0) - (diff < 0);
255 * Creates a new affinity chunk
257 static INLINE aff_chunk_t *new_aff_chunk(co_mst_env_t *env) {
258 aff_chunk_t *c = xmalloc(sizeof(*c) + (env->n_regs - 1) * sizeof(c->color_affinity[0]));
259 c->n = NEW_ARR_F(const ir_node *, 0);
260 c->interfere = NEW_ARR_F(ir_node *, 0);
262 c->weight_consistent = 0;
264 c->id = ++last_chunk_id;
266 pset_insert(env->chunkset, c, c->id);
271 * Frees all memory allocated by an affinity chunk.
273 static INLINE void delete_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
274 pset_remove(env->chunkset, c, c->id);
275 DEL_ARR_F(c->interfere);
282 * binary search of sorted nodes.
284 * @return the position where n is found in the array arr or ~pos
285 * if the nodes is not here.
287 static INLINE int nodes_bsearch(const ir_node **arr, const ir_node *n) {
288 int hi = ARR_LEN(arr);
292 int md = lo + ((hi - lo) >> 1);
305 /** Check if a node n can be found inside arr. */
306 static int node_contains(const ir_node **arr, const ir_node *n) {
307 int i = nodes_bsearch(arr, n);
312 * Insert a node into the sorted nodes list.
314 * @return 1 if the node was inserted, 0 else
316 static int nodes_insert(const ir_node ***arr, const ir_node *irn) {
317 int idx = nodes_bsearch(*arr, irn);
320 int i, n = ARR_LEN(*arr);
323 ARR_APP1(const ir_node *, *arr, irn);
328 for (i = n - 1; i >= idx; --i)
337 * Adds a node to an affinity chunk
339 static INLINE void aff_chunk_add_node(aff_chunk_t *c, co_mst_irn_t *node) {
342 if (! nodes_insert(&c->n, node->irn))
345 c->weight_consistent = 0;
348 for (i = node->n_neighs - 1; i >= 0; --i) {
349 ir_node *neigh = node->int_neighs[i];
350 nodes_insert(&c->interfere, neigh);
355 * In case there is no phase information for irn, initialize it.
357 static void *co_mst_irn_init(ir_phase *ph, const ir_node *irn, void *old) {
358 co_mst_irn_t *res = old ? old : phase_alloc(ph, sizeof(res[0]));
359 co_mst_env_t *env = ph->priv;
362 const arch_register_req_t *req;
363 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
371 res->int_neighs = NULL;
372 res->int_aff_neigh = 0;
373 res->col = arch_register_get_index(arch_get_irn_register(env->aenv, irn));
374 res->init_col = res->col;
375 INIT_LIST_HEAD(&res->list);
377 DB((dbg, LEVEL_4, "Creating phase info for %+F\n", irn));
379 /* set admissible registers */
380 res->adm_colors = bitset_obstack_alloc(phase_obst(ph), env->n_regs);
382 /* Exclude colors not assignable to the irn */
383 req = arch_get_register_req(env->aenv, irn, -1);
384 if (arch_register_req_is(req, limited))
385 rbitset_copy_to_bitset(req->limited, res->adm_colors);
387 bitset_set_all(res->adm_colors);
389 /* exclude global ignore registers as well */
390 bitset_andnot(res->adm_colors, env->ignore_regs);
392 /* compute the constraint factor */
393 res->constr_factor = (real_t) (1 + env->n_regs - bitset_popcnt(res->adm_colors)) / env->n_regs;
395 /* set the number of interfering affinity neighbours to -1, they are calculated later */
396 res->int_aff_neigh = -1;
398 /* build list of interfering neighbours */
400 be_ifg_foreach_neighbour(env->ifg, nodes_it, irn, neigh) {
401 if (! arch_irn_is(env->aenv, neigh, ignore)) {
402 obstack_ptr_grow(phase_obst(ph), neigh);
406 res->int_neighs = obstack_finish(phase_obst(ph));
413 * Check if affinity chunk @p chunk interferes with node @p irn.
415 static INLINE int aff_chunk_interferes(const aff_chunk_t *chunk, const ir_node *irn) {
416 return node_contains(chunk->interfere, irn);
420 * Check if there are interference edges from c1 to c2.
422 * @param c2 Another chunk
423 * @return 1 if there are interferences between nodes of c1 and c2, 0 otherwise.
425 static INLINE int aff_chunks_interfere(const aff_chunk_t *c1, const aff_chunk_t *c2) {
431 /* check if there is a node in c2 having an interfering neighbor in c1 */
432 for (i = ARR_LEN(c2->n) - 1; i >= 0; --i) {
433 const ir_node *irn = c2->n[i];
435 if (node_contains(c1->interfere, irn))
442 * Returns the affinity chunk of @p irn or creates a new
443 * one with @p irn as element if there is none assigned.
445 static INLINE aff_chunk_t *get_aff_chunk(co_mst_env_t *env, const ir_node *irn) {
446 co_mst_irn_t *node = get_co_mst_irn(env, irn);
451 * Let chunk(src) absorb the nodes of chunk(tgt) (only possible when there
452 * are no interference edges from chunk(src) to chunk(tgt)).
453 * @return 1 if successful, 0 if not possible
455 static int aff_chunk_absorb(co_mst_env_t *env, const ir_node *src, const ir_node *tgt) {
456 aff_chunk_t *c1 = get_aff_chunk(env, src);
457 aff_chunk_t *c2 = get_aff_chunk(env, tgt);
460 DB((dbg, LEVEL_4, "Attempt to let c1 (id %d): ", c1 ? c1->id : -1));
462 DBG_AFF_CHUNK(env, LEVEL_4, c1);
464 DB((dbg, LEVEL_4, "{%+F}", src));
466 DB((dbg, LEVEL_4, "\n\tabsorb c2 (id %d): ", c2 ? c2->id : -1));
468 DBG_AFF_CHUNK(env, LEVEL_4, c2);
470 DB((dbg, LEVEL_4, "{%+F}", tgt));
472 DB((dbg, LEVEL_4, "\n"));
477 /* no chunk exists */
478 co_mst_irn_t *mirn = get_co_mst_irn(env, src);
481 for (i = mirn->n_neighs - 1; i >= 0; --i) {
482 if (mirn->int_neighs[i] == tgt)
486 /* create one containing both nodes */
487 c1 = new_aff_chunk(env);
488 aff_chunk_add_node(c1, get_co_mst_irn(env, src));
489 aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
493 /* c2 already exists */
494 if (! aff_chunk_interferes(c2, src)) {
495 aff_chunk_add_node(c2, get_co_mst_irn(env, src));
499 } else if (c2 == NULL) {
500 /* c1 already exists */
501 if (! aff_chunk_interferes(c1, tgt)) {
502 aff_chunk_add_node(c1, get_co_mst_irn(env, tgt));
505 } else if (c1 != c2 && ! aff_chunks_interfere(c1, c2)) {
508 for (idx = 0, len = ARR_LEN(c2->n); idx < len; ++idx)
509 aff_chunk_add_node(c1, get_co_mst_irn(env, c2->n[idx]));
511 for (idx = 0, len = ARR_LEN(c2->interfere); idx < len; ++idx) {
512 const ir_node *irn = c2->interfere[idx];
513 nodes_insert(&c1->interfere, irn);
516 c1->weight_consistent = 0;
518 delete_aff_chunk(env, c2);
521 DB((dbg, LEVEL_4, " ... c1 interferes with c2, skipped\n"));
525 DB((dbg, LEVEL_4, " ... absorbed\n"));
530 * Assures that the weight of the given chunk is consistent.
532 static void aff_chunk_assure_weight(co_mst_env_t *env, aff_chunk_t *c) {
533 if (! c->weight_consistent) {
537 for (i = 0; i < env->n_regs; ++i) {
538 c->color_affinity[i].col = i;
539 c->color_affinity[i].cost = REAL(0.0);
542 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
543 const ir_node *n = c->n[idx];
544 const affinity_node_t *an = get_affinity_info(env->co, n);
545 co_mst_irn_t *node = get_co_mst_irn(env, n);
548 if (node->constr_factor > REAL(0.0)) {
550 bitset_foreach (node->adm_colors, col)
551 c->color_affinity[col].cost += node->constr_factor;
556 co_gs_foreach_neighb(an, neigh) {
557 const ir_node *m = neigh->irn;
558 const int m_idx = get_irn_idx(m);
560 /* skip ignore nodes */
561 if (arch_irn_is(env->aenv, m, ignore))
564 w += node_contains(c->n, m) ? neigh->costs : 0;
569 for (i = 0; i < env->n_regs; ++i)
570 c->color_affinity[i].cost *= (REAL(1.0) / ARR_LEN(c->n));
573 // c->weight = bitset_popcnt(c->nodes);
574 c->weight_consistent = 1;
579 * Count the number of interfering affinity neighbours
581 static int count_interfering_aff_neighs(co_mst_env_t *env, const affinity_node_t *an) {
582 const neighb_t *neigh;
583 const ir_node *irn = an->irn;
584 const co_mst_irn_t *node = get_co_mst_irn(env, irn);
587 co_gs_foreach_neighb(an, neigh) {
588 const ir_node *n = neigh->irn;
591 /* skip ignore nodes */
592 if (arch_irn_is(env->aenv, n, ignore))
595 /* check if the affinity neighbour interfere */
596 for (i = 0; i < node->n_neighs; ++i) {
597 if (node->int_neighs[i] == n) {
608 * Build chunks of nodes connected by affinity edges.
609 * We start at the heaviest affinity edge.
610 * The chunks of the two edge-defining nodes will be
611 * merged if there are no interference edges from one
612 * chunk to the other.
614 static void build_affinity_chunks(co_mst_env_t *env) {
615 void *nodes_it = be_ifg_nodes_iter_alloca(env->ifg);
616 aff_edge_t *edges = NEW_ARR_F(aff_edge_t, 0);
619 aff_chunk_t *curr_chunk;
621 /* at first we create the affinity edge objects */
622 be_ifg_foreach_node(env->ifg, nodes_it, n) {
623 int n_idx = get_irn_idx(n);
627 /* skip ignore nodes */
628 if (arch_irn_is(env->aenv, n, ignore))
631 n1 = get_co_mst_irn(env, n);
632 an = get_affinity_info(env->co, n);
637 if (n1->int_aff_neigh < 0)
638 n1->int_aff_neigh = count_interfering_aff_neighs(env, an);
640 /* build the affinity edges */
641 co_gs_foreach_neighb(an, neigh) {
642 const ir_node *m = neigh->irn;
643 int m_idx = get_irn_idx(m);
645 /* record the edge in only one direction */
650 /* skip ignore nodes */
651 if (arch_irn_is(env->aenv, m, ignore))
657 n2 = get_co_mst_irn(env, m);
658 if (n2->int_aff_neigh < 0) {
659 affinity_node_t *am = get_affinity_info(env->co, m);
660 n2->int_aff_neigh = count_interfering_aff_neighs(env, am);
663 * these weights are pure hackery ;-).
664 * It's not chriswue's fault but mine.
666 edge.weight = neigh->costs;
667 ARR_APP1(aff_edge_t, edges, edge);
673 /* now: sort edges and build the affinity chunks */
674 len = ARR_LEN(edges);
675 qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
676 for (i = 0; i < len; ++i) {
677 DBG((dbg, LEVEL_1, "edge (%u,%u) %f\n", edges[i].src->node_idx, edges[i].tgt->node_idx, edges[i].weight));
679 (void)aff_chunk_absorb(env, edges[i].src, edges[i].tgt);
682 /* now insert all chunks into a priority queue */
683 foreach_pset(env->chunkset, curr_chunk) {
684 aff_chunk_assure_weight(env, curr_chunk);
686 DBG((dbg, LEVEL_1, "entry #%d", curr_chunk->id));
687 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
688 DBG((dbg, LEVEL_1, "\n"));
690 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
693 foreach_phase_irn(&env->ph, n) {
694 co_mst_irn_t *mirn = get_co_mst_irn(env, n);
696 if (mirn->chunk == NULL) {
697 /* no chunk is allocated so far, do it now */
698 aff_chunk_t *curr_chunk = new_aff_chunk(env);
699 aff_chunk_add_node(curr_chunk, mirn);
701 aff_chunk_assure_weight(env, curr_chunk);
703 DBG((dbg, LEVEL_1, "entry #%d", curr_chunk->id));
704 DBG_AFF_CHUNK(env, LEVEL_1, curr_chunk);
705 DBG((dbg, LEVEL_1, "\n"));
707 pqueue_put(env->chunks, curr_chunk, curr_chunk->weight);
714 static __attribute__((unused)) void chunk_order_nodes(co_mst_env_t *env, aff_chunk_t *chunk)
716 pqueue *grow = new_pqueue();
717 const ir_node *max_node = NULL;
721 for (i = ARR_LEN(chunk->n) - 1; i >= 0; i--) {
722 const ir_node *irn = chunk->n[i];
723 affinity_node_t *an = get_affinity_info(env->co, irn);
727 if (arch_irn_is(env->aenv, irn, ignore))
731 co_gs_foreach_neighb(an, neigh)
734 if (w > max_weight) {
742 bitset_t *visited = bitset_irg_malloc(env->co->irg);
744 for (i = ARR_LEN(chunk->n) - 1; i >= 0; --i)
745 bitset_add_irn(visited, chunk->n[i]);
747 pqueue_put(grow, (void *) max_node, max_weight);
748 bitset_remv_irn(visited, max_node);
750 while (!pqueue_empty(grow)) {
751 ir_node *irn = pqueue_get(grow);
752 affinity_node_t *an = get_affinity_info(env->co, irn);
755 if (arch_irn_is(env->aenv, irn, ignore))
758 assert(i <= ARR_LEN(chunk->n));
763 /* build the affinity edges */
764 co_gs_foreach_neighb(an, neigh) {
765 co_mst_irn_t *node = get_co_mst_irn(env, neigh->irn);
767 if (bitset_contains_irn(visited, node->irn)) {
768 pqueue_put(grow, (void *) neigh->irn, neigh->costs);
769 bitset_remv_irn(visited, node->irn);
775 bitset_free(visited);
780 * Greedy collect affinity neighbours into thew new chunk @p chunk starting at node @p node.
782 static void expand_chunk_from(co_mst_env_t *env, co_mst_irn_t *node, bitset_t *visited,
783 aff_chunk_t *chunk, aff_chunk_t *orig_chunk, decide_func_t *decider, int col)
785 waitq *nodes = new_waitq();
787 DBG((dbg, LEVEL_1, "\n\tExpanding new chunk (#%d) from %+F, color %d:", chunk->id, node->irn, col));
789 /* init queue and chunk */
790 waitq_put(nodes, node);
791 bitset_set(visited, get_irn_idx(node->irn));
792 aff_chunk_add_node(chunk, node);
793 DB((dbg, LEVEL_1, " %+F", node->irn));
795 /* as long as there are nodes in the queue */
796 while (! waitq_empty(nodes)) {
797 co_mst_irn_t *n = waitq_get(nodes);
798 affinity_node_t *an = get_affinity_info(env->co, n->irn);
800 /* check all affinity neighbors */
803 co_gs_foreach_neighb(an, neigh) {
804 const ir_node *m = neigh->irn;
805 int m_idx = get_irn_idx(m);
808 /* skip ignore nodes */
809 if (arch_irn_is(env->aenv, m, ignore))
812 n2 = get_co_mst_irn(env, m);
814 if (! bitset_is_set(visited, m_idx) &&
817 ! aff_chunk_interferes(chunk, m) &&
818 node_contains(orig_chunk->n, m))
821 following conditions are met:
822 - neighbour is not visited
823 - neighbour likes the color
824 - neighbour has not yet a fixed color
825 - the new chunk doesn't interfere with the neighbour
826 - neighbour belongs or belonged once to the original chunk
828 bitset_set(visited, m_idx);
829 aff_chunk_add_node(chunk, n2);
830 DB((dbg, LEVEL_1, " %+F", n2->irn));
831 /* enqueue for further search */
832 waitq_put(nodes, n2);
838 DB((dbg, LEVEL_1, "\n"));
844 * Fragment the given chunk into chunks having given color and not having given color.
846 static aff_chunk_t *fragment_chunk(co_mst_env_t *env, int col, aff_chunk_t *c, waitq *tmp) {
847 bitset_t *visited = bitset_irg_malloc(env->co->irg);
849 aff_chunk_t *best = NULL;
851 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
854 aff_chunk_t *tmp_chunk;
855 decide_func_t *decider;
859 if (bitset_is_set(visited, get_irn_idx(irn)))
862 node = get_co_mst_irn(env, irn);
864 if (get_mst_irn_col(node) == col) {
865 decider = decider_has_color;
867 DBG((dbg, LEVEL_4, "\tcolor %d wanted", col));
870 decider = decider_hasnot_color;
872 DBG((dbg, LEVEL_4, "\tcolor %d forbidden", col));
875 /* create a new chunk starting at current node */
876 tmp_chunk = new_aff_chunk(env);
877 waitq_put(tmp, tmp_chunk);
878 expand_chunk_from(env, node, visited, tmp_chunk, c, decider, col);
879 assert(ARR_LEN(tmp_chunk->n) > 0 && "No nodes added to chunk");
881 /* remember the local best */
882 aff_chunk_assure_weight(env, tmp_chunk);
883 if (check_for_best && (! best || best->weight < tmp_chunk->weight))
887 assert(best && "No chunk found?");
888 bitset_free(visited);
893 * Resets the temporary fixed color of all nodes within wait queue @p nodes.
894 * ATTENTION: the queue is empty after calling this function!
896 static INLINE void reject_coloring(struct list_head *nodes) {
897 co_mst_irn_t *n, *temp;
898 DB((dbg, LEVEL_4, "\treject coloring for"));
899 list_for_each_entry_safe(co_mst_irn_t, n, temp, nodes, list) {
900 DB((dbg, LEVEL_4, " %+F", n->irn));
901 assert(n->tmp_col >= 0);
903 list_del_init(&n->list);
905 DB((dbg, LEVEL_4, "\n"));
908 static INLINE void materialize_coloring(struct list_head *nodes) {
909 co_mst_irn_t *n, *temp;
910 list_for_each_entry_safe(co_mst_irn_t, n, temp, nodes, list) {
911 assert(n->tmp_col >= 0);
914 list_del_init(&n->list);
918 static INLINE void set_temp_color(co_mst_irn_t *node, int col, struct list_head *changed)
921 assert(!node->fixed);
922 assert(node->tmp_col < 0);
923 assert(node->list.next == &node->list && node->list.prev == &node->list);
924 assert(bitset_is_set(node->adm_colors, col));
926 list_add_tail(&node->list, changed);
930 static INLINE int is_loose(co_mst_irn_t *node)
932 return !node->fixed && node->tmp_col < 0;
936 * Determines the costs for each color if it would be assigned to node @p node.
938 static void determine_color_costs(co_mst_env_t *env, co_mst_irn_t *node, col_cost_t *costs) {
939 int *neigh_cols = alloca(env->n_regs * sizeof(*neigh_cols));
944 for (i = 0; i < env->n_regs; ++i) {
947 costs[i].cost = bitset_is_set(node->adm_colors, i) ? node->constr_factor : REAL(0.0);
950 for (i = 0; i < node->n_neighs; ++i) {
951 co_mst_irn_t *n = get_co_mst_irn(env, node->int_neighs[i]);
952 int col = get_mst_irn_col(n);
957 costs[col].cost = REAL(0.0);
961 coeff = REAL(1.0) / n_loose;
962 for (i = 0; i < env->n_regs; ++i)
963 costs[i].cost *= REAL(1.0) - coeff * neigh_cols[i];
967 /* need forward declaration due to recursive call */
968 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);
971 * Tries to change node to a color but @p explude_col.
972 * @return 1 if succeeded, 0 otherwise.
974 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) {
975 int col = get_mst_irn_col(node);
978 /* neighbours has already a different color -> good, temporary fix it */
979 if (col != exclude_col) {
981 set_temp_color(node, col, changed);
985 /* The node has the color it should not have _and_ has not been visited yet. */
986 if (is_loose(node)) {
987 col_cost_t *costs = alloca(env->n_regs * sizeof(costs[0]));
989 /* Get the costs for giving the node a specific color. */
990 determine_color_costs(env, node, costs);
992 /* Since the node must not have the not_col, set the costs for that color to "infinity" */
993 costs[exclude_col].cost = REAL(0.0);
995 /* sort the colors according costs, cheapest first. */
996 qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost_gt);
998 /* Try recoloring the node using the color list. */
999 res = recolor_nodes(env, node, costs, changed, depth + 1, max_depth, trip);
1006 * Tries to bring node @p node to cheapest color and color all interfering neighbours with other colors.
1007 * ATTENTION: Expect @p costs already sorted by increasing costs.
1008 * @return 1 if coloring could be applied, 0 otherwise.
1010 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) {
1012 struct list_head local_changed;
1015 if (depth > *max_depth)
1018 if (depth >= recolor_limit)
1021 DBG((dbg, LEVEL_4, "\tRecoloring %+F with color-costs", node->irn));
1022 DBG_COL_COST(env, LEVEL_4, costs);
1023 DB((dbg, LEVEL_4, "\n"));
1025 for (i = 0; i < env->n_regs; ++i) {
1026 int tgt_col = costs[i].col;
1030 /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
1031 if (costs[i].cost == REAL(0.0))
1034 /* Set the new color of the node and mark the node as temporarily fixed. */
1035 assert(node->tmp_col < 0 && "Node must not have been temporary fixed.");
1036 INIT_LIST_HEAD(&local_changed);
1037 set_temp_color(node, tgt_col, &local_changed);
1038 DBG((dbg, LEVEL_4, "\tTemporary setting %+F to color %d\n", node->irn, tgt_col));
1040 /* try to color all interfering neighbours with current color forbidden */
1041 for (j = 0; j < node->n_neighs; ++j) {
1045 neigh = node->int_neighs[j];
1047 /* skip ignore nodes */
1048 if (arch_irn_is(env->aenv, neigh, ignore))
1051 nn = get_co_mst_irn(env, neigh);
1052 DB((dbg, LEVEL_4, "\tHandling neighbour %+F, at position %d (fixed: %d, tmp_col: %d, col: %d)\n",
1053 neigh, j, nn->fixed, nn->tmp_col, nn->col));
1056 Try to change the color of the neighbor and record all nodes which
1057 get changed in the tmp list. Add this list to the "changed" list for
1058 that color. If we did not succeed to change the color of the neighbor,
1059 we bail out and try the next color.
1061 if (get_mst_irn_col(nn) == tgt_col) {
1062 /* try to color neighbour with tgt_col forbidden */
1063 neigh_ok = change_node_color_excluded(env, nn, tgt_col, &local_changed, depth + 1, max_depth, trip);
1071 We managed to assign the target color to all neighbors, so from the perspective
1072 of the current node, every thing was ok and we can return safely.
1075 /* append the local_changed ones to global ones */
1076 list_splice(&local_changed, changed);
1080 /* coloring of neighbours failed, so we try next color */
1081 reject_coloring(&local_changed);
1089 * Tries to bring node @p node and all it's neighbours to color @p tgt_col.
1090 * @return 1 if color @p col could be applied, 0 otherwise
1092 static int change_node_color(co_mst_env_t *env, co_mst_irn_t *node, int tgt_col, struct list_head *changed) {
1093 int col = get_mst_irn_col(node);
1095 /* if node already has the target color -> good, temporary fix it */
1096 if (col == tgt_col) {
1097 DBG((dbg, LEVEL_4, "\t\tCNC: %+F has already color %d, fix temporary\n", node->irn, tgt_col));
1099 set_temp_color(node, tgt_col, changed);
1104 Node has not yet a fixed color and target color is admissible
1105 -> try to recolor node and it's affinity neighbours
1107 if (is_loose(node) && bitset_is_set(node->adm_colors, tgt_col)) {
1108 col_cost_t *costs = env->single_cols[tgt_col];
1109 int res, max_depth, trip;
1114 DBG((dbg, LEVEL_4, "\t\tCNC: Attempt to recolor %+F ===>>\n", node->irn));
1115 res = recolor_nodes(env, node, costs, changed, 0, &max_depth, &trip);
1116 DBG((dbg, LEVEL_4, "\t\tCNC: <<=== Recoloring of %+F %s\n", node->irn, res ? "succeeded" : "failed"));
1117 stat_ev_int("heur4_recolor_depth_max", max_depth);
1118 stat_ev_int("heur4_recolor_trip", trip);
1124 #ifdef DEBUG_libfirm
1125 if (firm_dbg_get_mask(dbg) & LEVEL_4) {
1126 if (!is_loose(node))
1127 DB((dbg, LEVEL_4, "\t\tCNC: %+F has already fixed color %d\n", node->irn, col));
1129 DB((dbg, LEVEL_4, "\t\tCNC: color %d not admissible for %+F (", tgt_col, node->irn));
1130 dbg_admissible_colors(env, node);
1131 DB((dbg, LEVEL_4, ")\n"));
1140 * Tries to color an affinity chunk (or at least a part of it).
1141 * Inserts uncolored parts of the chunk as a new chunk into the priority queue.
1143 static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c) {
1144 aff_chunk_t *best_chunk = NULL;
1145 int n_nodes = ARR_LEN(c->n);
1146 int best_color = -1;
1147 int n_int_chunks = 0;
1148 waitq *tmp_chunks = new_waitq();
1149 waitq *best_starts = NULL;
1150 col_cost_t *order = alloca(env->n_regs * sizeof(order[0]));
1152 int idx, len, i, nidx, pos;
1153 struct list_head changed;
1155 DB((dbg, LEVEL_2, "fragmentizing chunk #%d", c->id));
1156 DBG_AFF_CHUNK(env, LEVEL_2, c);
1157 DB((dbg, LEVEL_2, "\n"));
1159 stat_ev_ctx_push_fmt("heur4_color_chunk", "%d", c->id);
1161 ++env->chunk_visited;
1163 /* compute color preference */
1164 memset(order, 0, env->n_regs * sizeof(order[0]));
1166 for (pos = 0, len = ARR_LEN(c->interfere); pos < len; ++pos) {
1167 const ir_node *n = c->interfere[pos];
1168 co_mst_irn_t *node = get_co_mst_irn(env, n);
1169 aff_chunk_t *chunk = node->chunk;
1171 if (is_loose(node) && chunk && chunk->visited < env->chunk_visited) {
1172 assert(!chunk->deleted);
1173 chunk->visited = env->chunk_visited;
1176 aff_chunk_assure_weight(env, chunk);
1177 for (i = 0; i < env->n_regs; ++i)
1178 order[i].cost += chunk->color_affinity[i].cost;
1182 for (i = 0; i < env->n_regs; ++i) {
1183 real_t dislike = n_int_chunks > 0 ? REAL(1.0) - order[i].cost / n_int_chunks : REAL(0.0);
1185 order[i].cost = (REAL(1.0) - dislike_influence) * c->color_affinity[i].cost + dislike_influence * dislike;
1188 qsort(order, env->n_regs, sizeof(order[0]), cmp_col_cost_gt);
1190 DBG_COL_COST(env, LEVEL_2, order);
1191 DB((dbg, LEVEL_2, "\n"));
1193 /* check which color is the "best" for the given chunk.
1194 * if we found a color which was ok for all nodes, we take it
1195 * and do not look further. (see did_all flag usage below.)
1196 * If we have many colors which fit all nodes it is hard to decide
1197 * which one to take anyway.
1198 * TODO Sebastian: Perhaps we should at all nodes and figure out
1199 * a suitable color using costs as done above (determine_color_costs).
1201 for (i = 0; i < env->k; ++i) {
1202 int col = order[i].col;
1203 waitq *good_starts = new_waitq();
1204 aff_chunk_t *local_best;
1207 /* skip ignore colors */
1208 if (bitset_is_set(env->ignore_regs, col))
1211 DB((dbg, LEVEL_2, "\ttrying color %d\n", col));
1215 /* try to bring all nodes of given chunk to the current color. */
1216 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1217 const ir_node *irn = c->n[idx];
1218 co_mst_irn_t *node = get_co_mst_irn(env, irn);
1221 assert(! node->fixed && "Node must not have a fixed color.");
1222 DB((dbg, LEVEL_4, "\t\tBringing %+F from color %d to color %d ...\n", irn, node->col, col));
1225 The order of the colored nodes is important, so we record the successfully
1226 colored ones in the order they appeared.
1228 INIT_LIST_HEAD(&changed);
1230 good = change_node_color(env, node, col, &changed);
1231 stat_ev_tim_pop("heur4_recolor");
1233 waitq_put(good_starts, node);
1234 materialize_coloring(&changed);
1239 reject_coloring(&changed);
1241 n_succeeded += good;
1242 DB((dbg, LEVEL_4, "\t\t... %+F attempt from %d to %d %s\n", irn, node->col, col, good ? "succeeded" : "failed"));
1245 /* unfix all nodes */
1246 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1247 co_mst_irn_t *node = get_co_mst_irn(env, c->n[idx]);
1251 /* try next color when failed */
1252 if (n_succeeded == 0)
1255 /* fragment the chunk according to the coloring */
1256 local_best = fragment_chunk(env, col, c, tmp_chunks);
1258 /* search the best of the good list
1259 and make it the new best if it is better than the current */
1261 aff_chunk_assure_weight(env, local_best);
1263 DB((dbg, LEVEL_3, "\t\tlocal best chunk (id %d) for color %d: ", local_best->id, col));
1264 DBG_AFF_CHUNK(env, LEVEL_3, local_best);
1266 if (! best_chunk || best_chunk->weight < local_best->weight) {
1267 best_chunk = local_best;
1270 del_waitq(best_starts);
1271 best_starts = good_starts;
1272 DB((dbg, LEVEL_3, "\n\t\t... setting global best chunk (id %d), color %d\n", best_chunk->id, best_color));
1274 DB((dbg, LEVEL_3, "\n\t\t... omitting, global best is better\n"));
1275 del_waitq(good_starts);
1279 del_waitq(good_starts);
1282 /* if all nodes were recolored, bail out */
1283 if (n_succeeded == n_nodes)
1287 stat_ev_int("heur4_colors_tried", i);
1289 /* free all intermediate created chunks except best one */
1290 while (! waitq_empty(tmp_chunks)) {
1291 aff_chunk_t *tmp = waitq_get(tmp_chunks);
1292 if (tmp != best_chunk)
1293 delete_aff_chunk(env, tmp);
1295 del_waitq(tmp_chunks);
1297 /* return if coloring failed */
1300 del_waitq(best_starts);
1304 DB((dbg, LEVEL_2, "\tbest chunk #%d ", best_chunk->id));
1305 DBG_AFF_CHUNK(env, LEVEL_2, best_chunk);
1306 DB((dbg, LEVEL_2, "using color %d\n", best_color));
1308 for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx) {
1309 const ir_node *irn = best_chunk->n[idx];
1310 co_mst_irn_t *node = get_co_mst_irn(env, irn);
1313 /* bring the node to the color. */
1314 DB((dbg, LEVEL_4, "\tManifesting color %d for %+F, chunk #%d\n", best_color, node->irn, best_chunk->id));
1315 INIT_LIST_HEAD(&changed);
1317 res = change_node_color(env, node, best_color, &changed);
1318 stat_ev_tim_pop("heur4_recolor");
1320 materialize_coloring(&changed);
1323 assert(list_empty(&changed));
1326 /* remove the nodes in best chunk from original chunk */
1327 len = ARR_LEN(best_chunk->n);
1328 for (idx = 0; idx < len; ++idx) {
1329 const ir_node *irn = best_chunk->n[idx];
1330 int pos = nodes_bsearch(c->n, irn);
1335 len = ARR_LEN(c->n);
1336 for (idx = nidx = 0; idx < len; ++idx) {
1337 const ir_node *irn = c->n[idx];
1343 ARR_SHRINKLEN(c->n, nidx);
1346 /* we have to get the nodes back into the original chunk because they are scattered over temporary chunks */
1347 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1348 const ir_node *n = c->n[idx];
1349 co_mst_irn_t *nn = get_co_mst_irn(env, n);
1353 /* fragment the remaining chunk */
1354 visited = bitset_irg_malloc(env->co->irg);
1355 for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx)
1356 bitset_set(visited, get_irn_idx(best_chunk->n[idx]));
1358 for (idx = 0, len = ARR_LEN(c->n); idx < len; ++idx) {
1359 const ir_node *irn = c->n[idx];
1360 if (! bitset_is_set(visited, get_irn_idx(irn))) {
1361 aff_chunk_t *new_chunk = new_aff_chunk(env);
1362 co_mst_irn_t *node = get_co_mst_irn(env, irn);
1364 expand_chunk_from(env, node, visited, new_chunk, c, decider_always_yes, 0);
1365 aff_chunk_assure_weight(env, new_chunk);
1366 pqueue_put(env->chunks, new_chunk, new_chunk->weight);
1370 for (idx = 0, len = ARR_LEN(best_chunk->n); idx < len; ++idx) {
1371 const ir_node *n = best_chunk->n[idx];
1372 co_mst_irn_t *nn = get_co_mst_irn(env, n);
1376 /* clear obsolete chunks and free some memory */
1377 delete_aff_chunk(env, best_chunk);
1378 bitset_free(visited);
1380 del_waitq(best_starts);
1382 stat_ev_ctx_pop("heur4_color_chunk");
1386 * Main driver for mst safe coalescing algorithm.
1388 int co_solve_heuristic_mst(copy_opt_t *co) {
1389 unsigned n_regs = co->cls->n_regs;
1390 bitset_t *ignore_regs = bitset_alloca(n_regs);
1393 co_mst_env_t mst_env;
1398 phase_init(&mst_env.ph, "co_mst", co->irg, PHASE_DEFAULT_GROWTH, co_mst_irn_init, &mst_env);
1400 k = be_put_ignore_regs(co->cenv->birg, co->cls, ignore_regs);
1403 mst_env.n_regs = n_regs;
1405 mst_env.chunks = new_pqueue();
1407 mst_env.ignore_regs = ignore_regs;
1408 mst_env.ifg = co->cenv->ifg;
1409 mst_env.aenv = co->aenv;
1410 mst_env.chunkset = pset_new_ptr(512);
1411 mst_env.chunk_visited = 0;
1412 mst_env.single_cols = phase_alloc(&mst_env.ph, sizeof(*mst_env.single_cols) * n_regs);
1414 for (i = 0; i < n_regs; ++i) {
1415 col_cost_t *vec = phase_alloc(&mst_env.ph, sizeof(*vec) * n_regs);
1417 mst_env.single_cols[i] = vec;
1418 for (j = 0; j < n_regs; ++j) {
1420 vec[j].cost = REAL(0.0);
1424 vec[0].cost = REAL(1.0);
1427 DBG((dbg, LEVEL_1, "==== Coloring %+F, class %s ====\n", co->irg, co->cls->name));
1429 /* build affinity chunks */
1431 build_affinity_chunks(&mst_env);
1432 stat_ev_tim_pop("heur4_initial_chunk");
1434 /* color chunks as long as there are some */
1435 while (! pqueue_empty(mst_env.chunks)) {
1436 aff_chunk_t *chunk = pqueue_get(mst_env.chunks);
1438 color_aff_chunk(&mst_env, chunk);
1439 DB((dbg, LEVEL_4, "<<<====== Coloring chunk (%d) done\n", chunk->id));
1440 delete_aff_chunk(&mst_env, chunk);
1443 /* apply coloring */
1444 foreach_phase_irn(&mst_env.ph, irn) {
1446 const arch_register_t *reg;
1448 if (arch_irn_is(mst_env.aenv, irn, ignore))
1451 mirn = get_co_mst_irn(&mst_env, irn);
1452 // assert(mirn->fixed && "Node should have fixed color");
1454 /* skip nodes where color hasn't changed */
1455 if (mirn->init_col == mirn->col)
1458 reg = arch_register_for_index(co->cls, mirn->col);
1459 arch_set_irn_register(co->aenv, irn, reg);
1460 DB((dbg, LEVEL_1, "%+F set color from %d to %d\n", irn, mirn->init_col, mirn->col));
1463 /* free allocated memory */
1464 del_pqueue(mst_env.chunks);
1465 phase_free(&mst_env.ph);
1466 del_pset(mst_env.chunkset);
1468 stat_ev_tim_pop("heur4_total");
1473 static const lc_opt_table_entry_t options[] = {
1474 LC_OPT_ENT_INT ("limit", "limit recoloring", &recolor_limit),
1475 LC_OPT_ENT_DBL ("di", "dislike influence", &dislike_influence),
1480 void be_init_copyheur4(void) {
1481 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1482 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
1483 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
1484 lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
1485 lc_opt_entry_t *heur4_grp = lc_opt_get_grp(co_grp, "heur4");
1487 lc_opt_add_table(heur4_grp, options);
1488 FIRM_DBG_REGISTER(dbg, "firm.be.co.heur4");
1492 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyheur4);