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