2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Spillslot coalescer.
23 * @author Matthias Braun
39 #include "unionfind.h"
45 #include "bespillslots.h"
46 #include "bechordal_t.h"
47 #include "bejavacoal.h"
48 #include "benodesets.h"
49 #include "bestatevent.h"
50 #include "bespilloptions.h"
52 #include "beintlive_t.h"
56 #define DBG_COALESCING 1
57 #define DBG_INTERFERENCES 2
60 static firm_dbg_module_t *dbg = NULL;
63 typedef struct _spill_t {
65 /** mode of the spilled value */
67 /** alignment for the spilled value */
69 /** index into spillslot_unionfind unionfind structure */
73 typedef struct _affinity_edge_t {
78 struct _be_fec_env_t {
80 const arch_env_t *arch_env;
84 affinity_edge_t **affinity_edges;
88 /** Compare 2 affinity edges (used in quicksort) */
89 static int cmp_affinity(const void *d1, const void *d2)
91 const affinity_edge_t * const *e1 = d1;
92 const affinity_edge_t * const *e2 = d2;
94 // sort in descending order
95 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
98 static int cmp_spill(const void* d1, const void* d2, size_t size)
100 const spill_t* s1 = d1;
101 const spill_t* s2 = d2;
102 return s1->spill != s2->spill;
105 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
108 int hash = nodeset_hash(node);
111 res = set_find(env->spills, &spill, sizeof(spill), hash);
117 * ____ _ _ _ ____ _ _ _
118 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
119 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
120 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
121 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
125 static INLINE ir_node *get_memory_edge(const ir_node *node)
129 arity = get_irn_arity(node);
130 for(i = arity - 1; i >= 0; --i) {
131 ir_node *arg = get_irn_n(node, i);
132 if(get_irn_mode(arg) == mode_M)
139 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
140 const ir_mode *mode, int align)
143 int hash = nodeset_hash(node);
145 /* insert into set of spills if not already there */
147 res = set_find(env->spills, &spill, sizeof(spill), hash);
150 spill.spillslot = set_count(env->spills);
152 spill.alignment = align;
153 res = set_insert(env->spills, &spill, sizeof(spill), hash);
155 assert(res->mode == mode);
156 assert(res->alignment == align);
162 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
163 const ir_mode *mode, int align)
167 int hash = nodeset_hash(node);
168 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
170 assert(is_Phi(node));
173 res = set_find(env->spills, &spill, sizeof(spill), hash);
175 assert(res->mode == mode);
176 assert(res->alignment == align);
180 spill.spillslot = set_count(env->spills);
182 spill.alignment = align;
183 res = set_insert(env->spills, &spill, sizeof(spill), hash);
185 // collect attached spills and mem-phis
186 arity = get_irn_arity(node);
187 for(i = 0; i < arity; ++i) {
188 affinity_edge_t *affinty_edge;
189 ir_node *arg = get_irn_n(node, i);
193 arg_spill = collect_memphi(env, arg, mode, align);
195 arg_spill = collect_spill(env, arg, mode, align);
198 // add an affinity edge
199 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
200 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
201 affinty_edge->slot1 = res->spillslot;
202 affinty_edge->slot2 = arg_spill->spillslot;
203 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
209 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
210 const ir_mode *mode, int align)
212 ir_node *spillnode = get_memory_edge(node);
215 assert(spillnode != NULL);
217 if (is_Phi(spillnode)) {
218 spill = collect_memphi(env, spillnode, mode, align);
220 spill = collect_spill(env, spillnode, mode, align);
223 ARR_APP1(ir_node *, env->reloads, node);
228 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
229 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
230 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
231 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
234 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
235 int* spillslot_unionfind, int s1, int s2)
241 // merge spillslots and interferences
242 res = uf_union(spillslot_unionfind, s1, s2);
243 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
250 bitset_or(interferences[s1], interferences[s2]);
252 // update other interferences
253 spillcount = set_count(env->spills);
254 for(i = 0; i < spillcount; ++i) {
255 bitset_t *intfs = interferences[i];
256 if(bitset_is_set(intfs, s2))
257 bitset_set(intfs, s1);
264 * A greedy coalescing algorithm for spillslots:
265 * 1. Sort the list of affinity edges
266 * 2. Try to merge slots with affinity edges (most expensive slots first)
267 * 3. Try to merge everything else that is possible
269 static void do_greedy_coalescing(be_fec_env_t *env)
275 int affinity_edge_count;
276 bitset_t **interferences;
277 int* spillslot_unionfind;
279 spillcount = set_count(env->spills);
283 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
285 interferences = alloca(spillcount * sizeof(interferences[0]));
286 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
287 spilllist = alloca(spillcount * sizeof(spilllist[0]));
289 uf_init(spillslot_unionfind, 0, spillcount);
292 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
295 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
296 assert(spill->spillslot < spillcount);
297 spilllist[spill->spillslot] = spill;
300 for(i = 0; i < spillcount; ++i) {
301 interferences[i] = bitset_alloca(spillcount);
304 /* construct interferences */
305 for (i = 0; i < spillcount; ++i) {
306 ir_node *spill1 = spilllist[i]->spill;
308 if (is_NoMem(spill1))
311 for(i2 = i+1; i2 < spillcount; ++i2) {
312 ir_node *spill2 = spilllist[i2]->spill;
314 if (is_NoMem(spill2))
317 if (values_interfere(env->birg, spill1, spill2)) {
318 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
319 bitset_set(interferences[i], i2);
320 bitset_set(interferences[i2], i);
325 /* sort affinity edges */
326 affinity_edge_count = ARR_LEN(env->affinity_edges);
327 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
329 //dump_interference_graph(env, interferences, "before");
331 /* try to merge affine nodes */
332 for(i = 0; i < affinity_edge_count; ++i) {
333 const affinity_edge_t *edge = env->affinity_edges[i];
334 int s1 = uf_find(spillslot_unionfind, edge->slot1);
335 int s2 = uf_find(spillslot_unionfind, edge->slot2);
337 /* test if values interfere */
338 if (bitset_is_set(interferences[s1], s2)) {
339 assert(bitset_is_set(interferences[s2], s1));
343 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
345 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
348 // try to merge as much remaining spillslots as possible
349 for(i = 0; i < spillcount; ++i) {
350 int s1 = uf_find(spillslot_unionfind, i);
354 for(i2 = i+1; i2 < spillcount; ++i2) {
355 int s2 = uf_find(spillslot_unionfind, i2);
359 /* test if values interfere
360 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
361 * when node merging occurs
363 if(bitset_is_set(interferences[s1], s2)) {
364 assert(bitset_is_set(interferences[s2], s1));
368 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
370 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
371 // we can break the loop here, because s2 is the new supernode now
372 // and we'll test s2 again later anyway
378 // assign spillslots to spills
379 for(i = 0; i < spillcount; ++i) {
380 spill_t *spill = spilllist[i];
382 spill->spillslot = uf_find(spillslot_unionfind, i);
385 //dump_interference_graph(env, interferences, "after");
390 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
391 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
392 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
393 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
397 typedef struct _spill_slot_t {
403 typedef struct _memperm_entry_t {
408 struct _memperm_entry_t *next;
411 typedef struct _memperm_t {
414 memperm_entry_t *entries;
417 static int cmp_memperm(const void* d1, const void* d2, size_t size)
419 const memperm_t* e1 = d1;
420 const memperm_t* e2 = d2;
421 return e1->block != e2->block;
424 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
426 memperm_t entry, *res;
430 hash = nodeset_hash(block);
432 res = set_find(env->memperms, &entry, sizeof(entry), hash);
435 entry.entrycount = 0;
436 entry.entries = NULL;
437 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
443 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
445 ir_graph *irg = be_get_birg_irg(env->birg);
446 ir_type *frame = get_irg_frame_type(irg);
447 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
449 /* adjust size of the entity type... */
450 ir_type *enttype = get_entity_type(res);
451 set_type_size_bytes(enttype, slot->size);
459 * Enlarges a spillslot (if necessary) so that it can carry a value of size
460 * @p othersize and alignment @p otheralign.
462 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
464 if(othersize > slot->size) {
465 slot->size = othersize;
467 if(otheralign > slot->align) {
468 if(otheralign % slot->align != 0)
469 slot->align *= otheralign;
471 slot->align = otheralign;
472 } else if(slot->align % otheralign != 0) {
473 slot->align *= otheralign;
478 * Create stack entities for the spillslots and assign them to the spill and
481 static void assign_spillslots(be_fec_env_t *env)
483 const arch_env_t *arch_env = env->arch_env;
487 spill_slot_t* spillslots;
489 spillcount = set_count(env->spills);
490 spillslots = alloca(spillcount * sizeof(spillslots[0]));
492 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
494 // construct spillslots
495 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
496 int slotid = spill->spillslot;
497 const ir_mode *mode = spill->mode;
498 spill_slot_t *slot = & (spillslots[slotid]);
499 int size = get_mode_size_bytes(mode);
500 int align = spill->alignment;
502 if(slot->align == 0 && slot->size == 0) {
506 enlarge_spillslot(slot, align, size);
510 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
512 ir_node *node = spill->spill;
513 int slotid = spill->spillslot;
515 slot = &spillslots[slotid];
516 if(slot->entity == NULL) {
517 create_stack_entity(env, slot);
522 ir_node *block = get_nodes_block(node);
525 assert(is_Phi(node));
527 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
528 ir_node *arg = get_irn_n(node, i);
529 ir_node *predblock = get_Block_cfgpred_block(block, i);
533 argspill = get_spill(env, arg);
534 assert(argspill != NULL);
536 argslotid = argspill->spillslot;
537 if(slotid != argslotid) {
539 memperm_entry_t *entry;
540 spill_slot_t *argslot = &spillslots[argslotid];
541 if(argslot->entity == NULL) {
542 create_stack_entity(env, argslot);
545 memperm = get_memperm(env, predblock);
547 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
550 entry->in = argslot->entity;
551 entry->out = slot->entity;
552 entry->next = memperm->entries;
553 memperm->entrycount++;
554 memperm->entries = entry;
559 arch_set_frame_entity(arch_env, node, slot->entity);
563 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
564 ir_node* reload = env->reloads[i];
565 ir_node* spillnode = get_memory_edge(reload);
566 spill_t *spill = get_spill(env, spillnode);
567 const spill_slot_t *slot = & spillslots[spill->spillslot];
569 assert(slot->entity != NULL);
571 arch_set_frame_entity(arch_env, reload, slot->entity);
576 * Returns the last node in a block which is no control flow changing node
578 static ir_node *get_end_of_block_insertion_point(ir_node* block)
580 ir_node* ins = sched_last(block);
581 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
582 ins = sched_prev(ins);
588 ir_node *prev = sched_prev(ins);
598 static void create_memperms(be_fec_env_t *env)
600 const arch_env_t *arch_env = env->arch_env;
601 ir_graph *irg = be_get_birg_irg(env->birg);
604 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
606 memperm_entry_t *entry;
608 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
609 ir_node* mempermnode;
611 assert(memperm->entrycount > 0);
613 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
614 ir_node* arg = get_irn_n(entry->node, entry->pos);
618 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
619 memperm->entrycount, nodes);
621 // insert node into schedule
622 blockend = get_end_of_block_insertion_point(memperm->block);
623 sched_add_before(blockend, mempermnode);
624 be_stat_ev("mem_perm", memperm->entrycount);
627 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
629 ir_node* arg = get_irn_n(entry->node, entry->pos);
631 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
632 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
633 set_irg_current_block(irg, memperm->block);
634 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
635 sched_add_before(blockend, proj);
637 set_irn_n(entry->node, entry->pos, proj);
642 static int count_spillslots(const be_fec_env_t *env)
644 const spill_t *spill;
645 int spillcount = set_count(env->spills);
646 bitset_t *counted = bitset_alloca(spillcount);
650 for(spill = set_first(env->spills); spill != NULL;
651 spill = set_next(env->spills)) {
652 int spillslot = spill->spillslot;
653 if(!bitset_is_set(counted, spillslot)) {
655 bitset_set(counted, spillslot);
662 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
664 const arch_env_t *arch_env = birg->main_env->arch_env;
665 be_fec_env_t *env = xmalloc(sizeof(env[0]));
667 be_assure_liveness(birg);
669 obstack_init(&env->obst);
670 env->arch_env = arch_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);
680 void be_free_frame_entity_coalescer(be_fec_env_t *env)
682 del_set(env->memperms);
683 DEL_ARR_F(env->reloads);
684 DEL_ARR_F(env->affinity_edges);
685 del_set(env->spills);
686 obstack_free(&env->obst, NULL);
691 void be_assign_entities(be_fec_env_t *env)
693 if(be_stat_ev_is_active()) {
694 int count = set_count(env->spills);
695 be_stat_ev("spillslots", count);
698 if(be_coalesce_spill_slots) {
699 do_greedy_coalescing(env);
702 if(be_stat_ev_is_active()) {
703 int count = count_spillslots(env);
704 be_stat_ev("spillslots_after_coalescing", count);
707 assign_spillslots(env);
709 create_memperms(env);
713 * This walker function searches for reloads and collects all the spills
714 * and memphis attached to them.
716 static void collect_spills_walker(ir_node *node, void *data)
718 be_fec_env_t *env = data;
719 const arch_env_t *arch_env = env->arch_env;
721 const arch_register_class_t *cls;
724 /* classify returns classification of the irn the proj is attached to */
728 if (!arch_irn_class_is(arch_env, node, reload))
731 mode = get_irn_mode(node);
732 cls = arch_get_irn_reg_class(arch_env, node, -1);
733 align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
735 be_node_needs_frame_entity(env, node, mode, align);
738 void be_coalesce_spillslots(be_irg_t *birg)
740 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
742 /* collect reloads */
743 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
745 be_assign_entities(env);
747 be_free_frame_entity_coalescer(env);
750 void be_init_spillslots(void)
752 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
755 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);