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"
31 #define DBG_COALESCING 1
32 #define DBG_INTERFERENCES 2
35 static firm_dbg_module_t *dbg = NULL;
38 typedef struct _spill_t {
40 /** regclass of the spilled value */
41 const arch_register_class_t *cls;
42 /** index into spillslot_unionfind unionfind structure */
46 typedef struct _affinity_edge_t {
51 typedef struct _ss_env_t {
53 const arch_env_t *arch_env;
54 const be_chordal_env_t *chordal_env;
57 affinity_edge_t **affinity_edges;
61 /** Compare 2 affinity edges (used in quicksort) */
62 static int cmp_affinity(const void *d1, const void *d2) {
63 const affinity_edge_t * const *e1 = d1;
64 const affinity_edge_t * const *e2 = d2;
66 // sort in descending order
67 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
70 static int cmp_spill(const void* d1, const void* d2, size_t size) {
71 const spill_t* s1 = d1;
72 const spill_t* s2 = d2;
73 return s1->spill != s2->spill;
76 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
78 int hash = nodeset_hash(node);
81 res = set_find(env->spills, &spill, sizeof(spill), hash);
87 * ____ _ _ _ ____ _ _ _
88 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
89 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
90 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
91 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
95 static ir_node *get_memory_edge(const ir_node *node) {
98 arity = get_irn_arity(node);
99 for(i = arity - 1; i >= 0; --i) {
100 ir_node *arg = get_irn_n(node, i);
101 if(get_irn_mode(arg) == mode_M)
108 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
109 const arch_env_t *arch_env = env->arch_env;
110 const arch_register_class_t *cls;
112 int hash = nodeset_hash(node);
114 assert(arch_irn_class_is(arch_env, node, spill));
116 if(be_is_Spill(node)) {
117 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
119 // TODO add a way to detect the type of the spilled value
124 res = set_find(env->spills, &spill, sizeof(spill), hash);
127 spill.spillslot = set_count(env->spills);
129 res = set_insert(env->spills, &spill, sizeof(spill), hash);
135 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
138 int hash = nodeset_hash(node);
140 assert(is_Phi(node));
143 res = set_find(env->spills, &spill, sizeof(spill), hash);
148 spill.spillslot = set_count(env->spills);
150 res = set_insert(env->spills, &spill, sizeof(spill), hash);
152 // is 1 of the arguments a spill?
153 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
154 affinity_edge_t *affinty_edge;
155 ir_node* arg = get_irn_n(node, i);
158 if(be_is_Spill(arg)) {
159 arg_spill = collect_spill(env, arg);
161 // if it wasn't a spill then it must be a Mem-Phi
163 arg_spill = collect_memphi(env, arg);
166 if(res->cls == NULL) {
167 res->cls = arg_spill->cls;
169 assert(arg_spill->cls == NULL || res->cls == arg_spill->cls);
172 // add an affinity edge
173 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
174 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
175 affinty_edge->slot1 = res->spillslot;
176 affinty_edge->slot2 = arg_spill->spillslot;
177 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
184 * This walker function searches for reloads and collects all the spills
185 * and memphis attached to them.
187 static void collect_spills_walker(ir_node *node, void *data) {
188 ss_env_t *env = data;
189 const arch_env_t *arch_env = env->arch_env;
191 // @@@ ia32 classify returns classification of the irn the proj is attached
192 // too, why oh why?...
196 if(arch_irn_class_is(arch_env, node, reload)) {
197 ir_node *spillnode = get_memory_edge(node);
200 assert(spillnode != NULL);
202 if(is_Phi(spillnode)) {
203 spill = collect_memphi(env, spillnode);
205 spill = collect_spill(env, spillnode);
208 assert(!be_is_Reload(node) || spill->cls == arch_get_irn_reg_class(arch_env, node, -1));
209 ARR_APP1(ir_node*, env->reloads, node);
215 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
216 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
217 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
218 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
221 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
227 // merge spillslots and interferences
228 res = uf_union(spillslot_unionfind, s1, s2);
229 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
236 bitset_or(interferences[s1], interferences[s2]);
238 // update other interferences
239 spillcount = set_count(env->spills);
240 for(i = 0; i < spillcount; ++i) {
241 bitset_t *intfs = interferences[i];
242 if(bitset_is_set(intfs, s2))
243 bitset_set(intfs, s1);
251 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
259 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
261 f = fopen(name, "w");
264 fprintf(f, "graph: {\n");
266 spillcount = set_count(env->spills);
267 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
268 int slotid = spill->spillslot;
269 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
272 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
273 affinity_edge_t *edge = env->affinity_edges[i];
274 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
277 for(i = 0; i < spillcount; ++i) {
279 for(i2 = 0; i2 < spillcount; ++i2) {
280 if(bitset_is_set(interferences[i], i2)) {
281 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
290 static void show_stats(ss_env_t *env) {
296 spillcount = set_count(env->spills);
297 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
299 slotused = alloca(spillcount * sizeof(slotused[0]));
300 memset(slotused, 0, spillcount * sizeof(slotused[0]));
303 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
304 int slot = spill->spillslot;
305 if(slotused[slot] == 0) {
311 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
316 static void assign_spillslots(ss_env_t *env);
319 * A greedy coalescing algorithm for spillslots:
320 * 1. Sort the list of affinity edges
321 * 2. Try to merge slots with affinity edges (most expensive slots first)
322 * 3. Try to merge everything else that is possible
324 static void do_greedy_coalescing(ss_env_t *env)
330 int affinity_edge_count;
331 bitset_t **interferences;
332 int* spillslot_unionfind;
334 spillcount = set_count(env->spills);
338 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
340 interferences = alloca(spillcount * sizeof(interferences[0]));
341 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
342 spilllist = alloca(spillcount * sizeof(spilllist[0]));
344 uf_init(spillslot_unionfind, 0, spillcount);
347 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
350 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
351 assert(spill->spillslot < spillcount);
352 spilllist[spill->spillslot] = spill;
355 for(i = 0; i < spillcount; ++i) {
356 interferences[i] = bitset_alloca(spillcount);
359 // construct interferences
360 for(i = 0; i < spillcount; ++i) {
361 for(i2 = i+1; i2 < spillcount; ++i2) {
362 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
363 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
364 bitset_set(interferences[i], i2);
365 bitset_set(interferences[i2], i);
370 // sort affinity edges
371 affinity_edge_count = ARR_LEN(env->affinity_edges);
372 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
374 //dump_interference_graph(env, interferences, "before");
376 // try to merge affine nodes
377 for(i = 0; i < affinity_edge_count; ++i) {
378 const affinity_edge_t *edge = env->affinity_edges[i];
379 int s1 = uf_find(spillslot_unionfind, edge->slot1);
380 int s2 = uf_find(spillslot_unionfind, edge->slot2);
382 /* test if values interfere */
383 if(bitset_is_set(interferences[s1], s2)) {
384 assert(bitset_is_set(interferences[s2], s1));
388 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
390 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
393 // try to merge as much remaining spillslots as possible
394 for(i = 0; i < spillcount; ++i) {
395 int s1 = uf_find(spillslot_unionfind, i);
399 for(i2 = i+1; i2 < spillcount; ++i2) {
400 int s2 = uf_find(spillslot_unionfind, i2);
404 /* test if values interfere
405 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
406 * when node merging occurs
408 if(bitset_is_set(interferences[s1], s2)) {
409 assert(bitset_is_set(interferences[s2], s1));
413 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
415 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
416 // we can break the loop here, because s2 is the new supernode now
417 // and we'll test s2 again later anyway
423 // assign spillslots to spills
424 for(i = 0; i < spillcount; ++i) {
425 spill_t *spill = spilllist[i];
427 spill->spillslot = uf_find(spillslot_unionfind, i);
430 //dump_interference_graph(env, interferences, "after");
434 static void do_java_coalescing(ss_env_t *env)
440 be_java_coal_t *coal;
442 spillcount = set_count(env->spills);
446 spilllist = alloca(spillcount * sizeof(spilllist[0]));
449 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
452 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
454 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
455 assert(spill->spillslot < spillcount);
456 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
457 spilllist[spill->spillslot] = spill;
459 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
462 // construct interferences
463 for(i = 0; i < spillcount; ++i) {
464 for(i2 = i+1; i2 < spillcount; ++i2) {
465 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
466 be_java_coal_add_int_edge(coal, i, i2);
471 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
472 const affinity_edge_t *edge = env->affinity_edges[i];
475 int costs = (int) (edge->affinity * 10000);
476 be_java_coal_add_aff_edge(coal, n, m, costs);
479 be_java_coal_coalesce(coal);
481 // construct spillslots
482 for(i = 0; i < spillcount; ++i) {
483 spill_t *spill = spilllist[i];
484 spill->spillslot = be_java_coal_get_color(coal, i);
486 be_java_coal_destroy(coal);
492 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
493 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
494 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
495 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
499 typedef struct _spill_slot_t {
505 typedef struct _memperm_entry_t {
510 struct _memperm_entry_t *next;
513 typedef struct _memperm_t {
516 memperm_entry_t *entries;
519 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
520 const memperm_t* e1 = d1;
521 const memperm_t* e2 = d2;
522 return e1->block != e2->block;
525 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
526 memperm_t entry, *res;
530 hash = nodeset_hash(block);
532 res = set_find(env->memperms, &entry, sizeof(entry), hash);
535 entry.entrycount = 0;
536 entry.entries = NULL;
537 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
543 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
544 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
545 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
547 // adjust size of the entity type...
548 ir_type *enttype = get_entity_type(res);
549 set_type_size_bytes(enttype, slot->size);
556 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
557 const ir_mode *mode = arch_register_class_mode(spill->cls);
559 return get_mode_size_bytes(mode);
562 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
563 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
565 return arch_isa_get_reg_class_alignment(isa, spill->cls);
569 * Enlarges a spillslot (if necessary) so that it can carry a value of size
570 * @p othersize and alignment @p otheralign.
572 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
573 if(othersize > slot->size) {
574 slot->size = othersize;
576 if(otheralign > slot->align) {
577 if(otheralign % slot->align != 0)
578 slot->align *= otheralign;
580 slot->align = otheralign;
581 } else if(slot->align % otheralign != 0) {
582 slot->align *= otheralign;
587 * Create stack entities for the spillslots and assign them to the spill and
590 static void assign_spillslots(ss_env_t *env) {
591 const arch_env_t *arch_env = env->arch_env;
595 spill_slot_t* spillslots;
597 spillcount = set_count(env->spills);
598 spillslots = alloca(spillcount * sizeof(spillslots[0]));
600 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
602 // construct spillslots
603 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
604 int slotid = spill->spillslot;
605 spill_slot_t *slot = & (spillslots[slotid]);
606 int align = get_spillslotalign_for_spill(env, spill);
607 int size = get_spillslotsize_for_spill(env, spill);
609 if(slot->align == 0 && slot->size == 0) {
613 enlarge_spillslot(slot, align, size);
617 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
619 ir_node *node = spill->spill;
620 int slotid = spill->spillslot;
622 slot = &spillslots[slotid];
623 if(slot->entity == NULL) {
624 create_stack_entity(env, slot);
629 ir_node *block = get_nodes_block(node);
632 assert(is_Phi(node));
634 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
635 ir_node *arg = get_irn_n(node, i);
636 ir_node *predblock = get_Block_cfgpred_block(block, i);
640 argspill = get_spill(env, arg);
641 assert(argspill != NULL);
643 argslotid = argspill->spillslot;
644 if(slotid != argslotid) {
646 memperm_entry_t *entry;
647 spill_slot_t *argslot = &spillslots[argslotid];
648 if(argslot->entity == NULL) {
649 create_stack_entity(env, argslot);
652 memperm = get_memperm(env, predblock);
654 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
657 entry->in = argslot->entity;
658 entry->out = slot->entity;
659 entry->next = memperm->entries;
660 memperm->entrycount++;
661 memperm->entries = entry;
665 assert(arch_irn_class_is(arch_env, node, spill));
666 arch_set_frame_entity(arch_env, node, slot->entity);
670 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
671 ir_node* reload = env->reloads[i];
672 ir_node* spillnode = get_memory_edge(reload);
673 spill_t *spill = get_spill(env, spillnode);
674 const spill_slot_t *slot = & spillslots[spill->spillslot];
676 assert(slot->entity != NULL);
678 arch_set_frame_entity(arch_env, reload, slot->entity);
683 * Returns the last node in a block which is no control flow changing node
685 static ir_node *get_end_of_block_insertion_point(ir_node* block)
687 ir_node* ins = sched_last(block);
688 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
689 ins = sched_prev(ins);
695 ir_node *prev = sched_prev(ins);
705 static void create_memperms(ss_env_t *env) {
708 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
710 memperm_entry_t *entry;
712 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
713 ir_node* mempermnode;
715 assert(memperm->entrycount > 0);
717 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
718 ir_node* arg = get_irn_n(entry->node, entry->pos);
722 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
723 memperm->entrycount, nodes);
725 // insert node into schedule
726 blockend = get_end_of_block_insertion_point(memperm->block);
727 sched_add_before(blockend, mempermnode);
729 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
731 ir_node* arg = get_irn_n(entry->node, entry->pos);
733 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
734 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
735 set_irg_current_block(env->chordal_env->irg, memperm->block);
736 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
737 sched_add_before(blockend, proj);
739 set_irn_n(entry->node, entry->pos, proj);
744 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
747 obstack_init(&env.obst);
748 env.arch_env = chordal_env->birg->main_env->arch_env;
749 env.chordal_env = chordal_env;
750 env.spills = new_set(cmp_spill, 10);
751 env.reloads = NEW_ARR_F(ir_node*, 0);
752 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
753 env.memperms = new_set(cmp_memperm, 10);
754 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
755 //firm_dbg_set_mask(dbg, DBG_COALESCING);
757 /* Get initial spill slots */
758 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
760 do_greedy_coalescing(&env);
762 assign_spillslots(&env);
764 create_memperms(&env);
768 del_set(env.memperms);
769 DEL_ARR_F(env.reloads);
770 DEL_ARR_F(env.affinity_edges);
772 obstack_free(&env.obst, NULL);