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