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