fehler109
[libfirm] / ir / be / bespillslots.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Spillslot coalescer.
23  * @author      Matthias Braun
24  * @date        26.07.2006
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32
33 #include "set.h"
34 #include "array.h"
35 #include "irgwalk.h"
36 #include "ircons.h"
37 #include "irprintf.h"
38 #include "execfreq.h"
39 #include "unionfind.h"
40 #include "irdump_t.h"
41
42 #include "benode_t.h"
43 #include "besched.h"
44 #include "bespillslots.h"
45 #include "bechordal_t.h"
46 #include "bejavacoal.h"
47 #include "bestatevent.h"
48 #include "bespilloptions.h"
49 #include "bemodule.h"
50 #include "beintlive_t.h"
51 #include "beirg_t.h"
52 #include "bearch_t.h"
53
54 #define DBG_COALESCING          1
55 #define DBG_INTERFERENCES       2
56
57 DEBUG_ONLY(
58 static firm_dbg_module_t *dbg = NULL;
59 )
60
61 typedef struct _spill_t {
62         ir_node *spill;
63         /** mode of the spilled value */
64         const ir_mode *mode;
65         /** alignment for the spilled value */
66         int alignment;
67         /** index into spillslot_unionfind unionfind structure */
68         int spillslot;
69 } spill_t;
70
71 typedef struct _affinity_edge_t {
72         double affinity;
73         int slot1, slot2;
74 } affinity_edge_t;
75
76 struct _be_fec_env_t {
77         struct obstack obst;
78         const arch_env_t *arch_env;
79         be_irg_t *birg;
80         set *spills;
81         ir_node **reloads;
82         affinity_edge_t **affinity_edges;
83         set *memperms;
84 };
85
86 /** Compare 2 affinity edges (used in quicksort) */
87 static int cmp_affinity(const void *d1, const void *d2)
88 {
89         const affinity_edge_t * const *e1 = d1;
90         const affinity_edge_t * const *e2 = d2;
91
92         // sort in descending order
93         return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
94 }
95
96 static int cmp_spill(const void* d1, const void* d2, size_t size)
97 {
98         const spill_t* s1 = d1;
99         const spill_t* s2 = d2;
100         (void) size;
101
102         return s1->spill != s2->spill;
103 }
104
105 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
106 {
107         spill_t spill, *res;
108         int hash = hash_irn(node);
109
110         spill.spill = node;
111         res = set_find(env->spills, &spill, sizeof(spill), hash);
112
113         return res;
114 }
115
116 /*
117  *   ____      _ _           _     ____        _ _ _
118  *  / ___|___ | | | ___  ___| |_  / ___| _ __ (_) | |___
119  * | |   / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
120  * | |__| (_) | | |  __/ (__| |_   ___) | |_) | | | \__ \
121  *  \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
122  *                                      |_|
123  */
124
125 static INLINE ir_node *get_memory_edge(const ir_node *node)
126 {
127         int i, arity;
128
129         arity = get_irn_arity(node);
130         for(i = arity - 1; i >= 0; --i) {
131                 ir_node *arg = get_irn_n(node, i);
132                 if(get_irn_mode(arg) == mode_M)
133                         return arg;
134         }
135
136         return NULL;
137 }
138
139 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
140                                       const ir_mode *mode, int align)
141 {
142         spill_t spill, *res;
143         int hash = hash_irn(node);
144
145         /* insert into set of spills if not already there */
146         spill.spill = node;
147         res         = set_find(env->spills, &spill, sizeof(spill), hash);
148
149         if(res == NULL) {
150                 spill.spillslot = set_count(env->spills);
151                 spill.mode      = mode;
152                 spill.alignment = align;
153                 res             = set_insert(env->spills, &spill, sizeof(spill), hash);
154         } else {
155                 assert(res->mode == mode);
156                 assert(res->alignment == align);
157         }
158
159         return res;
160 }
161
162 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
163                                const ir_mode *mode, int align)
164 {
165         int i, arity;
166         spill_t spill, *res;
167         int hash = hash_irn(node);
168         const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
169
170         assert(is_Phi(node));
171
172         spill.spill = node;
173         res = set_find(env->spills, &spill, sizeof(spill), hash);
174         if(res != NULL) {
175                 assert(res->mode == mode);
176                 assert(res->alignment == align);
177                 return res;
178         }
179
180         spill.spillslot = set_count(env->spills);
181         spill.mode      = mode;
182         spill.alignment = align;
183         res             = set_insert(env->spills, &spill, sizeof(spill), hash);
184
185         // collect attached spills and mem-phis
186         arity = get_irn_arity(node);
187         for(i = 0; i < arity; ++i) {
188                 affinity_edge_t *affinty_edge;
189                 ir_node *arg = get_irn_n(node, i);
190                 spill_t *arg_spill;
191
192                 if(is_Phi(arg)) {
193                         arg_spill = collect_memphi(env, arg, mode, align);
194                 } else {
195                         arg_spill = collect_spill(env, arg, mode, align);
196                 }
197
198                 // add an affinity edge
199                 affinty_edge           = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
200                 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
201                 affinty_edge->slot1    = res->spillslot;
202                 affinty_edge->slot2    = arg_spill->spillslot;
203                 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
204         }
205
206         return res;
207 }
208
209 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
210                                 const ir_mode *mode, int align)
211 {
212         ir_node *spillnode = get_memory_edge(node);
213         spill_t *spill;
214
215         assert(spillnode != NULL);
216
217         if (is_Phi(spillnode)) {
218                 spill = collect_memphi(env, spillnode, mode, align);
219         } else {
220                 spill = collect_spill(env, spillnode, mode, align);
221         }
222
223         ARR_APP1(ir_node *, env->reloads, node);
224 }
225
226 /*
227  *   ____            _                      ____  _       _
228  *  / ___|___   __ _| | ___  ___  ___ ___  / ___|| | ___ | |_ ___
229  * | |   / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
230  * | |__| (_) | (_| | |  __/\__ \ (_|  __/  ___) | | (_) | |_\__ \
231  *  \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
232  */
233
234 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
235                                int* spillslot_unionfind, int s1, int s2)
236 {
237         int res;
238         int i;
239         int spillcount;
240
241         // merge spillslots and interferences
242         res = uf_union(spillslot_unionfind, s1, s2);
243         // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
244         if(res != 0) {
245                 int t = s1;
246                 s1 = s2;
247                 s2 = t;
248         }
249
250         bitset_or(interferences[s1], interferences[s2]);
251
252         // update other interferences
253         spillcount = set_count(env->spills);
254         for(i = 0; i < spillcount; ++i) {
255                 bitset_t *intfs = interferences[i];
256                 if(bitset_is_set(intfs, s2))
257                         bitset_set(intfs, s1);
258         }
259
260         return res;
261 }
262
263 static int my_values_interfere2(be_irg_t *birg, const ir_node *a,
264                                 const ir_node *b)
265 {
266         be_lv_t *lv = be_get_birg_liveness(birg);
267
268     int a2b = _value_dominates(a, b);
269     int b2a = _value_dominates(b, a);
270
271     /* If there is no dominance relation, they do not interfere. */
272     if((a2b | b2a) > 0) {
273         const ir_edge_t *edge;
274         ir_node *bb;
275
276         /*
277          * Adjust a and b so, that a dominates b if
278          * a dominates b or vice versa.
279          */
280         if(b2a) {
281             const ir_node *t = a;
282             a = b;
283             b = t;
284         }
285
286         bb = get_nodes_block(b);
287
288         /*
289          * If a is live end in b's block it is
290          * live at b's definition (a dominates b)
291          */
292         if(be_is_live_end(lv, bb, a))
293             return 1;
294
295         /*
296          * Look at all usages of a.
297          * If there's one usage of a in the block of b, then
298          * we check, if this use is dominated by b, if that's true
299          * a and b interfere. Note that b must strictly dominate the user,
300          * since if b is the last user of in the block, b and a do not
301          * interfere.
302          * Uses of a not in b's block can be disobeyed, because the
303          * check for a being live at the end of b's block is already
304          * performed.
305          */
306         foreach_out_edge(a, edge) {
307             const ir_node *user = get_edge_src_irn(edge);
308                         if(is_Sync(user)) {
309                                 const ir_edge_t *edge2;
310                                 foreach_out_edge(user, edge2) {
311                                         const ir_node *user2 = get_edge_src_irn(edge2);
312                                         assert(!is_Sync(user2));
313                                         if(get_nodes_block(user2) == bb && !is_Phi(user2) &&
314                                            _value_strictly_dominates(b, user2))
315                                                 return 1;
316                                 }
317                         } else {
318                                 if(get_nodes_block(user) == bb && !is_Phi(user) &&
319                                                 _value_strictly_dominates(b, user))
320                 return 1;
321                         }
322         }
323     }
324
325         return 0;
326 }
327
328 /**
329  * same as values_interfere but with special handling for Syncs
330  */
331 static int my_values_interfere(be_irg_t *birg, ir_node *a, ir_node *b)
332 {
333         if(is_Sync(a)) {
334                 int i, arity = get_irn_arity(a);
335                 for(i = 0; i < arity; ++i) {
336                         ir_node *in = get_irn_n(a, i);
337                         if(my_values_interfere(birg, in, b))
338                                 return 1;
339                 }
340                 return 0;
341         } else if(is_Sync(b)) {
342                 int i, arity = get_irn_arity(b);
343                 for(i = 0; i < arity; ++i) {
344                         ir_node *in = get_irn_n(b, i);
345                         /* a is not a sync, so no need for my_values_interfere */
346                         if(my_values_interfere2(birg, a, in))
347                                 return 1;
348                 }
349                 return 0;
350         }
351
352         return my_values_interfere2(birg, a, b);
353 }
354
355 /**
356  * A greedy coalescing algorithm for spillslots:
357  *  1. Sort the list of affinity edges
358  *  2. Try to merge slots with affinity edges (most expensive slots first)
359  *  3. Try to merge everything else that is possible
360  */
361 static void do_greedy_coalescing(be_fec_env_t *env)
362 {
363         int spillcount;
364         spill_t **spilllist;
365         spill_t *spill;
366         int i, i2;
367         int affinity_edge_count;
368         bitset_t **interferences;
369         int* spillslot_unionfind;
370
371         spillcount = set_count(env->spills);
372         if(spillcount == 0)
373                 return;
374
375         DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
376
377         interferences = alloca(spillcount * sizeof(interferences[0]));
378         spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
379         spilllist = alloca(spillcount * sizeof(spilllist[0]));
380
381         uf_init(spillslot_unionfind, 0, spillcount);
382
383         DEBUG_ONLY(
384                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
385         );
386
387         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
388                 assert(spill->spillslot < spillcount);
389                 spilllist[spill->spillslot] = spill;
390         }
391
392         for(i = 0; i < spillcount; ++i) {
393                 interferences[i] = bitset_alloca(spillcount);
394         }
395
396         /* construct interferences */
397         for (i = 0; i < spillcount; ++i) {
398                 ir_node *spill1 = spilllist[i]->spill;
399
400                 if (is_NoMem(spill1))
401                         continue;
402
403                 for(i2 = i+1; i2 < spillcount; ++i2) {
404                         ir_node *spill2 = spilllist[i2]->spill;
405
406                         if (is_NoMem(spill2))
407                                 continue;
408
409                         if (my_values_interfere(env->birg, spill1, spill2)) {
410                                 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
411                                 bitset_set(interferences[i], i2);
412                                 bitset_set(interferences[i2], i);
413                         }
414                 }
415         }
416
417         /* sort affinity edges */
418         affinity_edge_count = ARR_LEN(env->affinity_edges);
419         qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
420
421         //dump_interference_graph(env, interferences, "before");
422
423         /* try to merge affine nodes */
424         for(i = 0; i < affinity_edge_count; ++i) {
425                 const affinity_edge_t *edge = env->affinity_edges[i];
426                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
427                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
428
429                 /* test if values interfere */
430                 if (bitset_is_set(interferences[s1], s2)) {
431                         assert(bitset_is_set(interferences[s2], s1));
432                         continue;
433                 }
434
435                 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
436
437                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
438         }
439
440         // try to merge as much remaining spillslots as possible
441         for(i = 0; i < spillcount; ++i) {
442                 int s1 = uf_find(spillslot_unionfind, i);
443                 if(s1 != i)
444                         continue;
445
446                 for(i2 = i+1; i2 < spillcount; ++i2) {
447                         int s2 = uf_find(spillslot_unionfind, i2);
448                         if(s2 != i2)
449                                 continue;
450
451                         /* test if values interfere
452                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
453                          * when node merging occurs
454                          */
455                         if(bitset_is_set(interferences[s1], s2)) {
456                                 assert(bitset_is_set(interferences[s2], s1));
457                                 continue;
458                         }
459
460                         DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
461
462                         if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
463                                 // we can break the loop here, because s2 is the new supernode now
464                                 // and we'll test s2 again later anyway
465                                 break;
466                         }
467                 }
468         }
469
470         // assign spillslots to spills
471         for(i = 0; i < spillcount; ++i) {
472                 spill_t *spill = spilllist[i];
473
474                 spill->spillslot = uf_find(spillslot_unionfind, i);
475         }
476
477         //dump_interference_graph(env, interferences, "after");
478 }
479
480 /*
481  *     _            _               _____       _   _ _   _
482  *    / \   ___ ___(_) __ _ _ __   | ____|_ __ | |_(_) |_(_) ___  ___
483  *   / _ \ / __/ __| |/ _` | '_ \  |  _| | '_ \| __| | __| |/ _ \/ __|
484  *  / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| |  __/\__ \
485  * /_/   \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
486  *                    |___/
487  */
488
489 typedef struct _spill_slot_t {
490         int size;
491         int align;
492         ir_entity *entity;
493 } spill_slot_t;
494
495 typedef struct _memperm_entry_t {
496         ir_node* node;
497         int pos;
498         ir_entity *in;
499         ir_entity *out;
500         struct _memperm_entry_t *next;
501 } memperm_entry_t;
502
503 typedef struct _memperm_t {
504         ir_node *block;
505         int entrycount;
506         memperm_entry_t *entries;
507 } memperm_t;
508
509 static int cmp_memperm(const void* d1, const void* d2, size_t size)
510 {
511         const memperm_t* e1 = d1;
512         const memperm_t* e2 = d2;
513         (void) size;
514
515         return e1->block != e2->block;
516 }
517
518 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
519 {
520         memperm_t entry, *res;
521         int hash;
522
523         entry.block = block;
524         hash        = hash_irn(block);
525
526         res = set_find(env->memperms, &entry, sizeof(entry), hash);
527
528         if(res == NULL) {
529                 entry.entrycount = 0;
530                 entry.entries = NULL;
531                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
532         }
533
534         return res;
535 }
536
537 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
538 {
539         ir_graph *irg = be_get_birg_irg(env->birg);
540         ir_type *frame = get_irg_frame_type(irg);
541         ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
542
543         /* adjust size of the entity type... */
544         ir_type *enttype = get_entity_type(res);
545         set_type_size_bytes(enttype, slot->size);
546
547         slot->entity = res;
548
549         return res;
550 }
551
552 /**
553  * Enlarges a spillslot (if necessary) so that it can carry a value of size
554  * @p othersize and alignment @p otheralign.
555  */
556 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
557 {
558         if(othersize > slot->size) {
559                 slot->size = othersize;
560         }
561         if(otheralign > slot->align) {
562                 if(otheralign % slot->align != 0)
563                         slot->align *= otheralign;
564                 else
565                         slot->align = otheralign;
566         } else if(slot->align % otheralign != 0) {
567                 slot->align *= otheralign;
568         }
569 }
570
571
572 static void assign_spill_entity(const arch_env_t *arch_env, ir_node *node, ir_entity *entity)
573 {
574         if(is_NoMem(node))
575                 return;
576         if(is_Sync(node)) {
577                 int i, arity;
578
579                 arity = get_irn_arity(node);
580                 for(i = 0; i < arity; ++i) {
581                         ir_node *in = get_irn_n(node, i);
582                         assert(!is_Phi(in));
583
584                         assign_spill_entity(arch_env, in, entity);
585                 }
586                 return;
587         }
588
589         assert(arch_get_frame_entity(arch_env, node) == NULL);
590         arch_set_frame_entity(arch_env, node, entity);
591 }
592
593 /**
594  * Create stack entities for the spillslots and assign them to the spill and
595  * reload nodes.
596  */
597 static void assign_spillslots(be_fec_env_t *env)
598 {
599         const arch_env_t *arch_env = env->arch_env;
600         int i;
601         int spillcount;
602         spill_t *spill;
603         spill_slot_t* spillslots;
604
605         spillcount = set_count(env->spills);
606         spillslots = alloca(spillcount * sizeof(spillslots[0]));
607
608         memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
609
610         // construct spillslots
611         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
612                 int slotid = spill->spillslot;
613                 const ir_mode *mode = spill->mode;
614                 spill_slot_t *slot = & (spillslots[slotid]);
615                 int size = get_mode_size_bytes(mode);
616                 int align = spill->alignment;
617
618                 if(slot->align == 0 && slot->size == 0) {
619                         slot->align = align;
620                         slot->size = size;
621                 } else {
622                         enlarge_spillslot(slot, align, size);
623                 }
624         }
625
626         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
627                 spill_slot_t *slot;
628                 ir_node *node = spill->spill;
629                 int slotid = spill->spillslot;
630
631                 slot = &spillslots[slotid];
632                 if(slot->entity == NULL) {
633                         create_stack_entity(env, slot);
634                 }
635
636                 if(is_Phi(node)) {
637                         int i, arity;
638                         ir_node *block = get_nodes_block(node);
639
640                         // should be a PhiM
641                         assert(is_Phi(node));
642
643                         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
644                                 ir_node *arg = get_irn_n(node, i);
645                                 ir_node *predblock = get_Block_cfgpred_block(block, i);
646                                 spill_t *argspill;
647                                 int argslotid;
648
649                                 argspill = get_spill(env, arg);
650                                 assert(argspill != NULL);
651
652                                 argslotid = argspill->spillslot;
653                                 if(slotid != argslotid) {
654                                         memperm_t *memperm;
655                                         memperm_entry_t *entry;
656                                         spill_slot_t *argslot = &spillslots[argslotid];
657                                         if(argslot->entity == NULL) {
658                                                 create_stack_entity(env, argslot);
659                                         }
660
661                                         memperm = get_memperm(env, predblock);
662
663                                         entry = obstack_alloc(&env->obst, sizeof(entry[0]));
664                                         entry->node = node;
665                                         entry->pos = i;
666                                         entry->in = argslot->entity;
667                                         entry->out = slot->entity;
668                                         entry->next = memperm->entries;
669                                         memperm->entrycount++;
670                                         memperm->entries = entry;
671                                 }
672                         }
673                 } else {
674                         assign_spill_entity(arch_env, node, slot->entity);
675                 }
676         }
677
678         for(i = 0; i < ARR_LEN(env->reloads); ++i) {
679                 ir_node* reload = env->reloads[i];
680                 ir_node* spillnode = get_memory_edge(reload);
681                 spill_t *spill = get_spill(env, spillnode);
682                 const spill_slot_t *slot = & spillslots[spill->spillslot];
683
684                 assert(slot->entity != NULL);
685
686                 arch_set_frame_entity(arch_env, reload, slot->entity);
687         }
688 }
689
690 /**
691  * Returns the last node in a block which is no control flow changing node
692  */
693 static ir_node *get_end_of_block_insertion_point(ir_node* block)
694 {
695         ir_node* ins = sched_last(block);
696         while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
697                 ins = sched_prev(ins);
698                 assert(ins != NULL);
699         }
700
701         if(is_cfop(ins)) {
702                 while(1) {
703                         ir_node *prev = sched_prev(ins);
704                         if(!is_cfop(prev))
705                                 break;
706                         ins = prev;
707                 }
708         }
709
710         return ins;
711 }
712
713 static void create_memperms(be_fec_env_t *env)
714 {
715         const arch_env_t *arch_env = env->arch_env;
716         ir_graph *irg = be_get_birg_irg(env->birg);
717         memperm_t *memperm;
718
719         for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
720                 int i;
721                 memperm_entry_t *entry;
722                 ir_node *blockend;
723                 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
724                 ir_node* mempermnode;
725
726                 assert(memperm->entrycount > 0);
727
728                 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
729                         ir_node* arg = get_irn_n(entry->node, entry->pos);
730                         nodes[i] = arg;
731                 }
732
733                 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
734                                              memperm->entrycount, nodes);
735
736                 // insert node into schedule
737                 blockend = get_end_of_block_insertion_point(memperm->block);
738                 sched_add_before(blockend, mempermnode);
739                 stat_ev_dbl("mem_perm", memperm->entrycount);
740
741                 i = 0;
742                 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
743                         ir_node *proj;
744                         ir_node* arg = get_irn_n(entry->node, entry->pos);
745
746                         be_set_MemPerm_in_entity(mempermnode, i, entry->in);
747                         be_set_MemPerm_out_entity(mempermnode, i, entry->out);
748                         set_irg_current_block(irg, memperm->block);
749                         proj = new_Proj(mempermnode, get_irn_mode(arg), i);
750
751                         set_irn_n(entry->node, entry->pos, proj);
752                 }
753         }
754 }
755
756 static int count_spillslots(const be_fec_env_t *env)
757 {
758         const spill_t *spill;
759         int spillcount = set_count(env->spills);
760         bitset_t *counted = bitset_alloca(spillcount);
761         int slotcount;
762
763         slotcount = 0;
764         for(spill = set_first(env->spills); spill != NULL;
765             spill = set_next(env->spills)) {
766                 int spillslot = spill->spillslot;
767                 if(!bitset_is_set(counted, spillslot)) {
768                         slotcount++;
769                         bitset_set(counted, spillslot);
770                 }
771         }
772
773         return slotcount;
774 }
775
776 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
777 {
778         const arch_env_t *arch_env = birg->main_env->arch_env;
779         be_fec_env_t     *env      = xmalloc(sizeof(env[0]));
780
781         be_liveness_assure_chk(be_assure_liveness(birg));
782
783         obstack_init(&env->obst);
784         env->arch_env       = arch_env;
785         env->birg           = birg;
786         env->spills         = new_set(cmp_spill, 10);
787         env->reloads        = NEW_ARR_F(ir_node*, 0);
788         env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
789         env->memperms       = new_set(cmp_memperm, 10);
790
791         return env;
792 }
793
794 void be_free_frame_entity_coalescer(be_fec_env_t *env)
795 {
796         del_set(env->memperms);
797         DEL_ARR_F(env->reloads);
798         DEL_ARR_F(env->affinity_edges);
799         del_set(env->spills);
800         obstack_free(&env->obst, NULL);
801
802         free(env);
803 }
804
805 void be_assign_entities(be_fec_env_t *env)
806 {
807         stat_ev_dbl("spillslots", set_count(env->spills));
808
809         if(be_coalesce_spill_slots) {
810                 do_greedy_coalescing(env);
811         }
812
813         stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
814
815         assign_spillslots(env);
816
817         create_memperms(env);
818 }
819
820 /**
821  * This walker function searches for reloads and collects all the spills
822  * and memphis attached to them.
823  */
824 static void collect_spills_walker(ir_node *node, void *data)
825 {
826         be_fec_env_t *env = data;
827         const arch_env_t *arch_env = env->arch_env;
828         const ir_mode *mode;
829         const arch_register_class_t *cls;
830         int align;
831
832         /* classify returns classification of the irn the proj is attached to */
833         if (is_Proj(node))
834                 return;
835
836         if (!arch_irn_class_is(arch_env, node, reload))
837                 return;
838
839         mode  = get_irn_mode(node);
840         cls   = arch_get_irn_reg_class(arch_env, node, -1);
841         align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
842
843         be_node_needs_frame_entity(env, node, mode, align);
844 }
845
846 void be_coalesce_spillslots(be_irg_t *birg)
847 {
848         be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
849
850         /* collect reloads */
851         irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
852
853         be_assign_entities(env);
854
855         be_free_frame_entity_coalescer(env);
856 }
857
858 void be_init_spillslots(void)
859 {
860         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
861 }
862
863 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);