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"
29 #include "bestatevent.h"
30 #include "bespilloptions.h"
32 #define DBG_COALESCING 1
33 #define DBG_INTERFERENCES 2
36 static firm_dbg_module_t *dbg = NULL;
39 typedef struct _spill_t {
41 /** regclass of the spilled value */
42 const arch_register_class_t *cls;
43 /** index into spillslot_unionfind unionfind structure */
47 typedef struct _affinity_edge_t {
52 typedef struct _ss_env_t {
54 const arch_env_t *arch_env;
55 const be_chordal_env_t *chordal_env;
58 affinity_edge_t **affinity_edges;
62 /** Compare 2 affinity edges (used in quicksort) */
63 static int cmp_affinity(const void *d1, const void *d2) {
64 const affinity_edge_t * const *e1 = d1;
65 const affinity_edge_t * const *e2 = d2;
67 // sort in descending order
68 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
71 static int cmp_spill(const void* d1, const void* d2, size_t size) {
72 const spill_t* s1 = d1;
73 const spill_t* s2 = d2;
74 return s1->spill != s2->spill;
77 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
79 int hash = nodeset_hash(node);
82 res = set_find(env->spills, &spill, sizeof(spill), hash);
88 * ____ _ _ _ ____ _ _ _
89 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
90 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
91 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
92 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
96 static ir_node *get_memory_edge(const ir_node *node) {
99 arity = get_irn_arity(node);
100 for(i = arity - 1; i >= 0; --i) {
101 ir_node *arg = get_irn_n(node, i);
102 if(get_irn_mode(arg) == mode_M)
109 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
110 const arch_env_t *arch_env = env->arch_env;
111 const arch_register_class_t *cls;
113 int hash = nodeset_hash(node);
115 assert(arch_irn_class_is(arch_env, node, spill));
117 if(be_is_Spill(node)) {
118 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
120 // TODO add a way to detect the type of the spilled value
125 res = set_find(env->spills, &spill, sizeof(spill), hash);
128 spill.spillslot = set_count(env->spills);
130 res = set_insert(env->spills, &spill, sizeof(spill), hash);
136 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
139 int hash = nodeset_hash(node);
140 ir_exec_freq *exec_freq = env->chordal_env->birg->exec_freq;
142 assert(is_Phi(node));
145 res = set_find(env->spills, &spill, sizeof(spill), hash);
150 spill.spillslot = set_count(env->spills);
152 res = set_insert(env->spills, &spill, sizeof(spill), hash);
154 // is 1 of the arguments a spill?
155 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
156 affinity_edge_t *affinty_edge;
157 ir_node* arg = get_irn_n(node, i);
160 if(be_is_Spill(arg)) {
161 arg_spill = collect_spill(env, arg);
163 // if it wasn't a spill then it must be a Mem-Phi
165 arg_spill = collect_memphi(env, arg);
168 if(res->cls == NULL) {
169 res->cls = arg_spill->cls;
171 assert(arg_spill->cls == NULL || res->cls == arg_spill->cls);
174 // add an affinity edge
175 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
176 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
177 affinty_edge->slot1 = res->spillslot;
178 affinty_edge->slot2 = arg_spill->spillslot;
179 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
186 * This walker function searches for reloads and collects all the spills
187 * and memphis attached to them.
189 static void collect_spills_walker(ir_node *node, void *data) {
190 ss_env_t *env = data;
191 const arch_env_t *arch_env = env->arch_env;
193 /* classify returns classification of the irn the proj is attached to */
197 if (arch_irn_class_is(arch_env, node, reload)) {
198 ir_node *spillnode = get_memory_edge(node);
201 assert(spillnode != NULL);
203 if (is_Phi(spillnode)) {
204 spill = collect_memphi(env, spillnode);
207 spill = collect_spill(env, spillnode);
210 ARR_APP1(ir_node *, env->reloads, node);
216 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
217 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
218 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
219 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
222 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
228 // merge spillslots and interferences
229 res = uf_union(spillslot_unionfind, s1, s2);
230 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
237 bitset_or(interferences[s1], interferences[s2]);
239 // update other interferences
240 spillcount = set_count(env->spills);
241 for(i = 0; i < spillcount; ++i) {
242 bitset_t *intfs = interferences[i];
243 if(bitset_is_set(intfs, s2))
244 bitset_set(intfs, s1);
252 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
260 snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
262 f = fopen(name, "w");
265 fprintf(f, "graph: {\n");
267 spillcount = set_count(env->spills);
268 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
269 int slotid = spill->spillslot;
270 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
273 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
274 affinity_edge_t *edge = env->affinity_edges[i];
275 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
278 for(i = 0; i < spillcount; ++i) {
280 for(i2 = 0; i2 < spillcount; ++i2) {
281 if(bitset_is_set(interferences[i], i2)) {
282 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
291 static void show_stats(ss_env_t *env) {
297 spillcount = set_count(env->spills);
298 fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
300 slotused = alloca(spillcount * sizeof(slotused[0]));
301 memset(slotused, 0, spillcount * sizeof(slotused[0]));
304 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
305 int slot = spill->spillslot;
306 if(slotused[slot] == 0) {
312 fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
317 static void assign_spillslots(ss_env_t *env);
320 * A greedy coalescing algorithm for spillslots:
321 * 1. Sort the list of affinity edges
322 * 2. Try to merge slots with affinity edges (most expensive slots first)
323 * 3. Try to merge everything else that is possible
325 static void do_greedy_coalescing(ss_env_t *env)
331 int affinity_edge_count;
332 bitset_t **interferences;
333 int* spillslot_unionfind;
334 be_lv_t *lv = env->chordal_env->birg->lv;
336 spillcount = set_count(env->spills);
340 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
342 interferences = alloca(spillcount * sizeof(interferences[0]));
343 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
344 spilllist = alloca(spillcount * sizeof(spilllist[0]));
346 uf_init(spillslot_unionfind, 0, spillcount);
349 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
352 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
353 assert(spill->spillslot < spillcount);
354 spilllist[spill->spillslot] = spill;
357 for(i = 0; i < spillcount; ++i) {
358 interferences[i] = bitset_alloca(spillcount);
361 // construct interferences
362 for(i = 0; i < spillcount; ++i) {
363 for(i2 = i+1; i2 < spillcount; ++i2) {
364 if(values_interfere(lv, spilllist[i]->spill, spilllist[i2]->spill)) {
365 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
366 bitset_set(interferences[i], i2);
367 bitset_set(interferences[i2], i);
372 // sort affinity edges
373 affinity_edge_count = ARR_LEN(env->affinity_edges);
374 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
376 //dump_interference_graph(env, interferences, "before");
378 // try to merge affine nodes
379 for(i = 0; i < affinity_edge_count; ++i) {
380 const affinity_edge_t *edge = env->affinity_edges[i];
381 int s1 = uf_find(spillslot_unionfind, edge->slot1);
382 int s2 = uf_find(spillslot_unionfind, edge->slot2);
384 /* test if values interfere */
385 if(bitset_is_set(interferences[s1], s2)) {
386 assert(bitset_is_set(interferences[s2], s1));
390 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
392 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
395 // try to merge as much remaining spillslots as possible
396 for(i = 0; i < spillcount; ++i) {
397 int s1 = uf_find(spillslot_unionfind, i);
401 for(i2 = i+1; i2 < spillcount; ++i2) {
402 int s2 = uf_find(spillslot_unionfind, i2);
406 /* test if values interfere
407 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
408 * when node merging occurs
410 if(bitset_is_set(interferences[s1], s2)) {
411 assert(bitset_is_set(interferences[s2], s1));
415 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
417 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
418 // we can break the loop here, because s2 is the new supernode now
419 // and we'll test s2 again later anyway
425 // assign spillslots to spills
426 for(i = 0; i < spillcount; ++i) {
427 spill_t *spill = spilllist[i];
429 spill->spillslot = uf_find(spillslot_unionfind, i);
432 //dump_interference_graph(env, interferences, "after");
436 static void do_java_coalescing(ss_env_t *env)
442 be_java_coal_t *coal;
444 spillcount = set_count(env->spills);
448 spilllist = alloca(spillcount * sizeof(spilllist[0]));
451 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
454 coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
456 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
457 assert(spill->spillslot < spillcount);
458 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
459 spilllist[spill->spillslot] = spill;
461 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
464 // construct interferences
465 for(i = 0; i < spillcount; ++i) {
466 for(i2 = i+1; i2 < spillcount; ++i2) {
467 if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
468 be_java_coal_add_int_edge(coal, i, i2);
473 for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
474 const affinity_edge_t *edge = env->affinity_edges[i];
477 int costs = (int) (edge->affinity * 10000);
478 be_java_coal_add_aff_edge(coal, n, m, costs);
481 be_java_coal_coalesce(coal);
483 // construct spillslots
484 for(i = 0; i < spillcount; ++i) {
485 spill_t *spill = spilllist[i];
486 spill->spillslot = be_java_coal_get_color(coal, i);
488 be_java_coal_destroy(coal);
494 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
495 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
496 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
497 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
501 typedef struct _spill_slot_t {
507 typedef struct _memperm_entry_t {
512 struct _memperm_entry_t *next;
515 typedef struct _memperm_t {
518 memperm_entry_t *entries;
521 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
522 const memperm_t* e1 = d1;
523 const memperm_t* e2 = d2;
524 return e1->block != e2->block;
527 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
528 memperm_t entry, *res;
532 hash = nodeset_hash(block);
534 res = set_find(env->memperms, &entry, sizeof(entry), hash);
537 entry.entrycount = 0;
538 entry.entries = NULL;
539 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
545 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
546 ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
547 entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
549 // adjust size of the entity type...
550 ir_type *enttype = get_entity_type(res);
551 set_type_size_bytes(enttype, slot->size);
558 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
559 const ir_mode *mode = arch_register_class_mode(spill->cls);
561 return get_mode_size_bytes(mode);
564 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
565 const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
567 return arch_isa_get_reg_class_alignment(isa, spill->cls);
571 * Enlarges a spillslot (if necessary) so that it can carry a value of size
572 * @p othersize and alignment @p otheralign.
574 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
575 if(othersize > slot->size) {
576 slot->size = othersize;
578 if(otheralign > slot->align) {
579 if(otheralign % slot->align != 0)
580 slot->align *= otheralign;
582 slot->align = otheralign;
583 } else if(slot->align % otheralign != 0) {
584 slot->align *= otheralign;
589 * Create stack entities for the spillslots and assign them to the spill and
592 static void assign_spillslots(ss_env_t *env) {
593 const arch_env_t *arch_env = env->arch_env;
597 spill_slot_t* spillslots;
599 spillcount = set_count(env->spills);
600 spillslots = alloca(spillcount * sizeof(spillslots[0]));
602 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
604 // construct spillslots
605 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
606 int slotid = spill->spillslot;
607 spill_slot_t *slot = & (spillslots[slotid]);
608 int align = get_spillslotalign_for_spill(env, spill);
609 int size = get_spillslotsize_for_spill(env, spill);
611 if(slot->align == 0 && slot->size == 0) {
615 enlarge_spillslot(slot, align, size);
619 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
621 ir_node *node = spill->spill;
622 int slotid = spill->spillslot;
624 slot = &spillslots[slotid];
625 if(slot->entity == NULL) {
626 create_stack_entity(env, slot);
631 ir_node *block = get_nodes_block(node);
634 assert(is_Phi(node));
636 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
637 ir_node *arg = get_irn_n(node, i);
638 ir_node *predblock = get_Block_cfgpred_block(block, i);
642 argspill = get_spill(env, arg);
643 assert(argspill != NULL);
645 argslotid = argspill->spillslot;
646 if(slotid != argslotid) {
648 memperm_entry_t *entry;
649 spill_slot_t *argslot = &spillslots[argslotid];
650 if(argslot->entity == NULL) {
651 create_stack_entity(env, argslot);
654 memperm = get_memperm(env, predblock);
656 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
659 entry->in = argslot->entity;
660 entry->out = slot->entity;
661 entry->next = memperm->entries;
662 memperm->entrycount++;
663 memperm->entries = entry;
667 assert(arch_irn_class_is(arch_env, node, spill));
668 arch_set_frame_entity(arch_env, node, slot->entity);
672 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
673 ir_node* reload = env->reloads[i];
674 ir_node* spillnode = get_memory_edge(reload);
675 spill_t *spill = get_spill(env, spillnode);
676 const spill_slot_t *slot = & spillslots[spill->spillslot];
678 assert(slot->entity != NULL);
680 arch_set_frame_entity(arch_env, reload, slot->entity);
685 * Returns the last node in a block which is no control flow changing node
687 static ir_node *get_end_of_block_insertion_point(ir_node* block)
689 ir_node* ins = sched_last(block);
690 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
691 ins = sched_prev(ins);
697 ir_node *prev = sched_prev(ins);
707 static void create_memperms(ss_env_t *env) {
710 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
712 memperm_entry_t *entry;
714 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
715 ir_node* mempermnode;
717 assert(memperm->entrycount > 0);
719 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
720 ir_node* arg = get_irn_n(entry->node, entry->pos);
724 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
725 memperm->entrycount, nodes);
727 // insert node into schedule
728 blockend = get_end_of_block_insertion_point(memperm->block);
729 sched_add_before(blockend, mempermnode);
730 be_stat_ev("mem_perm", memperm->entrycount);
732 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
734 ir_node* arg = get_irn_n(entry->node, entry->pos);
736 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
737 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
738 set_irg_current_block(env->chordal_env->irg, memperm->block);
739 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
740 sched_add_before(blockend, proj);
742 set_irn_n(entry->node, entry->pos, proj);
747 static int count_spillslots(const ss_env_t *env) {
748 const spill_t *spill;
749 int spillcount = set_count(env->spills);
750 bitset_t *counted = bitset_alloca(spillcount);
754 for(spill = set_first(env->spills); spill != NULL;
755 spill = set_next(env->spills)) {
756 int spillslot = spill->spillslot;
757 if(!bitset_is_set(counted, spillslot)) {
759 bitset_set(counted, spillslot);
766 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
769 obstack_init(&env.obst);
770 env.arch_env = chordal_env->birg->main_env->arch_env;
771 env.chordal_env = chordal_env;
772 env.spills = new_set(cmp_spill, 10);
773 env.reloads = NEW_ARR_F(ir_node*, 0);
774 env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
775 env.memperms = new_set(cmp_memperm, 10);
776 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
777 //firm_dbg_set_mask(dbg, DBG_COALESCING);
779 /* Get initial spill slots */
780 irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
782 be_stat_ev("spillslots", set_count(env.spills));
784 if(be_coalesce_spill_slots) {
785 do_greedy_coalescing(&env);
786 if(be_stat_ev_is_active()) {
787 be_stat_ev("spillslots_after_coalescing", count_spillslots(&env));
791 assign_spillslots(&env);
793 create_memperms(&env);
797 del_set(env.memperms);
798 DEL_ARR_F(env.reloads);
799 DEL_ARR_F(env.affinity_edges);
801 obstack_free(&env.obst, NULL);