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"
44 #include "bespillslots.h"
45 #include "bechordal_t.h"
46 #include "bejavacoal.h"
47 #include "benodesets.h"
48 #include "bestatevent.h"
49 #include "bespilloptions.h"
51 #include "beintlive_t.h"
55 #define DBG_COALESCING 1
56 #define DBG_INTERFERENCES 2
59 static firm_dbg_module_t *dbg = NULL;
62 typedef struct _spill_t {
64 /** mode of the spilled value */
66 /** alignment for the spilled value */
68 /** index into spillslot_unionfind unionfind structure */
72 typedef struct _affinity_edge_t {
77 struct _be_fec_env_t {
79 const arch_env_t *arch_env;
83 affinity_edge_t **affinity_edges;
87 /** Compare 2 affinity edges (used in quicksort) */
88 static int cmp_affinity(const void *d1, const void *d2)
90 const affinity_edge_t * const *e1 = d1;
91 const affinity_edge_t * const *e2 = d2;
93 // sort in descending order
94 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
97 static int cmp_spill(const void* d1, const void* d2, size_t size)
99 const spill_t* s1 = d1;
100 const spill_t* s2 = d2;
101 return s1->spill != s2->spill;
104 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
107 int hash = nodeset_hash(node);
110 res = set_find(env->spills, &spill, sizeof(spill), hash);
116 * ____ _ _ _ ____ _ _ _
117 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
118 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
119 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
120 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
124 static INLINE ir_node *get_memory_edge(const ir_node *node)
128 arity = get_irn_arity(node);
129 for(i = arity - 1; i >= 0; --i) {
130 ir_node *arg = get_irn_n(node, i);
131 if(get_irn_mode(arg) == mode_M)
138 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
139 const ir_mode *mode, int align)
142 int hash = nodeset_hash(node);
144 /* insert into set of spills if not already there */
146 res = set_find(env->spills, &spill, sizeof(spill), hash);
149 spill.spillslot = set_count(env->spills);
151 spill.alignment = align;
152 res = set_insert(env->spills, &spill, sizeof(spill), hash);
154 assert(res->mode == mode);
155 assert(res->alignment == align);
161 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
162 const ir_mode *mode, int align)
166 int hash = nodeset_hash(node);
167 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
169 assert(is_Phi(node));
172 res = set_find(env->spills, &spill, sizeof(spill), hash);
174 assert(res->mode == mode);
175 assert(res->alignment == align);
179 spill.spillslot = set_count(env->spills);
181 spill.alignment = align;
182 res = set_insert(env->spills, &spill, sizeof(spill), hash);
184 // collect attached spills and mem-phis
185 arity = get_irn_arity(node);
186 for(i = 0; i < arity; ++i) {
187 affinity_edge_t *affinty_edge;
188 ir_node *arg = get_irn_n(node, i);
192 arg_spill = collect_memphi(env, arg, mode, align);
194 arg_spill = collect_spill(env, arg, mode, align);
197 // add an affinity edge
198 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
199 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
200 affinty_edge->slot1 = res->spillslot;
201 affinty_edge->slot2 = arg_spill->spillslot;
202 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
208 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
209 const ir_mode *mode, int align)
211 ir_node *spillnode = get_memory_edge(node);
214 assert(spillnode != NULL);
216 if (is_Phi(spillnode)) {
217 spill = collect_memphi(env, spillnode, mode, align);
219 spill = collect_spill(env, spillnode, mode, align);
222 ARR_APP1(ir_node *, env->reloads, node);
227 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
228 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
229 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
230 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
233 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
234 int* spillslot_unionfind, int s1, int s2)
240 // merge spillslots and interferences
241 res = uf_union(spillslot_unionfind, s1, s2);
242 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
249 bitset_or(interferences[s1], interferences[s2]);
251 // update other interferences
252 spillcount = set_count(env->spills);
253 for(i = 0; i < spillcount; ++i) {
254 bitset_t *intfs = interferences[i];
255 if(bitset_is_set(intfs, s2))
256 bitset_set(intfs, s1);
263 * A greedy coalescing algorithm for spillslots:
264 * 1. Sort the list of affinity edges
265 * 2. Try to merge slots with affinity edges (most expensive slots first)
266 * 3. Try to merge everything else that is possible
268 static void do_greedy_coalescing(be_fec_env_t *env)
274 int affinity_edge_count;
275 bitset_t **interferences;
276 int* spillslot_unionfind;
278 spillcount = set_count(env->spills);
282 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
284 interferences = alloca(spillcount * sizeof(interferences[0]));
285 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
286 spilllist = alloca(spillcount * sizeof(spilllist[0]));
288 uf_init(spillslot_unionfind, 0, spillcount);
291 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
294 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
295 assert(spill->spillslot < spillcount);
296 spilllist[spill->spillslot] = spill;
299 for(i = 0; i < spillcount; ++i) {
300 interferences[i] = bitset_alloca(spillcount);
303 /* construct interferences */
304 for (i = 0; i < spillcount; ++i) {
305 ir_node *spill1 = spilllist[i]->spill;
307 if (is_NoMem(spill1))
310 for(i2 = i+1; i2 < spillcount; ++i2) {
311 ir_node *spill2 = spilllist[i2]->spill;
313 if (is_NoMem(spill2))
316 if (values_interfere(env->birg, spill1, spill2)) {
317 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
318 bitset_set(interferences[i], i2);
319 bitset_set(interferences[i2], i);
324 /* sort affinity edges */
325 affinity_edge_count = ARR_LEN(env->affinity_edges);
326 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
328 //dump_interference_graph(env, interferences, "before");
330 /* try to merge affine nodes */
331 for(i = 0; i < affinity_edge_count; ++i) {
332 const affinity_edge_t *edge = env->affinity_edges[i];
333 int s1 = uf_find(spillslot_unionfind, edge->slot1);
334 int s2 = uf_find(spillslot_unionfind, edge->slot2);
336 /* test if values interfere */
337 if (bitset_is_set(interferences[s1], s2)) {
338 assert(bitset_is_set(interferences[s2], s1));
342 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
344 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
347 // try to merge as much remaining spillslots as possible
348 for(i = 0; i < spillcount; ++i) {
349 int s1 = uf_find(spillslot_unionfind, i);
353 for(i2 = i+1; i2 < spillcount; ++i2) {
354 int s2 = uf_find(spillslot_unionfind, i2);
358 /* test if values interfere
359 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
360 * when node merging occurs
362 if(bitset_is_set(interferences[s1], s2)) {
363 assert(bitset_is_set(interferences[s2], s1));
367 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
369 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
370 // we can break the loop here, because s2 is the new supernode now
371 // and we'll test s2 again later anyway
377 // assign spillslots to spills
378 for(i = 0; i < spillcount; ++i) {
379 spill_t *spill = spilllist[i];
381 spill->spillslot = uf_find(spillslot_unionfind, i);
384 //dump_interference_graph(env, interferences, "after");
389 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
390 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
391 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
392 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
396 typedef struct _spill_slot_t {
402 typedef struct _memperm_entry_t {
407 struct _memperm_entry_t *next;
410 typedef struct _memperm_t {
413 memperm_entry_t *entries;
416 static int cmp_memperm(const void* d1, const void* d2, size_t size)
418 const memperm_t* e1 = d1;
419 const memperm_t* e2 = d2;
420 return e1->block != e2->block;
423 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
425 memperm_t entry, *res;
429 hash = nodeset_hash(block);
431 res = set_find(env->memperms, &entry, sizeof(entry), hash);
434 entry.entrycount = 0;
435 entry.entries = NULL;
436 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
442 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
444 ir_graph *irg = be_get_birg_irg(env->birg);
445 ir_type *frame = get_irg_frame_type(irg);
446 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
448 /* adjust size of the entity type... */
449 ir_type *enttype = get_entity_type(res);
450 set_type_size_bytes(enttype, slot->size);
458 * Enlarges a spillslot (if necessary) so that it can carry a value of size
459 * @p othersize and alignment @p otheralign.
461 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
463 if(othersize > slot->size) {
464 slot->size = othersize;
466 if(otheralign > slot->align) {
467 if(otheralign % slot->align != 0)
468 slot->align *= otheralign;
470 slot->align = otheralign;
471 } else if(slot->align % otheralign != 0) {
472 slot->align *= otheralign;
477 * Create stack entities for the spillslots and assign them to the spill and
480 static void assign_spillslots(be_fec_env_t *env)
482 const arch_env_t *arch_env = env->arch_env;
486 spill_slot_t* spillslots;
488 spillcount = set_count(env->spills);
489 spillslots = alloca(spillcount * sizeof(spillslots[0]));
491 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
493 // construct spillslots
494 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
495 int slotid = spill->spillslot;
496 const ir_mode *mode = spill->mode;
497 spill_slot_t *slot = & (spillslots[slotid]);
498 int size = get_mode_size_bytes(mode);
499 int align = spill->alignment;
501 if(slot->align == 0 && slot->size == 0) {
505 enlarge_spillslot(slot, align, size);
509 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
511 ir_node *node = spill->spill;
512 int slotid = spill->spillslot;
514 slot = &spillslots[slotid];
515 if(slot->entity == NULL) {
516 create_stack_entity(env, slot);
521 ir_node *block = get_nodes_block(node);
524 assert(is_Phi(node));
526 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
527 ir_node *arg = get_irn_n(node, i);
528 ir_node *predblock = get_Block_cfgpred_block(block, i);
532 argspill = get_spill(env, arg);
533 assert(argspill != NULL);
535 argslotid = argspill->spillslot;
536 if(slotid != argslotid) {
538 memperm_entry_t *entry;
539 spill_slot_t *argslot = &spillslots[argslotid];
540 if(argslot->entity == NULL) {
541 create_stack_entity(env, argslot);
544 memperm = get_memperm(env, predblock);
546 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
549 entry->in = argslot->entity;
550 entry->out = slot->entity;
551 entry->next = memperm->entries;
552 memperm->entrycount++;
553 memperm->entries = entry;
558 arch_set_frame_entity(arch_env, node, slot->entity);
562 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
563 ir_node* reload = env->reloads[i];
564 ir_node* spillnode = get_memory_edge(reload);
565 spill_t *spill = get_spill(env, spillnode);
566 const spill_slot_t *slot = & spillslots[spill->spillslot];
568 assert(slot->entity != NULL);
570 arch_set_frame_entity(arch_env, reload, slot->entity);
575 * Returns the last node in a block which is no control flow changing node
577 static ir_node *get_end_of_block_insertion_point(ir_node* block)
579 ir_node* ins = sched_last(block);
580 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
581 ins = sched_prev(ins);
587 ir_node *prev = sched_prev(ins);
597 static void create_memperms(be_fec_env_t *env)
599 const arch_env_t *arch_env = env->arch_env;
600 ir_graph *irg = be_get_birg_irg(env->birg);
603 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
605 memperm_entry_t *entry;
607 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
608 ir_node* mempermnode;
610 assert(memperm->entrycount > 0);
612 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
613 ir_node* arg = get_irn_n(entry->node, entry->pos);
617 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
618 memperm->entrycount, nodes);
620 // insert node into schedule
621 blockend = get_end_of_block_insertion_point(memperm->block);
622 sched_add_before(blockend, mempermnode);
623 be_stat_ev("mem_perm", memperm->entrycount);
626 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
628 ir_node* arg = get_irn_n(entry->node, entry->pos);
630 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
631 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
632 set_irg_current_block(irg, memperm->block);
633 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
634 sched_add_before(blockend, proj);
636 set_irn_n(entry->node, entry->pos, proj);
641 static int count_spillslots(const be_fec_env_t *env)
643 const spill_t *spill;
644 int spillcount = set_count(env->spills);
645 bitset_t *counted = bitset_alloca(spillcount);
649 for(spill = set_first(env->spills); spill != NULL;
650 spill = set_next(env->spills)) {
651 int spillslot = spill->spillslot;
652 if(!bitset_is_set(counted, spillslot)) {
654 bitset_set(counted, spillslot);
661 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
663 const arch_env_t *arch_env = birg->main_env->arch_env;
664 be_fec_env_t *env = xmalloc(sizeof(env[0]));
666 be_assure_liveness(birg);
668 obstack_init(&env->obst);
669 env->arch_env = arch_env;
671 env->spills = new_set(cmp_spill, 10);
672 env->reloads = NEW_ARR_F(ir_node*, 0);
673 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
674 env->memperms = new_set(cmp_memperm, 10);
679 void be_free_frame_entity_coalescer(be_fec_env_t *env)
681 del_set(env->memperms);
682 DEL_ARR_F(env->reloads);
683 DEL_ARR_F(env->affinity_edges);
684 del_set(env->spills);
685 obstack_free(&env->obst, NULL);
690 void be_assign_entities(be_fec_env_t *env)
692 if(be_stat_ev_is_active()) {
693 int count = set_count(env->spills);
694 be_stat_ev("spillslots", count);
697 if(be_coalesce_spill_slots) {
698 do_greedy_coalescing(env);
701 if(be_stat_ev_is_active()) {
702 int count = count_spillslots(env);
703 be_stat_ev("spillslots_after_coalescing", count);
706 assign_spillslots(env);
708 create_memperms(env);
712 * This walker function searches for reloads and collects all the spills
713 * and memphis attached to them.
715 static void collect_spills_walker(ir_node *node, void *data)
717 be_fec_env_t *env = data;
718 const arch_env_t *arch_env = env->arch_env;
720 const arch_register_class_t *cls;
723 /* classify returns classification of the irn the proj is attached to */
727 if (!arch_irn_class_is(arch_env, node, reload))
730 mode = get_irn_mode(node);
731 cls = arch_get_irn_reg_class(arch_env, node, -1);
732 align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
734 be_node_needs_frame_entity(env, node, mode, align);
737 void be_coalesce_spillslots(be_irg_t *birg)
739 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
741 /* collect reloads */
742 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
744 be_assign_entities(env);
746 be_free_frame_entity_coalescer(env);
749 void be_init_spillslots(void)
751 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
754 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);