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