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;
103 return s1->spill != s2->spill;
106 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
109 int hash = nodeset_hash(node);
112 res = set_find(env->spills, &spill, sizeof(spill), hash);
118 * ____ _ _ _ ____ _ _ _
119 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
120 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
121 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
122 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
126 static INLINE ir_node *get_memory_edge(const ir_node *node)
130 arity = get_irn_arity(node);
131 for(i = arity - 1; i >= 0; --i) {
132 ir_node *arg = get_irn_n(node, i);
133 if(get_irn_mode(arg) == mode_M)
140 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
141 const ir_mode *mode, int align)
144 int hash = nodeset_hash(node);
146 /* insert into set of spills if not already there */
148 res = set_find(env->spills, &spill, sizeof(spill), hash);
151 spill.spillslot = set_count(env->spills);
153 spill.alignment = align;
154 res = set_insert(env->spills, &spill, sizeof(spill), hash);
156 assert(res->mode == mode);
157 assert(res->alignment == align);
163 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
164 const ir_mode *mode, int align)
168 int hash = nodeset_hash(node);
169 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
171 assert(is_Phi(node));
174 res = set_find(env->spills, &spill, sizeof(spill), hash);
176 assert(res->mode == mode);
177 assert(res->alignment == align);
181 spill.spillslot = set_count(env->spills);
183 spill.alignment = align;
184 res = set_insert(env->spills, &spill, sizeof(spill), hash);
186 // collect attached spills and mem-phis
187 arity = get_irn_arity(node);
188 for(i = 0; i < arity; ++i) {
189 affinity_edge_t *affinty_edge;
190 ir_node *arg = get_irn_n(node, i);
194 arg_spill = collect_memphi(env, arg, mode, align);
196 arg_spill = collect_spill(env, arg, mode, align);
199 // add an affinity edge
200 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
201 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
202 affinty_edge->slot1 = res->spillslot;
203 affinty_edge->slot2 = arg_spill->spillslot;
204 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
210 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
211 const ir_mode *mode, int align)
213 ir_node *spillnode = get_memory_edge(node);
216 assert(spillnode != NULL);
218 if (is_Phi(spillnode)) {
219 spill = collect_memphi(env, spillnode, mode, align);
221 spill = collect_spill(env, spillnode, mode, align);
224 ARR_APP1(ir_node *, env->reloads, node);
229 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
230 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
231 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
232 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
235 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
236 int* spillslot_unionfind, int s1, int s2)
242 // merge spillslots and interferences
243 res = uf_union(spillslot_unionfind, s1, s2);
244 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
251 bitset_or(interferences[s1], interferences[s2]);
253 // update other interferences
254 spillcount = set_count(env->spills);
255 for(i = 0; i < spillcount; ++i) {
256 bitset_t *intfs = interferences[i];
257 if(bitset_is_set(intfs, s2))
258 bitset_set(intfs, s1);
265 * A greedy coalescing algorithm for spillslots:
266 * 1. Sort the list of affinity edges
267 * 2. Try to merge slots with affinity edges (most expensive slots first)
268 * 3. Try to merge everything else that is possible
270 static void do_greedy_coalescing(be_fec_env_t *env)
276 int affinity_edge_count;
277 bitset_t **interferences;
278 int* spillslot_unionfind;
280 spillcount = set_count(env->spills);
284 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
286 interferences = alloca(spillcount * sizeof(interferences[0]));
287 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
288 spilllist = alloca(spillcount * sizeof(spilllist[0]));
290 uf_init(spillslot_unionfind, 0, spillcount);
293 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
296 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
297 assert(spill->spillslot < spillcount);
298 spilllist[spill->spillslot] = spill;
301 for(i = 0; i < spillcount; ++i) {
302 interferences[i] = bitset_alloca(spillcount);
305 /* construct interferences */
306 for (i = 0; i < spillcount; ++i) {
307 ir_node *spill1 = spilllist[i]->spill;
309 if (is_NoMem(spill1))
312 for(i2 = i+1; i2 < spillcount; ++i2) {
313 ir_node *spill2 = spilllist[i2]->spill;
315 if (is_NoMem(spill2))
318 if (values_interfere(env->birg, spill1, spill2)) {
319 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
320 bitset_set(interferences[i], i2);
321 bitset_set(interferences[i2], i);
326 /* sort affinity edges */
327 affinity_edge_count = ARR_LEN(env->affinity_edges);
328 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
330 //dump_interference_graph(env, interferences, "before");
332 /* try to merge affine nodes */
333 for(i = 0; i < affinity_edge_count; ++i) {
334 const affinity_edge_t *edge = env->affinity_edges[i];
335 int s1 = uf_find(spillslot_unionfind, edge->slot1);
336 int s2 = uf_find(spillslot_unionfind, edge->slot2);
338 /* test if values interfere */
339 if (bitset_is_set(interferences[s1], s2)) {
340 assert(bitset_is_set(interferences[s2], s1));
344 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
346 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
349 // try to merge as much remaining spillslots as possible
350 for(i = 0; i < spillcount; ++i) {
351 int s1 = uf_find(spillslot_unionfind, i);
355 for(i2 = i+1; i2 < spillcount; ++i2) {
356 int s2 = uf_find(spillslot_unionfind, i2);
360 /* test if values interfere
361 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
362 * when node merging occurs
364 if(bitset_is_set(interferences[s1], s2)) {
365 assert(bitset_is_set(interferences[s2], s1));
369 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
371 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
372 // we can break the loop here, because s2 is the new supernode now
373 // and we'll test s2 again later anyway
379 // assign spillslots to spills
380 for(i = 0; i < spillcount; ++i) {
381 spill_t *spill = spilllist[i];
383 spill->spillslot = uf_find(spillslot_unionfind, i);
386 //dump_interference_graph(env, interferences, "after");
391 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
392 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
393 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
394 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
398 typedef struct _spill_slot_t {
404 typedef struct _memperm_entry_t {
409 struct _memperm_entry_t *next;
412 typedef struct _memperm_t {
415 memperm_entry_t *entries;
418 static int cmp_memperm(const void* d1, const void* d2, size_t size)
420 const memperm_t* e1 = d1;
421 const memperm_t* e2 = d2;
424 return e1->block != e2->block;
427 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
429 memperm_t entry, *res;
433 hash = nodeset_hash(block);
435 res = set_find(env->memperms, &entry, sizeof(entry), hash);
438 entry.entrycount = 0;
439 entry.entries = NULL;
440 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
446 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
448 ir_graph *irg = be_get_birg_irg(env->birg);
449 ir_type *frame = get_irg_frame_type(irg);
450 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
452 /* adjust size of the entity type... */
453 ir_type *enttype = get_entity_type(res);
454 set_type_size_bytes(enttype, slot->size);
462 * Enlarges a spillslot (if necessary) so that it can carry a value of size
463 * @p othersize and alignment @p otheralign.
465 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
467 if(othersize > slot->size) {
468 slot->size = othersize;
470 if(otheralign > slot->align) {
471 if(otheralign % slot->align != 0)
472 slot->align *= otheralign;
474 slot->align = otheralign;
475 } else if(slot->align % otheralign != 0) {
476 slot->align *= otheralign;
481 * Create stack entities for the spillslots and assign them to the spill and
484 static void assign_spillslots(be_fec_env_t *env)
486 const arch_env_t *arch_env = env->arch_env;
490 spill_slot_t* spillslots;
492 spillcount = set_count(env->spills);
493 spillslots = alloca(spillcount * sizeof(spillslots[0]));
495 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
497 // construct spillslots
498 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
499 int slotid = spill->spillslot;
500 const ir_mode *mode = spill->mode;
501 spill_slot_t *slot = & (spillslots[slotid]);
502 int size = get_mode_size_bytes(mode);
503 int align = spill->alignment;
505 if(slot->align == 0 && slot->size == 0) {
509 enlarge_spillslot(slot, align, size);
513 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
515 ir_node *node = spill->spill;
516 int slotid = spill->spillslot;
518 slot = &spillslots[slotid];
519 if(slot->entity == NULL) {
520 create_stack_entity(env, slot);
525 ir_node *block = get_nodes_block(node);
528 assert(is_Phi(node));
530 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
531 ir_node *arg = get_irn_n(node, i);
532 ir_node *predblock = get_Block_cfgpred_block(block, i);
536 argspill = get_spill(env, arg);
537 assert(argspill != NULL);
539 argslotid = argspill->spillslot;
540 if(slotid != argslotid) {
542 memperm_entry_t *entry;
543 spill_slot_t *argslot = &spillslots[argslotid];
544 if(argslot->entity == NULL) {
545 create_stack_entity(env, argslot);
548 memperm = get_memperm(env, predblock);
550 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
553 entry->in = argslot->entity;
554 entry->out = slot->entity;
555 entry->next = memperm->entries;
556 memperm->entrycount++;
557 memperm->entries = entry;
562 arch_set_frame_entity(arch_env, node, slot->entity);
566 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
567 ir_node* reload = env->reloads[i];
568 ir_node* spillnode = get_memory_edge(reload);
569 spill_t *spill = get_spill(env, spillnode);
570 const spill_slot_t *slot = & spillslots[spill->spillslot];
572 assert(slot->entity != NULL);
574 arch_set_frame_entity(arch_env, reload, slot->entity);
579 * Returns the last node in a block which is no control flow changing node
581 static ir_node *get_end_of_block_insertion_point(ir_node* block)
583 ir_node* ins = sched_last(block);
584 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
585 ins = sched_prev(ins);
591 ir_node *prev = sched_prev(ins);
601 static void create_memperms(be_fec_env_t *env)
603 const arch_env_t *arch_env = env->arch_env;
604 ir_graph *irg = be_get_birg_irg(env->birg);
607 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
609 memperm_entry_t *entry;
611 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
612 ir_node* mempermnode;
614 assert(memperm->entrycount > 0);
616 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
617 ir_node* arg = get_irn_n(entry->node, entry->pos);
621 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
622 memperm->entrycount, nodes);
624 // insert node into schedule
625 blockend = get_end_of_block_insertion_point(memperm->block);
626 sched_add_before(blockend, mempermnode);
627 stat_ev_dbl("mem_perm", memperm->entrycount);
630 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
632 ir_node* arg = get_irn_n(entry->node, entry->pos);
634 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
635 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
636 set_irg_current_block(irg, memperm->block);
637 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
639 set_irn_n(entry->node, entry->pos, proj);
644 static int count_spillslots(const be_fec_env_t *env)
646 const spill_t *spill;
647 int spillcount = set_count(env->spills);
648 bitset_t *counted = bitset_alloca(spillcount);
652 for(spill = set_first(env->spills); spill != NULL;
653 spill = set_next(env->spills)) {
654 int spillslot = spill->spillslot;
655 if(!bitset_is_set(counted, spillslot)) {
657 bitset_set(counted, spillslot);
664 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
666 const arch_env_t *arch_env = birg->main_env->arch_env;
667 be_fec_env_t *env = xmalloc(sizeof(env[0]));
669 be_liveness_assure_chk(be_assure_liveness(birg));
671 obstack_init(&env->obst);
672 env->arch_env = arch_env;
674 env->spills = new_set(cmp_spill, 10);
675 env->reloads = NEW_ARR_F(ir_node*, 0);
676 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
677 env->memperms = new_set(cmp_memperm, 10);
682 void be_free_frame_entity_coalescer(be_fec_env_t *env)
684 del_set(env->memperms);
685 DEL_ARR_F(env->reloads);
686 DEL_ARR_F(env->affinity_edges);
687 del_set(env->spills);
688 obstack_free(&env->obst, NULL);
693 void be_assign_entities(be_fec_env_t *env)
695 stat_ev_dbl("spillslots", set_count(env->spills));
697 if(be_coalesce_spill_slots) {
698 do_greedy_coalescing(env);
701 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
703 assign_spillslots(env);
705 create_memperms(env);
709 * This walker function searches for reloads and collects all the spills
710 * and memphis attached to them.
712 static void collect_spills_walker(ir_node *node, void *data)
714 be_fec_env_t *env = data;
715 const arch_env_t *arch_env = env->arch_env;
717 const arch_register_class_t *cls;
720 /* classify returns classification of the irn the proj is attached to */
724 if (!arch_irn_class_is(arch_env, node, reload))
727 mode = get_irn_mode(node);
728 cls = arch_get_irn_reg_class(arch_env, node, -1);
729 align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
731 be_node_needs_frame_entity(env, node, mode, align);
734 void be_coalesce_spillslots(be_irg_t *birg)
736 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
738 /* collect reloads */
739 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
741 be_assign_entities(env);
743 be_free_frame_entity_coalescer(env);
746 void be_init_spillslots(void)
748 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
751 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);