19 #include "bechordal.h"
22 #include "phiclass_t.h"
23 #include "bephicoal_t.h"
29 #define INITIAL_SLOTS_PINNED_GLOBAL 256
30 //#define INITIAL_SLOTS_FREE_NODES 128
31 #define INITIAL_SLOTS_CHANGED_NODES 32
33 /* some things for readable code */
34 #define CHANGE_SAVE NULL
35 #define CHANGE_IMPOSSIBLE (ir_node *)1
36 #define CHANGE_NYI (ir_node *)2
37 #define is_conflicting_node(n) (((int)n) > 2)
40 * Models conflicts between nodes. These may be life range conflicts or
41 * pinning conflicts, which may occur while changing colors
43 typedef struct _conflict_t {
48 * If an irn is changed, the changes first get stored in a node_stat_t,
49 * to allow undo of changes in case of conflicts.
51 typedef struct _node_stat_t {
55 char status; /**< Bit 0: pinned, Bit 1: removed */
58 #define _set_pinned(nodestat) nodestat->status |= 1
59 #define _set_removed(nodestat) nodestat->status |= 2
60 #define _clear_pinned(nodestat) nodestat->status &= 255 ^ 1
61 #define _clear_removed(nodestat) nodestat->status &= 255 ^ 2
62 #define _is_pinned(nodestat) (nodestat->status & 1)
63 #define _is_removed(nodestat) (nodestat->status & 2)
66 * Central data structure. Contains infos needed during coalescing of the
67 * corresponding phi class.
69 typedef struct _phi_unit_t {
70 unsigned char phi_count; /**< the number of phi nodes in this unit */
72 unsigned char node_count; /**< size of the nodes-array */
73 unsigned char conflict_count; /**< size of the conflicts-array */
74 unsigned char conflict_count_org; /**< initial size of the conflicts-array */
75 ir_node **nodes; /**< [0] is the phi node. [1..node_count-1] the arguments of the phi not interfering with it */
76 conflict_t *conflicts; /**< pairs of conflicting ir_nodes. */
77 set *changed_nodes; /**< contains node_stat_t's. */
80 static firm_dbg_module_t *dbgphi = NULL;
83 * Contains ir_nodes of phi-classes whose colors may change unlimited times.
84 * These nodes are not optimizable, so there is no need to pin their color.
86 //static pset *free_nodes = NULL;
89 * Contains already optimized ir_nodes of phi-classes fully processed.
90 * So one can perform a check not to switch them twice or more.
92 static pset *pinned_global = NULL;
94 static int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
95 return ((node_stat_t *)x)->irn != ((node_stat_t *)y)->irn;
99 * Finds a node status entry of a node if existent.
101 static INLINE node_stat_t *pu_find_node(phi_unit_t *pu, ir_node *irn) {
104 return set_find(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
108 * Finds a node status entry of a node if existent. Otherwise it will return
109 * an initialized new entry for this node.
111 static INLINE node_stat_t *pu_find_or_insert_node(phi_unit_t *pu, ir_node *irn) {
114 find.color = NO_COLOR;
115 find.undo_color = NO_COLOR;
117 return set_insert(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
121 * @return The virtual color of a node, if set before, else just the real color.
123 static INLINE int pu_get_new_color(phi_unit_t *pu, ir_node *irn) {
124 node_stat_t *found = pu_find_node(pu, irn);
128 return get_irn_color(irn);
132 * Sets the virtual color of a node.
134 static INLINE void pu_set_new_color(phi_unit_t *pu, ir_node *irn, int color) {
135 node_stat_t *found = pu_find_or_insert_node(pu, irn);
136 found->undo_color = found->color;
137 found->color = color;
138 DBG((dbgphi, LEVEL_4, "%n %d\n", irn, color));
142 * Sets the virtual color of a node to the color it had,
143 * before the last call to pu_set_new_color
145 static INLINE void pu_undo_color(phi_unit_t *pu, ir_node *irn) {
146 node_stat_t *ns = pu_find_node(pu, irn);
147 assert(ns && "Nodes whose colors are undone must be in pu->changed_nodes");
148 ns->color = ns->undo_color;
149 DBG((dbgphi, LEVEL_3, "\t\tUndo: col(%n) := %d\n", irn, ns->undo_color));
153 * Checks if a node is removed from consideration respectively building
154 * a maximum independent set.
156 static INLINE int pu_is_node_removed(phi_unit_t *pu, ir_node *irn) {
157 node_stat_t *found = pu_find_node(pu, irn);
159 return _is_removed(found);
165 * Removes a node from the base set, out of which a maximum independet
166 * set gets build from.
168 static INLINE void pu_remove_node(phi_unit_t *pu, ir_node *irn) {
169 node_stat_t *found = pu_find_or_insert_node(pu, irn);
171 DBG((dbgphi, LEVEL_4, "%n\n", irn));
175 * Checks if a node is local pinned; i.e. it belongs to the same phi unit and
176 * has been optimized before the current processed one.
178 static INLINE int pu_is_node_pinned(phi_unit_t *pu, ir_node *irn) {
179 node_stat_t *found = pu_find_node(pu, irn);
181 return _is_pinned(found);
187 * Local-pins a node, so optimizations of further nodes of the same phi unit
188 * can handle situations in which a color change would undo prior optimizations.
190 static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
191 node_stat_t *found = pu_find_or_insert_node(pu, irn);
193 DBG((dbgphi, LEVEL_4, "%n\n", irn));
197 * If a local pinned conflict occurs, a new edge in the conflict graph is added.
198 * The next maximum independent set build, will regard it.
200 static INLINE void pu_add_conflict(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
201 int count = pu->conflict_count;
203 DBG((dbgphi, LEVEL_3, "\t %n -- %n\n", n1, n2));
204 assert(count != 255 && "Too much conflicts. Can hold max 255 entries");
205 if ((count & 15) == 0)
206 pu->conflicts = realloc(pu->conflicts, (count + 16)*sizeof(*pu->conflicts));
208 if ((int)n1 < (int)n2) {
209 pu->conflicts[count].n1 = n1;
210 pu->conflicts[count].n2 = n2;
212 pu->conflicts[count].n1 = n2;
213 pu->conflicts[count].n2 = n1;
216 pu->conflict_count++;
220 * Checks if two nodes are in a conflict.
222 static INLINE int pu_are_conflicting(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
223 const ir_node *o1, *o2;
226 if ((int)n1 < (int)n2) {
234 for (i = 0; i < pu->conflict_count; ++i)
235 if (pu->conflicts[i].n1 == o1 && pu->conflicts[i].n2 == o2)
241 * Checks if a node is a member of a phi unit.
242 * Other nodes should not be pinned global.
244 static INLINE int pu_is_global_pinnable(phi_unit_t *pu, ir_node *irn) {
246 for (i = 0; i < pu->node_count; ++i)
247 if (pu->nodes[i] == irn)
253 * Determines a maximum independent set with respect to the conflict edges
254 * in pu->conflicts and the nodes beeing all non-removed nodes of pu->nodes.
255 * TODO: make this 'un-greedy'
256 * TODO: be aware that phi nodes should find their way in the set.
257 * for 1 phi in greedy version this is no prob, cause is comes first at [0].
259 static int pu_get_mis(phi_unit_t *pu, struct obstack *res) {
263 DBG((dbgphi, LEVEL_2, "\t Max indep set:\n"));
264 for (i = 0; i < pu->node_count; ++i) {
266 if (pu_is_node_removed(pu, pu->nodes[i]))
268 mis = (ir_node**) obstack_base(res);
269 for (o = 0; o < size; ++o)
270 if (pu_are_conflicting(pu, pu->nodes[i], mis[o])) {
276 DBG((dbgphi, LEVEL_2, "\t\t%n\n", pu->nodes[i]));
277 obstack_ptr_grow(res, pu->nodes[i]);
285 * Performs virtual re-coloring of node @p n to color @p col. Virtual colors of
286 * other nodes are changed too, as required to preserve correctness. Function is
287 * aware of local and global pinning. Recursive.
288 * @param irn The node to set the color for
289 * @param col The color to set.
290 * @param trigger The irn that caused the wish to change the color of the irn
291 * @param changed_nodes An obstack on which all ir_nodes get growed on, which are changed
292 * @return CHANGE_SAVE iff setting the color is possible, with all transiteve effects.
293 * CHANGE_IMPOSSIBLE iff conflicts with reg-constraintsis occured.
294 * CHANGE_NYI iff an unhandled situation occurs.
295 * Else the first conflicting ir_node encountered is returned.
297 * ASSUMPTION: Assumes that a life range of a single value can't be spilt into
298 * several smaller intervals where other values can live in between.
300 static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_node *trigger, struct obstack *changed_nodes) {
302 struct obstack confl_ob;
303 ir_node **confl, *cn;
306 DBG((dbgphi, LEVEL_3, "\t\t%n \tcaused col(%n) \t%2d --> %2d\n", trigger, irn, pu_get_new_color(pu, irn), col));
307 obstack_init(&confl_ob);
308 irn_col = pu_get_new_color(pu, irn);
312 if (pset_find_ptr(pinned_global, irn) || pu_is_node_pinned(pu, irn)) {
317 /* get all nodes which would conflict with this change */
323 irn_bl = get_nodes_block(irn);
325 /* first check for a conflicting node which is 'living in' the irns block */
328 pset *live_ins = get_live_in(irn_bl);
329 for (n = pset_first(live_ins); n; n = pset_next(live_ins))
330 if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
331 DBG((dbgphi, LEVEL_4, "\t\t %n\ttroubles\n", n));
332 obstack_ptr_grow(&confl_ob, n);
333 pset_break(live_ins);
338 /* setup the queue of blocks. */
340 obstack_ptr_grow(&q, irn_bl);
344 /* process the queue. The code below looks for every block dominated
345 * by the irns one, and in which the irn is live, if there are
346 * conflicting nodes */
348 ir_node *curr_bl, *sub_bl;
351 curr_bl = ((ir_node **)obstack_base(&q))[out++];
353 /* Add to the result all nodes in the block, which have
354 * the target color and interfere with the irn */
355 for (i = 0, max = get_irn_n_outs(curr_bl); i < max; ++i) {
356 ir_node *n = get_irn_out(curr_bl, i);
357 if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
358 DBG((dbgphi, LEVEL_4, "\t\t %n\ttroubles\n", n));
359 obstack_ptr_grow(&confl_ob, n);
363 /* If irn lives out check i-dominated blocks where the irn lives in */
365 if (is_live_out(curr_bl, irn)) {
366 dominates_for_each(curr_bl, sub_bl)
367 if (is_live_in(sub_bl, irn)) {
368 obstack_ptr_grow(&q, sub_bl);
373 obstack_free(&q, NULL);
374 obstack_ptr_grow(&confl_ob, NULL);
375 confl = (ir_node **) obstack_finish(&confl_ob);
378 /* process all nodes which would conflict with this change */
379 for (i = 0, cn = confl[0]; cn; cn = confl[++i]) {
382 /* try to color the conflicting node cn with the color of the irn itself */
383 sub_res = _pu_color_irn(pu, cn, irn_col, irn, changed_nodes);
384 if (sub_res != CHANGE_SAVE) {
389 /* if we arrive here all sub changes can be applied, so it's save to change this irn */
392 DBG((dbgphi, LEVEL_3, "\t\t%n save\n", irn));
393 obstack_free(&confl_ob, NULL);
394 pu_set_new_color(pu, irn, col);
395 obstack_ptr_grow(changed_nodes, irn);
399 DBG((dbgphi, LEVEL_3, "\t\t%n conflicting\n", irn));
400 obstack_free(&confl_ob, NULL);
404 static ir_node *pu_color_irn(phi_unit_t *pu, ir_node *irn, int col) {
406 struct obstack ob_undo;
408 obstack_init(&ob_undo);
409 res = _pu_color_irn(pu, irn, col, irn, &ob_undo);
411 if (res != CHANGE_SAVE) { /* undo virtual changes caused by the last call */
413 ir_node *undo_node, **undo_nodes;
415 obstack_ptr_grow(&ob_undo, NULL);
416 undo_nodes = obstack_finish(&ob_undo);
417 for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i])
418 pu_undo_color(pu, undo_node);
421 obstack_free(&ob_undo, NULL);
426 * Tries to set as much members of a phi unit as possible to color @p col.
427 * All changes taken together are guaranteed to be conflict free.
429 static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
430 struct obstack ob_mis;
431 int i, redo, mis_size;
434 obstack_init(&ob_mis);
438 /* get a max independent set regarding current conflicts */
439 mis_size = pu_get_mis(pu, &ob_mis);
440 mis = obstack_finish(&ob_mis);
442 /* shortcut: if mis size is worse than best, then mis won't be better. */
443 if (mis_size < b_size)
446 /* check if its possible to set the color for all members of the maximum set*/
447 for (i = 0; i < mis_size; ++i) {
448 ir_node *test_node, *confl_node;
451 DBG((dbgphi, LEVEL_2, "\t Testing %n\n", test_node));
452 confl_node = pu_color_irn(pu, test_node, col);
454 if (confl_node == CHANGE_SAVE) {
455 DBG((dbgphi, LEVEL_2, "\t Save\n"));
456 //if (!pset_find_ptr(free_nodes, test_node))
457 pu_pin_node(pu, test_node);
458 } else if (confl_node == CHANGE_NYI) {
459 DBG((dbgphi, 0, "\t NYI\n"));
460 } else if (confl_node == CHANGE_IMPOSSIBLE) {
461 /* TODO: this may happen due to reg constraints --> remove from set ?? */
463 DBG((dbgphi, LEVEL_2, "\t Conflicting\n"));
464 assert(is_conflicting_node(confl_node));
466 if (pu_is_node_pinned(pu, confl_node)) {
467 /* changing test_node would change back a node of current phi unit */
468 pu_add_conflict(pu, confl_node, test_node);
471 if (pset_find_ptr(pinned_global, confl_node)) {
472 /* changing test_node would change back a node of a prior phi unit */
473 pu_remove_node(pu, test_node);
478 if (confl_node != CHANGE_SAVE) {
479 /* shortcut: color not possible for phi node (phi comes first) ==> exit */
484 /* break iteration over current mis, because it will change */
488 obstack_free(&ob_mis, mis);
492 obstack_free(&ob_mis, NULL);
497 * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
498 * number of copy instructions placed during phi destruction. Optimized version.
499 * Works only for phi-classes/phi-units with exactly 1 phi node, which is the
500 * case for approximately 80% of all phi units.
502 static void pu_coal_1_phi(phi_unit_t *pu) {
503 int size, col, b_size, b_color;
506 /* init best search result */
511 /* find optimum of all colors */
512 for (col = MAX_COLORS-1; col >= 0; --col) {
513 DBG((dbgphi, 1, "\tTrying color %d\n", col));
514 size = pu_try_color(pu, col, b_size);
516 /* did we find a better max ind. set? */
518 DBG((dbgphi, 1, "\tBetter size: %d\n", size));
521 b_changes = pu->changed_nodes;
525 del_set(pu->changed_nodes);
528 /* reset the phi unit to original state for next color */
529 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
530 pu->conflict_count = pu->conflict_count_org;
532 /* shortcut: if all members can be colored we are (very) happy */
533 if (b_size == pu->node_count)
537 /* now apply the found optimum */
540 DBG((dbgphi, 0, "\tBest color: %d Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count-1));
541 for (ns = set_first(b_changes); ns; ns = set_next(b_changes)) {
542 /* NO_COLOR is possible, if we had an undo; so the irn stays in the
543 * pu->changed_nodes with new color set to NO_COLOR. */
544 if (ns->color != NO_COLOR) {
545 DBG((dbgphi, 1, "\t color(%n) := %d\n", ns->irn, ns->color));
546 set_irn_color(ns->irn, ns->color);
547 if (pu_is_global_pinnable(pu, ns->irn) && ns->color == pu_get_new_color(pu, pu->nodes[0]))
548 pset_insert_ptr(pinned_global, ns->irn);
553 DBG((dbgphi, 1, "\tBest color: none\n"));
558 * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
559 * number of copy instructions placed during phi destruction.
560 * General purpose version.
562 static void pu_coal_n_phi(phi_unit_t *pu) {
567 * Prepares a phi class for further processing as a phi unit.
568 * @param pc The phi class to prepare.
569 * @return A so called phi unit containing some prepared informations
570 * needed by the following coalescing phase.
572 static phi_unit_t *new_pu(pset *pc) {
574 ir_node *n, *phi = NULL;
576 /* get the phi count of this class */
577 pu = calloc(1, sizeof(*pu));
578 for (n = pset_first(pc); n; n = pset_next(pc))
584 if (pu->phi_count == 1) {
591 /* build member set not containing phi interferers */
592 DBG((dbgphi, 1, "\tPhi-1 unit:\n"));
594 DBG((dbgphi, 1, "\t %n\n", phi));
595 obstack_ptr_grow(&ob, phi);
598 for (n = pset_first(pc); n; n = pset_next(pc)) {
601 if (!phi_ops_interfere(phi, n)) {
602 DBG((dbgphi, 1, "\t %n\n", n));
603 obstack_ptr_grow(&ob, n);
606 DBG((dbgphi, 1, "\t %n \tdropped\n", n));
607 /* TODO: What if the irn is only free wrt one phi class? */
608 //pset_insert_ptr(free_nodes, n);
611 tmp = obstack_finish(&ob);
612 pu->nodes = malloc(pu->node_count * sizeof(*pu->nodes));
613 memcpy(&pu->nodes[0], tmp, pu->node_count * sizeof(*tmp));
615 /* init conlict graph to life range interference */
616 DBG((dbgphi, 1, "\tInitial conflicts:\n"));
617 for (i = 0; i < pu->node_count; ++i)
618 for (o = i+1; o < pu->node_count; ++o)
619 if (phi_ops_interfere(pu->nodes[i], pu->nodes[o]))
620 pu_add_conflict(pu, pu->nodes[i], pu->nodes[o]);
621 pu->conflict_count_org = pu->conflict_count;
623 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
625 obstack_free(&ob, NULL);
627 DBG((dbgphi, 1, "\tPhi-n unit:\n"));
638 static void free_pu(phi_unit_t *pu) {
639 if (pu->phi_count == 1) {
641 free(pu->changed_nodes);
651 void be_phi_coalesce(pset *all_phi_classes) {
654 pinned_global = pset_new_ptr(INITIAL_SLOTS_PINNED_GLOBAL);
655 // free_nodes = pset_new_ptr(INITIAL_SLOTS_FREE_NODES);
657 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
658 phi_unit_t *pu = new_pu(pc);
659 if (pu->phi_count == 1)
666 // del_pset(free_nodes);
667 del_pset(pinned_global);
671 void be_phi_coal_init(void) {
672 dbgphi = firm_dbg_register("ir.be.phicoal");
673 firm_dbg_set_mask(dbgphi, DEBUG_LVL);