2 * Author: Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 #include "unionfind.h"
25 #include "bespillslots.h"
26 #include "bechordal_t.h"
27 #include "bejavacoal.h"
30 #define DBG_COALESCING 1
31 #define DBG_INTERFERENCES 2
34 static firm_dbg_module_t *dbg = NULL;
37 typedef struct _spill_t {
39 /** regclass of the spilled value */
40 const arch_register_class_t *cls;
41 /** index into spillslot_unionfind unionfind structure */
45 typedef struct _affinity_edge_t {
50 typedef struct _ss_env_t {
52 const arch_env_t *arch_env;
53 const be_chordal_env_t *chordal_env;
56 affinity_edge_t **affinity_edges;
60 /** Compare 2 affinity edges (used in quicksort) */
61 static int cmp_affinity(const void *d1, const void *d2) {
62 const affinity_edge_t * const *e1 = d1;
63 const affinity_edge_t * const *e2 = d2;
65 // sort in descending order
66 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
69 static int cmp_spill(const void* d1, const void* d2, size_t size) {
70 const spill_t* s1 = d1;
71 const spill_t* s2 = d2;
72 return s1->spill != s2->spill;
75 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
77 int hash = HASH_PTR(node);
80 res = set_find(env->spills, &spill, sizeof(spill), hash);
86 * ____ _ _ _ ____ _ _ _
87 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
88 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
89 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
90 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
94 static ir_node *get_memory_edge(const ir_node *node) {
97 arity = get_irn_arity(node);
98 for(i = arity - 1; i >= 0; --i) {
99 ir_node *arg = get_irn_n(node, i);
100 if(get_irn_mode(arg) == mode_M)
107 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
108 const arch_env_t *arch_env = env->arch_env;
109 const arch_register_class_t *cls;
111 int hash = HASH_PTR(node);
113 assert(arch_irn_class_is(arch_env, node, spill));
115 if(be_is_Spill(node)) {
116 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
118 // TODO add a way to detect the type of the spilled value
123 res = set_find(env->spills, &spill, sizeof(spill), hash);
126 spill.spillslot = set_count(env->spills);
128 res = set_insert(env->spills, &spill, sizeof(spill), hash);
134 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
137 int hash = HASH_PTR(node);
139 assert(is_Phi(node));
142 res = set_find(env->spills, &spill, sizeof(spill), hash);
147 spill.spillslot = set_count(env->spills);
149 res = set_insert(env->spills, &spill, sizeof(spill), hash);
151 // is 1 of the arguments a spill?
152 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
153 affinity_edge_t *affinty_edge;
154 ir_node* arg = get_irn_n(node, i);
157 if(be_is_Spill(arg)) {
158 arg_spill = collect_spill(env, arg);
160 // if it wasn't a spill then it must be a Mem-Phi
162 arg_spill = collect_memphi(env, arg);
165 if(res->cls == NULL) {
166 res->cls = arg_spill->cls;
168 assert(arg_spill->cls == NULL || res->cls == arg_spill->cls);
171 // add an affinity edge
172 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
173 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
174 affinty_edge->slot1 = res->spillslot;
175 affinty_edge->slot2 = arg_spill->spillslot;
176 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
183 * This walker function searches for reloads and collects all the spills
184 * and memphis attached to them.
186 static void collect_spills_walker(ir_node *node, void *data) {
187 ss_env_t *env = data;
188 const arch_env_t *arch_env = env->arch_env;
190 // @@@ ia32 classify returns classification of the irn the proj is attached
191 // too, why oh why?...
195 if(arch_irn_class_is(arch_env, node, reload)) {
196 ir_node *spillnode = get_memory_edge(node);
199 assert(spillnode != NULL);
201 if(is_Phi(spillnode)) {
202 spill = collect_memphi(env, spillnode);
204 spill = collect_spill(env, spillnode);
207 assert(!be_is_Reload(node) || spill->cls == arch_get_irn_reg_class(arch_env, node, -1));
208 ARR_APP1(ir_node*, env->reloads, node);
214 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
215 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
216 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
217 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
220 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
226 // merge spillslots and interferences
227 res = uf_union(spillslot_unionfind, s1, s2);
228 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
235 bitset_or(interferences[s1], interferences[s2]);
237 // update other interferences
238 spillcount = set_count(env->spills);
239 for(i = 0; i < spillcount; ++i) {
240 bitset_t *intfs = interferences[i];
241 if(bitset_is_set(intfs, s2))
242 bitset_set(intfs, s1);
250 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
258 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
260 f = fopen(name, "w");
263 fprintf(f, "graph: {\n");
265 spillcount = set_count(env->spills);
266 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
267 int slotid = spill->spillslot;
268 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
271 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
272 affinity_edge_t *edge = env->affinity_edges[i];
273 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
276 for(i = 0; i < spillcount; ++i) {
278 for(i2 = 0; i2 < spillcount; ++i2) {
279 if(bitset_is_set(interferences[i], i2)) {
280 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
289 static void show_stats(ss_env_t *env) {
295 spillcount = set_count(env->spills);
296 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
298 slotused = alloca(spillcount * sizeof(slotused[0]));
299 memset(slotused, 0, spillcount * sizeof(slotused[0]));
302 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
303 int slot = spill->spillslot;
304 if(slotused[slot] == 0) {
310 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
315 static void assign_spillslots(ss_env_t *env);
318 * A greedy coalescing algorithm for spillslots:
319 * 1. Sort the list of affinity edges
320 * 2. Try to merge slots with affinity edges (most expensive slots first)
321 * 3. Try to merge everything else that is possible
323 static void do_greedy_coalescing(ss_env_t *env)
329 int affinity_edge_count;
330 bitset_t **interferences;
331 int* spillslot_unionfind;
333 spillcount = set_count(env->spills);
337 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
339 interferences = alloca(spillcount * sizeof(interferences[0]));
340 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
341 spilllist = alloca(spillcount * sizeof(spilllist[0]));
343 uf_init(spillslot_unionfind, 0, spillcount);
346 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
349 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
350 assert(spill->spillslot < spillcount);
351 spilllist[spill->spillslot] = spill;
354 for(i = 0; i < spillcount; ++i) {
355 interferences[i] = bitset_alloca(spillcount);
358 // construct interferences
359 for(i = 0; i < spillcount; ++i) {
360 for(i2 = i+1; i2 < spillcount; ++i2) {
361 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
362 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
363 bitset_set(interferences[i], i2);
364 bitset_set(interferences[i2], i);
369 // sort affinity edges
370 affinity_edge_count = ARR_LEN(env->affinity_edges);
371 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
373 //dump_interference_graph(env, interferences, "before");
375 // try to merge affine nodes
376 for(i = 0; i < affinity_edge_count; ++i) {
377 const affinity_edge_t *edge = env->affinity_edges[i];
378 int s1 = uf_find(spillslot_unionfind, edge->slot1);
379 int s2 = uf_find(spillslot_unionfind, edge->slot2);
381 /* test if values interfere */
382 if(bitset_is_set(interferences[s1], s2)) {
383 assert(bitset_is_set(interferences[s2], s1));
387 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
389 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
392 // try to merge as much remaining spillslots as possible
393 for(i = 0; i < spillcount; ++i) {
394 int s1 = uf_find(spillslot_unionfind, i);
398 for(i2 = i+1; i2 < spillcount; ++i2) {
399 int s2 = uf_find(spillslot_unionfind, i2);
403 /* test if values interfere
404 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
405 * when node merging occurs
407 if(bitset_is_set(interferences[s1], s2)) {
408 assert(bitset_is_set(interferences[s2], s1));
412 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
414 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
415 // we can break the loop here, because s2 is the new supernode now
416 // and we'll test s2 again later anyway
422 // assign spillslots to spills
423 for(i = 0; i < spillcount; ++i) {
424 spill_t *spill = spilllist[i];
426 spill->spillslot = uf_find(spillslot_unionfind, i);
429 //dump_interference_graph(env, interferences, "after");
433 static void do_java_coalescing(ss_env_t *env)
439 be_java_coal_t *coal;
441 spillcount = set_count(env->spills);
445 spilllist = alloca(spillcount * sizeof(spilllist[0]));
448 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
451 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
453 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
454 assert(spill->spillslot < spillcount);
455 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
456 spilllist[spill->spillslot] = spill;
458 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
461 // construct interferences
462 for(i = 0; i < spillcount; ++i) {
463 for(i2 = i+1; i2 < spillcount; ++i2) {
464 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
465 be_java_coal_add_int_edge(coal, i, i2);
470 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
471 const affinity_edge_t *edge = env->affinity_edges[i];
474 int costs = (int) (edge->affinity * 10000);
475 be_java_coal_add_aff_edge(coal, n, m, costs);
478 be_java_coal_coalesce(coal);
480 // construct spillslots
481 for(i = 0; i < spillcount; ++i) {
482 spill_t *spill = spilllist[i];
483 spill->spillslot = be_java_coal_get_color(coal, i);
485 be_java_coal_destroy(coal);
491 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
492 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
493 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
494 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
498 typedef struct _spill_slot_t {
504 typedef struct _memperm_entry_t {
509 struct _memperm_entry_t *next;
512 typedef struct _memperm_t {
515 memperm_entry_t *entries;
518 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
519 const memperm_t* e1 = d1;
520 const memperm_t* e2 = d2;
521 return e1->block != e2->block;
524 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
525 memperm_t entry, *res;
529 hash = HASH_PTR(block);
531 res = set_find(env->memperms, &entry, sizeof(entry), hash);
534 entry.entrycount = 0;
535 entry.entries = NULL;
536 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
542 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
543 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
544 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
546 // adjust size of the entity type...
547 ir_type *enttype = get_entity_type(res);
548 set_type_size_bytes(enttype, slot->size);
555 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
556 const ir_mode *mode = arch_register_class_mode(spill->cls);
558 return get_mode_size_bytes(mode);
561 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
562 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
564 return arch_isa_get_reg_class_alignment(isa, spill->cls);
568 * Enlarges a spillslot (if necessary) so that it can carry a value of size
569 * @p othersize and alignment @p otheralign.
571 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
572 if(othersize > slot->size) {
573 slot->size = othersize;
575 if(otheralign > slot->align) {
576 if(otheralign % slot->align != 0)
577 slot->align *= otheralign;
579 slot->align = otheralign;
580 } else if(slot->align % otheralign != 0) {
581 slot->align *= otheralign;
586 * Create stack entities for the spillslots and assign them to the spill and
589 static void assign_spillslots(ss_env_t *env) {
590 const arch_env_t *arch_env = env->arch_env;
594 spill_slot_t* spillslots;
596 spillcount = set_count(env->spills);
597 spillslots = alloca(spillcount * sizeof(spillslots[0]));
599 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
601 // construct spillslots
602 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
603 int slotid = spill->spillslot;
604 spill_slot_t *slot = & (spillslots[slotid]);
605 int align = get_spillslotalign_for_spill(env, spill);
606 int size = get_spillslotsize_for_spill(env, spill);
608 if(slot->align == 0 && slot->size == 0) {
612 enlarge_spillslot(slot, align, size);
616 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
618 ir_node *node = spill->spill;
619 int slotid = spill->spillslot;
621 slot = &spillslots[slotid];
622 if(slot->entity == NULL) {
623 create_stack_entity(env, slot);
628 ir_node *block = get_nodes_block(node);
631 assert(is_Phi(node));
633 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
634 ir_node *arg = get_irn_n(node, i);
635 ir_node *predblock = get_Block_cfgpred_block(block, i);
639 argspill = get_spill(env, arg);
640 assert(argspill != NULL);
642 argslotid = argspill->spillslot;
643 if(slotid != argslotid) {
645 memperm_entry_t *entry;
646 spill_slot_t *argslot = &spillslots[argslotid];
647 if(argslot->entity == NULL) {
648 create_stack_entity(env, argslot);
651 memperm = get_memperm(env, predblock);
653 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
656 entry->in = argslot->entity;
657 entry->out = slot->entity;
658 entry->next = memperm->entries;
659 memperm->entrycount++;
660 memperm->entries = entry;
664 assert(arch_irn_class_is(arch_env, node, spill));
665 arch_set_frame_entity(arch_env, node, slot->entity);
669 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
670 ir_node* reload = env->reloads[i];
671 ir_node* spillnode = get_memory_edge(reload);
672 spill_t *spill = get_spill(env, spillnode);
673 const spill_slot_t *slot = & spillslots[spill->spillslot];
675 assert(slot->entity != NULL);
677 arch_set_frame_entity(arch_env, reload, slot->entity);
682 * Returns the last node in a block which is no control flow changing node
684 static ir_node *get_end_of_block_insertion_point(ir_node* block)
686 ir_node* ins = sched_last(block);
687 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
688 ins = sched_prev(ins);
694 ir_node *prev = sched_prev(ins);
704 static void create_memperms(ss_env_t *env) {
707 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
709 memperm_entry_t *entry;
711 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
712 ir_node* mempermnode;
714 assert(memperm->entrycount > 0);
716 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
717 ir_node* arg = get_irn_n(entry->node, entry->pos);
721 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
722 memperm->entrycount, nodes);
724 // insert node into schedule
725 blockend = get_end_of_block_insertion_point(memperm->block);
726 sched_add_before(blockend, mempermnode);
728 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
730 ir_node* arg = get_irn_n(entry->node, entry->pos);
732 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
733 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
734 set_irg_current_block(env->chordal_env->irg, memperm->block);
735 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
736 sched_add_before(blockend, proj);
738 set_irn_n(entry->node, entry->pos, proj);
743 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
746 obstack_init(&env.obst);
747 env.arch_env = chordal_env->birg->main_env->arch_env;
748 env.chordal_env = chordal_env;
749 env.spills = new_set(cmp_spill, 10);
750 env.reloads = NEW_ARR_F(ir_node*, 0);
751 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
752 env.memperms = new_set(cmp_memperm, 10);
753 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
754 //firm_dbg_set_mask(dbg, DBG_COALESCING);
756 /* Get initial spill slots */
757 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
759 do_greedy_coalescing(&env);
761 assign_spillslots(&env);
763 create_memperms(&env);
767 del_set(env.memperms);
768 DEL_ARR_F(env.reloads);
769 DEL_ARR_F(env.affinity_edges);
771 obstack_free(&env.obst, NULL);