refactoring: set_frame_entity is a special callbacks for users of the advanced spills...
[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         const arch_env_t      *arch_env;
72         ir_graph              *irg;
73         set                   *spills;
74         ir_node              **reloads;
75         affinity_edge_t      **affinity_edges;
76         set                   *memperms;
77         set_frame_entity_func  set_frame_entity;
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_irg_exec_freq(env->irg);
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           = OALLOC(&env->obst, affinity_edge_t);
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 != s1) {
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(ir_graph *irg, const ir_node *a,
247                                 const ir_node *b)
248 {
249         be_lv_t *lv = be_get_irg_liveness(irg);
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(ir_graph *irg, 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(irg, 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(irg, a, in))
330                                 return 1;
331                 }
332                 return 0;
333         }
334
335         return my_values_interfere2(irg, 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         struct obstack data;
354
355         spillcount = set_count(env->spills);
356         if (spillcount == 0)
357                 return;
358
359         obstack_init(&data);
360
361         DB((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
362
363         interferences       = OALLOCN(&data, bitset_t*, spillcount);
364         spillslot_unionfind = OALLOCN(&data, int,       spillcount);
365         spilllist           = OALLOCN(&data, spill_t*,  spillcount);
366
367         uf_init(spillslot_unionfind, spillcount);
368
369         DEBUG_ONLY(
370                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
371         );
372
373         for (spill = set_first(env->spills), i = 0; spill != NULL;
374             spill = set_next(env->spills), ++i) {
375                 assert(spill->spillslot < spillcount);
376                 spilllist[spill->spillslot] = spill;
377         }
378
379         for (i = 0; i < spillcount; ++i) {
380                 interferences[i] = bitset_obstack_alloc(&data, spillcount);
381         }
382
383         /* construct interferences */
384         for (i = 0; i < spillcount; ++i) {
385                 ir_node *spill1 = spilllist[i]->spill;
386
387                 if (is_NoMem(spill1))
388                         continue;
389
390                 for (i2 = i+1; i2 < spillcount; ++i2) {
391                         ir_node *spill2 = spilllist[i2]->spill;
392
393                         if (is_NoMem(spill2))
394                                 continue;
395
396                         if (my_values_interfere(env->irg, spill1, spill2)) {
397                                 DB((dbg, DBG_INTERFERENCES,
398                                      "Slot %d and %d interfere\n", i, i2));
399
400                                 bitset_set(interferences[i], i2);
401                                 bitset_set(interferences[i2], i);
402                         }
403                 }
404         }
405
406         /* sort affinity edges */
407         affinity_edge_count = ARR_LEN(env->affinity_edges);
408         qsort(env->affinity_edges, affinity_edge_count,
409               sizeof(env->affinity_edges[0]), cmp_affinity);
410
411         /*dump_interference_graph(env, interferences, "before"); */
412
413         /* try to merge affine nodes */
414         for (i = 0; i < affinity_edge_count; ++i) {
415                 const affinity_edge_t *edge = env->affinity_edges[i];
416                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
417                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
418
419                 /* test if values interfere */
420                 if (bitset_is_set(interferences[s1], s2)) {
421                         assert(bitset_is_set(interferences[s2], s1));
422                         continue;
423                 }
424
425                 DB((dbg, DBG_COALESCING,
426                      "Merging %d and %d because of affinity edge\n", s1, s2));
427
428                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
429         }
430
431         /* try to merge as much remaining spillslots as possible */
432         for (i = 0; i < spillcount; ++i) {
433                 int s1 = uf_find(spillslot_unionfind, i);
434                 if (s1 != i)
435                         continue;
436
437                 for (i2 = i+1; i2 < spillcount; ++i2) {
438                         int s2 = uf_find(spillslot_unionfind, i2);
439                         if (s2 != i2)
440                                 continue;
441
442                         /* test if values interfere
443                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
444                          * when node merging occurs
445                          */
446                         if (bitset_is_set(interferences[s1], s2)) {
447                                 assert(bitset_is_set(interferences[s2], s1));
448                                 continue;
449                         }
450
451                         DB((dbg, DBG_COALESCING,
452                              "Merging %d and %d because it is possible\n", s1, s2));
453
454                         if (merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
455                                 /* we can break the loop here, because s2 is the new supernode
456                                  * now and we'll test s2 again later anyway */
457                                 break;
458                         }
459                 }
460         }
461
462         /* assign spillslots to spills */
463         for (i = 0; i < spillcount; ++i) {
464                 spill_t *spill = spilllist[i];
465
466                 spill->spillslot = uf_find(spillslot_unionfind, i);
467         }
468
469         /*dump_interference_graph(env, interferences, "after");*/
470         obstack_free(&data, 0);
471 }
472
473
474
475 typedef struct _spill_slot_t {
476         int size;
477         int align;
478         ir_entity *entity;
479 } spill_slot_t;
480
481 typedef struct _memperm_entry_t {
482         ir_node* node;
483         int pos;
484         ir_entity *in;
485         ir_entity *out;
486         struct _memperm_entry_t *next;
487 } memperm_entry_t;
488
489 typedef struct _memperm_t {
490         ir_node *block;
491         int entrycount;
492         memperm_entry_t *entries;
493 } memperm_t;
494
495 static int cmp_memperm(const void* d1, const void* d2, size_t size)
496 {
497         const memperm_t* e1 = d1;
498         const memperm_t* e2 = d2;
499         (void) size;
500
501         return e1->block != e2->block;
502 }
503
504 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
505 {
506         memperm_t entry, *res;
507         int hash;
508
509         entry.block = block;
510         hash        = hash_irn(block);
511
512         res = set_find(env->memperms, &entry, sizeof(entry), hash);
513
514         if (res == NULL) {
515                 entry.entrycount = 0;
516                 entry.entries = NULL;
517                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
518         }
519
520         return res;
521 }
522
523 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
524 {
525         ir_graph *irg  = env->irg;
526         ir_type *frame = get_irg_frame_type(irg);
527         /* TODO: backend should be able to specify wether we want spill slots
528          * at begin or end of frame */
529         int        at_start = 1;
530         ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, at_start);
531
532         /* adjust size of the entity type... */
533         ir_type *enttype = get_entity_type(res);
534         set_type_size_bytes(enttype, slot->size);
535
536         slot->entity = res;
537
538         return res;
539 }
540
541 /**
542  * Enlarges a spillslot (if necessary) so that it can carry a value of size
543  * @p othersize and alignment @p otheralign.
544  */
545 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
546 {
547         if (othersize > slot->size) {
548                 slot->size = othersize;
549         }
550         if (otheralign > slot->align) {
551                 if (otheralign % slot->align != 0)
552                         slot->align *= otheralign;
553                 else
554                         slot->align = otheralign;
555         } else if (slot->align % otheralign != 0) {
556                 slot->align *= otheralign;
557         }
558 }
559
560 static void assign_spill_entity(be_fec_env_t *env,
561                                 ir_node *node, ir_entity *entity)
562 {
563         if (is_NoMem(node))
564                 return;
565         if (is_Sync(node)) {
566                 int i, arity;
567
568                 arity = get_irn_arity(node);
569                 for (i = 0; i < arity; ++i) {
570                         ir_node *in = get_irn_n(node, i);
571                         assert(!is_Phi(in));
572
573                         assign_spill_entity(env, in, entity);
574                 }
575                 return;
576         }
577
578         /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
579            instance */
580         node = skip_Proj(node);
581         assert(arch_get_frame_entity(node) == NULL);
582         env->set_frame_entity(node, entity);
583 }
584
585 /**
586  * Create stack entities for the spillslots and assign them to the spill and
587  * reload nodes.
588  */
589 static void assign_spillslots(be_fec_env_t *env)
590 {
591         int           spillcount = set_count(env->spills);
592         spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
593         spill_t      *spill;
594         int           i;
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 = OALLOC(&env->obst, memperm_entry_t);
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(env, 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                 env->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                 for (;;) {
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      = env->irg;
707         memperm_t        *memperm;
708
709         for (memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
710                 ir_node         **nodes = ALLOCAN(ir_node*, memperm->entrycount);
711                 memperm_entry_t  *entry;
712                 ir_node          *blockend;
713                 ir_node          *mempermnode;
714                 int               i;
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, 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(ir_graph *irg)
767 {
768         const arch_env_t *arch_env = be_get_irg_arch_env(irg);
769         be_fec_env_t     *env      = XMALLOC(be_fec_env_t);
770
771         be_liveness_assure_chk(be_assure_liveness(irg));
772
773         obstack_init(&env->obst);
774         env->arch_env       = arch_env;
775         env->irg            = irg;
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                         set_frame_entity_func set_frame_entity)
797 {
798         env->set_frame_entity = set_frame_entity;
799
800         stat_ev_dbl("spillslots", set_count(env->spills));
801
802         if (be_coalesce_spill_slots) {
803                 do_greedy_coalescing(env);
804         }
805
806         stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
807
808         assign_spillslots(env);
809
810         create_memperms(env);
811 }
812
813 /**
814  * This walker function searches for reloads and collects all the spills
815  * and memphis attached to them.
816  */
817 static void collect_spills_walker(ir_node *node, void *data)
818 {
819         be_fec_env_t *env = data;
820         const ir_mode *mode;
821         const arch_register_class_t *cls;
822         int align;
823
824         if (! (arch_irn_classify(node) & arch_irn_class_reload))
825                 return;
826
827         mode  = get_irn_mode(node);
828         cls   = arch_get_irn_reg_class_out(node);
829         align = arch_env_get_reg_class_alignment(env->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 }