16 #include "bechordal.h"
19 #include "phiclass_t.h"
20 #include "bephicoal_t.h"
22 #define DEBUG_LVL SET_LEVEL_3
25 #define INITIAL_SLOTS_PINNED_GLOBAL 256
26 #define INITIAL_SLOTS_FREE_NODES 128
27 #define INITIAL_SLOTS_CHANGED_NODES 32
29 /* some things for readable code */
30 #define CHANGE_SAVE NULL
31 #define CHANGE_IMPOSSIBLE (ir_node *)1
32 #define CHANGE_NYI (ir_node *)2
33 #define is_conflicting_node(n) (((int)n) > 2)
36 * Models conflicts between nodes. These may be life range conflicts or
37 * pinning conflicts which may occur while changing colors
39 typedef struct _conflict_t {
44 * If an irn is changed, the changes first get stored in a node_stat_t,
45 * to allow undo of changes in case of conflicts.
47 typedef struct _node_stat_t {
51 char status; /**< Bit 0: pinned, Bit 1: removed */
54 #define _set_pinned(nodestat) nodestat->status |= 1
55 #define _set_removed(nodestat) nodestat->status |= 2
56 #define _clear_pinned(nodestat) nodestat->status &= 255 ^ 1
57 #define _clear_removed(nodestat) nodestat->status &= 255 ^ 2
58 #define _is_pinned(nodestat) (nodestat->status & 1)
59 #define _is_removed(nodestat) (nodestat->status & 2)
62 * Central data structure. Contains infos needed during coalescing of the
63 * corresponding phi class.
65 typedef struct _phi_unit_t {
66 unsigned char phi_count; /**< the number of phi nodes in this unit */
68 unsigned char node_count; /**< size of the nodes-array */
69 unsigned char conflict_count; /**< size of the conflicts-array */
70 unsigned char conflict_count_org; /**< initial size of the conflicts-array */
71 ir_node **nodes; /**< [0] is the phi node. [1..node_count-1] the arguments of the phi not interfering with it */
72 conflict_t *conflicts; /**< pairs of conflicting ir_nodes. */
73 set *changed_nodes; /**< contains node_stat_t's. */
76 static firm_dbg_module_t *dbgphi = NULL;
79 * Contains ir_nodes of phi-classes whose colors may change unlimited times.
80 * These nodes are not optimizable, so there is no need to pin their color.
82 static pset *free_nodes = NULL;
85 * Contains already optimized ir_nodes of phi-classes fully processed.
86 * So one can perform a check not to switch them twice or more.
88 static pset *pinned_global = NULL;
90 int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
91 return ((node_stat_t *)x)->irn != ((node_stat_t *)y)->irn;
95 * Finds a node status entry of a node if existent.
97 static INLINE node_stat_t *pu_find_node(phi_unit_t *pu, ir_node *irn) {
100 return set_find(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
104 * Finds a node status entry of a node if existent. Otherwise it will return
105 * an initialized new entry for this node.
107 static INLINE node_stat_t *pu_find_or_insert_node(phi_unit_t *pu, ir_node *irn) {
110 find.color = NO_COLOR;
111 find.undo_color = NO_COLOR;
113 return set_insert(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
117 * @return The virtual color of a node, if set before, else just the real color.
119 static INLINE int pu_get_new_color(phi_unit_t *pu, ir_node *irn) {
120 node_stat_t *found = pu_find_node(pu, irn);
124 return get_irn_color(irn);
128 * Sets the virtual color of a node.
130 static INLINE void pu_set_new_color(phi_unit_t *pu, ir_node *irn, int color) {
131 node_stat_t *found = pu_find_or_insert_node(pu, irn);
133 * This is only correct if no color is set >=2 times while changing
134 * a single phi-unit-member */
135 found->undo_color = found->color;
136 found->color = color;
140 * Checks if a node is removed from consideration respectively building
141 * a maximum independent set.
143 static INLINE int pu_is_node_removed(phi_unit_t *pu, ir_node *irn) {
144 node_stat_t *found = pu_find_node(pu, irn);
146 return _is_removed(found);
152 * Removes a node from the base set, out of which a maximum independet
153 * set gets build from.
155 static INLINE void pu_remove_node(phi_unit_t *pu, ir_node *irn) {
156 node_stat_t *found = pu_find_or_insert_node(pu, irn);
161 * Checks if a node is local pinned; i.e. it belongs to the same phi unit and
162 * has been optimized before the current processed one.
164 static INLINE int pu_is_node_pinned(phi_unit_t *pu, ir_node *irn) {
165 node_stat_t *found = pu_find_node(pu, irn);
167 return _is_pinned(found);
173 * Local-pins a node, so optimizations of further nodes of the same phi unit
174 * can handle situations in which a color change would undo prior optimizations.
176 static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
177 node_stat_t *found = pu_find_or_insert_node(pu, irn);
182 * If a local pinned conflict occurs, a new edge in the conflict graph is added.
183 * The next maximum independent set build, will regard it.
185 static INLINE void pu_add_conflict(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
186 int count = pu->conflict_count;
188 assert(count != 255 && "Too much conflicts. Can hold max 255 entries");
189 if ((count & 15) == 0)
190 pu->conflicts = realloc(pu->conflicts, (count + 16)*sizeof(*pu->conflicts));
192 if ((int)n1 < (int)n2) {
193 pu->conflicts[count].n1 = n1;
194 pu->conflicts[count].n2 = n2;
196 pu->conflicts[count].n1 = n2;
197 pu->conflicts[count].n2 = n1;
200 pu->conflict_count++;
204 * Checks if two nodes are in a conflict.
206 static INLINE int pu_are_conflicting(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
207 const ir_node *o1, *o2;
210 if ((int)n1 < (int)n2) {
218 for (i = 0; i < pu->conflict_count; ++i)
219 if (pu->conflicts[i].n1 == o1 && pu->conflicts[i].n2 == o2)
225 * Determines a maximum independent set with respect to the conflict edges
226 * in pu->conflicts and the nodes beeing all non-removed nodes of pu->nodes.
227 * TODO: make this 'un-greedy'
228 * TODO: be aware that phi nodes should find their way in the set.
229 * for 1 phi in greedy version this is no prob, cause is comes first at [0].
231 int pu_get_max_ind_set(phi_unit_t *pu, struct obstack *res) {
235 DBG((dbgphi, 1, "\t\t Max indep set\n"));
236 for (i = 0; i < pu->node_count; ++i) {
238 if (pu_is_node_removed(pu, pu->nodes[i]))
240 mis = (ir_node**) obstack_base(res);
241 for (o = 0; o < size; ++o)
242 if (phi_ops_interfere(pu->nodes[i], mis[o])) {
248 DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", pu->nodes[i]));
249 obstack_ptr_grow(res, pu->nodes[i]);
257 * Performs virtual re-coloring of node @p n to color @p col. Virtual colors of
258 * other nodes are changed too, as required to preserve correctness. Function is
259 * aware of local and global pinning. Recursive.
260 * @param irn The node to set the color for
261 * @param col The color to set.
262 * @param trigger The irn that caused the wish to change the color of the irn
263 * @param changed_nodes An obstack on which all ir_nodes get growed on, which are changed
264 * @return CHANGE_SAVE iff setting the color is possible, with all transiteve effects.
265 * CHANGE_IMPOSSIBLE iff conflicts with reg-constraintsis occured.
266 * CHANGE_NYI iff an unhandled situation occurs.
267 * Else the first conflicting ir_node encountered is returned.
269 * ASSUMPTION: Assumes that a life range of a single value can't be spilt into
270 * several smaller intervals where other values can live in between.
272 static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_node *trigger, struct obstack *changed_nodes) {
274 struct obstack confl_ob;
275 ir_node **confl, *cn;
278 obstack_init(&confl_ob);
279 irn_col = pu_get_new_color(pu, irn);
283 if (pset_find_ptr(pinned_global, irn) || pu_is_node_pinned(pu, irn)) {
284 DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Pinned\n", trigger, irn, col));
289 /* get all nodes which would conflict with this change */
294 /* setup the queue */
296 obstack_ptr_grow(&q, get_nodes_block(irn));
300 /* process the queue */
302 ir_node *curr_bl, *sub_bl;
305 curr_bl = ((ir_node **)obstack_base(&q))[out++];
307 /* Add to the result all nodes in the block which live in target color
308 * and interfere with the irn */
309 for (i = 0, max = get_irn_n_outs(curr_bl); i < max; ++i) {
310 ir_node *n = get_irn_out(curr_bl, i);
311 if (!is_allocatable_irn(n))
313 if (n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n))
314 obstack_ptr_grow(&confl_ob, n);
317 /* If irn lives out check i-dominated blocks where the irn lives in */
319 if (is_live_out(curr_bl, irn)) {
320 dominates_for_each(curr_bl, sub_bl)
321 if (is_live_in(sub_bl, irn)) {
322 obstack_ptr_grow(&q, sub_bl);
327 obstack_free(&q, NULL);
328 obstack_ptr_grow(&confl_ob, NULL);
329 confl = (ir_node **) obstack_finish(&confl_ob);
332 /* process all nodes which would conflict with this change */
333 for (i = 0, cn = confl[0]; cn; cn = confl[++i]) {
336 /* try to color the conflicting node cn with the color of the irn itself */
337 DBG((dbgphi, LEVEL_3, "\t\t\t%n \t~~> %n := %d: Subcheck\n", trigger, irn, col));
338 sub_res = _pu_color_irn(pu, cn, irn_col, irn, changed_nodes);
339 if (sub_res != CHANGE_SAVE) {
344 /* if we arrive here all sub changes can be applied, so it's save to change this irn */
347 DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Save\n", trigger, irn, col));
348 obstack_free(&confl_ob, NULL);
349 pu_set_new_color(pu, irn, col);
350 obstack_ptr_grow(changed_nodes, irn);
354 DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Conflict\n", trigger, irn, col));
355 obstack_free(&confl_ob, NULL);
359 #define pu_color_irn(pu,irn,col,ob) _pu_color_irn(pu, irn, col, irn, ob)
362 * Tries to set as much members of a phi unit as possible to color @p col.
363 * All changes taken together are guaranteed to be conflict free.
365 static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
366 struct obstack ob_mis, ob_undo;
367 int i, redo, mis_size;
370 /* first init pessimistically. Just return if we can't get a better result */
373 obstack_init(&ob_mis);
374 obstack_init(&ob_undo);
378 /* get a max independent set regarding current conflicts */
379 mis_size = pu_get_max_ind_set(pu, &ob_mis);
380 mis = obstack_finish(&ob_mis);
382 /* shortcut: if mis size is worse than best, then mis won't be better. */
383 if (mis_size < b_size)
386 /* check if its possible to set the color for all members of the maximum set*/
387 for (i = 0; i < mis_size; ++i) {
388 ir_node *test_node, *confl_node;
391 DBG((dbgphi, 1, "\t\t Testing %n\n", test_node));
392 confl_node = pu_color_irn(pu, test_node, col, &ob_undo);
394 if (confl_node == CHANGE_SAVE) {
395 if (!pset_find_ptr(free_nodes, test_node))
396 pu_pin_node(pu, test_node);
397 obstack_free(&ob_undo, obstack_finish(&ob_undo));
401 ir_node *undo_node, **undo_nodes;
403 obstack_ptr_grow(&ob_undo, NULL);
404 undo_nodes = obstack_finish(&ob_undo);
405 for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i]) {
406 node_stat_t *ns = pu_find_node(pu, undo_node);
407 ns->color = ns->undo_color;
409 obstack_free(&ob_undo, undo_nodes);
411 if (is_conflicting_node(confl_node)) {
412 if (pu_is_node_pinned(pu, confl_node))
413 pu_add_conflict(pu, confl_node, test_node);
414 if (pset_find_ptr(pinned_global, confl_node))
415 pu_remove_node(pu, test_node);
419 /* shortcut: color not possible for phi node (phi comes first) ==> exit */
423 obstack_free(&ob_mis, mis);
427 obstack_free(&ob_undo, NULL);
428 obstack_free(&ob_mis, NULL);
433 * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
434 * number of copy instructions placed during phi destruction. Optimized version.
435 * Works only for phi-classes/phi-units with exactly 1 phi node, which is the
436 * case for approximately 80% of all phi units.
438 static void pu_coalesce_1_phi(phi_unit_t *pu) {
439 int size, col, b_size, b_color;
442 /* init best search result */
447 /* find optimum of all colors */
448 for (col = MAX_COLORS-1; col >= 0; --col) {
449 DBG((dbgphi, 1, "\tTrying color %d\n", col));
450 size = pu_try_color(pu, col, b_size);
452 /* did we find a better max ind. set? */
454 DBG((dbgphi, 1, "\t!! Better size: %d\n", size));
457 b_changes = pu->changed_nodes;
461 del_set(pu->changed_nodes);
464 /* reset the phi unit to original state for next color */
465 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
466 pu->conflict_count = pu->conflict_count_org;
468 /* shortcut: if all members can be colored we are (very) content */
469 if (b_size == pu->node_count)
473 /* now apply the found optimum */
476 DBG((dbgphi, 1, "\tBest color: %d Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count));
477 for (ns = set_first(b_changes); ns; ns = set_next(b_changes))
478 set_irn_color(ns->irn, ns->color);
481 DBG((dbgphi, 1, "\tBest color: none\n"));
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.
488 * General purpose version.
490 static void pu_coalesce_n_phi(phi_unit_t *pu) {
491 DBG((dbgphi, 1, "\n"));
496 * Prepares a phi class for further processing as a phi unit.
497 * @param pc The phi class to prepare.
498 * @return A so called phi unit containing some prepared informations
499 * needed by the following coalescing phase.
501 static phi_unit_t *new_pu(pset *pc) {
503 ir_node *n, *phi = NULL;
505 /* get the phi count of this class */
506 pu = calloc(1, sizeof(*pu));
507 for (n = pset_first(pc); n; n = pset_next(pc))
513 if (pu->phi_count == 1) {
520 /* build member set not containing phi interferers */
521 DBG((dbgphi, 1, "Phi-1 class:\n"));
522 pu->node_count = 1; /*for the phi*/
523 for (n = pset_first(pc); n; n = pset_next(pc)) {
526 if (!phi_ops_interfere(phi, n)) {
527 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
528 obstack_ptr_grow(&ob, n);
531 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
532 pset_insert_ptr(free_nodes, n);
535 tmp = obstack_finish(&ob);
536 pu->nodes = malloc(pu->node_count * sizeof(*pu->nodes));
538 memcpy(&pu->nodes[1], tmp, (pu->node_count-1) * sizeof(*tmp));
540 /* init conlict graph to life range interference */
541 for (i = 0; i < pu->node_count; ++i)
542 for (o = i+1; o < pu->node_count; ++o)
543 if (phi_ops_interfere(pu->nodes[i], pu->nodes[o]))
544 pu_add_conflict(pu, pu->nodes[i], pu->nodes[o]);
545 pu->conflict_count_org = pu->conflict_count;
547 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
549 obstack_free(&ob, NULL);
551 DBG((dbgphi, 1, "Phi-n class:\n"));
555 DBG((dbgphi, 1, "\n"));
563 static void free_pu(phi_unit_t *pu) {
564 if (pu->phi_count == 1) {
566 free(pu->changed_nodes);
576 void be_phi_coalesce(pset *all_phi_classes) {
579 pinned_global = pset_new_ptr(INITIAL_SLOTS_PINNED_GLOBAL);
580 free_nodes = pset_new_ptr(INITIAL_SLOTS_FREE_NODES);
582 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
583 phi_unit_t *pu = new_pu(pc);
584 if (pu->phi_count == 1)
585 pu_coalesce_1_phi(pu);
587 pu_coalesce_n_phi(pu);
591 del_pset(free_nodes);
592 del_pset(pinned_global);
596 void be_phi_coal_init(void) {
597 dbgphi = firm_dbg_register("ir.be.phicoal");
598 firm_dbg_set_mask(dbgphi, DEBUG_LVL);