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 be_chordal_env_t *chordal_env;
55 affinity_edge_t **affinity_edges;
59 /** Compare 2 affinity edges (used in quicksort) */
60 static int cmp_affinity(const void *d1, const void *d2) {
61 const affinity_edge_t *e1 = d1;
62 const affinity_edge_t *e2 = d2;
64 return e1->affinity < e2->affinity ? -1 : 1;
67 static int cmp_spill(const void* d1, const void* d2, size_t size) {
68 const spill_t* s1 = d1;
69 const spill_t* s2 = d2;
70 return s1->spill != s2->spill;
73 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
75 int hash = HASH_PTR(node);
78 res = set_find(env->spills, &spill, sizeof(spill), hash);
84 * ____ _ _ _ ____ _ _ _
85 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
86 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
87 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
88 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
92 static spill_t *collect_spill(ss_env_t *env, ir_node *node, const arch_register_class_t *cls) {
94 int hash = HASH_PTR(node);
96 assert(be_is_Spill(node));
99 res = set_find(env->spills, &spill, sizeof(spill), hash);
102 spill.spillslot = set_count(env->spills);
104 res = set_insert(env->spills, &spill, sizeof(spill), hash);
106 assert(cls == res->cls);
112 static spill_t *collect_memphi(ss_env_t *env, ir_node *node, const arch_register_class_t *cls) {
115 int hash = HASH_PTR(node);
117 assert(is_Phi(node));
120 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);
129 // is 1 of the arguments a spill?
130 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
131 affinity_edge_t *affinty_edge;
132 ir_node* arg = get_irn_n(node, i);
135 if(be_is_Spill(arg)) {
136 arg_spill = collect_spill(env, arg, cls);
138 // if it wasn't a spill then it must be a Mem-Phi
140 arg_spill = collect_memphi(env, arg, cls);
143 // add an affinity edge
144 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
145 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
146 affinty_edge->slot1 = res->spillslot;
147 affinty_edge->slot2 = arg_spill->spillslot;
148 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
155 * This walker function searches for reloads and collects all the spills
156 * and memphis attached to them.
158 static void collect_spills_walker(ir_node *node, void *data) {
159 ss_env_t *env = data;
161 if(be_is_Reload(node)) {
162 ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
163 const arch_env_t *arch_env = env->chordal_env->birg->main_env->arch_env;
164 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, node, -1);
167 collect_memphi(env, spill, cls);
169 collect_spill(env, spill, cls);
171 ARR_APP1(ir_node*, env->reloads, node);
177 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
178 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
179 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
180 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
183 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
189 // merge spillslots and interferences
190 res = uf_union(spillslot_unionfind, s1, s2);
191 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
198 bitset_or(interferences[s1], interferences[s2]);
200 // update other interferences
201 spillcount = set_count(env->spills);
202 for(i = 0; i < spillcount; ++i) {
203 bitset_t *intfs = interferences[i];
204 if(bitset_is_set(intfs, s2))
205 bitset_set(intfs, s1);
213 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
221 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
223 f = fopen(name, "w");
226 fprintf(f, "graph: {\n");
228 spillcount = set_count(env->spills);
229 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
230 int slotid = spill->spillslot;
231 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
234 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
235 affinity_edge_t *edge = env->affinity_edges[i];
236 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
239 for(i = 0; i < spillcount; ++i) {
241 for(i2 = 0; i2 < spillcount; ++i2) {
242 if(bitset_is_set(interferences[i], i2)) {
243 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
252 static void show_stats(ss_env_t *env) {
258 spillcount = set_count(env->spills);
259 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
261 slotused = alloca(spillcount * sizeof(slotused[0]));
262 memset(slotused, 0, spillcount * sizeof(slotused[0]));
265 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
266 int slot = spill->spillslot;
267 if(slotused[slot] == 0) {
273 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
278 static void assign_spillslots(ss_env_t *env);
281 * A greedy coalescing algorithm for spillslots:
282 * 1. Sort the list of affinity edges
283 * 2. Try to merge slots with affinity edges (most expensive slots first)
284 * 3. Try to merge everything else that is possible
286 static void do_greedy_coalescing(ss_env_t *env)
292 int affinity_edge_count;
293 bitset_t **interferences;
294 int* spillslot_unionfind;
296 spillcount = set_count(env->spills);
300 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
302 interferences = alloca(spillcount * sizeof(interferences[0]));
303 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
304 spilllist = alloca(spillcount * sizeof(spilllist[0]));
306 uf_init(spillslot_unionfind, 0, spillcount);
309 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
312 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
313 assert(spill->spillslot < spillcount);
314 spilllist[spill->spillslot] = spill;
317 for(i = 0; i < spillcount; ++i) {
318 interferences[i] = bitset_alloca(spillcount);
321 // construct interferences
322 for(i = 0; i < spillcount; ++i) {
323 for(i2 = i+1; i2 < spillcount; ++i2) {
324 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
325 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
326 bitset_set(interferences[i], i2);
327 bitset_set(interferences[i2], i);
332 // sort affinity edges
333 affinity_edge_count = ARR_LEN(env->affinity_edges);
334 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
336 //dump_interference_graph(env, interferences, "before");
338 // try to merge affine nodes
339 for(i = 0; i < affinity_edge_count; ++i) {
340 const affinity_edge_t *edge = env->affinity_edges[i];
341 int s1 = uf_find(spillslot_unionfind, edge->slot1);
342 int s2 = uf_find(spillslot_unionfind, edge->slot2);
344 /* test if values interfere */
345 if(bitset_is_set(interferences[s1], s2)) {
346 assert(bitset_is_set(interferences[s2], s1));
350 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
352 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
355 // try to merge as much remaining spillslots as possible
356 for(i = 0; i < spillcount; ++i) {
357 int s1 = uf_find(spillslot_unionfind, i);
361 for(i2 = i+1; i2 < spillcount; ++i2) {
362 int s2 = uf_find(spillslot_unionfind, i2);
366 /* test if values interfere
367 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
368 * when node merging occurs
370 if(bitset_is_set(interferences[s1], s2)) {
371 assert(bitset_is_set(interferences[s2], s1));
375 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
377 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
378 // we can break the loop here, because s2 is the new supernode now
379 // and we'll test s2 again later anyway
386 for(i = 0; i < spillcount; ++i) {
387 spill_t *spill = spilllist[i];
389 spill->spillslot = uf_find(spillslot_unionfind, i);
392 //dump_interference_graph(env, interferences, "after");
396 static void do_java_coalescing(ss_env_t *env)
402 be_java_coal_t *coal;
404 spillcount = set_count(env->spills);
408 spilllist = alloca(spillcount * sizeof(spilllist[0]));
411 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
414 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
416 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
417 assert(spill->spillslot < spillcount);
418 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
419 spilllist[spill->spillslot] = spill;
421 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
424 // construct interferences
425 for(i = 0; i < spillcount; ++i) {
426 for(i2 = i+1; i2 < spillcount; ++i2) {
427 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
428 be_java_coal_add_int_edge(coal, i, i2);
433 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
434 const affinity_edge_t *edge = env->affinity_edges[i];
437 int costs = (int) (edge->affinity * 10000);
438 be_java_coal_add_aff_edge(coal, n, m, costs);
441 be_java_coal_coalesce(coal);
443 // construct spillslots
444 for(i = 0; i < spillcount; ++i) {
445 spill_t *spill = spilllist[i];
446 spill->spillslot = be_java_coal_get_color(coal, i);
448 be_java_coal_destroy(coal);
454 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
455 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
456 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
457 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
461 typedef struct _spill_slot_t {
467 typedef struct _memperm_entry_t {
472 struct _memperm_entry_t *next;
475 typedef struct _memperm_t {
478 memperm_entry_t *entries;
481 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
482 const memperm_t* e1 = d1;
483 const memperm_t* e2 = d2;
484 return e1->block != e2->block;
487 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
488 memperm_t entry, *res;
492 hash = HASH_PTR(block);
494 res = set_find(env->memperms, &entry, sizeof(entry), hash);
497 entry.entrycount = 0;
498 entry.entries = NULL;
499 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
505 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
506 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
507 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
514 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
515 const ir_mode *mode = arch_register_class_mode(spill->cls);
517 return get_mode_size_bytes(mode);
520 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
521 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
523 return arch_isa_get_reg_class_alignment(isa, spill->cls);
527 * Enlarges a spillslot (if necessary) so that it can carry a value of size
528 * @p othersize and alignment @p otheralign.
530 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
531 if(othersize > slot->size) {
532 slot->size = othersize;
534 if(otheralign > slot->align) {
535 if(otheralign % slot->align != 0)
536 slot->align *= otheralign;
538 slot->align = otheralign;
539 } else if(slot->align % otheralign != 0) {
540 slot->align *= otheralign;
545 * Create stack entities for the spillslots and assign them to the spill and
548 static void assign_spillslots(ss_env_t *env) {
552 spill_slot_t* spillslots;
554 spillcount = set_count(env->spills);
555 spillslots = alloca(spillcount * sizeof(spillslots[0]));
557 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
559 // construct spillslots
560 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
561 int slotid = spill->spillslot;
562 spill_slot_t *slot = & (spillslots[slotid]);
563 int align = get_spillslotalign_for_spill(env, spill);
564 int size = get_spillslotsize_for_spill(env, spill);
566 if(slot->align == 0 && slot->size == 0) {
570 enlarge_spillslot(slot, align, size);
574 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
576 ir_node *node = spill->spill;
577 int slotid = spill->spillslot;
579 slot = &spillslots[slotid];
580 if(slot->entity == NULL) {
581 create_stack_entity(env, slot);
584 if(be_is_Spill(node)) {
585 be_set_frame_entity(node, slot->entity);
588 ir_node *block = get_nodes_block(node);
591 assert(is_Phi(node));
593 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
594 ir_node *arg = get_irn_n(node, i);
595 ir_node *predblock = get_Block_cfgpred_block(block, i);
599 argspill = get_spill(env, arg);
600 assert(argspill != NULL);
602 argslotid = argspill->spillslot;
603 if(slotid != argslotid) {
605 memperm_entry_t *entry;
606 spill_slot_t *argslot = &spillslots[argslotid];
607 if(argslot->entity == NULL) {
608 create_stack_entity(env, argslot);
611 memperm = get_memperm(env, predblock);
613 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
616 entry->in = argslot->entity;
617 entry->out = slot->entity;
618 entry->next = memperm->entries;
619 memperm->entrycount++;
620 memperm->entries = entry;
626 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
627 const ir_node* reload = env->reloads[i];
628 ir_node* spillnode = get_irn_n(reload, be_pos_Reload_mem);
629 spill_t *spill = get_spill(env, spillnode);
630 const spill_slot_t *slot = & spillslots[spill->spillslot];
632 assert(slot->entity != NULL);
634 be_set_frame_entity(reload, slot->entity);
639 * Returns the last node in a block which is no control flow changing node
641 static ir_node *get_end_of_block_insertion_point(ir_node* block)
643 ir_node* ins = sched_last(block);
644 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
645 ins = sched_prev(ins);
651 ir_node *prev = sched_prev(ins);
661 static void create_memperms(ss_env_t *env) {
664 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
666 memperm_entry_t *entry;
668 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
669 ir_node* mempermnode;
671 assert(memperm->entrycount > 0);
673 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
674 ir_node* arg = get_irn_n(entry->node, entry->pos);
678 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
679 memperm->entrycount, nodes);
681 // insert node into schedule
682 blockend = get_end_of_block_insertion_point(memperm->block);
683 sched_add_before(blockend, mempermnode);
685 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
687 ir_node* arg = get_irn_n(entry->node, entry->pos);
689 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
690 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
691 set_irg_current_block(env->chordal_env->irg, memperm->block);
692 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
693 sched_add_before(blockend, proj);
695 set_irn_n(entry->node, entry->pos, proj);
700 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
703 obstack_init(&env.obst);
704 env.chordal_env = chordal_env;
705 env.spills = new_set(cmp_spill, 10);
706 env.reloads = NEW_ARR_F(ir_node*, 0);
707 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
708 env.memperms = new_set(cmp_memperm, 10);
709 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
710 //firm_dbg_set_mask(dbg, DBG_COALESCING);
712 /* Get initial spill slots */
713 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
715 do_greedy_coalescing(&env);
717 assign_spillslots(&env);
719 create_memperms(&env);
723 del_set(env.memperms);
724 DEL_ARR_F(env.reloads);
725 DEL_ARR_F(env.affinity_edges);
727 obstack_free(&env.obst, NULL);