we don't need no stinking selfs
[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         } else {
142                 assert(res->mode == mode);
143                 assert(res->alignment == align);
144         }
145
146         return res;
147 }
148
149 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
150                                const ir_mode *mode, int align)
151 {
152         int i, arity;
153         spill_t spill, *res;
154         int hash = hash_irn(node);
155         const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
156
157         assert(is_Phi(node));
158
159         spill.spill = node;
160         res = set_find(env->spills, &spill, sizeof(spill), hash);
161         if(res != NULL) {
162                 assert(res->mode == mode);
163                 assert(res->alignment == align);
164                 return res;
165         }
166
167         spill.spillslot = set_count(env->spills);
168         spill.mode      = mode;
169         spill.alignment = align;
170         res             = set_insert(env->spills, &spill, sizeof(spill), hash);
171
172         /* collect attached spills and mem-phis */
173         arity = get_irn_arity(node);
174         for(i = 0; i < arity; ++i) {
175                 affinity_edge_t *affinty_edge;
176                 ir_node *arg = get_irn_n(node, i);
177                 spill_t *arg_spill;
178
179                 if(is_Phi(arg)) {
180                         arg_spill = collect_memphi(env, arg, mode, align);
181                 } else {
182                         arg_spill = collect_spill(env, arg, mode, align);
183                 }
184
185                 /* add an affinity edge */
186                 affinty_edge           = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
187                 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
188                 affinty_edge->slot1    = res->spillslot;
189                 affinty_edge->slot2    = arg_spill->spillslot;
190                 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
191         }
192
193         return res;
194 }
195
196 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
197                                 const ir_mode *mode, int align)
198 {
199         ir_node *spillnode = get_memory_edge(node);
200         spill_t *spill;
201
202         assert(spillnode != NULL);
203
204         /* walk upwards and collect all phis and spills on this way */
205         if (is_Phi(spillnode)) {
206                 spill = collect_memphi(env, spillnode, mode, align);
207         } else {
208                 spill = collect_spill(env, spillnode, mode, align);
209         }
210
211         ARR_APP1(ir_node *, env->reloads, node);
212 }
213
214
215
216 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
217                                int* spillslot_unionfind, int s1, int s2)
218 {
219         int res;
220         int i;
221         int spillcount;
222
223         /* merge spillslots and interferences */
224         res = uf_union(spillslot_unionfind, s1, s2);
225         /* we assume that we always merge s2 to s1 so swap s1, s2 if necessary */
226         if(res != 0) {
227                 int t = s1;
228                 s1 = s2;
229                 s2 = t;
230         }
231
232         bitset_or(interferences[s1], interferences[s2]);
233
234         /* update other interferences */
235         spillcount = set_count(env->spills);
236         for(i = 0; i < spillcount; ++i) {
237                 bitset_t *intfs = interferences[i];
238                 if(bitset_is_set(intfs, s2))
239                         bitset_set(intfs, s1);
240         }
241
242         return res;
243 }
244
245 static int my_values_interfere2(be_irg_t *birg, const ir_node *a,
246                                 const ir_node *b)
247 {
248         be_lv_t *lv = be_get_birg_liveness(birg);
249
250     int a2b = _value_dominates(a, b);
251     int b2a = _value_dominates(b, a);
252
253     /* If there is no dominance relation, they do not interfere. */
254     if((a2b | b2a) > 0) {
255         const ir_edge_t *edge;
256         ir_node *bb;
257
258         /*
259          * Adjust a and b so, that a dominates b if
260          * a dominates b or vice versa.
261          */
262         if(b2a) {
263             const ir_node *t = a;
264             a = b;
265             b = t;
266         }
267
268         bb = get_nodes_block(b);
269
270         /*
271          * If a is live end in b's block it is
272          * live at b's definition (a dominates b)
273          */
274         if(be_is_live_end(lv, bb, a))
275             return 1;
276
277         /*
278          * Look at all usages of a.
279          * If there's one usage of a in the block of b, then
280          * we check, if this use is dominated by b, if that's true
281          * a and b interfere. Note that b must strictly dominate the user,
282          * since if b is the last user of in the block, b and a do not
283          * interfere.
284          * Uses of a not in b's block can be disobeyed, because the
285          * check for a being live at the end of b's block is already
286          * performed.
287          */
288         foreach_out_edge(a, edge) {
289             const ir_node *user = get_edge_src_irn(edge);
290                         if(is_Sync(user)) {
291                                 const ir_edge_t *edge2;
292                                 foreach_out_edge(user, edge2) {
293                                         const ir_node *user2 = get_edge_src_irn(edge2);
294                                         assert(!is_Sync(user2));
295                                         if(get_nodes_block(user2) == bb && !is_Phi(user2) &&
296                                            _value_strictly_dominates(b, user2))
297                                                 return 1;
298                                 }
299                         } else {
300                                 if(get_nodes_block(user) == bb && !is_Phi(user) &&
301                                                 _value_strictly_dominates(b, user))
302                 return 1;
303                         }
304         }
305     }
306
307         return 0;
308 }
309
310 /**
311  * same as values_interfere but with special handling for Syncs
312  */
313 static int my_values_interfere(be_irg_t *birg, ir_node *a, ir_node *b)
314 {
315         if(is_Sync(a)) {
316                 int i, arity = get_irn_arity(a);
317                 for(i = 0; i < arity; ++i) {
318                         ir_node *in = get_irn_n(a, i);
319                         if(my_values_interfere(birg, in, b))
320                                 return 1;
321                 }
322                 return 0;
323         } else if(is_Sync(b)) {
324                 int i, arity = get_irn_arity(b);
325                 for(i = 0; i < arity; ++i) {
326                         ir_node *in = get_irn_n(b, i);
327                         /* a is not a sync, so no need for my_values_interfere */
328                         if(my_values_interfere2(birg, a, in))
329                                 return 1;
330                 }
331                 return 0;
332         }
333
334         return my_values_interfere2(birg, a, b);
335 }
336
337 /**
338  * A greedy coalescing algorithm for spillslots:
339  *  1. Sort the list of affinity edges
340  *  2. Try to merge slots with affinity edges (most expensive slots first)
341  *  3. Try to merge everything else that is possible
342  */
343 static void do_greedy_coalescing(be_fec_env_t *env)
344 {
345         int spillcount;
346         spill_t **spilllist;
347         spill_t *spill;
348         int i, i2;
349         int affinity_edge_count;
350         bitset_t **interferences;
351         int* spillslot_unionfind;
352
353         spillcount = set_count(env->spills);
354         if(spillcount == 0)
355                 return;
356
357         DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
358
359         interferences       = alloca(spillcount * sizeof(interferences[0]));
360         spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
361         spilllist           = alloca(spillcount * sizeof(spilllist[0]));
362
363         uf_init(spillslot_unionfind, 0, spillcount);
364
365         DEBUG_ONLY(
366                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
367         );
368
369         for(spill = set_first(env->spills), i = 0; spill != NULL;
370             spill = set_next(env->spills), ++i) {
371                 assert(spill->spillslot < spillcount);
372                 spilllist[spill->spillslot] = spill;
373         }
374
375         for(i = 0; i < spillcount; ++i) {
376                 interferences[i] = bitset_alloca(spillcount);
377         }
378
379         /* construct interferences */
380         for (i = 0; i < spillcount; ++i) {
381                 ir_node *spill1 = spilllist[i]->spill;
382
383                 if (is_NoMem(spill1))
384                         continue;
385
386                 for(i2 = i+1; i2 < spillcount; ++i2) {
387                         ir_node *spill2 = spilllist[i2]->spill;
388
389                         if (is_NoMem(spill2))
390                                 continue;
391
392                         if (my_values_interfere(env->birg, spill1, spill2)) {
393                                 DBG((dbg, DBG_INTERFERENCES,
394                                      "Slot %d and %d interfere\n", i, i2));
395
396                                 bitset_set(interferences[i], i2);
397                                 bitset_set(interferences[i2], i);
398                         }
399                 }
400         }
401
402         /* sort affinity edges */
403         affinity_edge_count = ARR_LEN(env->affinity_edges);
404         qsort(env->affinity_edges, affinity_edge_count,
405               sizeof(env->affinity_edges[0]), cmp_affinity);
406
407         /*dump_interference_graph(env, interferences, "before"); */
408
409         /* try to merge affine nodes */
410         for(i = 0; i < affinity_edge_count; ++i) {
411                 const affinity_edge_t *edge = env->affinity_edges[i];
412                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
413                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
414
415                 /* test if values interfere */
416                 if (bitset_is_set(interferences[s1], s2)) {
417                         assert(bitset_is_set(interferences[s2], s1));
418                         continue;
419                 }
420
421                 DBG((dbg, DBG_COALESCING,
422                      "Merging %d and %d because of affinity edge\n", s1, s2));
423
424                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
425         }
426
427         /* try to merge as much remaining spillslots as possible */
428         for(i = 0; i < spillcount; ++i) {
429                 int s1 = uf_find(spillslot_unionfind, i);
430                 if(s1 != i)
431                         continue;
432
433                 for(i2 = i+1; i2 < spillcount; ++i2) {
434                         int s2 = uf_find(spillslot_unionfind, i2);
435                         if(s2 != i2)
436                                 continue;
437
438                         /* test if values interfere
439                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
440                          * when node merging occurs
441                          */
442                         if(bitset_is_set(interferences[s1], s2)) {
443                                 assert(bitset_is_set(interferences[s2], s1));
444                                 continue;
445                         }
446
447                         DBG((dbg, DBG_COALESCING,
448                              "Merging %d and %d because it is possible\n", s1, s2));
449
450                         if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
451                                 /* we can break the loop here, because s2 is the new supernode
452                                  * now and we'll test s2 again later anyway */
453                                 break;
454                         }
455                 }
456         }
457
458         /* assign spillslots to spills */
459         for(i = 0; i < spillcount; ++i) {
460                 spill_t *spill = spilllist[i];
461
462                 spill->spillslot = uf_find(spillslot_unionfind, i);
463         }
464
465         /*dump_interference_graph(env, interferences, "after");*/
466 }
467
468
469
470 typedef struct _spill_slot_t {
471         int size;
472         int align;
473         ir_entity *entity;
474 } spill_slot_t;
475
476 typedef struct _memperm_entry_t {
477         ir_node* node;
478         int pos;
479         ir_entity *in;
480         ir_entity *out;
481         struct _memperm_entry_t *next;
482 } memperm_entry_t;
483
484 typedef struct _memperm_t {
485         ir_node *block;
486         int entrycount;
487         memperm_entry_t *entries;
488 } memperm_t;
489
490 static int cmp_memperm(const void* d1, const void* d2, size_t size)
491 {
492         const memperm_t* e1 = d1;
493         const memperm_t* e2 = d2;
494         (void) size;
495
496         return e1->block != e2->block;
497 }
498
499 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
500 {
501         memperm_t entry, *res;
502         int hash;
503
504         entry.block = block;
505         hash        = hash_irn(block);
506
507         res = set_find(env->memperms, &entry, sizeof(entry), hash);
508
509         if(res == NULL) {
510                 entry.entrycount = 0;
511                 entry.entries = NULL;
512                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
513         }
514
515         return res;
516 }
517
518 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
519 {
520         ir_graph *irg = be_get_birg_irg(env->birg);
521         ir_type *frame = get_irg_frame_type(irg);
522         ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
523
524         /* adjust size of the entity type... */
525         ir_type *enttype = get_entity_type(res);
526         set_type_size_bytes(enttype, slot->size);
527
528         slot->entity = res;
529
530         return res;
531 }
532
533 /**
534  * Enlarges a spillslot (if necessary) so that it can carry a value of size
535  * @p othersize and alignment @p otheralign.
536  */
537 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
538 {
539         if(othersize > slot->size) {
540                 slot->size = othersize;
541         }
542         if(otheralign > slot->align) {
543                 if(otheralign % slot->align != 0)
544                         slot->align *= otheralign;
545                 else
546                         slot->align = otheralign;
547         } else if(slot->align % otheralign != 0) {
548                 slot->align *= otheralign;
549         }
550 }
551
552
553 static void assign_spill_entity(const arch_env_t *arch_env, ir_node *node,
554                                 ir_entity *entity)
555 {
556         if(is_NoMem(node))
557                 return;
558         if(is_Sync(node)) {
559                 int i, arity;
560
561                 arity = get_irn_arity(node);
562                 for(i = 0; i < arity; ++i) {
563                         ir_node *in = get_irn_n(node, i);
564                         assert(!is_Phi(in));
565
566                         assign_spill_entity(arch_env, in, entity);
567                 }
568                 return;
569         }
570
571         /* beware: we might have Stores with Memory Proj's, ia32 fisttp for instance */
572         node = skip_Proj(node);
573         assert(arch_get_frame_entity(arch_env, node) == NULL);
574         arch_set_frame_entity(arch_env, node, entity);
575 }
576
577 /**
578  * Create stack entities for the spillslots and assign them to the spill and
579  * reload nodes.
580  */
581 static void assign_spillslots(be_fec_env_t *env)
582 {
583         const arch_env_t *arch_env = env->arch_env;
584         int i;
585         int spillcount;
586         spill_t *spill;
587         spill_slot_t* spillslots;
588
589         spillcount = set_count(env->spills);
590         spillslots = alloca(spillcount * sizeof(spillslots[0]));
591
592         memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
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 = obstack_alloc(&env->obst, sizeof(entry[0]));
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(arch_env, 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(arch_env, 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                 while(1) {
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                 int i;
709                 memperm_entry_t *entry;
710                 ir_node *blockend;
711                 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
712                 ir_node* mempermnode;
713
714                 assert(memperm->entrycount > 0);
715
716                 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
717                         ir_node* arg = get_irn_n(entry->node, entry->pos);
718                         nodes[i] = arg;
719                 }
720
721                 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
722                                              memperm->entrycount, nodes);
723
724                 /* insert node into schedule */
725                 blockend = get_end_of_block_insertion_point(memperm->block);
726                 sched_add_before(blockend, mempermnode);
727                 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(sizeof(env[0]));
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 arch_env_t *arch_env = env->arch_env;
816         const ir_mode *mode;
817         const arch_register_class_t *cls;
818         int align;
819
820         /* classify returns classification of the irn the proj is attached to */
821         if (is_Proj(node))
822                 return;
823
824         if (!arch_irn_class_is(arch_env, node, reload))
825                 return;
826
827         mode  = get_irn_mode(node);
828         cls   = arch_get_irn_reg_class(arch_env, node, -1);
829         align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
830
831         be_node_needs_frame_entity(env, node, mode, align);
832 }
833
834 void be_coalesce_spillslots(be_irg_t *birg)
835 {
836         be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
837
838         /* collect reloads */
839         irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
840
841         be_assign_entities(env);
842
843         be_free_frame_entity_coalescer(env);
844 }
845
846 void be_init_spillslots(void)
847 {
848         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
849 }
850
851 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);