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"
28 #include "benodesets.h"
29 #include "bestatevent.h"
32 #define DBG_COALESCING 1
33 #define DBG_INTERFERENCES 2
36 static firm_dbg_module_t *dbg = NULL;
39 typedef struct _spill_t {
41 /** regclass of the spilled value */
42 const arch_register_class_t *cls;
43 /** index into spillslot_unionfind unionfind structure */
47 typedef struct _affinity_edge_t {
52 typedef struct _ss_env_t {
54 const arch_env_t *arch_env;
55 const be_chordal_env_t *chordal_env;
58 affinity_edge_t **affinity_edges;
62 /** Compare 2 affinity edges (used in quicksort) */
63 static int cmp_affinity(const void *d1, const void *d2) {
64 const affinity_edge_t * const *e1 = d1;
65 const affinity_edge_t * const *e2 = d2;
67 // sort in descending order
68 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
71 static int cmp_spill(const void* d1, const void* d2, size_t size) {
72 const spill_t* s1 = d1;
73 const spill_t* s2 = d2;
74 return s1->spill != s2->spill;
77 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
79 int hash = nodeset_hash(node);
82 res = set_find(env->spills, &spill, sizeof(spill), hash);
88 * ____ _ _ _ ____ _ _ _
89 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
90 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
91 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
92 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
96 static ir_node *get_memory_edge(const ir_node *node) {
99 arity = get_irn_arity(node);
100 for(i = arity - 1; i >= 0; --i) {
101 ir_node *arg = get_irn_n(node, i);
102 if(get_irn_mode(arg) == mode_M)
109 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
110 const arch_env_t *arch_env = env->arch_env;
111 const arch_register_class_t *cls;
113 int hash = nodeset_hash(node);
115 assert(arch_irn_class_is(arch_env, node, spill));
117 if(be_is_Spill(node)) {
118 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
120 // TODO add a way to detect the type of the spilled value
125 res = set_find(env->spills, &spill, sizeof(spill), hash);
128 spill.spillslot = set_count(env->spills);
130 res = set_insert(env->spills, &spill, sizeof(spill), hash);
136 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
139 int hash = nodeset_hash(node);
141 assert(is_Phi(node));
144 res = set_find(env->spills, &spill, sizeof(spill), hash);
149 spill.spillslot = set_count(env->spills);
151 res = set_insert(env->spills, &spill, sizeof(spill), hash);
153 // is 1 of the arguments a spill?
154 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
155 affinity_edge_t *affinty_edge;
156 ir_node* arg = get_irn_n(node, i);
159 if(be_is_Spill(arg)) {
160 arg_spill = collect_spill(env, arg);
162 // if it wasn't a spill then it must be a Mem-Phi
164 arg_spill = collect_memphi(env, arg);
167 if(res->cls == NULL) {
168 res->cls = arg_spill->cls;
170 assert(arg_spill->cls == NULL || res->cls == arg_spill->cls);
173 // add an affinity edge
174 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
175 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
176 affinty_edge->slot1 = res->spillslot;
177 affinty_edge->slot2 = arg_spill->spillslot;
178 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
185 * This walker function searches for reloads and collects all the spills
186 * and memphis attached to them.
188 static void collect_spills_walker(ir_node *node, void *data) {
189 ss_env_t *env = data;
190 const arch_env_t *arch_env = env->arch_env;
192 // @@@ ia32 classify returns classification of the irn the proj is attached
193 // too, why oh why?...
197 if(arch_irn_class_is(arch_env, node, reload)) {
198 ir_node *spillnode = get_memory_edge(node);
201 assert(spillnode != NULL);
203 if(is_Phi(spillnode)) {
204 spill = collect_memphi(env, spillnode);
206 spill = collect_spill(env, spillnode);
209 assert(!be_is_Reload(node) || spill->cls == arch_get_irn_reg_class(arch_env, node, -1));
210 ARR_APP1(ir_node*, env->reloads, node);
216 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
217 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
218 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
219 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
222 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
228 // merge spillslots and interferences
229 res = uf_union(spillslot_unionfind, s1, s2);
230 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
237 bitset_or(interferences[s1], interferences[s2]);
239 // update other interferences
240 spillcount = set_count(env->spills);
241 for(i = 0; i < spillcount; ++i) {
242 bitset_t *intfs = interferences[i];
243 if(bitset_is_set(intfs, s2))
244 bitset_set(intfs, s1);
252 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
260 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
262 f = fopen(name, "w");
265 fprintf(f, "graph: {\n");
267 spillcount = set_count(env->spills);
268 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
269 int slotid = spill->spillslot;
270 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
273 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
274 affinity_edge_t *edge = env->affinity_edges[i];
275 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
278 for(i = 0; i < spillcount; ++i) {
280 for(i2 = 0; i2 < spillcount; ++i2) {
281 if(bitset_is_set(interferences[i], i2)) {
282 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
291 static void show_stats(ss_env_t *env) {
297 spillcount = set_count(env->spills);
298 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
300 slotused = alloca(spillcount * sizeof(slotused[0]));
301 memset(slotused, 0, spillcount * sizeof(slotused[0]));
304 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
305 int slot = spill->spillslot;
306 if(slotused[slot] == 0) {
312 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
317 static void assign_spillslots(ss_env_t *env);
320 * A greedy coalescing algorithm for spillslots:
321 * 1. Sort the list of affinity edges
322 * 2. Try to merge slots with affinity edges (most expensive slots first)
323 * 3. Try to merge everything else that is possible
325 static void do_greedy_coalescing(ss_env_t *env)
331 int affinity_edge_count;
332 bitset_t **interferences;
333 int* spillslot_unionfind;
335 spillcount = set_count(env->spills);
339 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
341 interferences = alloca(spillcount * sizeof(interferences[0]));
342 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
343 spilllist = alloca(spillcount * sizeof(spilllist[0]));
345 uf_init(spillslot_unionfind, 0, spillcount);
348 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
351 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
352 assert(spill->spillslot < spillcount);
353 spilllist[spill->spillslot] = spill;
356 for(i = 0; i < spillcount; ++i) {
357 interferences[i] = bitset_alloca(spillcount);
360 // construct interferences
361 for(i = 0; i < spillcount; ++i) {
362 for(i2 = i+1; i2 < spillcount; ++i2) {
363 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
364 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
365 bitset_set(interferences[i], i2);
366 bitset_set(interferences[i2], i);
371 // sort affinity edges
372 affinity_edge_count = ARR_LEN(env->affinity_edges);
373 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
375 //dump_interference_graph(env, interferences, "before");
377 // try to merge affine nodes
378 for(i = 0; i < affinity_edge_count; ++i) {
379 const affinity_edge_t *edge = env->affinity_edges[i];
380 int s1 = uf_find(spillslot_unionfind, edge->slot1);
381 int s2 = uf_find(spillslot_unionfind, edge->slot2);
383 /* test if values interfere */
384 if(bitset_is_set(interferences[s1], s2)) {
385 assert(bitset_is_set(interferences[s2], s1));
389 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
391 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
394 // try to merge as much remaining spillslots as possible
395 for(i = 0; i < spillcount; ++i) {
396 int s1 = uf_find(spillslot_unionfind, i);
400 for(i2 = i+1; i2 < spillcount; ++i2) {
401 int s2 = uf_find(spillslot_unionfind, i2);
405 /* test if values interfere
406 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
407 * when node merging occurs
409 if(bitset_is_set(interferences[s1], s2)) {
410 assert(bitset_is_set(interferences[s2], s1));
414 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
416 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
417 // we can break the loop here, because s2 is the new supernode now
418 // and we'll test s2 again later anyway
424 // assign spillslots to spills
425 for(i = 0; i < spillcount; ++i) {
426 spill_t *spill = spilllist[i];
428 spill->spillslot = uf_find(spillslot_unionfind, i);
431 //dump_interference_graph(env, interferences, "after");
435 static void do_java_coalescing(ss_env_t *env)
441 be_java_coal_t *coal;
443 spillcount = set_count(env->spills);
447 spilllist = alloca(spillcount * sizeof(spilllist[0]));
450 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
453 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
455 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
456 assert(spill->spillslot < spillcount);
457 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
458 spilllist[spill->spillslot] = spill;
460 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
463 // construct interferences
464 for(i = 0; i < spillcount; ++i) {
465 for(i2 = i+1; i2 < spillcount; ++i2) {
466 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
467 be_java_coal_add_int_edge(coal, i, i2);
472 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
473 const affinity_edge_t *edge = env->affinity_edges[i];
476 int costs = (int) (edge->affinity * 10000);
477 be_java_coal_add_aff_edge(coal, n, m, costs);
480 be_java_coal_coalesce(coal);
482 // construct spillslots
483 for(i = 0; i < spillcount; ++i) {
484 spill_t *spill = spilllist[i];
485 spill->spillslot = be_java_coal_get_color(coal, i);
487 be_java_coal_destroy(coal);
493 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
494 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
495 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
496 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
500 typedef struct _spill_slot_t {
506 typedef struct _memperm_entry_t {
511 struct _memperm_entry_t *next;
514 typedef struct _memperm_t {
517 memperm_entry_t *entries;
520 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
521 const memperm_t* e1 = d1;
522 const memperm_t* e2 = d2;
523 return e1->block != e2->block;
526 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
527 memperm_t entry, *res;
531 hash = nodeset_hash(block);
533 res = set_find(env->memperms, &entry, sizeof(entry), hash);
536 entry.entrycount = 0;
537 entry.entries = NULL;
538 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
544 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
545 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
546 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
548 // adjust size of the entity type...
549 ir_type *enttype = get_entity_type(res);
550 set_type_size_bytes(enttype, slot->size);
557 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
558 const ir_mode *mode = arch_register_class_mode(spill->cls);
560 return get_mode_size_bytes(mode);
563 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
564 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
566 return arch_isa_get_reg_class_alignment(isa, spill->cls);
570 * Enlarges a spillslot (if necessary) so that it can carry a value of size
571 * @p othersize and alignment @p otheralign.
573 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
574 if(othersize > slot->size) {
575 slot->size = othersize;
577 if(otheralign > slot->align) {
578 if(otheralign % slot->align != 0)
579 slot->align *= otheralign;
581 slot->align = otheralign;
582 } else if(slot->align % otheralign != 0) {
583 slot->align *= otheralign;
588 * Create stack entities for the spillslots and assign them to the spill and
591 static void assign_spillslots(ss_env_t *env) {
592 const arch_env_t *arch_env = env->arch_env;
596 spill_slot_t* spillslots;
598 spillcount = set_count(env->spills);
599 spillslots = alloca(spillcount * sizeof(spillslots[0]));
601 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
603 // construct spillslots
604 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
605 int slotid = spill->spillslot;
606 spill_slot_t *slot = & (spillslots[slotid]);
607 int align = get_spillslotalign_for_spill(env, spill);
608 int size = get_spillslotsize_for_spill(env, spill);
610 if(slot->align == 0 && slot->size == 0) {
614 enlarge_spillslot(slot, align, size);
618 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
620 ir_node *node = spill->spill;
621 int slotid = spill->spillslot;
623 slot = &spillslots[slotid];
624 if(slot->entity == NULL) {
625 create_stack_entity(env, slot);
630 ir_node *block = get_nodes_block(node);
633 assert(is_Phi(node));
635 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
636 ir_node *arg = get_irn_n(node, i);
637 ir_node *predblock = get_Block_cfgpred_block(block, i);
641 argspill = get_spill(env, arg);
642 assert(argspill != NULL);
644 argslotid = argspill->spillslot;
645 if(slotid != argslotid) {
647 memperm_entry_t *entry;
648 spill_slot_t *argslot = &spillslots[argslotid];
649 if(argslot->entity == NULL) {
650 create_stack_entity(env, argslot);
653 memperm = get_memperm(env, predblock);
655 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
658 entry->in = argslot->entity;
659 entry->out = slot->entity;
660 entry->next = memperm->entries;
661 memperm->entrycount++;
662 memperm->entries = entry;
666 assert(arch_irn_class_is(arch_env, node, spill));
667 arch_set_frame_entity(arch_env, node, slot->entity);
671 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
672 ir_node* reload = env->reloads[i];
673 ir_node* spillnode = get_memory_edge(reload);
674 spill_t *spill = get_spill(env, spillnode);
675 const spill_slot_t *slot = & spillslots[spill->spillslot];
677 assert(slot->entity != NULL);
679 arch_set_frame_entity(arch_env, reload, slot->entity);
684 * Returns the last node in a block which is no control flow changing node
686 static ir_node *get_end_of_block_insertion_point(ir_node* block)
688 ir_node* ins = sched_last(block);
689 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
690 ins = sched_prev(ins);
696 ir_node *prev = sched_prev(ins);
706 static void create_memperms(ss_env_t *env) {
709 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
711 memperm_entry_t *entry;
713 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
714 ir_node* mempermnode;
716 assert(memperm->entrycount > 0);
718 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
719 ir_node* arg = get_irn_n(entry->node, entry->pos);
723 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
724 memperm->entrycount, nodes);
726 // insert node into schedule
727 blockend = get_end_of_block_insertion_point(memperm->block);
728 sched_add_before(blockend, mempermnode);
729 be_stat_ev("mem_perm", memperm->entrycount);
731 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
733 ir_node* arg = get_irn_n(entry->node, entry->pos);
735 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
736 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
737 set_irg_current_block(env->chordal_env->irg, memperm->block);
738 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
739 sched_add_before(blockend, proj);
741 set_irn_n(entry->node, entry->pos, proj);
746 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env, int coalesce_spillslots) {
749 obstack_init(&env.obst);
750 env.arch_env = chordal_env->birg->main_env->arch_env;
751 env.chordal_env = chordal_env;
752 env.spills = new_set(cmp_spill, 10);
753 env.reloads = NEW_ARR_F(ir_node*, 0);
754 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
755 env.memperms = new_set(cmp_memperm, 10);
756 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
757 //firm_dbg_set_mask(dbg, DBG_COALESCING);
759 /* Get initial spill slots */
760 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
762 if(coalesce_spillslots)
763 do_greedy_coalescing(&env);
765 assign_spillslots(&env);
767 create_memperms(&env);
771 del_set(env.memperms);
772 DEL_ARR_F(env.reloads);
773 DEL_ARR_F(env.affinity_edges);
775 obstack_free(&env.obst, NULL);