2 * Copyright (C) 1995-2008 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
37 #include "unionfind.h"
42 #include "bespillslots.h"
43 #include "bechordal_t.h"
44 #include "bestatevent.h"
45 #include "bespilloptions.h"
47 #include "beintlive_t.h"
51 #define DBG_COALESCING 1
52 #define DBG_INTERFERENCES 2
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56 typedef struct _spill_t {
58 const ir_mode *mode; /**< mode of the spilled value */
59 int alignment; /**< alignment for the spilled value */
60 int spillslot; /**< index into spillslot_unionfind structure */
63 typedef struct _affinity_edge_t {
68 struct _be_fec_env_t {
70 const arch_env_t *arch_env;
74 affinity_edge_t **affinity_edges;
78 /** Compare 2 affinity edges (used in quicksort) */
79 static int cmp_affinity(const void *d1, const void *d2)
81 const affinity_edge_t * const *e1 = d1;
82 const affinity_edge_t * const *e2 = d2;
84 /* sort in descending order */
85 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
88 static int cmp_spill(const void* d1, const void* d2, size_t size)
90 const spill_t* s1 = d1;
91 const spill_t* s2 = d2;
94 return s1->spill != s2->spill;
97 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
100 int hash = hash_irn(node);
103 res = set_find(env->spills, &spill, sizeof(spill), hash);
109 static inline ir_node *get_memory_edge(const ir_node *node)
113 arity = get_irn_arity(node);
114 for(i = arity - 1; i >= 0; --i) {
115 ir_node *arg = get_irn_n(node, i);
116 if(get_irn_mode(arg) == mode_M)
123 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
124 const ir_mode *mode, int align)
127 int hash = hash_irn(node);
129 /* insert into set of spills if not already there */
131 res = set_find(env->spills, &spill, sizeof(spill), hash);
134 spill.spillslot = set_count(env->spills);
136 spill.alignment = align;
137 res = set_insert(env->spills, &spill, sizeof(spill), hash);
138 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
140 assert(res->mode == mode);
141 assert(res->alignment == align);
147 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
148 const ir_mode *mode, int align)
152 int hash = hash_irn(node);
153 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
155 assert(is_Phi(node));
158 res = set_find(env->spills, &spill, sizeof(spill), hash);
160 assert(res->mode == mode);
161 assert(res->alignment == align);
165 spill.spillslot = set_count(env->spills);
167 spill.alignment = align;
168 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
169 res = set_insert(env->spills, &spill, sizeof(spill), hash);
171 /* collect attached spills and mem-phis */
172 arity = get_irn_arity(node);
173 for(i = 0; i < arity; ++i) {
174 affinity_edge_t *affinty_edge;
175 ir_node *arg = get_irn_n(node, i);
179 arg_spill = collect_memphi(env, arg, mode, align);
181 arg_spill = collect_spill(env, arg, mode, align);
184 /* add an affinity edge */
185 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
186 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
187 affinty_edge->slot1 = res->spillslot;
188 affinty_edge->slot2 = arg_spill->spillslot;
189 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
195 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
196 const ir_mode *mode, int align)
198 ir_node *spillnode = get_memory_edge(node);
201 assert(spillnode != NULL);
203 /* walk upwards and collect all phis and spills on this way */
204 if (is_Phi(spillnode)) {
205 spill = collect_memphi(env, spillnode, mode, align);
207 spill = collect_spill(env, spillnode, mode, align);
210 ARR_APP1(ir_node *, env->reloads, node);
215 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
216 int* spillslot_unionfind, int s1, int s2)
222 /* merge spillslots and interferences */
223 res = uf_union(spillslot_unionfind, s1, s2);
224 /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
231 bitset_or(interferences[s1], interferences[s2]);
233 /* update other interferences */
234 spillcount = set_count(env->spills);
235 for(i = 0; i < spillcount; ++i) {
236 bitset_t *intfs = interferences[i];
237 if(bitset_is_set(intfs, s2))
238 bitset_set(intfs, s1);
244 static int my_values_interfere2(be_irg_t *birg, const ir_node *a,
247 be_lv_t *lv = be_get_birg_liveness(birg);
249 int a2b = _value_dominates(a, b);
250 int b2a = _value_dominates(b, a);
252 /* If there is no dominance relation, they do not interfere. */
253 if((a2b | b2a) > 0) {
254 const ir_edge_t *edge;
258 * Adjust a and b so, that a dominates b if
259 * a dominates b or vice versa.
262 const ir_node *t = a;
267 bb = get_nodes_block(b);
270 * If a is live end in b's block it is
271 * live at b's definition (a dominates b)
273 if(be_is_live_end(lv, bb, a))
277 * Look at all usages of a.
278 * If there's one usage of a in the block of b, then
279 * we check, if this use is dominated by b, if that's true
280 * a and b interfere. Note that b must strictly dominate the user,
281 * since if b is the last user of in the block, b and a do not
283 * Uses of a not in b's block can be disobeyed, because the
284 * check for a being live at the end of b's block is already
287 foreach_out_edge(a, edge) {
288 const ir_node *user = get_edge_src_irn(edge);
290 const ir_edge_t *edge2;
291 foreach_out_edge(user, edge2) {
292 const ir_node *user2 = get_edge_src_irn(edge2);
293 assert(!is_Sync(user2));
294 if(get_nodes_block(user2) == bb && !is_Phi(user2) &&
295 _value_strictly_dominates(b, user2))
299 if(get_nodes_block(user) == bb && !is_Phi(user) &&
300 _value_strictly_dominates(b, user))
310 * same as values_interfere but with special handling for Syncs
312 static int my_values_interfere(be_irg_t *birg, ir_node *a, ir_node *b)
315 int i, arity = get_irn_arity(a);
316 for(i = 0; i < arity; ++i) {
317 ir_node *in = get_irn_n(a, i);
318 if(my_values_interfere(birg, in, b))
322 } else if(is_Sync(b)) {
323 int i, arity = get_irn_arity(b);
324 for(i = 0; i < arity; ++i) {
325 ir_node *in = get_irn_n(b, i);
326 /* a is not a sync, so no need for my_values_interfere */
327 if(my_values_interfere2(birg, a, in))
333 return my_values_interfere2(birg, a, b);
337 * A greedy coalescing algorithm for spillslots:
338 * 1. Sort the list of affinity edges
339 * 2. Try to merge slots with affinity edges (most expensive slots first)
340 * 3. Try to merge everything else that is possible
342 static void do_greedy_coalescing(be_fec_env_t *env)
348 int affinity_edge_count;
349 bitset_t **interferences;
350 int* spillslot_unionfind;
352 spillcount = set_count(env->spills);
356 DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
358 interferences = ALLOCAN(bitset_t*, spillcount);
359 spillslot_unionfind = ALLOCAN(int, spillcount);
360 spilllist = ALLOCAN(spill_t*, spillcount);
362 uf_init(spillslot_unionfind, spillcount);
365 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
368 for(spill = set_first(env->spills), i = 0; spill != NULL;
369 spill = set_next(env->spills), ++i) {
370 assert(spill->spillslot < spillcount);
371 spilllist[spill->spillslot] = spill;
374 for(i = 0; i < spillcount; ++i) {
375 interferences[i] = bitset_alloca(spillcount);
378 /* construct interferences */
379 for (i = 0; i < spillcount; ++i) {
380 ir_node *spill1 = spilllist[i]->spill;
382 if (is_NoMem(spill1))
385 for(i2 = i+1; i2 < spillcount; ++i2) {
386 ir_node *spill2 = spilllist[i2]->spill;
388 if (is_NoMem(spill2))
391 if (my_values_interfere(env->birg, spill1, spill2)) {
392 DB((dbg, DBG_INTERFERENCES,
393 "Slot %d and %d interfere\n", i, i2));
395 bitset_set(interferences[i], i2);
396 bitset_set(interferences[i2], i);
401 /* sort affinity edges */
402 affinity_edge_count = ARR_LEN(env->affinity_edges);
403 qsort(env->affinity_edges, affinity_edge_count,
404 sizeof(env->affinity_edges[0]), cmp_affinity);
406 /*dump_interference_graph(env, interferences, "before"); */
408 /* try to merge affine nodes */
409 for(i = 0; i < affinity_edge_count; ++i) {
410 const affinity_edge_t *edge = env->affinity_edges[i];
411 int s1 = uf_find(spillslot_unionfind, edge->slot1);
412 int s2 = uf_find(spillslot_unionfind, edge->slot2);
414 /* test if values interfere */
415 if (bitset_is_set(interferences[s1], s2)) {
416 assert(bitset_is_set(interferences[s2], s1));
420 DB((dbg, DBG_COALESCING,
421 "Merging %d and %d because of affinity edge\n", s1, s2));
423 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
426 /* try to merge as much remaining spillslots as possible */
427 for(i = 0; i < spillcount; ++i) {
428 int s1 = uf_find(spillslot_unionfind, i);
432 for(i2 = i+1; i2 < spillcount; ++i2) {
433 int s2 = uf_find(spillslot_unionfind, i2);
437 /* test if values interfere
438 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
439 * when node merging occurs
441 if(bitset_is_set(interferences[s1], s2)) {
442 assert(bitset_is_set(interferences[s2], s1));
446 DB((dbg, DBG_COALESCING,
447 "Merging %d and %d because it is possible\n", s1, s2));
449 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
450 /* we can break the loop here, because s2 is the new supernode
451 * now and we'll test s2 again later anyway */
457 /* assign spillslots to spills */
458 for(i = 0; i < spillcount; ++i) {
459 spill_t *spill = spilllist[i];
461 spill->spillslot = uf_find(spillslot_unionfind, i);
464 /*dump_interference_graph(env, interferences, "after");*/
469 typedef struct _spill_slot_t {
475 typedef struct _memperm_entry_t {
480 struct _memperm_entry_t *next;
483 typedef struct _memperm_t {
486 memperm_entry_t *entries;
489 static int cmp_memperm(const void* d1, const void* d2, size_t size)
491 const memperm_t* e1 = d1;
492 const memperm_t* e2 = d2;
495 return e1->block != e2->block;
498 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
500 memperm_t entry, *res;
504 hash = hash_irn(block);
506 res = set_find(env->memperms, &entry, sizeof(entry), hash);
509 entry.entrycount = 0;
510 entry.entries = NULL;
511 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
517 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
519 ir_graph *irg = be_get_birg_irg(env->birg);
520 ir_type *frame = get_irg_frame_type(irg);
521 /* TODO: backend should be able to specify wether we want spill slots
522 * at begin or end of frame */
524 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
526 /* adjust size of the entity type... */
527 ir_type *enttype = get_entity_type(res);
528 set_type_size_bytes(enttype, slot->size);
536 * Enlarges a spillslot (if necessary) so that it can carry a value of size
537 * @p othersize and alignment @p otheralign.
539 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
541 if(othersize > slot->size) {
542 slot->size = othersize;
544 if(otheralign > slot->align) {
545 if(otheralign % slot->align != 0)
546 slot->align *= otheralign;
548 slot->align = otheralign;
549 } else if(slot->align % otheralign != 0) {
550 slot->align *= otheralign;
555 static void assign_spill_entity(ir_node *node, ir_entity *entity)
562 arity = get_irn_arity(node);
563 for(i = 0; i < arity; ++i) {
564 ir_node *in = get_irn_n(node, i);
567 assign_spill_entity(in, entity);
572 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for instance */
573 node = skip_Proj(node);
574 assert(arch_get_frame_entity(node) == NULL);
575 arch_set_frame_entity(node, entity);
579 * Create stack entities for the spillslots and assign them to the spill and
582 static void assign_spillslots(be_fec_env_t *env)
584 int spillcount = set_count(env->spills);
585 spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
589 /* construct spillslots */
590 for(spill = set_first(env->spills); spill != NULL;
591 spill = set_next(env->spills)) {
593 int slotid = spill->spillslot;
594 const ir_mode *mode = spill->mode;
595 spill_slot_t *slot = & (spillslots[slotid]);
596 int size = get_mode_size_bytes(mode);
597 int align = spill->alignment;
599 if(slot->align == 0 && slot->size == 0) {
603 enlarge_spillslot(slot, align, size);
607 for(spill = set_first(env->spills); spill != NULL;
608 spill = set_next(env->spills)) {
610 ir_node *node = spill->spill;
611 int slotid = spill->spillslot;
614 slot = &spillslots[slotid];
615 if(slot->entity == NULL) {
616 create_stack_entity(env, slot);
621 ir_node *block = get_nodes_block(node);
623 /* should be a PhiM */
624 assert(is_Phi(node));
626 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
627 ir_node *arg = get_irn_n(node, i);
628 ir_node *predblock = get_Block_cfgpred_block(block, i);
632 argspill = get_spill(env, arg);
633 assert(argspill != NULL);
635 argslotid = argspill->spillslot;
636 if(slotid != argslotid) {
638 memperm_entry_t *entry;
639 spill_slot_t *argslot = &spillslots[argslotid];
640 if(argslot->entity == NULL) {
641 create_stack_entity(env, argslot);
644 memperm = get_memperm(env, predblock);
646 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
649 entry->in = argslot->entity;
650 entry->out = slot->entity;
651 entry->next = memperm->entries;
652 memperm->entrycount++;
653 memperm->entries = entry;
657 assign_spill_entity(node, slot->entity);
661 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
662 ir_node *reload = env->reloads[i];
663 ir_node *spillnode = get_memory_edge(reload);
664 spill_t *spill = get_spill(env, spillnode);
665 const spill_slot_t *slot = & spillslots[spill->spillslot];
667 assert(slot->entity != NULL);
669 arch_set_frame_entity(reload, slot->entity);
674 * Returns the last node in a block which is no control flow changing node
676 static ir_node *get_end_of_block_insertion_point(ir_node* block)
678 ir_node* ins = sched_last(block);
679 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
680 ins = sched_prev(ins);
686 ir_node *prev = sched_prev(ins);
696 static void create_memperms(be_fec_env_t *env)
698 const arch_env_t *arch_env = env->arch_env;
699 ir_graph *irg = be_get_birg_irg(env->birg);
702 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
703 ir_node **nodes = ALLOCAN(ir_node*, memperm->entrycount);
704 memperm_entry_t *entry;
706 ir_node *mempermnode;
709 assert(memperm->entrycount > 0);
711 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
712 ir_node* arg = get_irn_n(entry->node, entry->pos);
716 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
717 memperm->entrycount, nodes);
719 /* insert node into schedule */
720 blockend = get_end_of_block_insertion_point(memperm->block);
721 sched_add_before(blockend, mempermnode);
722 stat_ev_dbl("mem_perm", memperm->entrycount);
725 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
727 ir_node* arg = get_irn_n(entry->node, entry->pos);
729 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
730 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
731 set_irg_current_block(irg, memperm->block);
732 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
734 set_irn_n(entry->node, entry->pos, proj);
739 static int count_spillslots(const be_fec_env_t *env)
741 const spill_t *spill;
742 int spillcount = set_count(env->spills);
743 bitset_t *counted = bitset_alloca(spillcount);
747 for(spill = set_first(env->spills); spill != NULL;
748 spill = set_next(env->spills)) {
749 int spillslot = spill->spillslot;
750 if(!bitset_is_set(counted, spillslot)) {
752 bitset_set(counted, spillslot);
759 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
761 const arch_env_t *arch_env = birg->main_env->arch_env;
762 be_fec_env_t *env = XMALLOC(be_fec_env_t);
764 be_liveness_assure_chk(be_assure_liveness(birg));
766 obstack_init(&env->obst);
767 env->arch_env = arch_env;
769 env->spills = new_set(cmp_spill, 10);
770 env->reloads = NEW_ARR_F(ir_node*, 0);
771 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
772 env->memperms = new_set(cmp_memperm, 10);
777 void be_free_frame_entity_coalescer(be_fec_env_t *env)
779 del_set(env->memperms);
780 DEL_ARR_F(env->reloads);
781 DEL_ARR_F(env->affinity_edges);
782 del_set(env->spills);
783 obstack_free(&env->obst, NULL);
788 void be_assign_entities(be_fec_env_t *env)
790 stat_ev_dbl("spillslots", set_count(env->spills));
792 if(be_coalesce_spill_slots) {
793 do_greedy_coalescing(env);
796 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
798 assign_spillslots(env);
800 create_memperms(env);
804 * This walker function searches for reloads and collects all the spills
805 * and memphis attached to them.
807 static void collect_spills_walker(ir_node *node, void *data)
809 be_fec_env_t *env = data;
811 const arch_register_class_t *cls;
814 /* classify returns classification of the irn the proj is attached to */
818 if (!arch_irn_class_is(node, reload))
821 mode = get_irn_mode(node);
822 cls = arch_get_irn_reg_class_out(node);
823 align = arch_env_get_reg_class_alignment(env->arch_env, cls);
825 be_node_needs_frame_entity(env, node, mode, align);
828 void be_coalesce_spillslots(be_irg_t *birg)
830 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
832 /* collect reloads */
833 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
835 be_assign_entities(env);
837 be_free_frame_entity_coalescer(env);
840 void be_init_spillslots(void)
842 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
845 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);