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 *e1 = d1;
63 const affinity_edge_t *e2 = d2;
65 return e1->affinity < e2->affinity ? -1 : 1;
68 static int cmp_spill(const void* d1, const void* d2, size_t size) {
69 const spill_t* s1 = d1;
70 const spill_t* s2 = d2;
71 return s1->spill != s2->spill;
74 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
76 int hash = HASH_PTR(node);
79 res = set_find(env->spills, &spill, sizeof(spill), hash);
85 * ____ _ _ _ ____ _ _ _
86 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
87 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
88 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
89 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
93 static ir_node *get_memory_edge(const ir_node *node) {
96 arity = get_irn_arity(node);
97 for(i = arity - 1; i >= 0; --i) {
98 ir_node *arg = get_irn_n(node, i);
99 if(get_irn_mode(arg) == mode_M)
106 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
107 const arch_env_t *arch_env = env->arch_env;
108 const arch_register_class_t *cls;
110 int hash = HASH_PTR(node);
112 assert(arch_irn_class_is(arch_env, node, spill));
114 if(be_is_Spill(node)) {
115 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
117 // TODO add a way to detect the type of the spilled value
122 res = set_find(env->spills, &spill, sizeof(spill), hash);
125 spill.spillslot = set_count(env->spills);
127 res = set_insert(env->spills, &spill, sizeof(spill), hash);
133 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
136 int hash = HASH_PTR(node);
138 assert(is_Phi(node));
141 res = set_find(env->spills, &spill, sizeof(spill), hash);
146 spill.spillslot = set_count(env->spills);
148 res = set_insert(env->spills, &spill, sizeof(spill), hash);
150 // is 1 of the arguments a spill?
151 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
152 affinity_edge_t *affinty_edge;
153 ir_node* arg = get_irn_n(node, i);
156 if(be_is_Spill(arg)) {
157 arg_spill = collect_spill(env, arg);
159 // if it wasn't a spill then it must be a Mem-Phi
161 arg_spill = collect_memphi(env, arg);
164 if(res->cls == NULL) {
165 res->cls = arg_spill->cls;
167 assert(arg_spill->cls == NULL || res->cls == arg_spill->cls);
170 // add an affinity edge
171 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
172 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
173 affinty_edge->slot1 = res->spillslot;
174 affinty_edge->slot2 = arg_spill->spillslot;
175 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
182 * This walker function searches for reloads and collects all the spills
183 * and memphis attached to them.
185 static void collect_spills_walker(ir_node *node, void *data) {
186 ss_env_t *env = data;
187 const arch_env_t *arch_env = env->arch_env;
189 // @@@ ia32 classify returns classification of the irn the proj is attached
190 // too, why oh why?...
194 if(arch_irn_class_is(arch_env, node, reload)) {
195 ir_node *spillnode = get_memory_edge(node);
198 assert(spillnode != NULL);
200 if(is_Phi(spillnode)) {
201 spill = collect_memphi(env, spillnode);
203 spill = collect_spill(env, spillnode);
206 assert(!be_is_Reload(node) || spill->cls == arch_get_irn_reg_class(arch_env, node, -1));
207 ARR_APP1(ir_node*, env->reloads, node);
213 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
214 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
215 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
216 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
219 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
225 // merge spillslots and interferences
226 res = uf_union(spillslot_unionfind, s1, s2);
227 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
234 bitset_or(interferences[s1], interferences[s2]);
236 // update other interferences
237 spillcount = set_count(env->spills);
238 for(i = 0; i < spillcount; ++i) {
239 bitset_t *intfs = interferences[i];
240 if(bitset_is_set(intfs, s2))
241 bitset_set(intfs, s1);
249 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
257 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
259 f = fopen(name, "w");
262 fprintf(f, "graph: {\n");
264 spillcount = set_count(env->spills);
265 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
266 int slotid = spill->spillslot;
267 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
270 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
271 affinity_edge_t *edge = env->affinity_edges[i];
272 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
275 for(i = 0; i < spillcount; ++i) {
277 for(i2 = 0; i2 < spillcount; ++i2) {
278 if(bitset_is_set(interferences[i], i2)) {
279 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
288 static void show_stats(ss_env_t *env) {
294 spillcount = set_count(env->spills);
295 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
297 slotused = alloca(spillcount * sizeof(slotused[0]));
298 memset(slotused, 0, spillcount * sizeof(slotused[0]));
301 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
302 int slot = spill->spillslot;
303 if(slotused[slot] == 0) {
309 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
314 static void assign_spillslots(ss_env_t *env);
317 * A greedy coalescing algorithm for spillslots:
318 * 1. Sort the list of affinity edges
319 * 2. Try to merge slots with affinity edges (most expensive slots first)
320 * 3. Try to merge everything else that is possible
322 static void do_greedy_coalescing(ss_env_t *env)
328 int affinity_edge_count;
329 bitset_t **interferences;
330 int* spillslot_unionfind;
332 spillcount = set_count(env->spills);
336 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
338 interferences = alloca(spillcount * sizeof(interferences[0]));
339 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
340 spilllist = alloca(spillcount * sizeof(spilllist[0]));
342 uf_init(spillslot_unionfind, 0, spillcount);
345 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
348 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
349 assert(spill->spillslot < spillcount);
350 spilllist[spill->spillslot] = spill;
353 for(i = 0; i < spillcount; ++i) {
354 interferences[i] = bitset_alloca(spillcount);
357 // construct interferences
358 for(i = 0; i < spillcount; ++i) {
359 for(i2 = i+1; i2 < spillcount; ++i2) {
360 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
361 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
362 bitset_set(interferences[i], i2);
363 bitset_set(interferences[i2], i);
368 // sort affinity edges
369 affinity_edge_count = ARR_LEN(env->affinity_edges);
370 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
372 //dump_interference_graph(env, interferences, "before");
374 // try to merge affine nodes
375 for(i = 0; i < affinity_edge_count; ++i) {
376 const affinity_edge_t *edge = env->affinity_edges[i];
377 int s1 = uf_find(spillslot_unionfind, edge->slot1);
378 int s2 = uf_find(spillslot_unionfind, edge->slot2);
380 /* test if values interfere */
381 if(bitset_is_set(interferences[s1], s2)) {
382 assert(bitset_is_set(interferences[s2], s1));
386 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
388 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
391 // try to merge as much remaining spillslots as possible
392 for(i = 0; i < spillcount; ++i) {
393 int s1 = uf_find(spillslot_unionfind, i);
397 for(i2 = i+1; i2 < spillcount; ++i2) {
398 int s2 = uf_find(spillslot_unionfind, i2);
402 /* test if values interfere
403 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
404 * when node merging occurs
406 if(bitset_is_set(interferences[s1], s2)) {
407 assert(bitset_is_set(interferences[s2], s1));
411 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
413 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
414 // we can break the loop here, because s2 is the new supernode now
415 // and we'll test s2 again later anyway
421 // assign spillslots to spills
422 for(i = 0; i < spillcount; ++i) {
423 spill_t *spill = spilllist[i];
425 spill->spillslot = uf_find(spillslot_unionfind, i);
428 //dump_interference_graph(env, interferences, "after");
432 static void do_java_coalescing(ss_env_t *env)
438 be_java_coal_t *coal;
440 spillcount = set_count(env->spills);
444 spilllist = alloca(spillcount * sizeof(spilllist[0]));
447 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
450 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
452 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
453 assert(spill->spillslot < spillcount);
454 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
455 spilllist[spill->spillslot] = spill;
457 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
460 // construct interferences
461 for(i = 0; i < spillcount; ++i) {
462 for(i2 = i+1; i2 < spillcount; ++i2) {
463 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
464 be_java_coal_add_int_edge(coal, i, i2);
469 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
470 const affinity_edge_t *edge = env->affinity_edges[i];
473 int costs = (int) (edge->affinity * 10000);
474 be_java_coal_add_aff_edge(coal, n, m, costs);
477 be_java_coal_coalesce(coal);
479 // construct spillslots
480 for(i = 0; i < spillcount; ++i) {
481 spill_t *spill = spilllist[i];
482 spill->spillslot = be_java_coal_get_color(coal, i);
484 be_java_coal_destroy(coal);
490 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
491 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
492 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
493 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
497 typedef struct _spill_slot_t {
503 typedef struct _memperm_entry_t {
508 struct _memperm_entry_t *next;
511 typedef struct _memperm_t {
514 memperm_entry_t *entries;
517 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
518 const memperm_t* e1 = d1;
519 const memperm_t* e2 = d2;
520 return e1->block != e2->block;
523 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
524 memperm_t entry, *res;
528 hash = HASH_PTR(block);
530 res = set_find(env->memperms, &entry, sizeof(entry), hash);
533 entry.entrycount = 0;
534 entry.entries = NULL;
535 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
541 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
542 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
543 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
545 // adjust size of the entity type...
546 ir_type *enttype = get_entity_type(res);
547 set_type_size_bytes(enttype, slot->size);
554 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
555 const ir_mode *mode = arch_register_class_mode(spill->cls);
557 return get_mode_size_bytes(mode);
560 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
561 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
563 return arch_isa_get_reg_class_alignment(isa, spill->cls);
567 * Enlarges a spillslot (if necessary) so that it can carry a value of size
568 * @p othersize and alignment @p otheralign.
570 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
571 if(othersize > slot->size) {
572 slot->size = othersize;
574 if(otheralign > slot->align) {
575 if(otheralign % slot->align != 0)
576 slot->align *= otheralign;
578 slot->align = otheralign;
579 } else if(slot->align % otheralign != 0) {
580 slot->align *= otheralign;
585 * Create stack entities for the spillslots and assign them to the spill and
588 static void assign_spillslots(ss_env_t *env) {
589 const arch_env_t *arch_env = env->arch_env;
593 spill_slot_t* spillslots;
595 spillcount = set_count(env->spills);
596 spillslots = alloca(spillcount * sizeof(spillslots[0]));
598 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
600 // construct spillslots
601 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
602 int slotid = spill->spillslot;
603 spill_slot_t *slot = & (spillslots[slotid]);
604 int align = get_spillslotalign_for_spill(env, spill);
605 int size = get_spillslotsize_for_spill(env, spill);
607 if(slot->align == 0 && slot->size == 0) {
611 enlarge_spillslot(slot, align, size);
615 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
617 ir_node *node = spill->spill;
618 int slotid = spill->spillslot;
620 slot = &spillslots[slotid];
621 if(slot->entity == NULL) {
622 create_stack_entity(env, slot);
627 ir_node *block = get_nodes_block(node);
630 assert(is_Phi(node));
632 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
633 ir_node *arg = get_irn_n(node, i);
634 ir_node *predblock = get_Block_cfgpred_block(block, i);
638 argspill = get_spill(env, arg);
639 assert(argspill != NULL);
641 argslotid = argspill->spillslot;
642 if(slotid != argslotid) {
644 memperm_entry_t *entry;
645 spill_slot_t *argslot = &spillslots[argslotid];
646 if(argslot->entity == NULL) {
647 create_stack_entity(env, argslot);
650 memperm = get_memperm(env, predblock);
652 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
655 entry->in = argslot->entity;
656 entry->out = slot->entity;
657 entry->next = memperm->entries;
658 memperm->entrycount++;
659 memperm->entries = entry;
663 assert(arch_irn_class_is(arch_env, node, spill));
664 arch_set_frame_entity(arch_env, node, slot->entity);
668 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
669 ir_node* reload = env->reloads[i];
670 ir_node* spillnode = get_memory_edge(reload);
671 spill_t *spill = get_spill(env, spillnode);
672 const spill_slot_t *slot = & spillslots[spill->spillslot];
674 assert(slot->entity != NULL);
676 arch_set_frame_entity(arch_env, reload, slot->entity);
681 * Returns the last node in a block which is no control flow changing node
683 static ir_node *get_end_of_block_insertion_point(ir_node* block)
685 ir_node* ins = sched_last(block);
686 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
687 ins = sched_prev(ins);
693 ir_node *prev = sched_prev(ins);
703 static void create_memperms(ss_env_t *env) {
706 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
708 memperm_entry_t *entry;
710 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
711 ir_node* mempermnode;
713 assert(memperm->entrycount > 0);
715 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
716 ir_node* arg = get_irn_n(entry->node, entry->pos);
720 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
721 memperm->entrycount, nodes);
723 // insert node into schedule
724 blockend = get_end_of_block_insertion_point(memperm->block);
725 sched_add_before(blockend, mempermnode);
727 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
729 ir_node* arg = get_irn_n(entry->node, entry->pos);
731 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
732 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
733 set_irg_current_block(env->chordal_env->irg, memperm->block);
734 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
735 sched_add_before(blockend, proj);
737 set_irn_n(entry->node, entry->pos, proj);
742 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
745 obstack_init(&env.obst);
746 env.arch_env = chordal_env->birg->main_env->arch_env;
747 env.chordal_env = chordal_env;
748 env.spills = new_set(cmp_spill, 10);
749 env.reloads = NEW_ARR_F(ir_node*, 0);
750 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
751 env.memperms = new_set(cmp_memperm, 10);
752 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
753 //firm_dbg_set_mask(dbg, DBG_COALESCING);
755 /* Get initial spill slots */
756 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
758 do_greedy_coalescing(&env);
760 assign_spillslots(&env);
762 create_memperms(&env);
766 del_set(env.memperms);
767 DEL_ARR_F(env.reloads);
768 DEL_ARR_F(env.affinity_edges);
770 obstack_free(&env.obst, NULL);