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");
395 static void do_java_coalescing(ss_env_t *env)
401 be_java_coal_t *coal;
403 spillcount = set_count(env->spills);
407 spilllist = alloca(spillcount * sizeof(spilllist[0]));
410 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
413 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
415 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
416 assert(spill->spillslot < spillcount);
417 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
418 spilllist[spill->spillslot] = spill;
420 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
423 // construct interferences
424 for(i = 0; i < spillcount; ++i) {
425 for(i2 = i+1; i2 < spillcount; ++i2) {
426 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
427 be_java_coal_add_int_edge(coal, i, i2);
432 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
433 const affinity_edge_t *edge = env->affinity_edges[i];
436 int costs = (int) (edge->affinity * 10000);
437 be_java_coal_add_aff_edge(coal, n, m, costs);
440 be_java_coal_coalesce(coal);
442 // construct spillslots
443 for(i = 0; i < spillcount; ++i) {
444 spill_t *spill = spilllist[i];
445 spill->spillslot = be_java_coal_get_color(coal, i);
447 be_java_coal_destroy(coal);
453 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
454 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
455 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
456 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
460 typedef struct _spill_slot_t {
466 typedef struct _memperm_entry_t {
471 struct _memperm_entry_t *next;
474 typedef struct _memperm_t {
477 memperm_entry_t *entries;
480 int cmp_memperm(const void* d1, const void* d2, size_t size) {
481 const memperm_t* e1 = d1;
482 const memperm_t* e2 = d2;
483 return e1->block != e2->block;
486 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
487 memperm_t entry, *res;
491 hash = HASH_PTR(block);
493 res = set_find(env->memperms, &entry, sizeof(entry), hash);
496 entry.entrycount = 0;
497 entry.entries = NULL;
498 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
504 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
505 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
506 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
513 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
514 const ir_mode *mode = arch_register_class_mode(spill->cls);
516 return get_mode_size_bytes(mode);
519 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
520 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
522 return arch_isa_get_reg_class_alignment(isa, spill->cls);
526 * Enlarges a spillslot (if necessary) so that it can carry a value of size
527 * @p othersize and alignment @p otheralign.
529 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
530 if(othersize > slot->size) {
531 slot->size = othersize;
533 if(otheralign > slot->align) {
534 if(otheralign % slot->align != 0)
535 slot->align *= otheralign;
537 slot->align = otheralign;
538 } else if(slot->align % otheralign != 0) {
539 slot->align *= otheralign;
544 * Create stack entities for the spillslots and assign them to the spill and
547 static void assign_spillslots(ss_env_t *env) {
551 spill_slot_t* spillslots;
553 spillcount = set_count(env->spills);
554 spillslots = alloca(spillcount * sizeof(spillslots[0]));
556 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
558 // construct spillslots
559 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
560 int slotid = spill->spillslot;
561 spill_slot_t *slot = & (spillslots[slotid]);
562 int align = get_spillslotalign_for_spill(env, spill);
563 int size = get_spillslotsize_for_spill(env, spill);
565 if(slot->align == 0 && slot->size == 0) {
569 enlarge_spillslot(slot, align, size);
573 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
575 ir_node *node = spill->spill;
576 int slotid = spill->spillslot;
578 slot = &spillslots[slotid];
579 if(slot->entity == NULL) {
580 create_stack_entity(env, slot);
583 if(be_is_Spill(node)) {
584 be_set_frame_entity(node, slot->entity);
589 assert(is_Phi(node));
591 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
592 ir_node *arg = get_irn_n(node, i);
596 argspill = get_spill(env, arg);
597 assert(argspill != NULL);
599 argslotid = argspill->spillslot;
600 if(slotid != argslotid) {
602 memperm_entry_t *entry;
603 spill_slot_t *argslot = &spillslots[argslotid];
604 if(argslot->entity == NULL) {
605 create_stack_entity(env, argslot);
608 memperm = get_memperm(env, get_nodes_block(arg));
610 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
613 entry->in = argslot->entity;
614 entry->out = slot->entity;
615 entry->next = memperm->entries;
616 memperm->entrycount++;
617 memperm->entries = entry;
623 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
624 const ir_node* reload = env->reloads[i];
625 ir_node* spillnode = get_irn_n(reload, be_pos_Reload_mem);
626 spill_t *spill = get_spill(env, spillnode);
627 const spill_slot_t *slot = & spillslots[spill->spillslot];
629 assert(slot->entity != NULL);
631 be_set_frame_entity(reload, slot->entity);
635 static void create_memperms(ss_env_t *env) {
638 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
640 memperm_entry_t *entry;
641 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
642 ir_node* mempermnode;
644 assert(memperm->entrycount > 0);
646 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
647 ir_node* arg = get_irn_n(entry->node, entry->pos);
651 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
652 memperm->entrycount, nodes);
654 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
656 ir_node* arg = get_irn_n(entry->node, entry->pos);
658 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
659 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
660 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
661 set_irn_n(arg, entry->pos, proj);
663 ir_printf("Memperm created in block %+F\n", memperm->block);
667 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
670 obstack_init(&env.obst);
671 env.chordal_env = chordal_env;
672 env.spills = new_set(cmp_spill, 10);
673 env.reloads = NEW_ARR_F(ir_node*, 0);
674 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
675 env.memperms = new_set(cmp_memperm, 10);
676 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
677 //firm_dbg_set_mask(dbg, DBG_COALESCING);
679 /* Get initial spill slots */
680 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
682 do_greedy_coalescing(&env);
684 assign_spillslots(&env);
686 create_memperms(&env);
690 del_set(env.memperms);
691 DEL_ARR_F(env.reloads);
692 DEL_ARR_F(env.affinity_edges);
694 obstack_free(&env.obst, NULL);