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
36 #include "unionfind.h"
42 #include "bespillslots.h"
43 #include "bechordal_t.h"
44 #include "bestatevent.h"
46 #include "beintlive_t.h"
50 #define DBG_COALESCING 1
51 #define DBG_INTERFERENCES 2
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55 typedef struct spill_t {
57 const ir_mode *mode; /**< mode of the spilled value */
58 int alignment; /**< alignment for the spilled value */
59 int spillslot; /**< index into spillslot_unionfind structure */
62 typedef struct affinity_edge_t {
73 affinity_edge_t **affinity_edges;
75 set_frame_entity_func set_frame_entity;
76 bool at_begin; /**< frame entities should be allocate at
77 the beginning of the stackframe */
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 = (const affinity_edge_t**)d1;
84 const affinity_edge_t * const *e2 = (const affinity_edge_t**)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 = (const spill_t*)d1;
93 const spill_t* s2 = (const spill_t*)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 = (spill_t*)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 = (spill_t*)set_find(env->spills, &spill, sizeof(spill), hash);
136 spill.spillslot = set_count(env->spills);
138 spill.alignment = align;
139 res = (spill_t*)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_irg_exec_freq(env->irg);
157 assert(is_Phi(node));
160 res = (spill_t*)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 = (spill_t*)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 = OALLOC(&env->obst, affinity_edge_t);
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);
202 assert(spillnode != NULL);
204 /* walk upwards and collect all phis and spills on this way */
205 if (is_Phi(spillnode)) {
206 collect_memphi(env, spillnode, mode, align);
208 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]));
373 foreach_set(env->spills, spill_t*, spill) {
374 assert(spill->spillslot < spillcount);
375 spilllist[spill->spillslot] = spill;
379 for (i = 0; i < spillcount; ++i) {
380 interferences[i] = bitset_obstack_alloc(&data, spillcount);
383 /* construct interferences */
384 for (i = 0; i < spillcount; ++i) {
385 ir_node *spill1 = spilllist[i]->spill;
387 if (is_NoMem(spill1))
390 for (i2 = i+1; i2 < spillcount; ++i2) {
391 ir_node *spill2 = spilllist[i2]->spill;
393 if (is_NoMem(spill2))
396 if (my_values_interfere(env->irg, spill1, spill2)) {
397 DB((dbg, DBG_INTERFERENCES,
398 "Slot %d and %d interfere\n", i, i2));
400 bitset_set(interferences[i], i2);
401 bitset_set(interferences[i2], i);
406 /* sort affinity edges */
407 affinity_edge_count = ARR_LEN(env->affinity_edges);
408 qsort(env->affinity_edges, affinity_edge_count,
409 sizeof(env->affinity_edges[0]), cmp_affinity);
411 /*dump_interference_graph(env, interferences, "before"); */
413 /* try to merge affine nodes */
414 for (i = 0; i < affinity_edge_count; ++i) {
415 const affinity_edge_t *edge = env->affinity_edges[i];
416 int s1 = uf_find(spillslot_unionfind, edge->slot1);
417 int s2 = uf_find(spillslot_unionfind, edge->slot2);
419 /* test if values interfere */
420 if (bitset_is_set(interferences[s1], s2)) {
421 assert(bitset_is_set(interferences[s2], s1));
425 DB((dbg, DBG_COALESCING,
426 "Merging %d and %d because of affinity edge\n", s1, s2));
428 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
431 /* try to merge as much remaining spillslots as possible */
432 for (i = 0; i < spillcount; ++i) {
433 int s1 = uf_find(spillslot_unionfind, i);
437 for (i2 = i+1; i2 < spillcount; ++i2) {
438 int s2 = uf_find(spillslot_unionfind, i2);
442 /* test if values interfere
443 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
444 * when node merging occurs
446 if (bitset_is_set(interferences[s1], s2)) {
447 assert(bitset_is_set(interferences[s2], s1));
451 DB((dbg, DBG_COALESCING,
452 "Merging %d and %d because it is possible\n", s1, s2));
454 if (merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
455 /* we can break the loop here, because s2 is the new supernode
456 * now and we'll test s2 again later anyway */
462 /* assign spillslots to spills */
463 for (i = 0; i < spillcount; ++i) {
464 spill_t *spill = spilllist[i];
466 spill->spillslot = uf_find(spillslot_unionfind, i);
469 /*dump_interference_graph(env, interferences, "after");*/
470 obstack_free(&data, 0);
475 typedef struct spill_slot_t {
481 typedef struct memperm_entry_t {
486 struct memperm_entry_t *next;
489 typedef struct memperm_t {
492 memperm_entry_t *entries;
495 static int cmp_memperm(const void* d1, const void* d2, size_t size)
497 const memperm_t* e1 = (const memperm_t*)d1;
498 const memperm_t* e2 = (const memperm_t*)d2;
501 return e1->block != e2->block;
504 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
506 memperm_t entry, *res;
510 hash = hash_irn(block);
512 res = (memperm_t*)set_find(env->memperms, &entry, sizeof(entry), hash);
515 entry.entrycount = 0;
516 entry.entries = NULL;
517 res = (memperm_t*)set_insert(env->memperms, &entry, sizeof(entry), hash);
523 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
525 ir_graph *irg = env->irg;
526 ir_type *frame = get_irg_frame_type(irg);
527 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align,
535 * Enlarges a spillslot (if necessary) so that it can carry a value of size
536 * @p othersize and alignment @p otheralign.
538 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
540 if (othersize > slot->size) {
541 slot->size = othersize;
543 if (otheralign > slot->align) {
544 if (otheralign % slot->align != 0)
545 slot->align *= otheralign;
547 slot->align = otheralign;
548 } else if (slot->align % otheralign != 0) {
549 slot->align *= otheralign;
553 static void assign_spill_entity(be_fec_env_t *env,
554 ir_node *node, ir_entity *entity)
561 arity = get_irn_arity(node);
562 for (i = 0; i < arity; ++i) {
563 ir_node *in = get_irn_n(node, i);
566 assign_spill_entity(env, in, entity);
571 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
573 node = skip_Proj(node);
574 assert(arch_get_frame_entity(node) == NULL);
575 env->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 foreach_set(env->spills, spill_t*, spill) {
591 int slotid = spill->spillslot;
592 const ir_mode *mode = spill->mode;
593 spill_slot_t *slot = & (spillslots[slotid]);
594 int size = get_mode_size_bytes(mode);
595 int align = spill->alignment;
597 if (slot->align == 0 && slot->size == 0) {
601 enlarge_spillslot(slot, align, size);
605 foreach_set(env->spills, spill_t*, spill) {
606 ir_node *node = spill->spill;
607 int slotid = spill->spillslot;
610 slot = &spillslots[slotid];
611 if (slot->entity == NULL) {
612 create_stack_entity(env, slot);
617 ir_node *block = get_nodes_block(node);
619 /* should be a PhiM */
620 assert(get_irn_mode(node) == mode_M);
622 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
623 ir_node *arg = get_irn_n(node, i);
624 ir_node *predblock = get_Block_cfgpred_block(block, i);
628 argspill = get_spill(env, arg);
629 assert(argspill != NULL);
631 argslotid = argspill->spillslot;
632 if (slotid != argslotid) {
634 memperm_entry_t *entry;
635 spill_slot_t *argslot = &spillslots[argslotid];
636 if (argslot->entity == NULL) {
637 create_stack_entity(env, argslot);
640 memperm = get_memperm(env, predblock);
642 entry = OALLOC(&env->obst, memperm_entry_t);
645 entry->in = argslot->entity;
646 entry->out = slot->entity;
647 entry->next = memperm->entries;
648 memperm->entrycount++;
649 memperm->entries = entry;
653 assign_spill_entity(env, node, slot->entity);
657 for (i = 0; i < ARR_LEN(env->reloads); ++i) {
658 ir_node *reload = env->reloads[i];
659 ir_node *spillnode = get_memory_edge(reload);
660 spill_t *spill = get_spill(env, spillnode);
661 const spill_slot_t *slot = & spillslots[spill->spillslot];
663 assert(slot->entity != NULL);
665 env->set_frame_entity(reload, slot->entity);
670 * Returns the last node in a block which is no control flow changing node
672 static ir_node *get_end_of_block_insertion_point(ir_node* block)
674 ir_node* ins = sched_last(block);
675 while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
676 ins = sched_prev(ins);
682 ir_node *prev = sched_prev(ins);
692 static void create_memperms(be_fec_env_t *env)
696 foreach_set(env->memperms, memperm_t*, memperm) {
697 ir_node **nodes = ALLOCAN(ir_node*, memperm->entrycount);
698 memperm_entry_t *entry;
700 ir_node *mempermnode;
703 assert(memperm->entrycount > 0);
705 for (entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
706 ir_node* arg = get_irn_n(entry->node, entry->pos);
710 mempermnode = be_new_MemPerm(memperm->block, memperm->entrycount,
713 /* insert node into schedule */
714 blockend = get_end_of_block_insertion_point(memperm->block);
715 sched_add_before(blockend, mempermnode);
716 stat_ev_dbl("mem_perm", memperm->entrycount);
719 for (entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
721 ir_node* arg = get_irn_n(entry->node, entry->pos);
723 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
724 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
725 proj = new_r_Proj(mempermnode, get_irn_mode(arg), i);
727 set_irn_n(entry->node, entry->pos, proj);
732 static int count_spillslots(const be_fec_env_t *env)
734 const spill_t *spill;
735 int spillcount = set_count(env->spills);
736 bitset_t *counted = bitset_alloca(spillcount);
740 foreach_set(env->spills, spill_t*, spill) {
741 int spillslot = spill->spillslot;
742 if (!bitset_is_set(counted, spillslot)) {
744 bitset_set(counted, spillslot);
751 be_fec_env_t *be_new_frame_entity_coalescer(ir_graph *irg)
753 be_fec_env_t *env = XMALLOCZ(be_fec_env_t);
755 be_assure_live_chk(irg);
757 obstack_init(&env->obst);
759 env->spills = new_set(cmp_spill, 10);
760 env->reloads = NEW_ARR_F(ir_node*, 0);
761 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
762 env->memperms = new_set(cmp_memperm, 10);
767 void be_free_frame_entity_coalescer(be_fec_env_t *env)
769 del_set(env->memperms);
770 DEL_ARR_F(env->reloads);
771 DEL_ARR_F(env->affinity_edges);
772 del_set(env->spills);
773 obstack_free(&env->obst, NULL);
778 void be_assign_entities(be_fec_env_t *env,
779 set_frame_entity_func set_frame_entity,
780 bool alloc_entities_at_begin)
782 env->set_frame_entity = set_frame_entity;
783 env->at_begin = alloc_entities_at_begin;
785 stat_ev_dbl("spillslots", set_count(env->spills));
787 if (be_coalesce_spill_slots) {
788 do_greedy_coalescing(env);
791 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
793 assign_spillslots(env);
795 create_memperms(env);
798 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots)
799 void be_init_spillslots(void)
801 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");