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"
43 #include "bespillslots.h"
44 #include "bechordal_t.h"
45 #include "bestatevent.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 {
69 struct _be_fec_env_t {
71 const arch_env_t *arch_env;
75 affinity_edge_t **affinity_edges;
79 /** Compare 2 affinity edges (used in quicksort) */
80 static int cmp_affinity(const void *d1, const void *d2)
82 const affinity_edge_t * const *e1 = d1;
83 const affinity_edge_t * const *e2 = d2;
85 /* sort in descending order */
86 return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
89 static int cmp_spill(const void* d1, const void* d2, size_t size)
91 const spill_t* s1 = d1;
92 const spill_t* s2 = d2;
95 return s1->spill != s2->spill;
98 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
101 int hash = hash_irn(node);
104 res = set_find(env->spills, &spill, sizeof(spill), hash);
110 static inline ir_node *get_memory_edge(const ir_node *node)
114 arity = get_irn_arity(node);
115 for (i = arity - 1; i >= 0; --i) {
116 ir_node *arg = get_irn_n(node, i);
117 if (get_irn_mode(arg) == mode_M)
124 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
125 const ir_mode *mode, int align)
128 int hash = hash_irn(node);
130 /* insert into set of spills if not already there */
132 res = set_find(env->spills, &spill, sizeof(spill), hash);
135 spill.spillslot = set_count(env->spills);
137 spill.alignment = align;
138 res = set_insert(env->spills, &spill, sizeof(spill), hash);
139 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
141 assert(res->mode == mode);
142 assert(res->alignment == align);
148 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
149 const ir_mode *mode, int align)
153 int hash = hash_irn(node);
154 const ir_exec_freq *exec_freq = be_get_irg_exec_freq(env->irg);
156 assert(is_Phi(node));
159 res = set_find(env->spills, &spill, sizeof(spill), hash);
161 assert(res->mode == mode);
162 assert(res->alignment == align);
166 spill.spillslot = set_count(env->spills);
168 spill.alignment = align;
169 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
170 res = set_insert(env->spills, &spill, sizeof(spill), hash);
172 /* collect attached spills and mem-phis */
173 arity = get_irn_arity(node);
174 for (i = 0; i < arity; ++i) {
175 affinity_edge_t *affinty_edge;
176 ir_node *arg = get_irn_n(node, i);
180 arg_spill = collect_memphi(env, arg, mode, align);
182 arg_spill = collect_spill(env, arg, mode, align);
185 /* add an affinity edge */
186 affinty_edge = OALLOC(&env->obst, affinity_edge_t);
187 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
188 affinty_edge->slot1 = res->spillslot;
189 affinty_edge->slot2 = arg_spill->spillslot;
190 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
196 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
197 const ir_mode *mode, int align)
199 ir_node *spillnode = get_memory_edge(node);
202 assert(spillnode != NULL);
204 /* walk upwards and collect all phis and spills on this way */
205 if (is_Phi(spillnode)) {
206 spill = collect_memphi(env, spillnode, mode, align);
208 spill = collect_spill(env, spillnode, mode, align);
211 ARR_APP1(ir_node *, env->reloads, node);
216 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
217 int* spillslot_unionfind, int s1, int s2)
223 /* merge spillslots and interferences */
224 res = uf_union(spillslot_unionfind, s1, s2);
225 /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
232 bitset_or(interferences[s1], interferences[s2]);
234 /* update other interferences */
235 spillcount = set_count(env->spills);
236 for (i = 0; i < spillcount; ++i) {
237 bitset_t *intfs = interferences[i];
238 if (bitset_is_set(intfs, s2))
239 bitset_set(intfs, s1);
245 static int my_values_interfere2(ir_graph *irg, const ir_node *a,
248 be_lv_t *lv = be_get_irg_liveness(irg);
250 int a2b = _value_dominates(a, b);
251 int b2a = _value_dominates(b, a);
253 /* If there is no dominance relation, they do not interfere. */
254 if ((a2b | b2a) > 0) {
255 const ir_edge_t *edge;
259 * Adjust a and b so, that a dominates b if
260 * a dominates b or vice versa.
263 const ir_node *t = a;
268 bb = get_nodes_block(b);
271 * If a is live end in b's block it is
272 * live at b's definition (a dominates b)
274 if (be_is_live_end(lv, bb, a))
278 * Look at all usages of a.
279 * If there's one usage of a in the block of b, then
280 * we check, if this use is dominated by b, if that's true
281 * a and b interfere. Note that b must strictly dominate the user,
282 * since if b is the last user of in the block, b and a do not
284 * Uses of a not in b's block can be disobeyed, because the
285 * check for a being live at the end of b's block is already
288 foreach_out_edge(a, edge) {
289 const ir_node *user = get_edge_src_irn(edge);
291 const ir_edge_t *edge2;
292 foreach_out_edge(user, edge2) {
293 const ir_node *user2 = get_edge_src_irn(edge2);
294 assert(!is_Sync(user2));
295 if (get_nodes_block(user2) == bb && !is_Phi(user2) &&
296 _value_strictly_dominates(b, user2))
300 if (get_nodes_block(user) == bb && !is_Phi(user) &&
301 _value_strictly_dominates(b, user))
311 * same as values_interfere but with special handling for Syncs
313 static int my_values_interfere(ir_graph *irg, ir_node *a, ir_node *b)
316 int i, arity = get_irn_arity(a);
317 for (i = 0; i < arity; ++i) {
318 ir_node *in = get_irn_n(a, i);
319 if (my_values_interfere(irg, in, b))
323 } else if (is_Sync(b)) {
324 int i, arity = get_irn_arity(b);
325 for (i = 0; i < arity; ++i) {
326 ir_node *in = get_irn_n(b, i);
327 /* a is not a sync, so no need for my_values_interfere */
328 if (my_values_interfere2(irg, a, in))
334 return my_values_interfere2(irg, a, b);
338 * A greedy coalescing algorithm for spillslots:
339 * 1. Sort the list of affinity edges
340 * 2. Try to merge slots with affinity edges (most expensive slots first)
341 * 3. Try to merge everything else that is possible
343 static void do_greedy_coalescing(be_fec_env_t *env)
349 int affinity_edge_count;
350 bitset_t **interferences;
351 int* spillslot_unionfind;
354 spillcount = set_count(env->spills);
360 DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
362 interferences = OALLOCN(&data, bitset_t*, spillcount);
363 spillslot_unionfind = OALLOCN(&data, int, spillcount);
364 spilllist = OALLOCN(&data, spill_t*, spillcount);
366 uf_init(spillslot_unionfind, spillcount);
369 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
372 for (spill = set_first(env->spills), i = 0; spill != NULL;
373 spill = set_next(env->spills), ++i) {
374 assert(spill->spillslot < spillcount);
375 spilllist[spill->spillslot] = spill;
378 for (i = 0; i < spillcount; ++i) {
379 interferences[i] = bitset_obstack_alloc(&data, spillcount);
382 /* construct interferences */
383 for (i = 0; i < spillcount; ++i) {
384 ir_node *spill1 = spilllist[i]->spill;
386 if (is_NoMem(spill1))
389 for (i2 = i+1; i2 < spillcount; ++i2) {
390 ir_node *spill2 = spilllist[i2]->spill;
392 if (is_NoMem(spill2))
395 if (my_values_interfere(env->irg, spill1, spill2)) {
396 DB((dbg, DBG_INTERFERENCES,
397 "Slot %d and %d interfere\n", i, i2));
399 bitset_set(interferences[i], i2);
400 bitset_set(interferences[i2], i);
405 /* sort affinity edges */
406 affinity_edge_count = ARR_LEN(env->affinity_edges);
407 qsort(env->affinity_edges, affinity_edge_count,
408 sizeof(env->affinity_edges[0]), cmp_affinity);
410 /*dump_interference_graph(env, interferences, "before"); */
412 /* try to merge affine nodes */
413 for (i = 0; i < affinity_edge_count; ++i) {
414 const affinity_edge_t *edge = env->affinity_edges[i];
415 int s1 = uf_find(spillslot_unionfind, edge->slot1);
416 int s2 = uf_find(spillslot_unionfind, edge->slot2);
418 /* test if values interfere */
419 if (bitset_is_set(interferences[s1], s2)) {
420 assert(bitset_is_set(interferences[s2], s1));
424 DB((dbg, DBG_COALESCING,
425 "Merging %d and %d because of affinity edge\n", s1, s2));
427 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
430 /* try to merge as much remaining spillslots as possible */
431 for (i = 0; i < spillcount; ++i) {
432 int s1 = uf_find(spillslot_unionfind, i);
436 for (i2 = i+1; i2 < spillcount; ++i2) {
437 int s2 = uf_find(spillslot_unionfind, i2);
441 /* test if values interfere
442 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
443 * when node merging occurs
445 if (bitset_is_set(interferences[s1], s2)) {
446 assert(bitset_is_set(interferences[s2], s1));
450 DB((dbg, DBG_COALESCING,
451 "Merging %d and %d because it is possible\n", s1, s2));
453 if (merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
454 /* we can break the loop here, because s2 is the new supernode
455 * now and we'll test s2 again later anyway */
461 /* assign spillslots to spills */
462 for (i = 0; i < spillcount; ++i) {
463 spill_t *spill = spilllist[i];
465 spill->spillslot = uf_find(spillslot_unionfind, i);
468 /*dump_interference_graph(env, interferences, "after");*/
469 obstack_free(&data, 0);
474 typedef struct _spill_slot_t {
480 typedef struct _memperm_entry_t {
485 struct _memperm_entry_t *next;
488 typedef struct _memperm_t {
491 memperm_entry_t *entries;
494 static int cmp_memperm(const void* d1, const void* d2, size_t size)
496 const memperm_t* e1 = d1;
497 const memperm_t* e2 = d2;
500 return e1->block != e2->block;
503 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
505 memperm_t entry, *res;
509 hash = hash_irn(block);
511 res = set_find(env->memperms, &entry, sizeof(entry), hash);
514 entry.entrycount = 0;
515 entry.entries = NULL;
516 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
522 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
524 ir_graph *irg = env->irg;
525 ir_type *frame = get_irg_frame_type(irg);
526 /* TODO: backend should be able to specify wether we want spill slots
527 * at begin or end of frame */
529 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
531 /* adjust size of the entity type... */
532 ir_type *enttype = get_entity_type(res);
533 set_type_size_bytes(enttype, slot->size);
541 * Enlarges a spillslot (if necessary) so that it can carry a value of size
542 * @p othersize and alignment @p otheralign.
544 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
546 if (othersize > slot->size) {
547 slot->size = othersize;
549 if (otheralign > slot->align) {
550 if (otheralign % slot->align != 0)
551 slot->align *= otheralign;
553 slot->align = otheralign;
554 } else if (slot->align % otheralign != 0) {
555 slot->align *= otheralign;
560 static void assign_spill_entity(ir_node *node, ir_entity *entity)
567 arity = get_irn_arity(node);
568 for (i = 0; i < arity; ++i) {
569 ir_node *in = get_irn_n(node, i);
572 assign_spill_entity(in, entity);
577 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
579 node = skip_Proj(node);
580 assert(arch_get_frame_entity(node) == NULL);
581 arch_set_frame_entity(node, entity);
585 * Create stack entities for the spillslots and assign them to the spill and
588 static void assign_spillslots(be_fec_env_t *env)
590 int spillcount = set_count(env->spills);
591 spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
595 /* construct spillslots */
596 for (spill = set_first(env->spills); spill != NULL;
597 spill = set_next(env->spills)) {
599 int slotid = spill->spillslot;
600 const ir_mode *mode = spill->mode;
601 spill_slot_t *slot = & (spillslots[slotid]);
602 int size = get_mode_size_bytes(mode);
603 int align = spill->alignment;
605 if (slot->align == 0 && slot->size == 0) {
609 enlarge_spillslot(slot, align, size);
613 for (spill = set_first(env->spills); spill != NULL;
614 spill = set_next(env->spills)) {
616 ir_node *node = spill->spill;
617 int slotid = spill->spillslot;
620 slot = &spillslots[slotid];
621 if (slot->entity == NULL) {
622 create_stack_entity(env, slot);
627 ir_node *block = get_nodes_block(node);
629 /* should be a PhiM */
630 assert(is_Phi(node));
632 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
633 ir_node *arg = get_irn_n(node, i);
634 ir_node *predblock = get_Block_cfgpred_block(block, i);
638 argspill = get_spill(env, arg);
639 assert(argspill != NULL);
641 argslotid = argspill->spillslot;
642 if (slotid != argslotid) {
644 memperm_entry_t *entry;
645 spill_slot_t *argslot = &spillslots[argslotid];
646 if (argslot->entity == NULL) {
647 create_stack_entity(env, argslot);
650 memperm = get_memperm(env, predblock);
652 entry = OALLOC(&env->obst, memperm_entry_t);
655 entry->in = argslot->entity;
656 entry->out = slot->entity;
657 entry->next = memperm->entries;
658 memperm->entrycount++;
659 memperm->entries = entry;
663 assign_spill_entity(node, slot->entity);
667 for (i = 0; i < ARR_LEN(env->reloads); ++i) {
668 ir_node *reload = env->reloads[i];
669 ir_node *spillnode = get_memory_edge(reload);
670 spill_t *spill = get_spill(env, spillnode);
671 const spill_slot_t *slot = & spillslots[spill->spillslot];
673 assert(slot->entity != NULL);
675 arch_set_frame_entity(reload, slot->entity);
680 * Returns the last node in a block which is no control flow changing node
682 static ir_node *get_end_of_block_insertion_point(ir_node* block)
684 ir_node* ins = sched_last(block);
685 while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
686 ins = sched_prev(ins);
692 ir_node *prev = sched_prev(ins);
702 static void create_memperms(be_fec_env_t *env)
704 const arch_env_t *arch_env = env->arch_env;
705 ir_graph *irg = env->irg;
708 for (memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
709 ir_node **nodes = ALLOCAN(ir_node*, memperm->entrycount);
710 memperm_entry_t *entry;
712 ir_node *mempermnode;
715 assert(memperm->entrycount > 0);
717 for (entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
718 ir_node* arg = get_irn_n(entry->node, entry->pos);
722 mempermnode = be_new_MemPerm(arch_env, memperm->block,
723 memperm->entrycount, nodes);
725 /* insert node into schedule */
726 blockend = get_end_of_block_insertion_point(memperm->block);
727 sched_add_before(blockend, mempermnode);
728 stat_ev_dbl("mem_perm", memperm->entrycount);
731 for (entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
733 ir_node* arg = get_irn_n(entry->node, entry->pos);
735 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
736 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
737 set_irg_current_block(irg, memperm->block);
738 proj = new_Proj(mempermnode, get_irn_mode(arg), i);
740 set_irn_n(entry->node, entry->pos, proj);
745 static int count_spillslots(const be_fec_env_t *env)
747 const spill_t *spill;
748 int spillcount = set_count(env->spills);
749 bitset_t *counted = bitset_alloca(spillcount);
753 for (spill = set_first(env->spills); spill != NULL;
754 spill = set_next(env->spills)) {
755 int spillslot = spill->spillslot;
756 if (!bitset_is_set(counted, spillslot)) {
758 bitset_set(counted, spillslot);
765 be_fec_env_t *be_new_frame_entity_coalescer(ir_graph *irg)
767 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
768 be_fec_env_t *env = XMALLOC(be_fec_env_t);
770 be_liveness_assure_chk(be_assure_liveness(irg));
772 obstack_init(&env->obst);
773 env->arch_env = arch_env;
775 env->spills = new_set(cmp_spill, 10);
776 env->reloads = NEW_ARR_F(ir_node*, 0);
777 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
778 env->memperms = new_set(cmp_memperm, 10);
783 void be_free_frame_entity_coalescer(be_fec_env_t *env)
785 del_set(env->memperms);
786 DEL_ARR_F(env->reloads);
787 DEL_ARR_F(env->affinity_edges);
788 del_set(env->spills);
789 obstack_free(&env->obst, NULL);
794 void be_assign_entities(be_fec_env_t *env)
796 stat_ev_dbl("spillslots", set_count(env->spills));
798 if (be_coalesce_spill_slots) {
799 do_greedy_coalescing(env);
802 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
804 assign_spillslots(env);
806 create_memperms(env);
810 * This walker function searches for reloads and collects all the spills
811 * and memphis attached to them.
813 static void collect_spills_walker(ir_node *node, void *data)
815 be_fec_env_t *env = data;
817 const arch_register_class_t *cls;
820 if (! (arch_irn_classify(node) & arch_irn_class_reload))
823 mode = get_irn_mode(node);
824 cls = arch_get_irn_reg_class_out(node);
825 align = arch_env_get_reg_class_alignment(env->arch_env, cls);
827 be_node_needs_frame_entity(env, node, mode, align);
830 void be_coalesce_spillslots(ir_graph *irg)
832 be_fec_env_t *env = be_new_frame_entity_coalescer(irg);
834 /* collect reloads */
835 irg_walk_graph(irg, NULL, collect_spills_walker, env);
837 be_assign_entities(env);
839 be_free_frame_entity_coalescer(env);
842 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);
843 void be_init_spillslots(void)
845 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");