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"
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 */
62 typedef struct affinity_edge_t {
74 affinity_edge_t **affinity_edges;
76 set_frame_entity_func set_frame_entity;
77 bool at_begin; /**< frame entities should be allocate at
78 the beginning of the stackframe */
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 = (const affinity_edge_t**)d1;
85 const affinity_edge_t * const *e2 = (const affinity_edge_t**)d2;
86 double aff1 = (*e1)->affinity;
87 double aff2 = (*e2)->affinity;
89 /* sort in descending order */
92 } else if (aff1 > aff2) {
95 int slot11 = (*e1)->slot1;
96 int slot21 = (*e2)->slot1;
97 if (slot11 < slot21) {
99 } else if (slot11 > slot21) {
102 int slot12 = (*e1)->slot2;
103 int slot22 = (*e2)->slot2;
104 return (slot12<slot22) - (slot12<slot22);
109 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
111 assert(rbitset_is_set(env->spills_set, get_irn_idx(node)));
112 return (spill_t*)get_irn_link(node);
115 static inline ir_node *get_memory_edge(const ir_node *node)
119 arity = get_irn_arity(node);
120 for (i = arity - 1; i >= 0; --i) {
121 ir_node *arg = get_irn_n(node, i);
122 if (get_irn_mode(arg) == mode_M)
129 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
130 const ir_mode *mode, int align)
134 /* already in spill set? */
135 unsigned idx = get_irn_idx(node);
136 if (rbitset_is_set(env->spills_set, idx)) {
137 spill_t *spill = get_spill(env, node);
138 assert(spill->mode == mode);
139 assert(spill->alignment == align);
142 rbitset_set(env->spills_set, idx);
144 spill = OALLOC(&env->obst, spill_t);
145 /* insert into set of spills if not already there */
148 spill->alignment = align;
149 spill->spillslot = (int)ARR_LEN(env->spills);
150 ARR_APP1(spill_t*, env->spills, spill);
151 set_irn_link(node, spill);
152 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill->spillslot, node));
155 int arity = get_irn_arity(node);
157 for (i = 0; i < arity; ++i) {
158 affinity_edge_t *affinty_edge;
159 ir_node *arg = get_irn_n(node, i);
160 spill_t *arg_spill = collect_spill(env, arg, mode, align);
161 ir_node *block = get_nodes_block(arg);
163 /* add an affinity edge */
164 affinty_edge = OALLOC(&env->obst, affinity_edge_t);
165 affinty_edge->affinity = get_block_execfreq(block);
166 affinty_edge->slot1 = spill->spillslot;
167 affinty_edge->slot2 = arg_spill->spillslot;
168 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
175 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
176 const ir_mode *mode, int align)
178 ir_node *spillnode = get_memory_edge(node);
179 assert(spillnode != NULL);
181 /* walk upwards and collect all phis and spills on this way */
182 collect_spill(env, spillnode, mode, align);
184 ARR_APP1(ir_node *, env->reloads, node);
187 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
188 int* spillslot_unionfind, int s1, int s2)
194 /* merge spillslots and interferences */
195 res = uf_union(spillslot_unionfind, s1, s2);
196 /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
203 bitset_or(interferences[s1], interferences[s2]);
205 /* update other interferences */
206 spillcount = ARR_LEN(env->spills);
207 for (i = 0; i < spillcount; ++i) {
208 bitset_t *intfs = interferences[i];
209 if (bitset_is_set(intfs, s2))
210 bitset_set(intfs, s1);
216 static bool my_values_interfere2(ir_graph *const irg, ir_node const *a, ir_node const *b)
218 if (value_dominates(b, a)) {
219 /* Adjust a and b so, that a dominates b if
220 * a dominates b or vice versa. */
221 ir_node const *const t = a;
224 } else if (!value_dominates(a, b)) {
225 /* If there is no dominance relation, they do not interfere. */
229 ir_node *const bb = get_nodes_block(b);
231 /* If a is live end in b's block it is
232 * live at b's definition (a dominates b) */
233 be_lv_t *const lv = be_get_irg_liveness(irg);
234 if (be_is_live_end(lv, bb, a))
237 /* Look at all usages of a.
238 * If there's one usage of a in the block of b, then
239 * we check, if this use is dominated by b, if that's true
240 * a and b interfere. Note that b must strictly dominate the user,
241 * since if b is the last user of in the block, b and a do not
243 * Uses of a not in b's block can be disobeyed, because the
244 * check for a being live at the end of b's block is already
246 foreach_out_edge(a, edge) {
247 ir_node const *const user = get_edge_src_irn(edge);
249 foreach_out_edge(user, edge2) {
250 ir_node const *const user2 = get_edge_src_irn(edge2);
251 assert(!is_Sync(user2));
252 if (get_nodes_block(user2) == bb && !is_Phi(user2) &&
253 _value_strictly_dominates_intrablock(b, user2))
257 if (get_nodes_block(user) == bb && !is_Phi(user) &&
258 _value_strictly_dominates_intrablock(b, user))
267 * same as values_interfere but with special handling for Syncs
269 static int my_values_interfere(ir_graph *irg, ir_node *a, ir_node *b)
272 int i, arity = get_irn_arity(a);
273 for (i = 0; i < arity; ++i) {
274 ir_node *in = get_irn_n(a, i);
275 if (my_values_interfere(irg, in, b))
279 } else if (is_Sync(b)) {
280 int i, arity = get_irn_arity(b);
281 for (i = 0; i < arity; ++i) {
282 ir_node *in = get_irn_n(b, i);
283 /* a is not a sync, so no need for my_values_interfere */
284 if (my_values_interfere2(irg, a, in))
290 return my_values_interfere2(irg, a, b);
294 * A greedy coalescing algorithm for spillslots:
295 * 1. Sort the list of affinity edges
296 * 2. Try to merge slots with affinity edges (most expensive slots first)
297 * 3. Try to merge everything else that is possible
299 static void do_greedy_coalescing(be_fec_env_t *env)
301 spill_t **spills = env->spills;
302 size_t spillcount = ARR_LEN(spills);
304 size_t affinity_edge_count;
305 bitset_t **interferences;
306 int* spillslot_unionfind;
314 DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
316 interferences = OALLOCN(&data, bitset_t*, spillcount);
317 spillslot_unionfind = OALLOCN(&data, int, spillcount);
319 uf_init(spillslot_unionfind, spillcount);
321 for (i = 0; i < spillcount; ++i) {
322 interferences[i] = bitset_obstack_alloc(&data, spillcount);
325 /* construct interferences */
326 for (i = 0; i < spillcount; ++i) {
328 ir_node *spill1 = spills[i]->spill;
329 if (is_NoMem(spill1))
332 for (i2 = i+1; i2 < spillcount; ++i2) {
333 ir_node *spill2 = spills[i2]->spill;
334 if (is_NoMem(spill2))
337 if (my_values_interfere(env->irg, spill1, spill2)) {
338 DB((dbg, DBG_INTERFERENCES,
339 "Slot %d and %d interfere\n", i, i2));
341 bitset_set(interferences[i], i2);
342 bitset_set(interferences[i2], i);
347 /* sort affinity edges */
348 affinity_edge_count = ARR_LEN(env->affinity_edges);
349 qsort(env->affinity_edges, affinity_edge_count,
350 sizeof(env->affinity_edges[0]), cmp_affinity);
352 /* try to merge affine nodes */
353 for (i = 0; i < affinity_edge_count; ++i) {
354 const affinity_edge_t *edge = env->affinity_edges[i];
355 int s1 = uf_find(spillslot_unionfind, edge->slot1);
356 int s2 = uf_find(spillslot_unionfind, edge->slot2);
358 /* test if values interfere */
359 if (bitset_is_set(interferences[s1], s2)) {
360 assert(bitset_is_set(interferences[s2], s1));
364 DB((dbg, DBG_COALESCING,
365 "Merging %d and %d because of affinity edge\n", s1, s2));
367 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
370 /* try to merge as much remaining spillslots as possible */
371 for (i = 0; i < spillcount; ++i) {
373 int s1 = uf_find(spillslot_unionfind, i);
377 for (i2 = i+1; i2 < spillcount; ++i2) {
378 int s2 = uf_find(spillslot_unionfind, i2);
382 /* test if values interfere
383 * we have to test n1-n2 and n2-n1, because only 1 side gets updated
384 * when node merging occurs
386 if (bitset_is_set(interferences[s1], s2)) {
387 assert(bitset_is_set(interferences[s2], s1));
391 DB((dbg, DBG_COALESCING,
392 "Merging %d and %d because it is possible\n", s1, s2));
394 if (merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
395 /* we can break the loop here, because s2 is the new supernode
396 * now and we'll test s2 again later anyway */
402 /* assign spillslots to spills */
403 for (i = 0; i < spillcount; ++i) {
404 spills[i]->spillslot = uf_find(spillslot_unionfind, i);
407 obstack_free(&data, 0);
410 typedef struct spill_slot_t {
416 typedef struct memperm_entry_t {
421 struct memperm_entry_t *next;
424 typedef struct memperm_t {
427 memperm_entry_t *entries;
430 static int cmp_memperm(const void* d1, const void* d2, size_t size)
432 const memperm_t* e1 = (const memperm_t*)d1;
433 const memperm_t* e2 = (const memperm_t*)d2;
436 return e1->block != e2->block;
439 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
441 memperm_t entry, *res;
445 hash = hash_irn(block);
447 res = set_find(memperm_t, env->memperms, &entry, sizeof(entry), hash);
450 entry.entrycount = 0;
451 entry.entries = NULL;
452 res = set_insert(memperm_t, env->memperms, &entry, sizeof(entry), hash);
458 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
460 ir_graph *irg = env->irg;
461 ir_type *frame = get_irg_frame_type(irg);
462 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align,
470 * Enlarges a spillslot (if necessary) so that it can carry a value of size
471 * @p othersize and alignment @p otheralign.
473 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
475 if (othersize > slot->size) {
476 slot->size = othersize;
478 if (otheralign > slot->align) {
479 if (otheralign % slot->align != 0)
480 slot->align *= otheralign;
482 slot->align = otheralign;
483 } else if (slot->align % otheralign != 0) {
484 slot->align *= otheralign;
488 static void assign_spill_entity(be_fec_env_t *env,
489 ir_node *node, ir_entity *entity)
496 arity = get_irn_arity(node);
497 for (i = 0; i < arity; ++i) {
498 ir_node *in = get_irn_n(node, i);
501 assign_spill_entity(env, in, entity);
506 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
508 node = skip_Proj(node);
509 assert(arch_get_frame_entity(node) == NULL);
510 env->set_frame_entity(node, entity);
514 * Create stack entities for the spillslots and assign them to the spill and
517 static void assign_spillslots(be_fec_env_t *env)
519 spill_t **spills = env->spills;
520 size_t spillcount = ARR_LEN(spills);
521 spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
524 /* construct spillslots */
525 for (s = 0; s < spillcount; ++s) {
526 const spill_t *spill = spills[s];
527 int slotid = spill->spillslot;
528 const ir_mode *mode = spill->mode;
529 spill_slot_t *slot = & (spillslots[slotid]);
530 int size = get_mode_size_bytes(mode);
531 int align = spill->alignment;
533 if (slot->align == 0 && slot->size == 0) {
537 enlarge_spillslot(slot, align, size);
541 for (s = 0; s < spillcount; ++s) {
542 const spill_t *spill = spills[s];
543 ir_node *node = spill->spill;
544 int slotid = spill->spillslot;
545 spill_slot_t *slot = &spillslots[slotid];
547 if (slot->entity == NULL) {
548 create_stack_entity(env, slot);
552 int arity = get_irn_arity(node);
554 ir_node *block = get_nodes_block(node);
556 /* should be a PhiM */
557 assert(get_irn_mode(node) == mode_M);
559 for (i = 0; i < arity; ++i) {
560 ir_node *arg = get_irn_n(node, i);
561 ir_node *predblock = get_Block_cfgpred_block(block, i);
562 spill_t *argspill = get_spill(env, arg);
563 int argslotid = argspill->spillslot;
565 if (slotid != argslotid) {
567 memperm_entry_t *entry;
568 spill_slot_t *argslot = &spillslots[argslotid];
569 if (argslot->entity == NULL) {
570 create_stack_entity(env, argslot);
573 memperm = get_memperm(env, predblock);
575 entry = OALLOC(&env->obst, memperm_entry_t);
578 entry->in = argslot->entity;
579 entry->out = slot->entity;
580 entry->next = memperm->entries;
581 memperm->entrycount++;
582 memperm->entries = entry;
586 assign_spill_entity(env, node, slot->entity);
590 for (s = 0; s < ARR_LEN(env->reloads); ++s) {
591 ir_node *reload = env->reloads[s];
592 ir_node *spillnode = get_memory_edge(reload);
593 const spill_t *spill = get_spill(env, spillnode);
594 const spill_slot_t *slot = &spillslots[spill->spillslot];
596 assert(slot->entity != NULL);
598 env->set_frame_entity(reload, slot->entity);
603 * Returns the last node in a block which is no control flow changing node
605 static ir_node *get_end_of_block_insertion_point(ir_node* block)
607 ir_node* ins = sched_last(block);
608 while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
609 ins = sched_prev(ins);
615 ir_node *prev = sched_prev(ins);
625 static void create_memperms(be_fec_env_t *env)
627 foreach_set(env->memperms, memperm_t, memperm) {
628 ir_node **nodes = ALLOCAN(ir_node*, memperm->entrycount);
629 memperm_entry_t *entry;
631 ir_node *mempermnode;
634 assert(memperm->entrycount > 0);
636 for (entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
637 ir_node* arg = get_irn_n(entry->node, entry->pos);
641 mempermnode = be_new_MemPerm(memperm->block, memperm->entrycount,
644 /* insert node into schedule */
645 blockend = get_end_of_block_insertion_point(memperm->block);
646 sched_add_before(blockend, mempermnode);
647 stat_ev_dbl("mem_perm", memperm->entrycount);
650 for (entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
652 ir_node* arg = get_irn_n(entry->node, entry->pos);
654 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
655 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
656 proj = new_r_Proj(mempermnode, get_irn_mode(arg), i);
658 set_irn_n(entry->node, entry->pos, proj);
663 static unsigned count_spillslots(const be_fec_env_t *env)
665 size_t spillcount = ARR_LEN(env->spills);
666 unsigned slotcount = 0;
669 unsigned *const counted = rbitset_alloca(spillcount);
670 for (s = 0; s < spillcount; ++s) {
671 spill_t *spill = env->spills[s];
672 int spillslot = spill->spillslot;
673 if (!rbitset_is_set(counted, spillslot)) {
675 rbitset_set(counted, spillslot);
682 be_fec_env_t *be_new_frame_entity_coalescer(ir_graph *irg)
684 be_fec_env_t *env = XMALLOCZ(be_fec_env_t);
686 be_assure_live_chk(irg);
688 obstack_init(&env->obst);
690 env->spills = NEW_ARR_F(spill_t*, 0);
691 env->spills_set = rbitset_malloc(get_irg_last_idx(irg));
692 env->reloads = NEW_ARR_F(ir_node*, 0);
693 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
694 env->memperms = new_set(cmp_memperm, 10);
696 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
701 void be_free_frame_entity_coalescer(be_fec_env_t *env)
703 ir_free_resources(env->irg, IR_RESOURCE_IRN_LINK);
705 del_set(env->memperms);
706 DEL_ARR_F(env->reloads);
707 DEL_ARR_F(env->affinity_edges);
708 DEL_ARR_F(env->spills);
709 xfree(env->spills_set);
710 obstack_free(&env->obst, NULL);
715 void be_assign_entities(be_fec_env_t *env,
716 set_frame_entity_func set_frame_entity,
717 bool alloc_entities_at_begin)
719 env->set_frame_entity = set_frame_entity;
720 env->at_begin = alloc_entities_at_begin;
722 if (stat_ev_enabled) {
723 stat_ev_dbl("spillslots", ARR_LEN(env->spills));
726 if (be_coalesce_spill_slots) {
727 do_greedy_coalescing(env);
730 if (stat_ev_enabled) {
731 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
734 assign_spillslots(env);
736 create_memperms(env);
739 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots)
740 void be_init_spillslots(void)
742 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");