2 * Author: Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 #include "unionfind.h"
24 #include "bespillslots.h"
25 #include "bechordal_t.h"
26 #include "bejavacoal.h"
29 #define DBG_COALESCING 1
30 #define DBG_INTERFERENCES 2
33 static firm_dbg_module_t *dbg = NULL;
36 typedef struct _spill_t {
38 /** regclass of the spilled value */
39 const arch_register_class_t *cls;
40 /** index into spillslot_unionfind unionfind structure */
44 typedef struct _affinity_edge_t {
49 typedef struct _ss_env_t {
51 const be_chordal_env_t *chordal_env;
54 affinity_edge_t **affinity_edges;
58 /** Compare 2 affinity edges (used in quicksort) */
59 static int cmp_affinity(const void *d1, const void *d2) {
60 const affinity_edge_t *e1 = d1;
61 const affinity_edge_t *e2 = d2;
63 return e1->affinity < e2->affinity ? -1 : 1;
66 int cmp_spill(const void* d1, const void* d2, size_t size) {
67 const spill_t* s1 = d1;
68 const spill_t* s2 = d2;
69 return s1->spill != s2->spill;
72 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
74 int hash = HASH_PTR(node);
77 res = set_find(env->spills, &spill, sizeof(spill), hash);
83 * ____ _ _ _ ____ _ _ _
84 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
85 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
86 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
87 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
91 static spill_t *collect_spill(ss_env_t *env, ir_node *node, const arch_register_class_t *cls) {
93 int hash = HASH_PTR(node);
95 assert(be_is_Spill(node));
98 res = set_find(env->spills, &spill, sizeof(spill), hash);
101 spill.spillslot = set_count(env->spills);
103 res = set_insert(env->spills, &spill, sizeof(spill), hash);
105 assert(cls == res->cls);
111 static spill_t *collect_memphi(ss_env_t *env, ir_node *node, const arch_register_class_t *cls) {
114 int hash = HASH_PTR(node);
116 assert(is_Phi(node));
119 res = set_find(env->spills, &spill, sizeof(spill), hash);
124 spill.spillslot = set_count(env->spills);
126 res = set_insert(env->spills, &spill, sizeof(spill), hash);
128 // is 1 of the arguments a spill?
129 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
130 affinity_edge_t *affinty_edge;
131 ir_node* arg = get_irn_n(node, i);
134 if(be_is_Spill(arg)) {
135 arg_spill = collect_spill(env, arg, cls);
137 // if it wasn't a spill then it must be a Mem-Phi
139 arg_spill = collect_memphi(env, arg, cls);
142 // add an affinity edge
143 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
144 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
145 affinty_edge->slot1 = res->spillslot;
146 affinty_edge->slot2 = arg_spill->spillslot;
147 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
154 * This walker function searches for reloads and collects all the spills
155 * and memphis attached to them.
157 static void collect_spills_walker(ir_node *node, void *data) {
158 ss_env_t *env = data;
160 if(be_is_Reload(node)) {
161 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
162 const arch_env_t *arch_env = env->chordal_env->birg->main_env->arch_env;
163 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, node, -1);
166 collect_memphi(env, spill, cls);
168 collect_spill(env, spill, cls);
170 ARR_APP1(ir_node*, env->reloads, node);
176 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
177 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
178 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
179 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
182 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
188 // merge spillslots and interferences
189 res = uf_union(spillslot_unionfind, s1, s2);
190 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
197 bitset_or(interferences[s1], interferences[s2]);
199 // update other interferences
200 spillcount = set_count(env->spills);
201 for(i = 0; i < spillcount; ++i) {
202 bitset_t *intfs = interferences[i];
203 if(bitset_is_set(intfs, s2))
204 bitset_set(intfs, s1);
212 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
220 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
222 f = fopen(name, "w");
225 fprintf(f, "graph: {\n");
227 spillcount = set_count(env->spills);
228 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
229 int slotid = spill->spillslot;
230 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
233 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
234 affinity_edge_t *edge = env->affinity_edges[i];
235 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
238 for(i = 0; i < spillcount; ++i) {
240 for(i2 = 0; i2 < spillcount; ++i2) {
241 if(bitset_is_set(interferences[i], i2)) {
242 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
251 static void show_stats(ss_env_t *env) {
257 spillcount = set_count(env->spills);
258 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
260 slotused = alloca(spillcount * sizeof(slotused[0]));
261 memset(slotused, 0, spillcount * sizeof(slotused[0]));
264 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
265 int slot = spill->spillslot;
266 if(slotused[slot] == 0) {
272 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
277 static void assign_spillslots(ss_env_t *env);
280 * A greedy coalescing algorithm for spillslots:
281 * 1. Sort the list of affinity edges
282 * 2. Try to merge slots with affinity edges (most expensive slots first)
283 * 3. Try to merge everything else that is possible
285 static void do_greedy_coalescing(ss_env_t *env)
291 int affinity_edge_count;
292 bitset_t **interferences;
293 int* spillslot_unionfind;
295 spillcount = set_count(env->spills);
299 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
301 interferences = alloca(spillcount * sizeof(interferences[0]));
302 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
303 spilllist = alloca(spillcount * sizeof(spilllist[0]));
305 uf_init(spillslot_unionfind, 0, spillcount);
308 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
311 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
312 assert(spill->spillslot < spillcount);
313 spilllist[spill->spillslot] = spill;
316 for(i = 0; i < spillcount; ++i) {
317 interferences[i] = bitset_alloca(spillcount);
320 // construct interferences
321 for(i = 0; i < spillcount; ++i) {
322 for(i2 = i+1; i2 < spillcount; ++i2) {
323 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
324 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
325 bitset_set(interferences[i], i2);
326 bitset_set(interferences[i2], i);
331 // sort affinity edges
332 affinity_edge_count = ARR_LEN(env->affinity_edges);
333 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
335 //dump_interference_graph(env, interferences, "before");
337 // try to merge affine nodes
338 for(i = 0; i < affinity_edge_count; ++i) {
339 const affinity_edge_t *edge = env->affinity_edges[i];
340 int s1 = uf_find(spillslot_unionfind, edge->slot1);
341 int s2 = uf_find(spillslot_unionfind, edge->slot2);
343 /* test if values interfere */
344 if(bitset_is_set(interferences[s1], s2)) {
345 assert(bitset_is_set(interferences[s2], s1));
349 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
351 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
354 // try to merge as much remaining spillslots as possible
355 for(i = 0; i < spillcount; ++i) {
356 int s1 = uf_find(spillslot_unionfind, i);
360 for(i2 = i+1; i2 < spillcount; ++i2) {
361 int s2 = uf_find(spillslot_unionfind, i2);
365 /* test if values interfere
366 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
367 * when node merging occurs
369 if(bitset_is_set(interferences[s1], s2)) {
370 assert(bitset_is_set(interferences[s2], s1));
374 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
376 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
377 // we can break the loop here, because s2 is the new supernode now
378 // and we'll test s2 again later anyway
385 for(i = 0; i < spillcount; ++i) {
386 spill_t *spill = spilllist[i];
388 spill->spillslot = uf_find(spillslot_unionfind, i);
391 //dump_interference_graph(env, interferences, "after");
394 static void do_java_coalescing(ss_env_t *env)
400 be_java_coal_t *coal;
402 spillcount = set_count(env->spills);
406 spilllist = alloca(spillcount * sizeof(spilllist[0]));
409 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
412 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
414 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
415 assert(spill->spillslot < spillcount);
416 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
417 spilllist[spill->spillslot] = spill;
419 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
422 // construct interferences
423 for(i = 0; i < spillcount; ++i) {
424 for(i2 = i+1; i2 < spillcount; ++i2) {
425 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
426 be_java_coal_add_int_edge(coal, i, i2);
431 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
432 const affinity_edge_t *edge = env->affinity_edges[i];
435 int costs = (int) (edge->affinity * 10000);
436 be_java_coal_add_aff_edge(coal, n, m, costs);
439 be_java_coal_coalesce(coal);
441 // construct spillslots
442 for(i = 0; i < spillcount; ++i) {
443 spill_t *spill = spilllist[i];
444 spill->spillslot = be_java_coal_get_color(coal, i);
446 be_java_coal_destroy(coal);
451 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
452 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
453 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
454 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
458 typedef struct _spill_slot_t {
464 typedef struct _memperm_entry_t {
469 struct _memperm_entry_t *next;
472 typedef struct _memperm_t {
475 memperm_entry_t *entries;
478 int cmp_memperm(const void* d1, const void* d2, size_t size) {
479 const memperm_t* e1 = d1;
480 const memperm_t* e2 = d2;
481 return e1->block != e2->block;
484 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
485 memperm_t entry, *res;
489 hash = HASH_PTR(block);
491 res = set_find(env->memperms, &entry, sizeof(entry), hash);
494 entry.entrycount = 0;
495 entry.entries = NULL;
496 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
502 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
503 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
504 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
511 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
512 const ir_mode *mode = arch_register_class_mode(spill->cls);
514 return get_mode_size_bytes(mode);
517 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
518 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
520 return arch_isa_get_reg_class_alignment(isa, spill->cls);
524 * Enlarges a spillslot (if necessary) so that it can carry a value of size
525 * @p othersize and alignment @p otheralign.
527 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
528 if(othersize > slot->size) {
529 slot->size = othersize;
531 if(otheralign > slot->align) {
532 if(otheralign % slot->align != 0)
533 slot->align *= otheralign;
535 slot->align = otheralign;
536 } else if(slot->align % otheralign != 0) {
537 slot->align *= otheralign;
542 * Create stack entities for the spillslots and assign them to the spill and
545 static void assign_spillslots(ss_env_t *env) {
549 spill_slot_t* spillslots;
551 spillcount = set_count(env->spills);
552 spillslots = alloca(spillcount * sizeof(spillslots[0]));
554 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
556 // construct spillslots
557 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
558 int slotid = spill->spillslot;
559 spill_slot_t *slot = & (spillslots[slotid]);
560 int align = get_spillslotalign_for_spill(env, spill);
561 int size = get_spillslotsize_for_spill(env, spill);
563 if(slot->align == 0 && slot->size == 0) {
567 enlarge_spillslot(slot, align, size);
571 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
573 ir_node *node = spill->spill;
574 int slotid = spill->spillslot;
576 slot = &spillslots[slotid];
577 if(slot->entity == NULL) {
578 create_stack_entity(env, slot);
581 if(be_is_Spill(node)) {
582 be_set_frame_entity(node, slot->entity);
587 assert(is_Phi(node));
589 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
590 ir_node *arg = get_irn_n(node, i);
594 argspill = get_spill(env, arg);
595 assert(argspill != NULL);
597 argslotid = argspill->spillslot;
598 if(slotid != argslotid) {
600 memperm_entry_t *entry;
601 spill_slot_t *argslot = &spillslots[argslotid];
602 if(argslot->entity == NULL) {
603 create_stack_entity(env, argslot);
606 memperm = get_memperm(env, get_nodes_block(arg));
608 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
611 entry->in = argslot->entity;
612 entry->out = slot->entity;
613 entry->next = memperm->entries;
614 memperm->entrycount++;
615 memperm->entries = entry;
621 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
622 const ir_node* reload = env->reloads[i];
623 ir_node* spillnode = get_irn_n(reload, be_pos_Reload_mem);
624 spill_t *spill = get_spill(env, spillnode);
625 const spill_slot_t *slot = & spillslots[spill->spillslot];
627 assert(slot->entity != NULL);
629 be_set_frame_entity(reload, slot->entity);
633 static void create_memperms(ss_env_t *env) {
636 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
638 memperm_entry_t *entry;
639 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
640 ir_node* mempermnode;
642 assert(memperm->entrycount > 0);
644 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
645 ir_node* arg = get_irn_n(entry->node, entry->pos);
649 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
650 memperm->entrycount, nodes);
652 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
654 ir_node* arg = get_irn_n(entry->node, entry->pos);
656 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
657 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
658 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
659 set_irn_n(arg, entry->pos, proj);
661 ir_printf("Memperm created in block %+F\n", memperm->block);
665 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
668 obstack_init(&env.obst);
669 env.chordal_env = chordal_env;
670 env.spills = new_set(cmp_spill, 10);
671 env.reloads = NEW_ARR_F(ir_node*, 0);
672 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
673 env.memperms = new_set(cmp_memperm, 10);
674 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
675 //firm_dbg_set_mask(dbg, DBG_COALESCING);
677 /* Get initial spill slots */
678 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
680 do_greedy_coalescing(&env);
681 //do_java_coalescing(&env);
683 assign_spillslots(&env);
685 create_memperms(&env);
689 del_set(env.memperms);
690 DEL_ARR_F(env.reloads);
691 DEL_ARR_F(env.affinity_edges);
693 obstack_free(&env.obst, NULL);