e39319d0b16f24dfae3f3575e4804df5cd59d5ed
[libfirm] / ir / be / bespillslots.c
1 /*
2  * Author:      Matthias Braun
3  * Date:                26.7.06
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "set.h"
14
15 #include "irgwalk.h"
16 #include "ircons.h"
17 #include "irprintf.h"
18 #include "execfreq.h"
19 #include "unionfind.h"
20 #include "type.h"
21 #include "irdump_t.h"
22
23 #include "benode_t.h"
24 #include "besched.h"
25 #include "bespillslots.h"
26 #include "bechordal_t.h"
27 #include "bejavacoal.h"
28
29
30 #define DBG_COALESCING          1
31 #define DBG_INTERFERENCES       2
32
33 DEBUG_ONLY(
34 static firm_dbg_module_t *dbg = NULL;
35 )
36
37 typedef struct _spill_t {
38         ir_node *spill;
39         /** regclass of the spilled value */
40         const arch_register_class_t *cls;
41         /** index into spillslot_unionfind unionfind structure */
42         int spillslot;
43 } spill_t;
44
45 typedef struct _affinity_edge_t {
46         double affinity;
47         int slot1, slot2;
48 } affinity_edge_t;
49
50 typedef struct _ss_env_t {
51         struct obstack obst;
52         const arch_env_t *arch_env;
53         const be_chordal_env_t *chordal_env;
54         set *spills;
55         ir_node **reloads;
56         affinity_edge_t **affinity_edges;
57         set *memperms;
58 } ss_env_t;
59
60 /** Compare 2 affinity edges (used in quicksort) */
61 static int cmp_affinity(const void *d1, const void *d2) {
62         const affinity_edge_t *e1 = d1;
63         const affinity_edge_t *e2 = d2;
64
65         return e1->affinity < e2->affinity ? -1 : 1;
66 }
67
68 static int cmp_spill(const void* d1, const void* d2, size_t size) {
69         const spill_t* s1 = d1;
70         const spill_t* s2 = d2;
71         return s1->spill != s2->spill;
72 }
73
74 static spill_t *get_spill(ss_env_t *env, ir_node *node) {
75         spill_t spill, *res;
76         int hash = HASH_PTR(node);
77
78         spill.spill = node;
79         res = set_find(env->spills, &spill, sizeof(spill), hash);
80
81         return res;
82 }
83
84 /*
85  *   ____      _ _           _     ____        _ _ _
86  *  / ___|___ | | | ___  ___| |_  / ___| _ __ (_) | |___
87  * | |   / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
88  * | |__| (_) | | |  __/ (__| |_   ___) | |_) | | | \__ \
89  *  \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
90  *                                      |_|
91  */
92
93 static ir_node *get_memory_edge(const ir_node *node) {
94         int i, arity;
95
96         arity = get_irn_arity(node);
97         for(i = arity - 1; i >= 0; --i) {
98                 ir_node *arg = get_irn_n(node, i);
99                 if(get_irn_mode(arg) == mode_M)
100                         return arg;
101         }
102
103         return NULL;
104 }
105
106 static spill_t *collect_spill(ss_env_t *env, ir_node *node) {
107         const arch_env_t *arch_env = env->arch_env;
108         const arch_register_class_t *cls;
109         spill_t spill, *res;
110         int hash = HASH_PTR(node);
111
112         assert(arch_irn_class_is(arch_env, node, spill));
113
114         if(be_is_Spill(node)) {
115                 cls = arch_get_irn_reg_class(arch_env, node, be_pos_Spill_val);
116         } else {
117                 // TODO add a way to detect the type of the spilled value
118                 assert(0);
119         }
120
121         spill.spill = node;
122         res = set_find(env->spills, &spill, sizeof(spill), hash);
123
124         if(res == NULL) {
125                 spill.spillslot = set_count(env->spills);
126                 spill.cls = cls;
127                 res = set_insert(env->spills, &spill, sizeof(spill), hash);
128         }
129
130         return res;
131 }
132
133 static spill_t *collect_memphi(ss_env_t *env, ir_node *node) {
134         int i, arity;
135         spill_t spill, *res;
136         int hash = HASH_PTR(node);
137
138         assert(is_Phi(node));
139
140         spill.spill = node;
141         res = set_find(env->spills, &spill, sizeof(spill), hash);
142         if(res != NULL) {
143                 return res;
144         }
145
146         spill.spillslot = set_count(env->spills);
147         spill.cls = NULL;
148         res = set_insert(env->spills, &spill, sizeof(spill), hash);
149
150         // is 1 of the arguments a spill?
151         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
152                 affinity_edge_t *affinty_edge;
153                 ir_node* arg = get_irn_n(node, i);
154                 spill_t* arg_spill;
155
156                 if(be_is_Spill(arg)) {
157                         arg_spill = collect_spill(env, arg);
158                 } else {
159                         // if it wasn't a spill then it must be a Mem-Phi
160                         assert(is_Phi(arg));
161                         arg_spill = collect_memphi(env, arg);
162                 }
163
164                 if(i == 0) {
165                         res->cls = arg_spill->cls;
166                 } else {
167                         assert(res->cls == arg_spill->cls);
168                 }
169
170                 // add an affinity edge
171                 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
172                 affinty_edge->affinity = get_block_execfreq(env->chordal_env->exec_freq, get_nodes_block(arg));
173                 affinty_edge->slot1 = res->spillslot;
174                 affinty_edge->slot2 = arg_spill->spillslot;
175                 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
176         }
177
178         return res;
179 }
180
181 /**
182  * This walker function searches for reloads and collects all the spills
183  * and memphis attached to them.
184  */
185 static void collect_spills_walker(ir_node *node, void *data) {
186         ss_env_t *env = data;
187         const arch_env_t *arch_env = env->arch_env;
188
189         // @@@ ia32 classify returns classification of the irn the proj is attached
190         // too, why oh why?...
191         if(is_Proj(node))
192                 return;
193
194         if(arch_irn_class_is(arch_env, node, reload)) {
195                 ir_node *spillnode = get_memory_edge(node);
196                 spill_t *spill;
197
198                 assert(spillnode != NULL);
199
200                 if(is_Phi(spillnode)) {
201                         spill = collect_memphi(env, spillnode);
202                 } else {
203                         spill = collect_spill(env, spillnode);
204                 }
205
206                 assert(!be_is_Reload(node) || spill->cls == arch_get_irn_reg_class(arch_env, node, -1));
207                 ARR_APP1(ir_node*, env->reloads, node);
208         }
209 }
210
211 /*
212  *   ____            _                      ____  _       _
213  *  / ___|___   __ _| | ___  ___  ___ ___  / ___|| | ___ | |_ ___
214  * | |   / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
215  * | |__| (_) | (_| | |  __/\__ \ (_|  __/  ___) | | (_) | |_\__ \
216  *  \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
217  */
218
219 static int merge_interferences(ss_env_t *env, bitset_t** interferences, int* spillslot_unionfind, int s1, int s2)
220 {
221         int res;
222         int i;
223         int spillcount;
224
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
228         if(res != 0) {
229                 int t = s1;
230                 s1 = s2;
231                 s2 = t;
232         }
233
234         bitset_or(interferences[s1], interferences[s2]);
235
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);
242         }
243
244         return res;
245 }
246
247 #if 0
248
249 static void dump_interference_graph(ss_env_t *env, bitset_t **interferences, const char* suffix) {
250         char name[256];
251         int i;
252         int spillcount;
253         spill_t *spill;
254         FILE *f;
255         static int cnt = 0;
256
257         snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
258
259         f = fopen(name, "w");
260         assert(f != NULL);
261
262         fprintf(f, "graph: {\n");
263
264         spillcount = set_count(env->spills);
265         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
266                 int slotid = spill->spillslot;
267                 fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
268         }
269
270         for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
271                 affinity_edge_t *edge = env->affinity_edges[i];
272                 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
273         }
274
275         for(i = 0; i < spillcount; ++i) {
276                 int i2;
277                 for(i2 = 0; i2 < spillcount; ++i2) {
278                         if(bitset_is_set(interferences[i], i2)) {
279                                 fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
280                         }
281                 }
282         }
283
284         fprintf(f, "}\n");
285         fclose(f);
286 }
287
288 static void show_stats(ss_env_t *env) {
289         int spillcount;
290         int slotcount;
291         int *slotused;
292         spill_t *spill;
293
294         spillcount = set_count(env->spills);
295         fprintf(stderr, "%s: Collected %d spills\n", get_irg_dump_name(env->chordal_env->birg->irg), spillcount);
296
297         slotused = alloca(spillcount * sizeof(slotused[0]));
298         memset(slotused, 0, spillcount * sizeof(slotused[0]));
299
300         slotcount = 0;
301         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
302                 int slot = spill->spillslot;
303                 if(slotused[slot] == 0) {
304                         slotused[slot] = 1;
305                         slotcount++;
306                 }
307         }
308
309         fprintf(stderr, "%s: Coalesced to %d spillslots\n", get_irg_dump_name(env->chordal_env->birg->irg), slotcount);
310 }
311
312 #endif
313
314 static void assign_spillslots(ss_env_t *env);
315
316 /**
317  * A greedy coalescing algorithm for spillslots:
318  *  1. Sort the list of affinity edges
319  *  2. Try to merge slots with affinity edges (most expensive slots first)
320  *  3. Try to merge everything else that is possible
321  */
322 static void do_greedy_coalescing(ss_env_t *env)
323 {
324         int spillcount;
325         spill_t **spilllist;
326         spill_t *spill;
327         int i, i2;
328         int affinity_edge_count;
329         bitset_t **interferences;
330         int* spillslot_unionfind;
331
332         spillcount = set_count(env->spills);
333         if(spillcount == 0)
334                 return;
335
336         DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
337
338         interferences = alloca(spillcount * sizeof(interferences[0]));
339         spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
340         spilllist = alloca(spillcount * sizeof(spilllist[0]));
341
342         uf_init(spillslot_unionfind, 0, spillcount);
343
344         DEBUG_ONLY(
345                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
346         );
347
348         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
349                 assert(spill->spillslot < spillcount);
350                 spilllist[spill->spillslot] = spill;
351         }
352
353         for(i = 0; i < spillcount; ++i) {
354                 interferences[i] = bitset_alloca(spillcount);
355         }
356
357         // construct interferences
358         for(i = 0; i < spillcount; ++i) {
359                 for(i2 = i+1; i2 < spillcount; ++i2) {
360                         if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
361                                 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
362                                 bitset_set(interferences[i], i2);
363                                 bitset_set(interferences[i2], i);
364                         }
365                 }
366         }
367
368         // sort affinity edges
369         affinity_edge_count = ARR_LEN(env->affinity_edges);
370         qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
371
372         //dump_interference_graph(env, interferences, "before");
373
374         // try to merge affine nodes
375         for(i = 0; i < affinity_edge_count; ++i) {
376                 const affinity_edge_t *edge = env->affinity_edges[i];
377                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
378                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
379
380                 /* test if values interfere */
381                 if(bitset_is_set(interferences[s1], s2)) {
382                         assert(bitset_is_set(interferences[s2], s1));
383                         continue;
384                 }
385
386                 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
387
388                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
389         }
390
391         // try to merge as much remaining spillslots as possible
392         for(i = 0; i < spillcount; ++i) {
393                 int s1 = uf_find(spillslot_unionfind, i);
394                 if(s1 != i)
395                         continue;
396
397                 for(i2 = i+1; i2 < spillcount; ++i2) {
398                         int s2 = uf_find(spillslot_unionfind, i2);
399                         if(s2 != i2)
400                                 continue;
401
402                         /* test if values interfere
403                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
404                          * when node merging occurs
405                          */
406                         if(bitset_is_set(interferences[s1], s2)) {
407                                 assert(bitset_is_set(interferences[s2], s1));
408                                 continue;
409                         }
410
411                         DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
412
413                         if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
414                                 // we can break the loop here, because s2 is the new supernode now
415                                 // and we'll test s2 again later anyway
416                                 break;
417                         }
418                 }
419         }
420
421         // assign spillslots to spills
422         for(i = 0; i < spillcount; ++i) {
423                 spill_t *spill = spilllist[i];
424
425                 spill->spillslot = uf_find(spillslot_unionfind, i);
426         }
427
428         //dump_interference_graph(env, interferences, "after");
429 }
430
431 #if 0
432 static void do_java_coalescing(ss_env_t *env)
433 {
434         int spillcount;
435         spill_t **spilllist;
436         spill_t *spill;
437         int i, i2;
438         be_java_coal_t *coal;
439
440         spillcount = set_count(env->spills);
441         if(spillcount == 0)
442                 return;
443
444         spilllist = alloca(spillcount * sizeof(spilllist[0]));
445
446         DEBUG_ONLY(
447                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
448         );
449
450         coal = be_java_coal_init("spillslot coalescing", spillcount, spillcount, 1);
451
452         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
453                 assert(spill->spillslot < spillcount);
454                 DEBUG_ONLY(assert(spilllist[spill->spillslot] == NULL));
455                 spilllist[spill->spillslot] = spill;
456
457                 be_java_coal_set_color(coal, spill->spillslot, spill->spillslot);
458         }
459
460         // construct interferences
461         for(i = 0; i < spillcount; ++i) {
462                 for(i2 = i+1; i2 < spillcount; ++i2) {
463                         if(values_interfere(env->chordal_env->lv, spilllist[i]->spill, spilllist[i2]->spill)) {
464                                 be_java_coal_add_int_edge(coal, i, i2);
465                         }
466                 }
467         }
468
469         for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
470                 const affinity_edge_t *edge = env->affinity_edges[i];
471                 int n = edge->slot1;
472                 int m = edge->slot2;
473                 int costs = (int) (edge->affinity * 10000);
474                 be_java_coal_add_aff_edge(coal, n, m, costs);
475         }
476
477         be_java_coal_coalesce(coal);
478
479         // construct spillslots
480         for(i = 0; i < spillcount; ++i) {
481                 spill_t *spill = spilllist[i];
482                 spill->spillslot = be_java_coal_get_color(coal, i);
483         }
484         be_java_coal_destroy(coal);
485 }
486 #endif
487
488 /*
489  *     _            _               _____       _   _ _   _
490  *    / \   ___ ___(_) __ _ _ __   | ____|_ __ | |_(_) |_(_) ___  ___
491  *   / _ \ / __/ __| |/ _` | '_ \  |  _| | '_ \| __| | __| |/ _ \/ __|
492  *  / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| |  __/\__ \
493  * /_/   \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
494  *                    |___/
495  */
496
497 typedef struct _spill_slot_t {
498         int size;
499         int align;
500         entity   *entity;
501 } spill_slot_t;
502
503 typedef struct _memperm_entry_t {
504         ir_node* node;
505         int pos;
506         entity *in;
507         entity *out;
508         struct _memperm_entry_t *next;
509 } memperm_entry_t;
510
511 typedef struct _memperm_t {
512         ir_node *block;
513         int entrycount;
514         memperm_entry_t *entries;
515 } memperm_t;
516
517 static int cmp_memperm(const void* d1, const void* d2, size_t size) {
518         const memperm_t* e1 = d1;
519         const memperm_t* e2 = d2;
520         return e1->block != e2->block;
521 }
522
523 static memperm_t *get_memperm(ss_env_t *env, ir_node *block) {
524         memperm_t entry, *res;
525         int hash;
526
527         entry.block = block;
528         hash = HASH_PTR(block);
529
530         res = set_find(env->memperms, &entry, sizeof(entry), hash);
531
532         if(res == NULL) {
533                 entry.entrycount = 0;
534                 entry.entries = NULL;
535                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
536         }
537
538         return res;
539 }
540
541 static entity* create_stack_entity(ss_env_t *env, spill_slot_t *slot) {
542         ir_type* frame = get_irg_frame_type(env->chordal_env->irg);
543         entity* res = frame_alloc_area(frame, slot->size, slot->align, 0);
544
545         // adjust size of the entity type...
546         ir_type *enttype = get_entity_type(res);
547         set_type_size_bytes(enttype, slot->size);
548
549         slot->entity = res;
550
551         return res;
552 }
553
554 static int get_spillslotsize_for_spill(ss_env_t *env, spill_t *spill) {
555         const ir_mode *mode = arch_register_class_mode(spill->cls);
556
557         return get_mode_size_bytes(mode);
558 }
559
560 static int get_spillslotalign_for_spill(ss_env_t *env, spill_t *spill) {
561         const arch_isa_t *isa = env->chordal_env->birg->main_env->arch_env->isa;
562
563         return arch_isa_get_reg_class_alignment(isa, spill->cls);
564 }
565
566 /**
567  * Enlarges a spillslot (if necessary) so that it can carry a value of size
568  * @p othersize and alignment @p otheralign.
569  */
570 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize) {
571         if(othersize > slot->size) {
572                 slot->size = othersize;
573         }
574         if(otheralign > slot->align) {
575                 if(otheralign % slot->align != 0)
576                         slot->align *= otheralign;
577                 else
578                         slot->align = otheralign;
579         } else if(slot->align % otheralign != 0) {
580                 slot->align *= otheralign;
581         }
582 }
583
584 /**
585  * Create stack entities for the spillslots and assign them to the spill and
586  * reload nodes.
587  */
588 static void assign_spillslots(ss_env_t *env) {
589         const arch_env_t *arch_env = env->arch_env;
590         int i;
591         int spillcount;
592         spill_t *spill;
593         spill_slot_t* spillslots;
594
595         spillcount = set_count(env->spills);
596         spillslots = alloca(spillcount * sizeof(spillslots[0]));
597
598         memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
599
600         // construct spillslots
601         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
602                 int slotid = spill->spillslot;
603                 spill_slot_t *slot = & (spillslots[slotid]);
604                 int align = get_spillslotalign_for_spill(env, spill);
605                 int size = get_spillslotsize_for_spill(env, spill);
606
607                 if(slot->align == 0 && slot->size == 0) {
608                         slot->align = align;
609                         slot->size = size;
610                 } else {
611                         enlarge_spillslot(slot, align, size);
612                 }
613         }
614
615         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
616                 spill_slot_t *slot;
617                 ir_node *node = spill->spill;
618                 int slotid = spill->spillslot;
619
620                 slot = &spillslots[slotid];
621                 if(slot->entity == NULL) {
622                         create_stack_entity(env, slot);
623                 }
624
625                 if(is_Phi(node)) {
626                         int i, arity;
627                         ir_node *block = get_nodes_block(node);
628
629                         // should be a PhiM
630                         assert(is_Phi(node));
631
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);
635                                 spill_t *argspill;
636                                 int argslotid;
637
638                                 argspill = get_spill(env, arg);
639                                 assert(argspill != NULL);
640
641                                 argslotid = argspill->spillslot;
642                                 if(slotid != argslotid) {
643                                         memperm_t *memperm;
644                                         memperm_entry_t *entry;
645                                         spill_slot_t *argslot = &spillslots[argslotid];
646                                         if(argslot->entity == NULL) {
647                                                 create_stack_entity(env, argslot);
648                                         }
649
650                                         memperm = get_memperm(env, predblock);
651
652                                         entry = obstack_alloc(&env->obst, sizeof(entry[0]));
653                                         entry->node = node;
654                                         entry->pos = i;
655                                         entry->in = argslot->entity;
656                                         entry->out = slot->entity;
657                                         entry->next = memperm->entries;
658                                         memperm->entrycount++;
659                                         memperm->entries = entry;
660                                 }
661                         }
662                 } else {
663                         assert(arch_irn_class_is(arch_env, node, spill));
664                         arch_set_frame_entity(arch_env, node, slot->entity);
665                 }
666         }
667
668         for(i = 0; i < ARR_LEN(env->reloads); ++i) {
669                 ir_node* reload = env->reloads[i];
670                 ir_node* spillnode = get_memory_edge(reload);
671                 spill_t *spill = get_spill(env, spillnode);
672                 const spill_slot_t *slot = & spillslots[spill->spillslot];
673
674                 assert(slot->entity != NULL);
675
676                 arch_set_frame_entity(arch_env, reload, slot->entity);
677         }
678 }
679
680 /**
681  * Returns the last node in a block which is no control flow changing node
682  */
683 static ir_node *get_end_of_block_insertion_point(ir_node* block)
684 {
685         ir_node* ins = sched_last(block);
686         while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
687                 ins = sched_prev(ins);
688                 assert(ins != NULL);
689         }
690
691         if(is_cfop(ins)) {
692                 while(1) {
693                         ir_node *prev = sched_prev(ins);
694                         if(!is_cfop(prev))
695                                 break;
696                         ins = prev;
697                 }
698         }
699
700         return ins;
701 }
702
703 static void create_memperms(ss_env_t *env) {
704         memperm_t *memperm;
705
706         for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
707                 int i;
708                 memperm_entry_t *entry;
709                 ir_node *blockend;
710                 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
711                 ir_node* mempermnode;
712
713                 assert(memperm->entrycount > 0);
714
715                 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
716                         ir_node* arg = get_irn_n(entry->node, entry->pos);
717                         nodes[i] = arg;
718                 }
719
720                 mempermnode = be_new_MemPerm(env->chordal_env->birg->main_env->arch_env, env->chordal_env->irg, memperm->block,
721                         memperm->entrycount, nodes);
722
723                 // insert node into schedule
724                 blockend = get_end_of_block_insertion_point(memperm->block);
725                 sched_add_before(blockend, mempermnode);
726
727                 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
728                         ir_node *proj;
729                         ir_node* arg = get_irn_n(entry->node, entry->pos);
730
731                         be_set_MemPerm_in_entity(mempermnode, i, entry->in);
732                         be_set_MemPerm_out_entity(mempermnode, i, entry->out);
733                         set_irg_current_block(env->chordal_env->irg, memperm->block);
734                         proj = new_Proj(mempermnode, get_irn_mode(arg), i);
735                         sched_add_before(blockend, proj);
736
737                         set_irn_n(entry->node, entry->pos, proj);
738                 }
739         }
740 }
741
742 void be_coalesce_spillslots(const be_chordal_env_t *chordal_env) {
743         ss_env_t env;
744
745         obstack_init(&env.obst);
746         env.arch_env = chordal_env->birg->main_env->arch_env;
747         env.chordal_env = chordal_env;
748         env.spills = new_set(cmp_spill, 10);
749         env.reloads = NEW_ARR_F(ir_node*, 0);
750         env.affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
751         env.memperms = new_set(cmp_memperm, 10);
752         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
753         //firm_dbg_set_mask(dbg, DBG_COALESCING);
754
755         /* Get initial spill slots */
756         irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, &env);
757
758         do_greedy_coalescing(&env);
759
760         assign_spillslots(&env);
761
762         create_memperms(&env);
763
764         //show_stats(&env);
765
766         del_set(env.memperms);
767         DEL_ARR_F(env.reloads);
768         DEL_ARR_F(env.affinity_edges);
769         del_set(env.spills);
770         obstack_free(&env.obst, NULL);
771 }