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 {
74 affinity_edge_t **affinity_edges;
76 set_frame_entity_func set_frame_entity;
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 = (const affinity_edge_t**)d1;
83 const affinity_edge_t * const *e2 = (const affinity_edge_t**)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 = (const spill_t*)d1;
92 const spill_t* s2 = (const spill_t*)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 = (spill_t*)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 = (spill_t*)set_find(env->spills, &spill, sizeof(spill), hash);
135 spill.spillslot = set_count(env->spills);
137 spill.alignment = align;
138 res = (spill_t*)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 = (spill_t*)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 = (spill_t*)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]));
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 /* TODO: backend should be able to specify wether we want spill slots
528 * at begin or end of frame */
530 ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
532 /* adjust size of the entity type... */
533 ir_type *enttype = get_entity_type(res);
534 set_type_size_bytes(enttype, slot->size);
542 * Enlarges a spillslot (if necessary) so that it can carry a value of size
543 * @p othersize and alignment @p otheralign.
545 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
547 if (othersize > slot->size) {
548 slot->size = othersize;
550 if (otheralign > slot->align) {
551 if (otheralign % slot->align != 0)
552 slot->align *= otheralign;
554 slot->align = otheralign;
555 } else if (slot->align % otheralign != 0) {
556 slot->align *= otheralign;
560 static void assign_spill_entity(be_fec_env_t *env,
561 ir_node *node, ir_entity *entity)
568 arity = get_irn_arity(node);
569 for (i = 0; i < arity; ++i) {
570 ir_node *in = get_irn_n(node, i);
573 assign_spill_entity(env, in, entity);
578 /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
580 node = skip_Proj(node);
581 assert(arch_get_frame_entity(node) == NULL);
582 env->set_frame_entity(node, entity);
586 * Create stack entities for the spillslots and assign them to the spill and
589 static void assign_spillslots(be_fec_env_t *env)
591 int spillcount = set_count(env->spills);
592 spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
596 /* construct spillslots */
597 foreach_set(env->spills, spill_t*, spill) {
598 int slotid = spill->spillslot;
599 const ir_mode *mode = spill->mode;
600 spill_slot_t *slot = & (spillslots[slotid]);
601 int size = get_mode_size_bytes(mode);
602 int align = spill->alignment;
604 if (slot->align == 0 && slot->size == 0) {
608 enlarge_spillslot(slot, align, size);
612 foreach_set(env->spills, spill_t*, spill) {
613 ir_node *node = spill->spill;
614 int slotid = spill->spillslot;
617 slot = &spillslots[slotid];
618 if (slot->entity == NULL) {
619 create_stack_entity(env, slot);
624 ir_node *block = get_nodes_block(node);
626 /* should be a PhiM */
627 assert(is_Phi(node));
629 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
630 ir_node *arg = get_irn_n(node, i);
631 ir_node *predblock = get_Block_cfgpred_block(block, i);
635 argspill = get_spill(env, arg);
636 assert(argspill != NULL);
638 argslotid = argspill->spillslot;
639 if (slotid != argslotid) {
641 memperm_entry_t *entry;
642 spill_slot_t *argslot = &spillslots[argslotid];
643 if (argslot->entity == NULL) {
644 create_stack_entity(env, argslot);
647 memperm = get_memperm(env, predblock);
649 entry = OALLOC(&env->obst, memperm_entry_t);
652 entry->in = argslot->entity;
653 entry->out = slot->entity;
654 entry->next = memperm->entries;
655 memperm->entrycount++;
656 memperm->entries = entry;
660 assign_spill_entity(env, node, slot->entity);
664 for (i = 0; i < ARR_LEN(env->reloads); ++i) {
665 ir_node *reload = env->reloads[i];
666 ir_node *spillnode = get_memory_edge(reload);
667 spill_t *spill = get_spill(env, spillnode);
668 const spill_slot_t *slot = & spillslots[spill->spillslot];
670 assert(slot->entity != NULL);
672 env->set_frame_entity(reload, slot->entity);
677 * Returns the last node in a block which is no control flow changing node
679 static ir_node *get_end_of_block_insertion_point(ir_node* block)
681 ir_node* ins = sched_last(block);
682 while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
683 ins = sched_prev(ins);
689 ir_node *prev = sched_prev(ins);
699 static void create_memperms(be_fec_env_t *env)
703 foreach_set(env->memperms, memperm_t*, memperm) {
704 ir_node **nodes = ALLOCAN(ir_node*, memperm->entrycount);
705 memperm_entry_t *entry;
707 ir_node *mempermnode;
710 assert(memperm->entrycount > 0);
712 for (entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
713 ir_node* arg = get_irn_n(entry->node, entry->pos);
717 mempermnode = be_new_MemPerm(memperm->block, memperm->entrycount,
720 /* insert node into schedule */
721 blockend = get_end_of_block_insertion_point(memperm->block);
722 sched_add_before(blockend, mempermnode);
723 stat_ev_dbl("mem_perm", memperm->entrycount);
726 for (entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
728 ir_node* arg = get_irn_n(entry->node, entry->pos);
730 be_set_MemPerm_in_entity(mempermnode, i, entry->in);
731 be_set_MemPerm_out_entity(mempermnode, i, entry->out);
732 proj = new_r_Proj(mempermnode, get_irn_mode(arg), i);
734 set_irn_n(entry->node, entry->pos, proj);
739 static int count_spillslots(const be_fec_env_t *env)
741 const spill_t *spill;
742 int spillcount = set_count(env->spills);
743 bitset_t *counted = bitset_alloca(spillcount);
747 foreach_set(env->spills, spill_t*, spill) {
748 int spillslot = spill->spillslot;
749 if (!bitset_is_set(counted, spillslot)) {
751 bitset_set(counted, spillslot);
758 be_fec_env_t *be_new_frame_entity_coalescer(ir_graph *irg)
760 be_fec_env_t *env = XMALLOCZ(be_fec_env_t);
762 be_liveness_assure_chk(be_assure_liveness(irg));
764 obstack_init(&env->obst);
766 env->spills = new_set(cmp_spill, 10);
767 env->reloads = NEW_ARR_F(ir_node*, 0);
768 env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
769 env->memperms = new_set(cmp_memperm, 10);
774 void be_free_frame_entity_coalescer(be_fec_env_t *env)
776 del_set(env->memperms);
777 DEL_ARR_F(env->reloads);
778 DEL_ARR_F(env->affinity_edges);
779 del_set(env->spills);
780 obstack_free(&env->obst, NULL);
785 void be_assign_entities(be_fec_env_t *env,
786 set_frame_entity_func set_frame_entity)
788 env->set_frame_entity = set_frame_entity;
790 stat_ev_dbl("spillslots", set_count(env->spills));
792 if (be_coalesce_spill_slots) {
793 do_greedy_coalescing(env);
796 stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
798 assign_spillslots(env);
800 create_memperms(env);
804 * This walker function searches for reloads and collects all the spills
805 * and memphis attached to them.
807 static void collect_spills_walker(ir_node *node, void *data)
809 be_fec_env_t *env = (be_fec_env_t*)data;
811 const arch_register_class_t *cls;
814 const arch_env_t *arch_env;
816 if (! (arch_irn_classify(node) & arch_irn_class_reload))
819 mode = get_irn_mode(node);
820 cls = arch_get_irn_reg_class_out(node);
821 irg = get_irn_irg(node);
822 arch_env = be_get_irg_arch_env(irg);
823 align = arch_env_get_reg_class_alignment(arch_env, cls);
825 be_node_needs_frame_entity(env, node, mode, align);
828 void be_coalesce_spillslots(ir_graph *irg)
830 be_fec_env_t *env = be_new_frame_entity_coalescer(irg);
832 /* collect reloads */
833 irg_walk_graph(irg, NULL, collect_spills_walker, env);
835 be_assign_entities(env, be_node_set_frame_entity);
837 be_free_frame_entity_coalescer(env);
840 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);
841 void be_init_spillslots(void)
843 FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");