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
39 #include "unionfind.h"
44 #include "bespillslots.h"
45 #include "bechordal_t.h"
46 #include "bestatevent.h"
47 #include "bespilloptions.h"
49 #include "beintlive_t.h"
53 #define DBG_COALESCING 1
54 #define DBG_INTERFERENCES 2
56 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
58 typedef struct _spill_t {
60 const ir_mode *mode; /**< mode of the spilled value */
61 int alignment; /**< alignment for the spilled value */
62 int spillslot; /**< index into spillslot_unionfind structure */
65 typedef struct _affinity_edge_t {
70 struct _be_fec_env_t {
72 const arch_env_t *arch_env;
76 affinity_edge_t **affinity_edges;
80 /** Compare 2 affinity edges (used in quicksort) */
81 static int cmp_affinity(const void *d1, const void *d2)
83 const affinity_edge_t * const *e1 = d1;
84 const affinity_edge_t * const *e2 = d2;
86 /* sort in descending order */
87 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
90 static int cmp_spill(const void* d1, const void* d2, size_t size)
92 const spill_t* s1 = d1;
93 const spill_t* s2 = d2;
96 return s1->spill != s2->spill;
99 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
102 int hash = hash_irn(node);
105 res = set_find(env->spills, &spill, sizeof(spill), hash);
111 static INLINE ir_node *get_memory_edge(const ir_node *node)
115 arity = get_irn_arity(node);
116 for(i = arity - 1; i >= 0; --i) {
117 ir_node *arg = get_irn_n(node, i);
118 if(get_irn_mode(arg) == mode_M)
125 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
126 const ir_mode *mode, int align)
129 int hash = hash_irn(node);
131 /* insert into set of spills if not already there */
133 res = set_find(env->spills, &spill, sizeof(spill), hash);
136 spill.spillslot = set_count(env->spills);
138 spill.alignment = align;
139 res = set_insert(env->spills, &spill, sizeof(spill), hash);
140 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
142 assert(res->mode == mode);
143 assert(res->alignment == align);
149 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
150 const ir_mode *mode, int align)
154 int hash = hash_irn(node);
155 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
157 assert(is_Phi(node));
160 res = set_find(env->spills, &spill, sizeof(spill), hash);
162 assert(res->mode == mode);
163 assert(res->alignment == align);
167 spill.spillslot = set_count(env->spills);
169 spill.alignment = align;
170 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
171 res = set_insert(env->spills, &spill, sizeof(spill), hash);
173 /* collect attached spills and mem-phis */
174 arity = get_irn_arity(node);
175 for(i = 0; i < arity; ++i) {
176 affinity_edge_t *affinty_edge;
177 ir_node *arg = get_irn_n(node, i);
181 arg_spill = collect_memphi(env, arg, mode, align);
183 arg_spill = collect_spill(env, arg, mode, align);
186 /* add an affinity edge */
187 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
188 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
189 affinty_edge->slot1 = res->spillslot;
190 affinty_edge->slot2 = arg_spill->spillslot;
191 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
197 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
198 const ir_mode *mode, int align)
200 ir_node *spillnode = get_memory_edge(node);
203 assert(spillnode != NULL);
205 /* walk upwards and collect all phis and spills on this way */
206 if (is_Phi(spillnode)) {
207 spill = collect_memphi(env, spillnode, mode, align);
209 spill = collect_spill(env, spillnode, mode, align);
212 ARR_APP1(ir_node *, env->reloads, node);
217 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
218 int* spillslot_unionfind, int s1, int s2)
224 /* merge spillslots and interferences */
225 res = uf_union(spillslot_unionfind, s1, s2);
226 /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
233 bitset_or(interferences[s1], interferences[s2]);
235 /* update other interferences */
236 spillcount = set_count(env->spills);
237 for(i = 0; i < spillcount; ++i) {
238 bitset_t *intfs = interferences[i];
239 if(bitset_is_set(intfs, s2))
240 bitset_set(intfs, s1);
246 static int my_values_interfere2(be_irg_t *birg, const ir_node *a,
249 be_lv_t *lv = be_get_birg_liveness(birg);
251 int a2b = _value_dominates(a, b);
252 int b2a = _value_dominates(b, a);
254 /* If there is no dominance relation, they do not interfere. */
255 if((a2b | b2a) > 0) {
256 const ir_edge_t *edge;
260 * Adjust a and b so, that a dominates b if
261 * a dominates b or vice versa.
264 const ir_node *t = a;
269 bb = get_nodes_block(b);
272 * If a is live end in b's block it is
273 * live at b's definition (a dominates b)
275 if(be_is_live_end(lv, bb, a))
279 * Look at all usages of a.
280 * If there's one usage of a in the block of b, then
281 * we check, if this use is dominated by b, if that's true
282 * a and b interfere. Note that b must strictly dominate the user,
283 * since if b is the last user of in the block, b and a do not
285 * Uses of a not in b's block can be disobeyed, because the
286 * check for a being live at the end of b's block is already
289 foreach_out_edge(a, edge) {
290 const ir_node *user = get_edge_src_irn(edge);
292 const ir_edge_t *edge2;
293 foreach_out_edge(user, edge2) {
294 const ir_node *user2 = get_edge_src_irn(edge2);
295 assert(!is_Sync(user2));
296 if(get_nodes_block(user2) == bb && !is_Phi(user2) &&
297 _value_strictly_dominates(b, user2))
301 if(get_nodes_block(user) == bb && !is_Phi(user) &&
302 _value_strictly_dominates(b, user))
312 * same as values_interfere but with special handling for Syncs
314 static int my_values_interfere(be_irg_t *birg, ir_node *a, ir_node *b)
317 int i, arity = get_irn_arity(a);
318 for(i = 0; i < arity; ++i) {
319 ir_node *in = get_irn_n(a, i);
320 if(my_values_interfere(birg, in, b))
324 } else if(is_Sync(b)) {
325 int i, arity = get_irn_arity(b);
326 for(i = 0; i < arity; ++i) {
327 ir_node *in = get_irn_n(b, i);
328 /* a is not a sync, so no need for my_values_interfere */
329 if(my_values_interfere2(birg, a, in))
335 return my_values_interfere2(birg, a, b);
339 * A greedy coalescing algorithm for spillslots:
340 * 1. Sort the list of affinity edges
341 * 2. Try to merge slots with affinity edges (most expensive slots first)
342 * 3. Try to merge everything else that is possible
344 static void do_greedy_coalescing(be_fec_env_t *env)
350 int affinity_edge_count;
351 bitset_t **interferences;
352 int* spillslot_unionfind;
354 spillcount = set_count(env->spills);
358 DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
360 interferences = alloca(spillcount * sizeof(interferences[0]));
361 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
362 spilllist = alloca(spillcount * sizeof(spilllist[0]));
364 uf_init(spillslot_unionfind, 0, spillcount);
367 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
370 for(spill = set_first(env->spills), i = 0; spill != NULL;
371 spill = set_next(env->spills), ++i) {
372 assert(spill->spillslot < spillcount);
373 spilllist[spill->spillslot] = spill;
376 for(i = 0; i < spillcount; ++i) {
377 interferences[i] = bitset_alloca(spillcount);
380 /* construct interferences */
381 for (i = 0; i < spillcount; ++i) {
382 ir_node *spill1 = spilllist[i]->spill;
384 if (is_NoMem(spill1))
387 for(i2 = i+1; i2 < spillcount; ++i2) {
388 ir_node *spill2 = spilllist[i2]->spill;
390 if (is_NoMem(spill2))
393 if (my_values_interfere(env->birg, spill1, spill2)) {
394 DB((dbg, DBG_INTERFERENCES,
395 "Slot %d and %d interfere\n", i, i2));
397 bitset_set(interferences[i], i2);
398 bitset_set(interferences[i2], i);
403 /* sort affinity edges */
404 affinity_edge_count = ARR_LEN(env->affinity_edges);
405 qsort(env->affinity_edges, affinity_edge_count,
406 sizeof(env->affinity_edges[0]), cmp_affinity);
408 /*dump_interference_graph(env, interferences, "before"); */
410 /* try to merge affine nodes */
411 for(i = 0; i < affinity_edge_count; ++i) {
412 const affinity_edge_t *edge = env->affinity_edges[i];
413 int s1 = uf_find(spillslot_unionfind, edge->slot1);
414 int s2 = uf_find(spillslot_unionfind, edge->slot2);
416 /* test if values interfere */
417 if (bitset_is_set(interferences[s1], s2)) {
418 assert(bitset_is_set(interferences[s2], s1));
422 DB((dbg, DBG_COALESCING,
423 "Merging %d and %d because of affinity edge\n", s1, s2));
425 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
428 /* try to merge as much remaining spillslots as possible */
429 for(i = 0; i < spillcount; ++i) {
430 int s1 = uf_find(spillslot_unionfind, i);
434 for(i2 = i+1; i2 < spillcount; ++i2) {
435 int s2 = uf_find(spillslot_unionfind, i2);
439 /* test if values interfere
440 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
441 * when node merging occurs
443 if(bitset_is_set(interferences[s1], s2)) {
444 assert(bitset_is_set(interferences[s2], s1));
448 DB((dbg, DBG_COALESCING,
449 "Merging %d and %d because it is possible\n", s1, s2));
451 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
452 /* we can break the loop here, because s2 is the new supernode
453 * now and we'll test s2 again later anyway */
459 /* assign spillslots to spills */
460 for(i = 0; i < spillcount; ++i) {
461 spill_t *spill = spilllist[i];
463 spill->spillslot = uf_find(spillslot_unionfind, i);
466 /*dump_interference_graph(env, interferences, "after");*/
471 typedef struct _spill_slot_t {
477 typedef struct _memperm_entry_t {
482 struct _memperm_entry_t *next;
485 typedef struct _memperm_t {
488 memperm_entry_t *entries;
491 static int cmp_memperm(const void* d1, const void* d2, size_t size)
493 const memperm_t* e1 = d1;
494 const memperm_t* e2 = d2;
497 return e1->block != e2->block;
500 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
502 memperm_t entry, *res;
506 hash = hash_irn(block);
508 res = set_find(env->memperms, &entry, sizeof(entry), hash);
511 entry.entrycount = 0;
512 entry.entries = NULL;
513 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
519 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
521 ir_graph *irg = be_get_birg_irg(env->birg);
522 ir_type *frame = get_irg_frame_type(irg);
523 /* TODO: backend should be able to specify wether we want spill slots
524 * at begin or end of frame */
526 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
528 /* adjust size of the entity type... */
529 ir_type *enttype = get_entity_type(res);
530 set_type_size_bytes(enttype, slot->size);
538 * Enlarges a spillslot (if necessary) so that it can carry a value of size
539 * @p othersize and alignment @p otheralign.
541 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
543 if(othersize > slot->size) {
544 slot->size = othersize;
546 if(otheralign > slot->align) {
547 if(otheralign % slot->align != 0)
548 slot->align *= otheralign;
550 slot->align = otheralign;
551 } else if(slot->align % otheralign != 0) {
552 slot->align *= otheralign;
557 static void assign_spill_entity(ir_node *node, ir_entity *entity)
564 arity = get_irn_arity(node);
565 for(i = 0; i < arity; ++i) {
566 ir_node *in = get_irn_n(node, i);
569 assign_spill_entity(in, entity);
574 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for instance */
575 node = skip_Proj(node);
576 assert(arch_get_frame_entity(node) == NULL);
577 arch_set_frame_entity(node, entity);
581 * Create stack entities for the spillslots and assign them to the spill and
584 static void assign_spillslots(be_fec_env_t *env)
589 spill_slot_t* spillslots;
591 spillcount = set_count(env->spills);
592 spillslots = alloca(spillcount * sizeof(spillslots[0]));
594 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
596 /* construct spillslots */
597 for(spill = set_first(env->spills); spill != NULL;
598 spill = set_next(env->spills)) {
600 int slotid = spill->spillslot;
601 const ir_mode *mode = spill->mode;
602 spill_slot_t *slot = & (spillslots[slotid]);
603 int size = get_mode_size_bytes(mode);
604 int align = spill->alignment;
606 if(slot->align == 0 && slot->size == 0) {
610 enlarge_spillslot(slot, align, size);
614 for(spill = set_first(env->spills); spill != NULL;
615 spill = set_next(env->spills)) {
617 ir_node *node = spill->spill;
618 int slotid = spill->spillslot;
621 slot = &spillslots[slotid];
622 if(slot->entity == NULL) {
623 create_stack_entity(env, slot);
628 ir_node *block = get_nodes_block(node);
630 /* should be a PhiM */
631 assert(is_Phi(node));
633 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
634 ir_node *arg = get_irn_n(node, i);
635 ir_node *predblock = get_Block_cfgpred_block(block, i);
639 argspill = get_spill(env, arg);
640 assert(argspill != NULL);
642 argslotid = argspill->spillslot;
643 if(slotid != argslotid) {
645 memperm_entry_t *entry;
646 spill_slot_t *argslot = &spillslots[argslotid];
647 if(argslot->entity == NULL) {
648 create_stack_entity(env, argslot);
651 memperm = get_memperm(env, predblock);
653 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
656 entry->in = argslot->entity;
657 entry->out = slot->entity;
658 entry->next = memperm->entries;
659 memperm->entrycount++;
660 memperm->entries = entry;
664 assign_spill_entity(node, slot->entity);
668 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
669 ir_node *reload = env->reloads[i];
670 ir_node *spillnode = get_memory_edge(reload);
671 spill_t *spill = get_spill(env, spillnode);
672 const spill_slot_t *slot = & spillslots[spill->spillslot];
674 assert(slot->entity != NULL);
676 arch_set_frame_entity(reload, slot->entity);
681 * Returns the last node in a block which is no control flow changing node
683 static ir_node *get_end_of_block_insertion_point(ir_node* block)
685 ir_node* ins = sched_last(block);
686 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
687 ins = sched_prev(ins);
693 ir_node *prev = sched_prev(ins);
703 static void create_memperms(be_fec_env_t *env)
705 const arch_env_t *arch_env = env->arch_env;
706 ir_graph *irg = be_get_birg_irg(env->birg);
709 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
711 memperm_entry_t *entry;
713 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
714 ir_node* mempermnode;
716 assert(memperm->entrycount > 0);
718 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
719 ir_node* arg = get_irn_n(entry->node, entry->pos);
723 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
724 memperm->entrycount, nodes);
726 /* insert node into schedule */
727 blockend = get_end_of_block_insertion_point(memperm->block);
728 sched_add_before(blockend, mempermnode);
729 stat_ev_dbl("mem_perm", memperm->entrycount);
732 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
734 ir_node* arg = get_irn_n(entry->node, entry->pos);
736 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
737 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
738 set_irg_current_block(irg, memperm->block);
739 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
741 set_irn_n(entry->node, entry->pos, proj);
746 static int count_spillslots(const be_fec_env_t *env)
748 const spill_t *spill;
749 int spillcount = set_count(env->spills);
750 bitset_t *counted = bitset_alloca(spillcount);
754 for(spill = set_first(env->spills); spill != NULL;
755 spill = set_next(env->spills)) {
756 int spillslot = spill->spillslot;
757 if(!bitset_is_set(counted, spillslot)) {
759 bitset_set(counted, spillslot);
766 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
768 const arch_env_t *arch_env = birg->main_env->arch_env;
769 be_fec_env_t *env = XMALLOC(be_fec_env_t);
771 be_liveness_assure_chk(be_assure_liveness(birg));
773 obstack_init(&env->obst);
774 env->arch_env = arch_env;
776 env->spills = new_set(cmp_spill, 10);
777 env->reloads = NEW_ARR_F(ir_node*, 0);
778 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
779 env->memperms = new_set(cmp_memperm, 10);
784 void be_free_frame_entity_coalescer(be_fec_env_t *env)
786 del_set(env->memperms);
787 DEL_ARR_F(env->reloads);
788 DEL_ARR_F(env->affinity_edges);
789 del_set(env->spills);
790 obstack_free(&env->obst, NULL);
795 void be_assign_entities(be_fec_env_t *env)
797 stat_ev_dbl("spillslots", set_count(env->spills));
799 if(be_coalesce_spill_slots) {
800 do_greedy_coalescing(env);
803 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
805 assign_spillslots(env);
807 create_memperms(env);
811 * This walker function searches for reloads and collects all the spills
812 * and memphis attached to them.
814 static void collect_spills_walker(ir_node *node, void *data)
816 be_fec_env_t *env = data;
818 const arch_register_class_t *cls;
821 /* classify returns classification of the irn the proj is attached to */
825 if (!arch_irn_class_is(node, reload))
828 mode = get_irn_mode(node);
829 cls = arch_get_irn_reg_class(node, -1);
830 align = arch_env_get_reg_class_alignment(env->arch_env, cls);
832 be_node_needs_frame_entity(env, node, mode, align);
835 void be_coalesce_spillslots(be_irg_t *birg)
837 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
839 /* collect reloads */
840 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
842 be_assign_entities(env);
844 be_free_frame_entity_coalescer(env);
847 void be_init_spillslots(void)
849 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
852 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);