19 #include "bechordal.h"
22 #include "phiclass_t.h"
23 #include "bephicoal_t.h"
25 #define DEBUG_LVL SET_LEVEL_3
28 #define INITIAL_SLOTS_PINNED_GLOBAL 256
29 #define INITIAL_SLOTS_FREE_NODES 128
30 #define INITIAL_SLOTS_CHANGED_NODES 32
32 /* some things for readable code */
33 #define CHANGE_SAVE NULL
34 #define CHANGE_IMPOSSIBLE (ir_node *)1
35 #define CHANGE_NYI (ir_node *)2
36 #define is_conflicting_node(n) (((int)n) > 2)
39 * Models conflicts between nodes. These may be life range conflicts or
40 * pinning conflicts which may occur while changing colors
42 typedef struct _conflict_t {
47 * If an irn is changed, the changes first get stored in a node_stat_t,
48 * to allow undo of changes in case of conflicts.
50 typedef struct _node_stat_t {
54 char status; /**< Bit 0: pinned, Bit 1: removed */
57 #define _set_pinned(nodestat) nodestat->status |= 1
58 #define _set_removed(nodestat) nodestat->status |= 2
59 #define _clear_pinned(nodestat) nodestat->status &= 255 ^ 1
60 #define _clear_removed(nodestat) nodestat->status &= 255 ^ 2
61 #define _is_pinned(nodestat) (nodestat->status & 1)
62 #define _is_removed(nodestat) (nodestat->status & 2)
65 * Central data structure. Contains infos needed during coalescing of the
66 * corresponding phi class.
68 typedef struct _phi_unit_t {
69 unsigned char phi_count; /**< the number of phi nodes in this unit */
71 unsigned char node_count; /**< size of the nodes-array */
72 unsigned char conflict_count; /**< size of the conflicts-array */
73 unsigned char conflict_count_org; /**< initial size of the conflicts-array */
74 ir_node **nodes; /**< [0] is the phi node. [1..node_count-1] the arguments of the phi not interfering with it */
75 conflict_t *conflicts; /**< pairs of conflicting ir_nodes. */
76 set *changed_nodes; /**< contains node_stat_t's. */
79 static firm_dbg_module_t *dbgphi = NULL;
82 * Contains ir_nodes of phi-classes whose colors may change unlimited times.
83 * These nodes are not optimizable, so there is no need to pin their color.
85 static pset *free_nodes = NULL;
88 * Contains already optimized ir_nodes of phi-classes fully processed.
89 * So one can perform a check not to switch them twice or more.
91 static pset *pinned_global = NULL;
93 static int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
94 return ((node_stat_t *)x)->irn != ((node_stat_t *)y)->irn;
98 * Finds a node status entry of a node if existent.
100 static INLINE node_stat_t *pu_find_node(phi_unit_t *pu, ir_node *irn) {
103 return set_find(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
107 * Finds a node status entry of a node if existent. Otherwise it will return
108 * an initialized new entry for this node.
110 static INLINE node_stat_t *pu_find_or_insert_node(phi_unit_t *pu, ir_node *irn) {
113 find.color = NO_COLOR;
114 find.undo_color = NO_COLOR;
116 return set_insert(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
120 * @return The virtual color of a node, if set before, else just the real color.
122 static INLINE int pu_get_new_color(phi_unit_t *pu, ir_node *irn) {
123 node_stat_t *found = pu_find_node(pu, irn);
127 return get_irn_color(irn);
131 * Sets the virtual color of a node.
133 static INLINE void pu_set_new_color(phi_unit_t *pu, ir_node *irn, int color) {
134 node_stat_t *found = pu_find_or_insert_node(pu, irn);
135 found->undo_color = found->color;
136 found->color = color;
137 DBG((dbgphi, LEVEL_4, "%n %d\n", irn, color));
141 * Sets the virtual color of a node to the color it had,
142 * before the last call to pu_set_new_color
144 static INLINE void pu_undo_color(phi_unit_t *pu, ir_node *irn) {
145 node_stat_t *ns = pu_find_node(pu, irn);
146 assert(ns && "Nodes whose colors are undone must be in pu->changed_nodes");
147 ns->color = ns->undo_color;
148 DBG((dbgphi, LEVEL_3, "\t\tUndo: col(%n) := %d\n", irn, ns->undo_color));
152 * Checks if a node is removed from consideration respectively building
153 * a maximum independent set.
155 static INLINE int pu_is_node_removed(phi_unit_t *pu, ir_node *irn) {
156 node_stat_t *found = pu_find_node(pu, irn);
158 return _is_removed(found);
164 * Removes a node from the base set, out of which a maximum independet
165 * set gets build from.
167 static INLINE void pu_remove_node(phi_unit_t *pu, ir_node *irn) {
168 node_stat_t *found = pu_find_or_insert_node(pu, irn);
170 DBG((dbgphi, LEVEL_4, "%n\n", irn));
174 * Checks if a node is local pinned; i.e. it belongs to the same phi unit and
175 * has been optimized before the current processed one.
177 static INLINE int pu_is_node_pinned(phi_unit_t *pu, ir_node *irn) {
178 node_stat_t *found = pu_find_node(pu, irn);
180 return _is_pinned(found);
186 * Local-pins a node, so optimizations of further nodes of the same phi unit
187 * can handle situations in which a color change would undo prior optimizations.
189 static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
190 node_stat_t *found = pu_find_or_insert_node(pu, irn);
192 DBG((dbgphi, LEVEL_4, "%n\n", irn));
196 * If a local pinned conflict occurs, a new edge in the conflict graph is added.
197 * The next maximum independent set build, will regard it.
199 static INLINE void pu_add_conflict(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
200 int count = pu->conflict_count;
202 DBG((dbgphi, LEVEL_3, "\t %n -- %n\n", n1, n2));
203 assert(count != 255 && "Too much conflicts. Can hold max 255 entries");
204 if ((count & 15) == 0)
205 pu->conflicts = realloc(pu->conflicts, (count + 16)*sizeof(*pu->conflicts));
207 if ((int)n1 < (int)n2) {
208 pu->conflicts[count].n1 = n1;
209 pu->conflicts[count].n2 = n2;
211 pu->conflicts[count].n1 = n2;
212 pu->conflicts[count].n2 = n1;
215 pu->conflict_count++;
219 * Checks if two nodes are in a conflict.
221 static INLINE int pu_are_conflicting(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
222 const ir_node *o1, *o2;
225 if ((int)n1 < (int)n2) {
233 for (i = 0; i < pu->conflict_count; ++i)
234 if (pu->conflicts[i].n1 == o1 && pu->conflicts[i].n2 == o2)
240 * Determines a maximum independent set with respect to the conflict edges
241 * in pu->conflicts and the nodes beeing all non-removed nodes of pu->nodes.
242 * TODO: make this 'un-greedy'
243 * TODO: be aware that phi nodes should find their way in the set.
244 * for 1 phi in greedy version this is no prob, cause is comes first at [0].
246 static int pu_get_max_ind_set(phi_unit_t *pu, struct obstack *res) {
250 DBG((dbgphi, LEVEL_2, "\t Max indep set:\n"));
251 for (i = 0; i < pu->node_count; ++i) {
253 if (pu_is_node_removed(pu, pu->nodes[i]))
255 mis = (ir_node**) obstack_base(res);
256 for (o = 0; o < size; ++o)
257 if (phi_ops_interfere(pu->nodes[i], mis[o])) {
263 DBG((dbgphi, LEVEL_2, "\t\t%n\n", pu->nodes[i]));
264 obstack_ptr_grow(res, pu->nodes[i]);
272 * Performs virtual re-coloring of node @p n to color @p col. Virtual colors of
273 * other nodes are changed too, as required to preserve correctness. Function is
274 * aware of local and global pinning. Recursive.
275 * @param irn The node to set the color for
276 * @param col The color to set.
277 * @param trigger The irn that caused the wish to change the color of the irn
278 * @param changed_nodes An obstack on which all ir_nodes get growed on, which are changed
279 * @return CHANGE_SAVE iff setting the color is possible, with all transiteve effects.
280 * CHANGE_IMPOSSIBLE iff conflicts with reg-constraintsis occured.
281 * CHANGE_NYI iff an unhandled situation occurs.
282 * Else the first conflicting ir_node encountered is returned.
284 * ASSUMPTION: Assumes that a life range of a single value can't be spilt into
285 * several smaller intervals where other values can live in between.
287 static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_node *trigger, struct obstack *changed_nodes) {
289 struct obstack confl_ob;
290 ir_node **confl, *cn;
293 DBG((dbgphi, LEVEL_3, "\t\t%n \tcaused col(%n) \t%2d --> %2d\n", trigger, irn, pu_get_new_color(pu, irn), col));
294 obstack_init(&confl_ob);
295 irn_col = pu_get_new_color(pu, irn);
299 if (pset_find_ptr(pinned_global, irn) || pu_is_node_pinned(pu, irn)) {
304 /* get all nodes which would conflict with this change */
310 irn_bl = get_nodes_block(irn);
312 /* first check for a conflicting node which is 'living in' the irns block */
315 pset *live_ins = get_live_in(irn_bl);
316 for (n = pset_first(live_ins); n; n = pset_next(live_ins))
317 if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
318 DBG((dbgphi, LEVEL_4, "\t\t %n\ttroubles\n", n));
319 obstack_ptr_grow(&confl_ob, n);
320 pset_break(live_ins);
325 /* setup the queue of blocks. */
327 obstack_ptr_grow(&q, irn_bl);
331 /* process the queue. The code below looks for every block dominated
332 * by the irns one, and in which the irn is live, if there are
333 * conflicting nodes */
335 ir_node *curr_bl, *sub_bl;
338 curr_bl = ((ir_node **)obstack_base(&q))[out++];
340 /* Add to the result all nodes in the block, which have
341 * the target color and interfere with the irn */
342 for (i = 0, max = get_irn_n_outs(curr_bl); i < max; ++i) {
343 ir_node *n = get_irn_out(curr_bl, i);
344 if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
345 DBG((dbgphi, LEVEL_4, "\t\t %n\ttroubles\n", n));
346 obstack_ptr_grow(&confl_ob, n);
350 /* If irn lives out check i-dominated blocks where the irn lives in */
352 if (is_live_out(curr_bl, irn)) {
353 dominates_for_each(curr_bl, sub_bl)
354 if (is_live_in(sub_bl, irn)) {
355 obstack_ptr_grow(&q, sub_bl);
360 obstack_free(&q, NULL);
361 obstack_ptr_grow(&confl_ob, NULL);
362 confl = (ir_node **) obstack_finish(&confl_ob);
365 /* process all nodes which would conflict with this change */
366 for (i = 0, cn = confl[0]; cn; cn = confl[++i]) {
369 /* try to color the conflicting node cn with the color of the irn itself */
370 sub_res = _pu_color_irn(pu, cn, irn_col, irn, changed_nodes);
371 if (sub_res != CHANGE_SAVE) {
376 /* if we arrive here all sub changes can be applied, so it's save to change this irn */
379 DBG((dbgphi, LEVEL_3, "\t\t%n save\n", irn));
380 obstack_free(&confl_ob, NULL);
381 pu_set_new_color(pu, irn, col);
382 obstack_ptr_grow(changed_nodes, irn);
386 DBG((dbgphi, LEVEL_3, "\t\t%n conflicting\n", irn));
387 obstack_free(&confl_ob, NULL);
391 static ir_node *pu_color_irn(phi_unit_t *pu, ir_node *irn, int col) {
393 struct obstack ob_undo;
395 obstack_init(&ob_undo);
396 res = _pu_color_irn(pu, irn, col, irn, &ob_undo);
398 if (res != CHANGE_SAVE) { /* undo virtual changes caused by the last call */
400 ir_node *undo_node, **undo_nodes;
402 obstack_ptr_grow(&ob_undo, NULL);
403 undo_nodes = obstack_finish(&ob_undo);
404 for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i])
405 pu_undo_color(pu, undo_node);
408 obstack_free(&ob_undo, NULL);
413 * Tries to set as much members of a phi unit as possible to color @p col.
414 * All changes taken together are guaranteed to be conflict free.
416 static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
417 struct obstack ob_mis;
418 int i, redo, mis_size;
421 /* first init pessimistically. Just return if we can't get a better result */
424 obstack_init(&ob_mis);
429 /* get a max independent set regarding current conflicts */
430 mis_size = pu_get_max_ind_set(pu, &ob_mis);
431 mis = obstack_finish(&ob_mis);
433 /* shortcut: if mis size is worse than best, then mis won't be better. */
434 if (mis_size < b_size)
437 /* check if its possible to set the color for all members of the maximum set*/
438 for (i = 0; i < mis_size; ++i) {
439 ir_node *test_node, *confl_node;
442 DBG((dbgphi, LEVEL_2, "\t Testing %n\n", test_node));
443 confl_node = pu_color_irn(pu, test_node, col);
445 if (confl_node == CHANGE_SAVE) {
446 DBG((dbgphi, LEVEL_2, "\t Save\n"));
447 if (!pset_find_ptr(free_nodes, test_node))
448 pu_pin_node(pu, test_node);
451 DBG((dbgphi, LEVEL_2, "\t Conflicting\n"));
452 /* adjust conflict graph */
453 if (is_conflicting_node(confl_node)) {
455 if (pu_is_node_pinned(pu, confl_node)) {
456 pu_add_conflict(pu, confl_node, test_node);
459 if (pset_find_ptr(pinned_global, confl_node)) {
460 pu_remove_node(pu, test_node);
464 /* TODO: CHANGE_IMPOSSIBLE due to reg constraints --> remove from set ?? */
467 /* shortcut: color not possible for phi node (phi comes first) ==> exit */
471 /* shortcut: if the best size achievable with this mis is worse than best, then get another mis. */
472 if (mis_size - impossibles < b_size) {
477 obstack_free(&ob_mis, mis);
481 obstack_free(&ob_mis, NULL);
486 * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
487 * number of copy instructions placed during phi destruction. Optimized version.
488 * Works only for phi-classes/phi-units with exactly 1 phi node, which is the
489 * case for approximately 80% of all phi units.
491 static void pu_coalesce_1_phi(phi_unit_t *pu) {
492 int size, col, b_size, b_color;
495 /* init best search result */
500 /* find optimum of all colors */
501 for (col = MAX_COLORS-1; col >= 0; --col) {
502 DBG((dbgphi, 1, "\tTrying color %d\n", col));
503 size = pu_try_color(pu, col, b_size);
505 /* did we find a better max ind. set? */
507 DBG((dbgphi, 1, "\tBetter size: %d\n", size));
510 b_changes = pu->changed_nodes;
514 del_set(pu->changed_nodes);
517 /* reset the phi unit to original state for next color */
518 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
519 pu->conflict_count = pu->conflict_count_org;
521 /* shortcut: if all members can be colored we are (very) happy */
522 if (b_size == pu->node_count)
526 /* now apply the found optimum */
529 DBG((dbgphi, 1, "\tBest color: %d Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count-1));
530 for (ns = set_first(b_changes); ns; ns = set_next(b_changes)) {
531 /* NO_COLOR is possible, if we had an undo; so the irn stays in the
532 * pu->changed_nodes with new color set to NO_COLOR. */
533 if (ns->color != NO_COLOR) {
534 DBG((dbgphi, 1, "\t color(%n) := %d\n", ns->irn, ns->color));
535 set_irn_color(ns->irn, ns->color);
540 DBG((dbgphi, 1, "\tBest color: none\n"));
545 * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
546 * number of copy instructions placed during phi destruction.
547 * General purpose version.
549 static void pu_coalesce_n_phi(phi_unit_t *pu) {
554 * Prepares a phi class for further processing as a phi unit.
555 * @param pc The phi class to prepare.
556 * @return A so called phi unit containing some prepared informations
557 * needed by the following coalescing phase.
559 static phi_unit_t *new_pu(pset *pc) {
561 ir_node *n, *phi = NULL;
563 /* get the phi count of this class */
564 pu = calloc(1, sizeof(*pu));
565 for (n = pset_first(pc); n; n = pset_next(pc))
571 if (pu->phi_count == 1) {
578 /* build member set not containing phi interferers */
579 DBG((dbgphi, 1, "\tPhi-1 class:\n"));
580 pu->node_count = 1; /*for the phi*/
581 for (n = pset_first(pc); n; n = pset_next(pc)) {
584 if (!phi_ops_interfere(phi, n)) {
585 DBG((dbgphi, 1, "\t %n\n", n));
586 obstack_ptr_grow(&ob, n);
589 DBG((dbgphi, 1, "\t %n \tdropped\n", n));
590 pset_insert_ptr(free_nodes, n);
593 tmp = obstack_finish(&ob);
594 pu->nodes = malloc(pu->node_count * sizeof(*pu->nodes));
596 memcpy(&pu->nodes[1], tmp, (pu->node_count-1) * sizeof(*tmp));
598 /* init conlict graph to life range interference */
599 DBG((dbgphi, 1, "\tInitial conflicts:\n"));
600 for (i = 0; i < pu->node_count; ++i)
601 for (o = i+1; o < pu->node_count; ++o)
602 if (phi_ops_interfere(pu->nodes[i], pu->nodes[o]))
603 pu_add_conflict(pu, pu->nodes[i], pu->nodes[o]);
604 pu->conflict_count_org = pu->conflict_count;
606 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
608 obstack_free(&ob, NULL);
610 DBG((dbgphi, 1, "\tPhi-n class:\n"));
621 static void free_pu(phi_unit_t *pu) {
622 if (pu->phi_count == 1) {
624 free(pu->changed_nodes);
634 void be_phi_coalesce(pset *all_phi_classes) {
637 pinned_global = pset_new_ptr(INITIAL_SLOTS_PINNED_GLOBAL);
638 free_nodes = pset_new_ptr(INITIAL_SLOTS_FREE_NODES);
640 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
641 phi_unit_t *pu = new_pu(pc);
642 if (pu->phi_count == 1)
643 pu_coalesce_1_phi(pu);
645 pu_coalesce_n_phi(pu);
649 del_pset(free_nodes);
650 del_pset(pinned_global);
654 void be_phi_coal_init(void) {
655 dbgphi = firm_dbg_register("ir.be.phicoal");
656 firm_dbg_set_mask(dbgphi, DEBUG_LVL);