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
21 * Author: Matthias Braun
23 * Copyright: (c) Universitaet Karlsruhe
24 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
38 #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"
52 #define DBG_COALESCING 1
53 #define DBG_INTERFERENCES 2
56 static firm_dbg_module_t *dbg = NULL;
59 typedef struct _spill_t {
61 /** mode of the spilled value */
63 /** alignment for the spilled value */
65 /** index into spillslot_unionfind unionfind structure */
69 typedef struct _affinity_edge_t {
74 struct _be_fec_env_t {
76 const arch_env_t *arch_env;
80 affinity_edge_t **affinity_edges;
84 /** Compare 2 affinity edges (used in quicksort) */
85 static int cmp_affinity(const void *d1, const void *d2)
87 const affinity_edge_t * const *e1 = d1;
88 const affinity_edge_t * const *e2 = d2;
90 // sort in descending order
91 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
94 static int cmp_spill(const void* d1, const void* d2, size_t size)
96 const spill_t* s1 = d1;
97 const spill_t* s2 = d2;
98 return s1->spill != s2->spill;
101 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
104 int hash = nodeset_hash(node);
107 res = set_find(env->spills, &spill, sizeof(spill), hash);
113 * ____ _ _ _ ____ _ _ _
114 * / ___|___ | | | ___ ___| |_ / ___| _ __ (_) | |___
115 * | | / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
116 * | |__| (_) | | | __/ (__| |_ ___) | |_) | | | \__ \
117 * \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
121 static INLINE ir_node *get_memory_edge(const ir_node *node)
125 arity = get_irn_arity(node);
126 for(i = arity - 1; i >= 0; --i) {
127 ir_node *arg = get_irn_n(node, i);
128 if(get_irn_mode(arg) == mode_M)
135 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
136 const ir_mode *mode, int align)
139 int hash = nodeset_hash(node);
141 /* insert into set of spills if not already there */
143 res = set_find(env->spills, &spill, sizeof(spill), hash);
146 spill.spillslot = set_count(env->spills);
148 spill.alignment = align;
149 res = set_insert(env->spills, &spill, sizeof(spill), hash);
151 assert(res->mode == mode);
152 assert(res->alignment == align);
158 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
159 const ir_mode *mode, int align)
163 int hash = nodeset_hash(node);
164 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
166 assert(is_Phi(node));
169 res = set_find(env->spills, &spill, sizeof(spill), hash);
171 assert(res->mode == mode);
172 assert(res->alignment == align);
176 spill.spillslot = set_count(env->spills);
178 spill.alignment = align;
179 res = set_insert(env->spills, &spill, sizeof(spill), hash);
181 // collect attached spills and mem-phis
182 arity = get_irn_arity(node);
183 for(i = 0; i < arity; ++i) {
184 affinity_edge_t *affinty_edge;
185 ir_node *arg = get_irn_n(node, i);
189 arg_spill = collect_memphi(env, arg, mode, align);
191 arg_spill = collect_spill(env, arg, mode, align);
194 // add an affinity edge
195 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
196 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
197 affinty_edge->slot1 = res->spillslot;
198 affinty_edge->slot2 = arg_spill->spillslot;
199 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
205 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
206 const ir_mode *mode, int align)
208 ir_node *spillnode = get_memory_edge(node);
211 assert(spillnode != NULL);
213 if (is_Phi(spillnode)) {
214 spill = collect_memphi(env, spillnode, mode, align);
216 spill = collect_spill(env, spillnode, mode, align);
219 ARR_APP1(ir_node *, env->reloads, node);
224 * / ___|___ __ _| | ___ ___ ___ ___ / ___|| | ___ | |_ ___
225 * | | / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
226 * | |__| (_) | (_| | | __/\__ \ (_| __/ ___) | | (_) | |_\__ \
227 * \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
230 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
231 int* spillslot_unionfind, int s1, int s2)
237 // merge spillslots and interferences
238 res = uf_union(spillslot_unionfind, s1, s2);
239 // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
246 bitset_or(interferences[s1], interferences[s2]);
248 // update other interferences
249 spillcount = set_count(env->spills);
250 for(i = 0; i < spillcount; ++i) {
251 bitset_t *intfs = interferences[i];
252 if(bitset_is_set(intfs, s2))
253 bitset_set(intfs, s1);
260 * A greedy coalescing algorithm for spillslots:
261 * 1. Sort the list of affinity edges
262 * 2. Try to merge slots with affinity edges (most expensive slots first)
263 * 3. Try to merge everything else that is possible
265 static void do_greedy_coalescing(be_fec_env_t *env)
271 int affinity_edge_count;
272 bitset_t **interferences;
273 int* spillslot_unionfind;
274 const be_lv_t *lv = be_get_birg_liveness(env->birg);
276 spillcount = set_count(env->spills);
280 DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
282 interferences = alloca(spillcount * sizeof(interferences[0]));
283 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
284 spilllist = alloca(spillcount * sizeof(spilllist[0]));
286 uf_init(spillslot_unionfind, 0, spillcount);
289 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
292 for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
293 assert(spill->spillslot < spillcount);
294 spilllist[spill->spillslot] = spill;
297 for(i = 0; i < spillcount; ++i) {
298 interferences[i] = bitset_alloca(spillcount);
301 // construct interferences
302 for(i = 0; i < spillcount; ++i) {
303 ir_node *spill1 = spilllist[i]->spill;
307 for(i2 = i+1; i2 < spillcount; ++i2) {
308 ir_node *spill2 = spilllist[i2]->spill;
312 if(values_interfere(lv, spill1, spill2)) {
313 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n",
315 bitset_set(interferences[i], i2);
316 bitset_set(interferences[i2], i);
321 // sort affinity edges
322 affinity_edge_count = ARR_LEN(env->affinity_edges);
323 qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
325 //dump_interference_graph(env, interferences, "before");
327 // try to merge affine nodes
328 for(i = 0; i < affinity_edge_count; ++i) {
329 const affinity_edge_t *edge = env->affinity_edges[i];
330 int s1 = uf_find(spillslot_unionfind, edge->slot1);
331 int s2 = uf_find(spillslot_unionfind, edge->slot2);
333 /* test if values interfere */
334 if(bitset_is_set(interferences[s1], s2)) {
335 assert(bitset_is_set(interferences[s2], s1));
339 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
341 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
344 // try to merge as much remaining spillslots as possible
345 for(i = 0; i < spillcount; ++i) {
346 int s1 = uf_find(spillslot_unionfind, i);
350 for(i2 = i+1; i2 < spillcount; ++i2) {
351 int s2 = uf_find(spillslot_unionfind, i2);
355 /* test if values interfere
356 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
357 * when node merging occurs
359 if(bitset_is_set(interferences[s1], s2)) {
360 assert(bitset_is_set(interferences[s2], s1));
364 DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
366 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
367 // we can break the loop here, because s2 is the new supernode now
368 // and we'll test s2 again later anyway
374 // assign spillslots to spills
375 for(i = 0; i < spillcount; ++i) {
376 spill_t *spill = spilllist[i];
378 spill->spillslot = uf_find(spillslot_unionfind, i);
381 //dump_interference_graph(env, interferences, "after");
386 * / \ ___ ___(_) __ _ _ __ | ____|_ __ | |_(_) |_(_) ___ ___
387 * / _ \ / __/ __| |/ _` | '_ \ | _| | '_ \| __| | __| |/ _ \/ __|
388 * / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| | __/\__ \
389 * /_/ \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
393 typedef struct _spill_slot_t {
399 typedef struct _memperm_entry_t {
404 struct _memperm_entry_t *next;
407 typedef struct _memperm_t {
410 memperm_entry_t *entries;
413 static int cmp_memperm(const void* d1, const void* d2, size_t size)
415 const memperm_t* e1 = d1;
416 const memperm_t* e2 = d2;
417 return e1->block != e2->block;
420 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
422 memperm_t entry, *res;
426 hash = nodeset_hash(block);
428 res = set_find(env->memperms, &entry, sizeof(entry), hash);
431 entry.entrycount = 0;
432 entry.entries = NULL;
433 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
439 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
441 ir_graph *irg = be_get_birg_irg(env->birg);
442 ir_type *frame = get_irg_frame_type(irg);
443 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
445 // adjust size of the entity type...
446 ir_type *enttype = get_entity_type(res);
447 set_type_size_bytes(enttype, slot->size);
455 * Enlarges a spillslot (if necessary) so that it can carry a value of size
456 * @p othersize and alignment @p otheralign.
458 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
460 if(othersize > slot->size) {
461 slot->size = othersize;
463 if(otheralign > slot->align) {
464 if(otheralign % slot->align != 0)
465 slot->align *= otheralign;
467 slot->align = otheralign;
468 } else if(slot->align % otheralign != 0) {
469 slot->align *= otheralign;
474 * Create stack entities for the spillslots and assign them to the spill and
477 static void assign_spillslots(be_fec_env_t *env)
479 const arch_env_t *arch_env = env->arch_env;
483 spill_slot_t* spillslots;
485 spillcount = set_count(env->spills);
486 spillslots = alloca(spillcount * sizeof(spillslots[0]));
488 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
490 // construct spillslots
491 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
492 int slotid = spill->spillslot;
493 const ir_mode *mode = spill->mode;
494 spill_slot_t *slot = & (spillslots[slotid]);
495 int size = get_mode_size_bytes(mode);
496 int align = spill->alignment;
498 if(slot->align == 0 && slot->size == 0) {
502 enlarge_spillslot(slot, align, size);
506 for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
508 ir_node *node = spill->spill;
509 int slotid = spill->spillslot;
511 slot = &spillslots[slotid];
512 if(slot->entity == NULL) {
513 create_stack_entity(env, slot);
518 ir_node *block = get_nodes_block(node);
521 assert(is_Phi(node));
523 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
524 ir_node *arg = get_irn_n(node, i);
525 ir_node *predblock = get_Block_cfgpred_block(block, i);
529 argspill = get_spill(env, arg);
530 assert(argspill != NULL);
532 argslotid = argspill->spillslot;
533 if(slotid != argslotid) {
535 memperm_entry_t *entry;
536 spill_slot_t *argslot = &spillslots[argslotid];
537 if(argslot->entity == NULL) {
538 create_stack_entity(env, argslot);
541 memperm = get_memperm(env, predblock);
543 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
546 entry->in = argslot->entity;
547 entry->out = slot->entity;
548 entry->next = memperm->entries;
549 memperm->entrycount++;
550 memperm->entries = entry;
555 arch_set_frame_entity(arch_env, node, slot->entity);
559 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
560 ir_node* reload = env->reloads[i];
561 ir_node* spillnode = get_memory_edge(reload);
562 spill_t *spill = get_spill(env, spillnode);
563 const spill_slot_t *slot = & spillslots[spill->spillslot];
565 assert(slot->entity != NULL);
567 arch_set_frame_entity(arch_env, reload, slot->entity);
572 * Returns the last node in a block which is no control flow changing node
574 static ir_node *get_end_of_block_insertion_point(ir_node* block)
576 ir_node* ins = sched_last(block);
577 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
578 ins = sched_prev(ins);
584 ir_node *prev = sched_prev(ins);
594 static void create_memperms(be_fec_env_t *env)
596 const arch_env_t *arch_env = env->arch_env;
597 ir_graph *irg = be_get_birg_irg(env->birg);
600 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
602 memperm_entry_t *entry;
604 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
605 ir_node* mempermnode;
607 assert(memperm->entrycount > 0);
609 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
610 ir_node* arg = get_irn_n(entry->node, entry->pos);
614 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
615 memperm->entrycount, nodes);
617 // insert node into schedule
618 blockend = get_end_of_block_insertion_point(memperm->block);
619 sched_add_before(blockend, mempermnode);
620 be_stat_ev("mem_perm", memperm->entrycount);
623 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
625 ir_node* arg = get_irn_n(entry->node, entry->pos);
627 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
628 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
629 set_irg_current_block(irg, memperm->block);
630 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
631 sched_add_before(blockend, proj);
633 set_irn_n(entry->node, entry->pos, proj);
638 static int count_spillslots(const be_fec_env_t *env)
640 const spill_t *spill;
641 int spillcount = set_count(env->spills);
642 bitset_t *counted = bitset_alloca(spillcount);
646 for(spill = set_first(env->spills); spill != NULL;
647 spill = set_next(env->spills)) {
648 int spillslot = spill->spillslot;
649 if(!bitset_is_set(counted, spillslot)) {
651 bitset_set(counted, spillslot);
658 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
660 const arch_env_t *arch_env = birg->main_env->arch_env;
662 be_fec_env_t *env = xmalloc(sizeof(env[0]));
664 be_assure_liveness(birg);
666 obstack_init(&env->obst);
667 env->arch_env = arch_env;
669 env->spills = new_set(cmp_spill, 10);
670 env->reloads = NEW_ARR_F(ir_node*, 0);
671 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
672 env->memperms = new_set(cmp_memperm, 10);
677 void be_free_frame_entity_coalescer(be_fec_env_t *env)
679 del_set(env->memperms);
680 DEL_ARR_F(env->reloads);
681 DEL_ARR_F(env->affinity_edges);
682 del_set(env->spills);
683 obstack_free(&env->obst, NULL);
688 void be_assign_entities(be_fec_env_t *env)
690 if(be_stat_ev_is_active()) {
691 int count = set_count(env->spills);
692 be_stat_ev("spillslots", count);
695 if(be_coalesce_spill_slots) {
696 do_greedy_coalescing(env);
699 if(be_stat_ev_is_active()) {
700 int count = count_spillslots(env);
701 be_stat_ev("spillslots_after_coalescing", count);
704 assign_spillslots(env);
706 create_memperms(env);
710 * This walker function searches for reloads and collects all the spills
711 * and memphis attached to them.
713 static void collect_spills_walker(ir_node *node, void *data)
715 be_fec_env_t *env = data;
716 const arch_env_t *arch_env = env->arch_env;
718 const arch_register_class_t *cls;
721 /* classify returns classification of the irn the proj is attached to */
725 if (!arch_irn_class_is(arch_env, node, reload))
728 mode = get_irn_mode(node);
729 cls = arch_get_irn_reg_class(arch_env, node, -1);
730 align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
732 be_node_needs_frame_entity(env, node, mode, align);
735 void be_coalesce_spillslots(be_irg_t *birg)
737 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
739 /* collect reloads */
740 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
742 be_assign_entities(env);
744 be_free_frame_entity_coalescer(env);
747 void be_init_spillslots(void)
749 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
752 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);