identifiers starting with _ are reserved; remove this bad practice
[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;
66         int    slot2;
67 } affinity_edge_t;
68
69 struct be_fec_env_t {
70         struct obstack         obst;
71         ir_graph              *irg;
72         set                   *spills;
73         ir_node              **reloads;
74         affinity_edge_t      **affinity_edges;
75         set                   *memperms;
76         set_frame_entity_func  set_frame_entity;
77 };
78
79 /** Compare 2 affinity edges (used in quicksort) */
80 static int cmp_affinity(const void *d1, const void *d2)
81 {
82         const affinity_edge_t * const *e1 = d1;
83         const affinity_edge_t * const *e2 = d2;
84
85         /* sort in descending order */
86         return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
87 }
88
89 static int cmp_spill(const void* d1, const void* d2, size_t size)
90 {
91         const spill_t* s1 = d1;
92         const spill_t* s2 = d2;
93         (void) size;
94
95         return s1->spill != s2->spill;
96 }
97
98 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
99 {
100         spill_t spill, *res;
101         int hash = hash_irn(node);
102
103         spill.spill = node;
104         res = set_find(env->spills, &spill, sizeof(spill), hash);
105
106         return res;
107 }
108
109
110 static inline ir_node *get_memory_edge(const ir_node *node)
111 {
112         int i, arity;
113
114         arity = get_irn_arity(node);
115         for (i = arity - 1; i >= 0; --i) {
116                 ir_node *arg = get_irn_n(node, i);
117                 if (get_irn_mode(arg) == mode_M)
118                         return arg;
119         }
120
121         return NULL;
122 }
123
124 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
125                                       const ir_mode *mode, int align)
126 {
127         spill_t spill, *res;
128         int     hash = hash_irn(node);
129
130         /* insert into set of spills if not already there */
131         spill.spill = node;
132         res         = set_find(env->spills, &spill, sizeof(spill), hash);
133
134         if (res == NULL) {
135                 spill.spillslot = set_count(env->spills);
136                 spill.mode      = mode;
137                 spill.alignment = align;
138                 res             = set_insert(env->spills, &spill, sizeof(spill), hash);
139                 DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
140         } else {
141                 assert(res->mode == mode);
142                 assert(res->alignment == align);
143         }
144
145         return res;
146 }
147
148 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
149                                const ir_mode *mode, int align)
150 {
151         int i, arity;
152         spill_t spill, *res;
153         int hash = hash_irn(node);
154         const ir_exec_freq *exec_freq = be_get_irg_exec_freq(env->irg);
155
156         assert(is_Phi(node));
157
158         spill.spill = node;
159         res = set_find(env->spills, &spill, sizeof(spill), hash);
160         if (res != NULL) {
161                 assert(res->mode == mode);
162                 assert(res->alignment == align);
163                 return res;
164         }
165
166         spill.spillslot = set_count(env->spills);
167         spill.mode      = mode;
168         spill.alignment = align;
169         DB((dbg, DBG_COALESCING, "Slot %d: %+F\n", spill.spillslot, node));
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           = OALLOC(&env->obst, affinity_edge_t);
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 != s1) {
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(ir_graph *irg, const ir_node *a,
246                                 const ir_node *b)
247 {
248         be_lv_t *lv = be_get_irg_liveness(irg);
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(ir_graph *irg, 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(irg, 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(irg, a, in))
329                                 return 1;
330                 }
331                 return 0;
332         }
333
334         return my_values_interfere2(irg, 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         struct obstack data;
353
354         spillcount = set_count(env->spills);
355         if (spillcount == 0)
356                 return;
357
358         obstack_init(&data);
359
360         DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
361
362         interferences       = OALLOCN(&data, bitset_t*, spillcount);
363         spillslot_unionfind = OALLOCN(&data, int,       spillcount);
364         spilllist           = OALLOCN(&data, spill_t*,  spillcount);
365
366         uf_init(spillslot_unionfind, spillcount);
367
368         DEBUG_ONLY(
369                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
370         );
371
372         for (spill = set_first(env->spills), i = 0; spill != NULL;
373             spill = set_next(env->spills), ++i) {
374                 assert(spill->spillslot < spillcount);
375                 spilllist[spill->spillslot] = spill;
376         }
377
378         for (i = 0; i < spillcount; ++i) {
379                 interferences[i] = bitset_obstack_alloc(&data, spillcount);
380         }
381
382         /* construct interferences */
383         for (i = 0; i < spillcount; ++i) {
384                 ir_node *spill1 = spilllist[i]->spill;
385
386                 if (is_NoMem(spill1))
387                         continue;
388
389                 for (i2 = i+1; i2 < spillcount; ++i2) {
390                         ir_node *spill2 = spilllist[i2]->spill;
391
392                         if (is_NoMem(spill2))
393                                 continue;
394
395                         if (my_values_interfere(env->irg, spill1, spill2)) {
396                                 DB((dbg, DBG_INTERFERENCES,
397                                      "Slot %d and %d interfere\n", i, i2));
398
399                                 bitset_set(interferences[i], i2);
400                                 bitset_set(interferences[i2], i);
401                         }
402                 }
403         }
404
405         /* sort affinity edges */
406         affinity_edge_count = ARR_LEN(env->affinity_edges);
407         qsort(env->affinity_edges, affinity_edge_count,
408               sizeof(env->affinity_edges[0]), cmp_affinity);
409
410         /*dump_interference_graph(env, interferences, "before"); */
411
412         /* try to merge affine nodes */
413         for (i = 0; i < affinity_edge_count; ++i) {
414                 const affinity_edge_t *edge = env->affinity_edges[i];
415                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
416                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
417
418                 /* test if values interfere */
419                 if (bitset_is_set(interferences[s1], s2)) {
420                         assert(bitset_is_set(interferences[s2], s1));
421                         continue;
422                 }
423
424                 DB((dbg, DBG_COALESCING,
425                      "Merging %d and %d because of affinity edge\n", s1, s2));
426
427                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
428         }
429
430         /* try to merge as much remaining spillslots as possible */
431         for (i = 0; i < spillcount; ++i) {
432                 int s1 = uf_find(spillslot_unionfind, i);
433                 if (s1 != i)
434                         continue;
435
436                 for (i2 = i+1; i2 < spillcount; ++i2) {
437                         int s2 = uf_find(spillslot_unionfind, i2);
438                         if (s2 != i2)
439                                 continue;
440
441                         /* test if values interfere
442                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
443                          * when node merging occurs
444                          */
445                         if (bitset_is_set(interferences[s1], s2)) {
446                                 assert(bitset_is_set(interferences[s2], s1));
447                                 continue;
448                         }
449
450                         DB((dbg, DBG_COALESCING,
451                              "Merging %d and %d because it is possible\n", s1, s2));
452
453                         if (merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
454                                 /* we can break the loop here, because s2 is the new supernode
455                                  * now and we'll test s2 again later anyway */
456                                 break;
457                         }
458                 }
459         }
460
461         /* assign spillslots to spills */
462         for (i = 0; i < spillcount; ++i) {
463                 spill_t *spill = spilllist[i];
464
465                 spill->spillslot = uf_find(spillslot_unionfind, i);
466         }
467
468         /*dump_interference_graph(env, interferences, "after");*/
469         obstack_free(&data, 0);
470 }
471
472
473
474 typedef struct spill_slot_t {
475         int size;
476         int align;
477         ir_entity *entity;
478 } spill_slot_t;
479
480 typedef struct memperm_entry_t {
481         ir_node* node;
482         int pos;
483         ir_entity *in;
484         ir_entity *out;
485         struct memperm_entry_t *next;
486 } memperm_entry_t;
487
488 typedef struct memperm_t {
489         ir_node *block;
490         int entrycount;
491         memperm_entry_t *entries;
492 } memperm_t;
493
494 static int cmp_memperm(const void* d1, const void* d2, size_t size)
495 {
496         const memperm_t* e1 = d1;
497         const memperm_t* e2 = d2;
498         (void) size;
499
500         return e1->block != e2->block;
501 }
502
503 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
504 {
505         memperm_t entry, *res;
506         int hash;
507
508         entry.block = block;
509         hash        = hash_irn(block);
510
511         res = set_find(env->memperms, &entry, sizeof(entry), hash);
512
513         if (res == NULL) {
514                 entry.entrycount = 0;
515                 entry.entries = NULL;
516                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
517         }
518
519         return res;
520 }
521
522 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
523 {
524         ir_graph *irg  = env->irg;
525         ir_type *frame = get_irg_frame_type(irg);
526         /* TODO: backend should be able to specify wether we want spill slots
527          * at begin or end of frame */
528         int        at_start = 1;
529         ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
530
531         /* adjust size of the entity type... */
532         ir_type *enttype = get_entity_type(res);
533         set_type_size_bytes(enttype, slot->size);
534
535         slot->entity = res;
536
537         return res;
538 }
539
540 /**
541  * Enlarges a spillslot (if necessary) so that it can carry a value of size
542  * @p othersize and alignment @p otheralign.
543  */
544 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
545 {
546         if (othersize > slot->size) {
547                 slot->size = othersize;
548         }
549         if (otheralign > slot->align) {
550                 if (otheralign % slot->align != 0)
551                         slot->align *= otheralign;
552                 else
553                         slot->align = otheralign;
554         } else if (slot->align % otheralign != 0) {
555                 slot->align *= otheralign;
556         }
557 }
558
559 static void assign_spill_entity(be_fec_env_t *env,
560                                 ir_node *node, ir_entity *entity)
561 {
562         if (is_NoMem(node))
563                 return;
564         if (is_Sync(node)) {
565                 int i, arity;
566
567                 arity = get_irn_arity(node);
568                 for (i = 0; i < arity; ++i) {
569                         ir_node *in = get_irn_n(node, i);
570                         assert(!is_Phi(in));
571
572                         assign_spill_entity(env, in, entity);
573                 }
574                 return;
575         }
576
577         /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
578            instance */
579         node = skip_Proj(node);
580         assert(arch_get_frame_entity(node) == NULL);
581         env->set_frame_entity(node, entity);
582 }
583
584 /**
585  * Create stack entities for the spillslots and assign them to the spill and
586  * reload nodes.
587  */
588 static void assign_spillslots(be_fec_env_t *env)
589 {
590         int           spillcount = set_count(env->spills);
591         spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
592         spill_t      *spill;
593         int           i;
594
595         /* construct spillslots */
596         for (spill = set_first(env->spills); spill != NULL;
597                 spill = set_next(env->spills)) {
598
599                 int slotid = spill->spillslot;
600                 const ir_mode *mode = spill->mode;
601                 spill_slot_t *slot = & (spillslots[slotid]);
602                 int size = get_mode_size_bytes(mode);
603                 int align = spill->alignment;
604
605                 if (slot->align == 0 && slot->size == 0) {
606                         slot->align = align;
607                         slot->size = size;
608                 } else {
609                         enlarge_spillslot(slot, align, size);
610                 }
611         }
612
613         for (spill = set_first(env->spills); spill != NULL;
614             spill = set_next(env->spills)) {
615
616                 ir_node      *node   = spill->spill;
617                 int           slotid = spill->spillslot;
618                 spill_slot_t *slot;
619
620                 slot = &spillslots[slotid];
621                 if (slot->entity == NULL) {
622                         create_stack_entity(env, slot);
623                 }
624
625                 if (is_Phi(node)) {
626                         int i, arity;
627                         ir_node *block = get_nodes_block(node);
628
629                         /* should be a PhiM */
630                         assert(is_Phi(node));
631
632                         for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
633                                 ir_node *arg = get_irn_n(node, i);
634                                 ir_node *predblock = get_Block_cfgpred_block(block, i);
635                                 spill_t *argspill;
636                                 int argslotid;
637
638                                 argspill = get_spill(env, arg);
639                                 assert(argspill != NULL);
640
641                                 argslotid = argspill->spillslot;
642                                 if (slotid != argslotid) {
643                                         memperm_t *memperm;
644                                         memperm_entry_t *entry;
645                                         spill_slot_t *argslot = &spillslots[argslotid];
646                                         if (argslot->entity == NULL) {
647                                                 create_stack_entity(env, argslot);
648                                         }
649
650                                         memperm = get_memperm(env, predblock);
651
652                                         entry = OALLOC(&env->obst, memperm_entry_t);
653                                         entry->node = node;
654                                         entry->pos = i;
655                                         entry->in = argslot->entity;
656                                         entry->out = slot->entity;
657                                         entry->next = memperm->entries;
658                                         memperm->entrycount++;
659                                         memperm->entries = entry;
660                                 }
661                         }
662                 } else {
663                         assign_spill_entity(env, node, slot->entity);
664                 }
665         }
666
667         for (i = 0; i < ARR_LEN(env->reloads); ++i) {
668                 ir_node            *reload    = env->reloads[i];
669                 ir_node            *spillnode = get_memory_edge(reload);
670                 spill_t            *spill     = get_spill(env, spillnode);
671                 const spill_slot_t *slot      = & spillslots[spill->spillslot];
672
673                 assert(slot->entity != NULL);
674
675                 env->set_frame_entity(reload, slot->entity);
676         }
677 }
678
679 /**
680  * Returns the last node in a block which is no control flow changing node
681  */
682 static ir_node *get_end_of_block_insertion_point(ir_node* block)
683 {
684         ir_node* ins = sched_last(block);
685         while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
686                 ins = sched_prev(ins);
687                 assert(ins != NULL);
688         }
689
690         if (is_cfop(ins)) {
691                 for (;;) {
692                         ir_node *prev = sched_prev(ins);
693                         if (!is_cfop(prev))
694                                 break;
695                         ins = prev;
696                 }
697         }
698
699         return ins;
700 }
701
702 static void create_memperms(be_fec_env_t *env)
703 {
704         ir_graph         *irg      = env->irg;
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(memperm->block, memperm->entrycount,
722                                              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(ir_graph *irg)
765 {
766         be_fec_env_t *env = XMALLOCZ(be_fec_env_t);
767
768         be_liveness_assure_chk(be_assure_liveness(irg));
769
770         obstack_init(&env->obst);
771         env->irg            = irg;
772         env->spills         = new_set(cmp_spill, 10);
773         env->reloads        = NEW_ARR_F(ir_node*, 0);
774         env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
775         env->memperms       = new_set(cmp_memperm, 10);
776
777         return env;
778 }
779
780 void be_free_frame_entity_coalescer(be_fec_env_t *env)
781 {
782         del_set(env->memperms);
783         DEL_ARR_F(env->reloads);
784         DEL_ARR_F(env->affinity_edges);
785         del_set(env->spills);
786         obstack_free(&env->obst, NULL);
787
788         free(env);
789 }
790
791 void be_assign_entities(be_fec_env_t *env,
792                         set_frame_entity_func set_frame_entity)
793 {
794         env->set_frame_entity = set_frame_entity;
795
796         stat_ev_dbl("spillslots", set_count(env->spills));
797
798         if (be_coalesce_spill_slots) {
799                 do_greedy_coalescing(env);
800         }
801
802         stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
803
804         assign_spillslots(env);
805
806         create_memperms(env);
807 }
808
809 /**
810  * This walker function searches for reloads and collects all the spills
811  * and memphis attached to them.
812  */
813 static void collect_spills_walker(ir_node *node, void *data)
814 {
815         be_fec_env_t                *env = data;
816         const ir_mode               *mode;
817         const arch_register_class_t *cls;
818         int                          align;
819         ir_graph                    *irg;
820         const arch_env_t            *arch_env;
821
822         if (! (arch_irn_classify(node) & arch_irn_class_reload))
823                 return;
824
825         mode     = get_irn_mode(node);
826         cls      = arch_get_irn_reg_class_out(node);
827         irg      = get_irn_irg(node);
828         arch_env = be_get_irg_arch_env(irg);
829         align    = arch_env_get_reg_class_alignment(arch_env, cls);
830
831         be_node_needs_frame_entity(env, node, mode, align);
832 }
833
834 void be_coalesce_spillslots(ir_graph *irg)
835 {
836         be_fec_env_t *env = be_new_frame_entity_coalescer(irg);
837
838         /* collect reloads */
839         irg_walk_graph(irg, NULL, collect_spills_walker, env);
840
841         be_assign_entities(env, be_node_set_frame_entity);
842
843         be_free_frame_entity_coalescer(env);
844 }
845
846 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);
847 void be_init_spillslots(void)
848 {
849         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
850 }