fe4df32f4a1548b953a5f942430582afc6d0371d
[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  */
26 #include "config.h"
27
28 #include <stdlib.h>
29
30 #include "set.h"
31 #include "array.h"
32 #include "irgwalk.h"
33 #include "ircons.h"
34 #include "irprintf.h"
35 #include "execfreq.h"
36 #include "unionfind.h"
37 #include "irdump_t.h"
38
39 #include "benode.h"
40 #include "besched.h"
41 #include "bespill.h"
42 #include "bespillslots.h"
43 #include "bechordal_t.h"
44 #include "bestatevent.h"
45 #include "bemodule.h"
46 #include "beintlive_t.h"
47 #include "beirg.h"
48 #include "bearch.h"
49
50 #define DBG_COALESCING      1
51 #define DBG_INTERFERENCES   2
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 typedef struct spill_t {
56         ir_node       *spill;
57         const ir_mode *mode;      /**< mode of the spilled value */
58         int            alignment; /**< alignment for the spilled value */
59         int            spillslot; /**< index into spillslot_unionfind structure */
60 } spill_t;
61
62 typedef struct affinity_edge_t {
63         double affinity;
64         int    slot1;
65         int    slot2;
66 } affinity_edge_t;
67
68 struct be_fec_env_t {
69         struct obstack         obst;
70         ir_graph              *irg;
71         set                   *spills;
72         ir_node              **reloads;
73         affinity_edge_t      **affinity_edges;
74         set                   *memperms;
75         set_frame_entity_func  set_frame_entity;
76         bool                   at_begin;  /**< frame entities should be allocate at
77                                                the beginning of the stackframe */
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 = (const affinity_edge_t**)d1;
84         const affinity_edge_t * const *e2 = (const affinity_edge_t**)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 = (const spill_t*)d1;
93         const spill_t* s2 = (const spill_t*)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 = (spill_t*)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         = (spill_t*)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             = (spill_t*)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 = (spill_t*)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             = (spill_t*)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
202         assert(spillnode != NULL);
203
204         /* walk upwards and collect all phis and spills on this way */
205         if (is_Phi(spillnode)) {
206                 collect_memphi(env, spillnode, mode, align);
207         } else {
208                 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         i = 0;
373         foreach_set(env->spills, spill_t*, spill) {
374                 assert(spill->spillslot < spillcount);
375                 spilllist[spill->spillslot] = spill;
376                 ++i;
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 = (const memperm_t*)d1;
498         const memperm_t* e2 = (const memperm_t*)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 = (memperm_t*)set_find(env->memperms, &entry, sizeof(entry), hash);
513
514         if (res == NULL) {
515                 entry.entrycount = 0;
516                 entry.entries = NULL;
517                 res = (memperm_t*)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         ir_entity *res   = frame_alloc_area(frame, slot->size, slot->align,
528                                             env->at_begin);
529         slot->entity = res;
530
531         return res;
532 }
533
534 /**
535  * Enlarges a spillslot (if necessary) so that it can carry a value of size
536  * @p othersize and alignment @p otheralign.
537  */
538 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
539 {
540         if (othersize > slot->size) {
541                 slot->size = othersize;
542         }
543         if (otheralign > slot->align) {
544                 if (otheralign % slot->align != 0)
545                         slot->align *= otheralign;
546                 else
547                         slot->align = otheralign;
548         } else if (slot->align % otheralign != 0) {
549                 slot->align *= otheralign;
550         }
551 }
552
553 static void assign_spill_entity(be_fec_env_t *env,
554                                 ir_node *node, ir_entity *entity)
555 {
556         if (is_NoMem(node))
557                 return;
558         if (is_Sync(node)) {
559                 int i, arity;
560
561                 arity = get_irn_arity(node);
562                 for (i = 0; i < arity; ++i) {
563                         ir_node *in = get_irn_n(node, i);
564                         assert(!is_Phi(in));
565
566                         assign_spill_entity(env, in, entity);
567                 }
568                 return;
569         }
570
571         /* beware: we might have Stores with Memory Proj's, ia32 fisttp for
572            instance */
573         node = skip_Proj(node);
574         assert(arch_get_frame_entity(node) == NULL);
575         env->set_frame_entity(node, entity);
576 }
577
578 /**
579  * Create stack entities for the spillslots and assign them to the spill and
580  * reload nodes.
581  */
582 static void assign_spillslots(be_fec_env_t *env)
583 {
584         int           spillcount = set_count(env->spills);
585         spill_slot_t *spillslots = ALLOCANZ(spill_slot_t, spillcount);
586         spill_t      *spill;
587         size_t        i;
588
589         /* construct spillslots */
590         foreach_set(env->spills, spill_t*, spill) {
591                 int slotid = spill->spillslot;
592                 const ir_mode *mode = spill->mode;
593                 spill_slot_t *slot = & (spillslots[slotid]);
594                 int size = get_mode_size_bytes(mode);
595                 int align = spill->alignment;
596
597                 if (slot->align == 0 && slot->size == 0) {
598                         slot->align = align;
599                         slot->size = size;
600                 } else {
601                         enlarge_spillslot(slot, align, size);
602                 }
603         }
604
605         foreach_set(env->spills, spill_t*, spill) {
606                 ir_node      *node   = spill->spill;
607                 int           slotid = spill->spillslot;
608                 spill_slot_t *slot;
609
610                 slot = &spillslots[slotid];
611                 if (slot->entity == NULL) {
612                         create_stack_entity(env, slot);
613                 }
614
615                 if (is_Phi(node)) {
616                         int i, arity;
617                         ir_node *block = get_nodes_block(node);
618
619                         /* should be a PhiM */
620                         assert(get_irn_mode(node) == mode_M);
621
622                         for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
623                                 ir_node *arg = get_irn_n(node, i);
624                                 ir_node *predblock = get_Block_cfgpred_block(block, i);
625                                 spill_t *argspill;
626                                 int argslotid;
627
628                                 argspill = get_spill(env, arg);
629                                 assert(argspill != NULL);
630
631                                 argslotid = argspill->spillslot;
632                                 if (slotid != argslotid) {
633                                         memperm_t *memperm;
634                                         memperm_entry_t *entry;
635                                         spill_slot_t *argslot = &spillslots[argslotid];
636                                         if (argslot->entity == NULL) {
637                                                 create_stack_entity(env, argslot);
638                                         }
639
640                                         memperm = get_memperm(env, predblock);
641
642                                         entry = OALLOC(&env->obst, memperm_entry_t);
643                                         entry->node = node;
644                                         entry->pos = i;
645                                         entry->in = argslot->entity;
646                                         entry->out = slot->entity;
647                                         entry->next = memperm->entries;
648                                         memperm->entrycount++;
649                                         memperm->entries = entry;
650                                 }
651                         }
652                 } else {
653                         assign_spill_entity(env, node, slot->entity);
654                 }
655         }
656
657         for (i = 0; i < ARR_LEN(env->reloads); ++i) {
658                 ir_node            *reload    = env->reloads[i];
659                 ir_node            *spillnode = get_memory_edge(reload);
660                 spill_t            *spill     = get_spill(env, spillnode);
661                 const spill_slot_t *slot      = & spillslots[spill->spillslot];
662
663                 assert(slot->entity != NULL);
664
665                 env->set_frame_entity(reload, slot->entity);
666         }
667 }
668
669 /**
670  * Returns the last node in a block which is no control flow changing node
671  */
672 static ir_node *get_end_of_block_insertion_point(ir_node* block)
673 {
674         ir_node* ins = sched_last(block);
675         while (is_Proj(ins) && get_irn_mode(ins) == mode_X) {
676                 ins = sched_prev(ins);
677                 assert(ins != NULL);
678         }
679
680         if (is_cfop(ins)) {
681                 for (;;) {
682                         ir_node *prev = sched_prev(ins);
683                         if (!is_cfop(prev))
684                                 break;
685                         ins = prev;
686                 }
687         }
688
689         return ins;
690 }
691
692 static void create_memperms(be_fec_env_t *env)
693 {
694         memperm_t *memperm;
695
696         foreach_set(env->memperms, memperm_t*, memperm) {
697                 ir_node         **nodes = ALLOCAN(ir_node*, memperm->entrycount);
698                 memperm_entry_t  *entry;
699                 ir_node          *blockend;
700                 ir_node          *mempermnode;
701                 int               i;
702
703                 assert(memperm->entrycount > 0);
704
705                 for (entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
706                         ir_node* arg = get_irn_n(entry->node, entry->pos);
707                         nodes[i] = arg;
708                 }
709
710                 mempermnode = be_new_MemPerm(memperm->block, memperm->entrycount,
711                                              nodes);
712
713                 /* insert node into schedule */
714                 blockend = get_end_of_block_insertion_point(memperm->block);
715                 sched_add_before(blockend, mempermnode);
716                 stat_ev_dbl("mem_perm", memperm->entrycount);
717
718                 i = 0;
719                 for (entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
720                         ir_node *proj;
721                         ir_node* arg = get_irn_n(entry->node, entry->pos);
722
723                         be_set_MemPerm_in_entity(mempermnode, i, entry->in);
724                         be_set_MemPerm_out_entity(mempermnode, i, entry->out);
725                         proj = new_r_Proj(mempermnode, get_irn_mode(arg), i);
726
727                         set_irn_n(entry->node, entry->pos, proj);
728                 }
729         }
730 }
731
732 static int count_spillslots(const be_fec_env_t *env)
733 {
734         const spill_t *spill;
735         int spillcount = set_count(env->spills);
736         bitset_t *counted = bitset_alloca(spillcount);
737         int slotcount;
738
739         slotcount = 0;
740         foreach_set(env->spills, spill_t*, spill) {
741                 int spillslot = spill->spillslot;
742                 if (!bitset_is_set(counted, spillslot)) {
743                         slotcount++;
744                         bitset_set(counted, spillslot);
745                 }
746         }
747
748         return slotcount;
749 }
750
751 be_fec_env_t *be_new_frame_entity_coalescer(ir_graph *irg)
752 {
753         be_fec_env_t *env = XMALLOCZ(be_fec_env_t);
754
755         be_assure_live_chk(irg);
756
757         obstack_init(&env->obst);
758         env->irg            = irg;
759         env->spills         = new_set(cmp_spill, 10);
760         env->reloads        = NEW_ARR_F(ir_node*, 0);
761         env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
762         env->memperms       = new_set(cmp_memperm, 10);
763
764         return env;
765 }
766
767 void be_free_frame_entity_coalescer(be_fec_env_t *env)
768 {
769         del_set(env->memperms);
770         DEL_ARR_F(env->reloads);
771         DEL_ARR_F(env->affinity_edges);
772         del_set(env->spills);
773         obstack_free(&env->obst, NULL);
774
775         free(env);
776 }
777
778 void be_assign_entities(be_fec_env_t *env,
779                         set_frame_entity_func set_frame_entity,
780                         bool alloc_entities_at_begin)
781 {
782         env->set_frame_entity = set_frame_entity;
783         env->at_begin         = alloc_entities_at_begin;
784
785         stat_ev_dbl("spillslots", set_count(env->spills));
786
787         if (be_coalesce_spill_slots) {
788                 do_greedy_coalescing(env);
789         }
790
791         stat_ev_dbl("spillslots_after_coalescing", count_spillslots(env));
792
793         assign_spillslots(env);
794
795         create_memperms(env);
796 }
797
798 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots)
799 void be_init_spillslots(void)
800 {
801         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
802 }