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 "bejavacoal.h"
47 #include "bestatevent.h"
48 #include "bespilloptions.h"
50 #include "beintlive_t.h"
54 #define DBG_COALESCING 1
55 #define DBG_INTERFERENCES 2
57 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59 typedef struct _spill_t {
61 const ir_mode *mode; /**< mode of the spilled value */
62 int alignment; /**< alignment for the spilled value */
63 int spillslot; /**< index into spillslot_unionfind structure */
66 typedef struct _affinity_edge_t {
71 struct _be_fec_env_t {
73 const arch_env_t *arch_env;
77 affinity_edge_t **affinity_edges;
81 /** Compare 2 affinity edges (used in quicksort) */
82 static int cmp_affinity(const void *d1, const void *d2)
84 const affinity_edge_t * const *e1 = d1;
85 const affinity_edge_t * const *e2 = d2;
87 /* sort in descending order */
88 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
91 static int cmp_spill(const void* d1, const void* d2, size_t size)
93 const spill_t* s1 = d1;
94 const spill_t* s2 = d2;
97 return s1->spill != s2->spill;
100 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
103 int hash = hash_irn(node);
106 res = set_find(env->spills, &spill, sizeof(spill), hash);
112 static INLINE ir_node *get_memory_edge(const ir_node *node)
116 arity = get_irn_arity(node);
117 for(i = arity - 1; i >= 0; --i) {
118 ir_node *arg = get_irn_n(node, i);
119 if(get_irn_mode(arg) == mode_M)
126 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
127 const ir_mode *mode, int align)
130 int hash = hash_irn(node);
132 /* insert into set of spills if not already there */
134 res = set_find(env->spills, &spill, sizeof(spill), hash);
137 spill.spillslot = set_count(env->spills);
139 spill.alignment = align;
140 res = set_insert(env->spills, &spill, sizeof(spill), hash);
141 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
143 assert(res->mode == mode);
144 assert(res->alignment == align);
150 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
151 const ir_mode *mode, int align)
155 int hash = hash_irn(node);
156 const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
158 assert(is_Phi(node));
161 res = set_find(env->spills, &spill, sizeof(spill), hash);
163 assert(res->mode == mode);
164 assert(res->alignment == align);
168 spill.spillslot = set_count(env->spills);
170 spill.alignment = align;
171 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
172 res = set_insert(env->spills, &spill, sizeof(spill), hash);
174 /* collect attached spills and mem-phis */
175 arity = get_irn_arity(node);
176 for(i = 0; i < arity; ++i) {
177 affinity_edge_t *affinty_edge;
178 ir_node *arg = get_irn_n(node, i);
182 arg_spill = collect_memphi(env, arg, mode, align);
184 arg_spill = collect_spill(env, arg, mode, align);
187 /* add an affinity edge */
188 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
189 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
190 affinty_edge->slot1 = res->spillslot;
191 affinty_edge->slot2 = arg_spill->spillslot;
192 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
198 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
199 const ir_mode *mode, int align)
201 ir_node *spillnode = get_memory_edge(node);
204 assert(spillnode != NULL);
206 /* walk upwards and collect all phis and spills on this way */
207 if (is_Phi(spillnode)) {
208 spill = collect_memphi(env, spillnode, mode, align);
210 spill = collect_spill(env, spillnode, mode, align);
213 ARR_APP1(ir_node *, env->reloads, node);
218 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
219 int* spillslot_unionfind, int s1, int s2)
225 /* merge spillslots and interferences */
226 res = uf_union(spillslot_unionfind, s1, s2);
227 /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
234 bitset_or(interferences[s1], interferences[s2]);
236 /* update other interferences */
237 spillcount = set_count(env->spills);
238 for(i = 0; i < spillcount; ++i) {
239 bitset_t *intfs = interferences[i];
240 if(bitset_is_set(intfs, s2))
241 bitset_set(intfs, s1);
247 static int my_values_interfere2(be_irg_t *birg, const ir_node *a,
250 be_lv_t *lv = be_get_birg_liveness(birg);
252 int a2b = _value_dominates(a, b);
253 int b2a = _value_dominates(b, a);
255 /* If there is no dominance relation, they do not interfere. */
256 if((a2b | b2a) > 0) {
257 const ir_edge_t *edge;
261 * Adjust a and b so, that a dominates b if
262 * a dominates b or vice versa.
265 const ir_node *t = a;
270 bb = get_nodes_block(b);
273 * If a is live end in b's block it is
274 * live at b's definition (a dominates b)
276 if(be_is_live_end(lv, bb, a))
280 * Look at all usages of a.
281 * If there's one usage of a in the block of b, then
282 * we check, if this use is dominated by b, if that's true
283 * a and b interfere. Note that b must strictly dominate the user,
284 * since if b is the last user of in the block, b and a do not
286 * Uses of a not in b's block can be disobeyed, because the
287 * check for a being live at the end of b's block is already
290 foreach_out_edge(a, edge) {
291 const ir_node *user = get_edge_src_irn(edge);
293 const ir_edge_t *edge2;
294 foreach_out_edge(user, edge2) {
295 const ir_node *user2 = get_edge_src_irn(edge2);
296 assert(!is_Sync(user2));
297 if(get_nodes_block(user2) == bb && !is_Phi(user2) &&
298 _value_strictly_dominates(b, user2))
302 if(get_nodes_block(user) == bb && !is_Phi(user) &&
303 _value_strictly_dominates(b, user))
313 * same as values_interfere but with special handling for Syncs
315 static int my_values_interfere(be_irg_t *birg, ir_node *a, ir_node *b)
318 int i, arity = get_irn_arity(a);
319 for(i = 0; i < arity; ++i) {
320 ir_node *in = get_irn_n(a, i);
321 if(my_values_interfere(birg, in, b))
325 } else if(is_Sync(b)) {
326 int i, arity = get_irn_arity(b);
327 for(i = 0; i < arity; ++i) {
328 ir_node *in = get_irn_n(b, i);
329 /* a is not a sync, so no need for my_values_interfere */
330 if(my_values_interfere2(birg, a, in))
336 return my_values_interfere2(birg, a, b);
340 * A greedy coalescing algorithm for spillslots:
341 * 1. Sort the list of affinity edges
342 * 2. Try to merge slots with affinity edges (most expensive slots first)
343 * 3. Try to merge everything else that is possible
345 static void do_greedy_coalescing(be_fec_env_t *env)
351 int affinity_edge_count;
352 bitset_t **interferences;
353 int* spillslot_unionfind;
355 spillcount = set_count(env->spills);
359 DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
361 interferences = alloca(spillcount * sizeof(interferences[0]));
362 spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
363 spilllist = alloca(spillcount * sizeof(spilllist[0]));
365 uf_init(spillslot_unionfind, 0, spillcount);
368 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
371 for(spill = set_first(env->spills), i = 0; spill != NULL;
372 spill = set_next(env->spills), ++i) {
373 assert(spill->spillslot < spillcount);
374 spilllist[spill->spillslot] = spill;
377 for(i = 0; i < spillcount; ++i) {
378 interferences[i] = bitset_alloca(spillcount);
381 /* construct interferences */
382 for (i = 0; i < spillcount; ++i) {
383 ir_node *spill1 = spilllist[i]->spill;
385 if (is_NoMem(spill1))
388 for(i2 = i+1; i2 < spillcount; ++i2) {
389 ir_node *spill2 = spilllist[i2]->spill;
391 if (is_NoMem(spill2))
394 if (my_values_interfere(env->birg, spill1, spill2)) {
395 DB((dbg, DBG_INTERFERENCES,
396 "Slot %d and %d interfere\n", i, i2));
398 bitset_set(interferences[i], i2);
399 bitset_set(interferences[i2], i);
404 /* sort affinity edges */
405 affinity_edge_count = ARR_LEN(env->affinity_edges);
406 qsort(env->affinity_edges, affinity_edge_count,
407 sizeof(env->affinity_edges[0]), cmp_affinity);
409 /*dump_interference_graph(env, interferences, "before"); */
411 /* try to merge affine nodes */
412 for(i = 0; i < affinity_edge_count; ++i) {
413 const affinity_edge_t *edge = env->affinity_edges[i];
414 int s1 = uf_find(spillslot_unionfind, edge->slot1);
415 int s2 = uf_find(spillslot_unionfind, edge->slot2);
417 /* test if values interfere */
418 if (bitset_is_set(interferences[s1], s2)) {
419 assert(bitset_is_set(interferences[s2], s1));
423 DB((dbg, DBG_COALESCING,
424 "Merging %d and %d because of affinity edge\n", s1, s2));
426 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
429 /* try to merge as much remaining spillslots as possible */
430 for(i = 0; i < spillcount; ++i) {
431 int s1 = uf_find(spillslot_unionfind, i);
435 for(i2 = i+1; i2 < spillcount; ++i2) {
436 int s2 = uf_find(spillslot_unionfind, i2);
440 /* test if values interfere
441 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
442 * when node merging occurs
444 if(bitset_is_set(interferences[s1], s2)) {
445 assert(bitset_is_set(interferences[s2], s1));
449 DB((dbg, DBG_COALESCING,
450 "Merging %d and %d because it is possible\n", s1, s2));
452 if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
453 /* we can break the loop here, because s2 is the new supernode
454 * now and we'll test s2 again later anyway */
460 /* assign spillslots to spills */
461 for(i = 0; i < spillcount; ++i) {
462 spill_t *spill = spilllist[i];
464 spill->spillslot = uf_find(spillslot_unionfind, i);
467 /*dump_interference_graph(env, interferences, "after");*/
472 typedef struct _spill_slot_t {
478 typedef struct _memperm_entry_t {
483 struct _memperm_entry_t *next;
486 typedef struct _memperm_t {
489 memperm_entry_t *entries;
492 static int cmp_memperm(const void* d1, const void* d2, size_t size)
494 const memperm_t* e1 = d1;
495 const memperm_t* e2 = d2;
498 return e1->block != e2->block;
501 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
503 memperm_t entry, *res;
507 hash = hash_irn(block);
509 res = set_find(env->memperms, &entry, sizeof(entry), hash);
512 entry.entrycount = 0;
513 entry.entries = NULL;
514 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
520 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
522 ir_graph *irg = be_get_birg_irg(env->birg);
523 ir_type *frame = get_irg_frame_type(irg);
524 /* TODO: backend should be able to specify wether we want spill slots
525 * at begin or end of frame */
527 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
529 /* adjust size of the entity type... */
530 ir_type *enttype = get_entity_type(res);
531 set_type_size_bytes(enttype, slot->size);
539 * Enlarges a spillslot (if necessary) so that it can carry a value of size
540 * @p othersize and alignment @p otheralign.
542 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
544 if(othersize > slot->size) {
545 slot->size = othersize;
547 if(otheralign > slot->align) {
548 if(otheralign % slot->align != 0)
549 slot->align *= otheralign;
551 slot->align = otheralign;
552 } else if(slot->align % otheralign != 0) {
553 slot->align *= otheralign;
558 static void assign_spill_entity(const arch_env_t *arch_env, ir_node *node,
566 arity = get_irn_arity(node);
567 for(i = 0; i < arity; ++i) {
568 ir_node *in = get_irn_n(node, i);
571 assign_spill_entity(arch_env, in, entity);
576 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for instance */
577 node = skip_Proj(node);
578 assert(arch_get_frame_entity(arch_env, node) == NULL);
579 arch_set_frame_entity(arch_env, node, entity);
583 * Create stack entities for the spillslots and assign them to the spill and
586 static void assign_spillslots(be_fec_env_t *env)
588 const arch_env_t *arch_env = env->arch_env;
592 spill_slot_t* spillslots;
594 spillcount = set_count(env->spills);
595 spillslots = alloca(spillcount * sizeof(spillslots[0]));
597 memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
599 /* construct spillslots */
600 for(spill = set_first(env->spills); spill != NULL;
601 spill = set_next(env->spills)) {
603 int slotid = spill->spillslot;
604 const ir_mode *mode = spill->mode;
605 spill_slot_t *slot = & (spillslots[slotid]);
606 int size = get_mode_size_bytes(mode);
607 int align = spill->alignment;
609 if(slot->align == 0 && slot->size == 0) {
613 enlarge_spillslot(slot, align, size);
617 for(spill = set_first(env->spills); spill != NULL;
618 spill = set_next(env->spills)) {
620 ir_node *node = spill->spill;
621 int slotid = spill->spillslot;
624 slot = &spillslots[slotid];
625 if(slot->entity == NULL) {
626 create_stack_entity(env, slot);
631 ir_node *block = get_nodes_block(node);
633 /* should be a PhiM */
634 assert(is_Phi(node));
636 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
637 ir_node *arg = get_irn_n(node, i);
638 ir_node *predblock = get_Block_cfgpred_block(block, i);
642 argspill = get_spill(env, arg);
643 assert(argspill != NULL);
645 argslotid = argspill->spillslot;
646 if(slotid != argslotid) {
648 memperm_entry_t *entry;
649 spill_slot_t *argslot = &spillslots[argslotid];
650 if(argslot->entity == NULL) {
651 create_stack_entity(env, argslot);
654 memperm = get_memperm(env, predblock);
656 entry = obstack_alloc(&env->obst, sizeof(entry[0]));
659 entry->in = argslot->entity;
660 entry->out = slot->entity;
661 entry->next = memperm->entries;
662 memperm->entrycount++;
663 memperm->entries = entry;
667 assign_spill_entity(arch_env, node, slot->entity);
671 for(i = 0; i < ARR_LEN(env->reloads); ++i) {
672 ir_node *reload = env->reloads[i];
673 ir_node *spillnode = get_memory_edge(reload);
674 spill_t *spill = get_spill(env, spillnode);
675 const spill_slot_t *slot = & spillslots[spill->spillslot];
677 assert(slot->entity != NULL);
679 arch_set_frame_entity(arch_env, reload, slot->entity);
684 * Returns the last node in a block which is no control flow changing node
686 static ir_node *get_end_of_block_insertion_point(ir_node* block)
688 ir_node* ins = sched_last(block);
689 while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
690 ins = sched_prev(ins);
696 ir_node *prev = sched_prev(ins);
706 static void create_memperms(be_fec_env_t *env)
708 const arch_env_t *arch_env = env->arch_env;
709 ir_graph *irg = be_get_birg_irg(env->birg);
712 for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
714 memperm_entry_t *entry;
716 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
717 ir_node* mempermnode;
719 assert(memperm->entrycount > 0);
721 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
722 ir_node* arg = get_irn_n(entry->node, entry->pos);
726 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
727 memperm->entrycount, nodes);
729 /* insert node into schedule */
730 blockend = get_end_of_block_insertion_point(memperm->block);
731 sched_add_before(blockend, mempermnode);
732 stat_ev_dbl("mem_perm", memperm->entrycount);
735 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
737 ir_node* arg = get_irn_n(entry->node, entry->pos);
739 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
740 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
741 set_irg_current_block(irg, memperm->block);
742 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
744 set_irn_n(entry->node, entry->pos, proj);
749 static int count_spillslots(const be_fec_env_t *env)
751 const spill_t *spill;
752 int spillcount = set_count(env->spills);
753 bitset_t *counted = bitset_alloca(spillcount);
757 for(spill = set_first(env->spills); spill != NULL;
758 spill = set_next(env->spills)) {
759 int spillslot = spill->spillslot;
760 if(!bitset_is_set(counted, spillslot)) {
762 bitset_set(counted, spillslot);
769 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
771 const arch_env_t *arch_env = birg->main_env->arch_env;
772 be_fec_env_t *env = XMALLOC(be_fec_env_t);
774 be_liveness_assure_chk(be_assure_liveness(birg));
776 obstack_init(&env->obst);
777 env->arch_env = arch_env;
779 env->spills = new_set(cmp_spill, 10);
780 env->reloads = NEW_ARR_F(ir_node*, 0);
781 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
782 env->memperms = new_set(cmp_memperm, 10);
787 void be_free_frame_entity_coalescer(be_fec_env_t *env)
789 del_set(env->memperms);
790 DEL_ARR_F(env->reloads);
791 DEL_ARR_F(env->affinity_edges);
792 del_set(env->spills);
793 obstack_free(&env->obst, NULL);
798 void be_assign_entities(be_fec_env_t *env)
800 stat_ev_dbl("spillslots", set_count(env->spills));
802 if(be_coalesce_spill_slots) {
803 do_greedy_coalescing(env);
806 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
808 assign_spillslots(env);
810 create_memperms(env);
814 * This walker function searches for reloads and collects all the spills
815 * and memphis attached to them.
817 static void collect_spills_walker(ir_node *node, void *data)
819 be_fec_env_t *env = data;
820 const arch_env_t *arch_env = env->arch_env;
822 const arch_register_class_t *cls;
825 /* classify returns classification of the irn the proj is attached to */
829 if (!arch_irn_class_is(arch_env, node, reload))
832 mode = get_irn_mode(node);
833 cls = arch_get_irn_reg_class(arch_env, node, -1);
834 align = arch_env_get_reg_class_alignment(arch_env, cls);
836 be_node_needs_frame_entity(env, node, mode, align);
839 void be_coalesce_spillslots(be_irg_t *birg)
841 be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
843 /* collect reloads */
844 irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
846 be_assign_entities(env);
848 be_free_frame_entity_coalescer(env);
851 void be_init_spillslots(void)
853 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
856 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);