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