added nolea option to switch of LEA optimization
[libfirm] / ir / be / bespill.c
1 /**
2  * Author:      Daniel Grund, Sebastian Hack
3  * Date:                29.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "pset.h"
14 #include "irnode_t.h"
15 #include "ircons_t.h"
16 #include "iredges_t.h"
17 #include "ident_t.h"
18 #include "type_t.h"
19 #include "entity_t.h"
20 #include "debug.h"
21 #include "irgwalk.h"
22 #include "array.h"
23 #include "pdeq.h"
24
25 #include "belive_t.h"
26 #include "besched_t.h"
27 #include "bespill.h"
28 #include "benode_t.h"
29 #include "bechordal_t.h"
30
31 #define REMAT
32 /* This enables re-computation of values. Current state: Unfinished and buggy. */
33 #define BUGGY_REMAT
34
35 typedef struct _reloader_t reloader_t;
36 typedef struct _spill_info_t spill_info_t;
37
38 struct _reloader_t {
39         reloader_t *next;
40         ir_node *reloader;
41 };
42
43 struct _spill_info_t {
44         ir_node *spilled_node;
45         reloader_t *reloaders;
46 };
47
48 typedef struct _spill_ctx_t {
49         ir_node *spilled;  /**< The spilled node. */
50         ir_node *user;     /**< The node this spill is for. */
51         ir_node *spill;    /**< The spill itself. */
52 } spill_ctx_t;
53
54 struct _spill_env_t {
55         const arch_register_class_t *cls;
56         const be_chordal_env_t *chordal_env;
57         struct obstack obst;
58         set *spill_ctxs;
59         set *spills;                            /**< all spill_info_t's, which must be placed */
60         pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed separately */
61         decide_irn_t is_mem_phi;        /**< callback func to decide if a phi needs special spilling */
62         void *data;                                     /**< data passed to all callbacks */
63         DEBUG_ONLY(firm_dbg_module_t *dbg;)
64 };
65
66 static int cmp_spillctx(const void *a, const void *b, size_t n) {
67         const spill_ctx_t *p = a;
68         const spill_ctx_t *q = b;
69         return !(p->user == q->user && p->spilled == q->spilled);
70 }
71
72 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
73         const spill_info_t *xx = x;
74         const spill_info_t *yy = y;
75         return ! (xx->spilled_node == yy->spilled_node);
76 }
77
78 DEBUG_ONLY(
79 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
80         env->dbg = dbg;
81 }
82 )
83
84 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env, decide_irn_t is_mem_phi, void *data) {
85         spill_env_t *env = xmalloc(sizeof(env[0]));
86         env->spill_ctxs  = new_set(cmp_spillctx, 1024);
87         env->spills      = new_set(cmp_spillinfo, 1024);
88         env->cls         = chordal_env->cls;
89         env->is_mem_phi  = is_mem_phi;
90         env->data        = data;
91         env->chordal_env = chordal_env;
92         obstack_init(&env->obst);
93         return env;
94 }
95
96 void be_delete_spill_env(spill_env_t *senv) {
97         del_set(senv->spill_ctxs);
98         del_set(senv->spills);
99         obstack_free(&senv->obst, NULL);
100         free(senv);
101 }
102
103 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
104         spill_ctx_t templ;
105
106         templ.spilled = to_spill;
107         templ.user    = ctx_irn;
108         templ.spill   = NULL;
109
110         return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
111 }
112
113 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
114         spill_ctx_t *ctx;
115         DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", irn, ctx_irn));
116
117         ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
118         if(!ctx->spill) {
119                 const be_main_env_t *env = senv->chordal_env->birg->main_env;
120                 ctx->spill = be_spill(env->arch_env, irn, ctx_irn);
121         }
122
123         return ctx->spill;
124 }
125
126 /**
127  * If the first usage of a phi result would be out of memory
128  * there is no sense in allocating a register for it.
129  * Thus we spill it and all its operands to the same spill slot.
130  * Therefore the phi/dataB becomes a phi/Memory
131  */
132 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn) {
133         int i, n = get_irn_arity(phi);
134         ir_node **ins, *bl = get_nodes_block(phi);
135         ir_graph *irg = senv->chordal_env->irg;
136         spill_ctx_t *ctx;
137
138         assert(is_Phi(phi));
139         DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", phi, ctx_irn));
140
141         /* search an existing spill for this context */
142         ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
143
144         /* if not found spill the phi */
145         if(!ctx->spill) {
146                 /* build a new PhiM with dummy in-array */
147                 NEW_ARR_A(ir_node *, ins, n);
148                 for(i=0; i<n; ++i)
149                         ins[i] = new_r_Unknown(irg, mode_M);
150                 ctx->spill = new_r_Phi(senv->chordal_env->irg, bl, n, ins, mode_M);
151
152                 /* re-wire the phiM */
153                 for(i=0; i<n; ++i) {
154                         ir_node *arg = get_irn_n(phi, i);
155                         ir_node *sub_res;
156
157                         if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg))
158                                 sub_res = be_spill_phi(senv, arg, ctx_irn);
159                         else
160                                 sub_res = be_spill_irn(senv, arg, ctx_irn);
161
162                         set_irn_n(ctx->spill, i, sub_res);
163                 }
164         }
165         return ctx->spill;
166 }
167
168 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
169         ir_node *res;
170         if (pset_find_ptr(senv->mem_phis, to_spill))
171                 res = be_spill_phi(senv, to_spill, to_spill);
172         else
173                 res = be_spill_irn(senv, to_spill, to_spill);
174
175         return res;
176 }
177
178 static void phi_walker(ir_node *irn, void *env) {
179         spill_env_t *senv = env;
180         const arch_env_t *arch = senv->chordal_env->birg->main_env->arch_env;
181
182         if (is_Phi(irn) && arch_irn_has_reg_class(arch, irn, 0, senv->cls)
183                         && senv->is_mem_phi(irn, senv->data)) {
184                 DBG((senv->dbg, LEVEL_1, "  %+F\n", irn));
185                 pset_insert_ptr(senv->mem_phis, irn);
186         }
187 }
188
189 #ifdef REMAT
190
191 #ifdef BUGGY_REMAT
192
193 /**
194  * Check if a spilled node could be rematerialized.
195  *
196  * @param senv      the spill environment
197  * @param spill     the Spill node
198  * @param spilled   the node that was spilled
199  * @param reloader  a irn that requires a reload
200  */
201 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
202         int pos, max;
203
204         /* check for 'normal' spill and general remat condition */
205         if (!be_is_Spill(spill) || !arch_irn_is(senv->chordal_env->birg->main_env->arch_env, spilled, rematerializable))
206                 return 0;
207
208         /* check availability of original arguments */
209         if (is_Block(reloader)) {
210
211                 /* we want to remat at the end of a block.
212                  * thus all arguments must be alive at the end of the block
213                  */
214                 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
215                         ir_node *arg = get_irn_n(spilled, pos);
216                         if (!is_live_end(reloader, arg))
217                                 return 0;
218                 }
219
220         } else {
221
222                 /* we want to remat before the insn reloader
223                  * thus an arguments is alive if
224                  *   - it interferes with the reloaders result
225                  * or
226                  *   - or it is (last-) used by reloader itself
227                  */
228                 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
229                         ir_node *arg = get_irn_n(spilled, pos);
230                         int i, m;
231
232                         if (values_interfere(reloader, arg))
233                                 goto is_alive;
234
235                         for (i=0, m=get_irn_arity(reloader); i<m; ++i) {
236                                 ir_node *rel_arg = get_irn_n(reloader, i);
237                                 if (rel_arg == arg)
238                                         goto is_alive;
239                         }
240
241                         /* arg is not alive before reloader */
242                         return 0;
243
244 is_alive:       ;
245
246                 }
247
248         }
249
250         return 1;
251 }
252
253 #else /* BUGGY_REMAT */
254
255 /**
256  * A very simple rematerialization checker.
257  *
258  * @param senv      the spill environment
259  * @param spill     the Spill node
260  * @param spilled   the node that was spilled
261  * @param reloader  a irn that requires a reload
262  */
263 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
264         const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
265
266         return get_irn_arity(spilled) == 0 &&
267                    be_is_Spill(spill) &&
268                    arch_irn_is(aenv, spilled, rematerializable);
269 }
270
271 #endif /* BUGGY_REMAT */
272
273 #endif /* REMAT */
274
275 /**
276  * Rematerialize a node.
277  *
278  * @param senv      the spill environment
279  * @param spilled   the node that was spilled
280  * @param reloader  a irn that requires a reload
281  */
282 static ir_node *do_remat(spill_env_t *senv, ir_node *spilled, ir_node *reloader) {
283         ir_node *res;
284         ir_node *bl = (is_Block(reloader)) ? reloader : get_nodes_block(reloader);
285
286         /* recompute the value */
287         res = new_ir_node(get_irn_dbg_info(spilled), senv->chordal_env->irg, bl,
288                 get_irn_op(spilled),
289                 get_irn_mode(spilled),
290                 get_irn_arity(spilled),
291                 get_irn_in(spilled) + 1);
292         copy_node_attr(spilled, res);
293
294         DBG((senv->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
295
296         /* insert in schedule */
297         if (is_Block(reloader)) {
298                 ir_node *insert = sched_skip(reloader, 0, sched_skip_cf_predicator, (void *) senv->chordal_env->birg->main_env->arch_env);
299                 sched_add_after(insert, res);
300         } else {
301                 sched_add_before(reloader, res);
302         }
303
304         return res;
305 }
306
307 void be_insert_spills_reloads(spill_env_t *senv, pset *reload_set) {
308         const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
309         ir_graph *irg          = senv->chordal_env->irg;
310         ir_node *irn;
311         spill_info_t *si;
312         pdeq *possibly_dead;
313
314         /* get all special spilled phis */
315         DBG((senv->dbg, LEVEL_1, "Mem-phis:\n"));
316         senv->mem_phis = pset_new_ptr_default();
317         irg_walk_graph(senv->chordal_env->irg, phi_walker, NULL, senv);
318
319         /* Add reloads for mem_phis */
320         /* BETTER: These reloads (1) should only be inserted, if they are really needed */
321         DBG((senv->dbg, LEVEL_1, "Reloads for mem-phis:\n"));
322         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
323                 const ir_edge_t *e;
324                 DBG((senv->dbg, LEVEL_1, " Mem-phi %+F\n", irn));
325                 foreach_out_edge(irn, e) {
326                         ir_node *user = e->src;
327                         if (is_Phi(user) && !pset_find_ptr(senv->mem_phis, user)) {
328                                         ir_node *use_bl = get_nodes_block(user);
329                                         DBG((senv->dbg, LEVEL_1, " non-mem-phi user %+F\n", user));
330                                         be_add_reload_on_edge(senv, irn, use_bl, e->pos); /* (1) */
331                         }
332                 }
333         }
334
335         /* process each spilled node */
336         DBG((senv->dbg, LEVEL_1, "Insert spills and reloads:\n"));
337         possibly_dead = new_pdeq();
338         for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
339                 reloader_t *rld;
340                 ir_mode *mode = get_irn_mode(si->spilled_node);
341                 pset *values = pset_new_ptr(16);
342
343                 /* go through all reloads for this spill */
344                 for(rld = si->reloaders; rld; rld = rld->next) {
345                         ir_node *new_val;
346
347                         /* the spill for this reloader */
348                         ir_node *spill   = be_spill_node(senv, si->spilled_node);
349
350 #ifdef REMAT
351                         if (check_remat_conditions(senv, spill, si->spilled_node, rld->reloader)) {
352                                 new_val = do_remat(senv, si->spilled_node, rld->reloader);
353                                 pdeq_putl(possibly_dead, spill);
354                         }
355                         else
356 #endif
357                                 /* do a reload */
358                                 new_val = be_reload(aenv, senv->cls, rld->reloader, mode, spill);
359
360                         DBG((senv->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
361                         pset_insert_ptr(values, new_val);
362                         if(reload_set)
363                                 pset_insert_ptr(reload_set, new_val);
364                 }
365
366                 /* introduce copies, rewire the uses */
367                 assert(pset_count(values) > 0 && "???");
368                 pset_insert_ptr(values, si->spilled_node);
369                 be_ssa_constr_set_ignore(senv->chordal_env->dom_front, values, senv->mem_phis);
370
371                 del_pset(values);
372         }
373
374         foreach_pset(senv->mem_phis, irn) {
375                 int i, n;
376                 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
377                         pdeq_putl(possibly_dead, get_irn_n(irn, i));
378                         set_irn_n(irn, i, new_r_Bad(senv->chordal_env->irg));
379                 }
380                 sched_remove(irn);
381         }
382
383         /* check if possibly dead nodes are really dead yet */
384         while (! pdeq_empty(possibly_dead)) {
385                 ir_node *irn = pdeq_getr(possibly_dead);
386                 const ir_edge_t *edge = get_irn_out_edge_first(irn);
387
388                 if (! edge) {
389                         int i;
390                         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
391                                 pdeq_putl(possibly_dead, get_irn_n(irn, i));
392                                 set_irn_n(irn, i, new_r_Bad(senv->chordal_env->irg));
393                         }
394                         sched_remove(irn);
395                 }
396         }
397         del_pdeq(possibly_dead);
398         del_pset(senv->mem_phis);
399 }
400
401 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
402         spill_info_t templ, *res;
403         reloader_t *rel;
404
405         templ.spilled_node = to_spill;
406         templ.reloaders    = NULL;
407         res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
408
409         rel           = obstack_alloc(&senv->obst, sizeof(rel[0]));
410         rel->reloader = before;
411         rel->next     = res->reloaders;
412         res->reloaders = rel;
413 }
414
415 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
416         ir_node *insert_bl = get_irn_arity(bl) == 1 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
417         be_add_reload(senv, to_spill, insert_bl);
418 }
419
420
421
422 /****************************************
423
424         SPILL SLOT MANAGEMENT AND OPTS
425
426 ****************************************/
427
428 typedef struct _spill_slot_t {
429         unsigned size;
430         unsigned align;
431         pset *members;
432         ir_mode *largest_mode;  /* the mode of all members with largest size */
433 } spill_slot_t;
434
435 typedef struct _ss_env_t {
436         struct obstack ob;
437         be_chordal_env_t *cenv;
438         pmap *slots;            /* maps spill_contexts to spill_slots */
439         pmap *types;    /* maps modes to types */
440         DEBUG_ONLY(firm_dbg_module_t *dbg;)
441 } ss_env_t;
442
443
444 /**
445  * Walker: compute the spill slots
446  */
447 static void compute_spill_slots_walker(ir_node *spill, void *env) {
448         ss_env_t *ssenv = env;
449         ir_node *ctx;
450         pmap_entry *entry;
451         spill_slot_t *ss;
452
453         if (!be_is_Spill(spill))
454                 return;
455
456         /* check, if this spill is for a context already known */
457         ctx = be_get_Spill_context(spill);
458         entry = pmap_find(ssenv->slots, ctx);
459
460         if (!entry) {
461                 struct _arch_env_t *arch_env     = ssenv->cenv->birg->main_env->arch_env;
462                 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
463                 ir_mode *largest_mode            = arch_register_class_mode(cls);
464
465                 /* this is a new spill context */
466                 ss = obstack_alloc(&ssenv->ob, sizeof(*ss));
467                 ss->members      = pset_new_ptr(8);
468                 ss->largest_mode = largest_mode;
469                 ss->size         = get_mode_size_bytes(ss->largest_mode);
470                 ss->align        = arch_isa_get_reg_class_alignment(arch_env->isa, cls);
471                 pmap_insert(ssenv->slots, ctx, ss);
472         } else {
473                 /* values with the same spill_ctx must go into the same spill slot */
474                 ss = entry->value;
475
476 #ifndef NDEBUG
477                 /* ugly mega assert :-) */
478                 {
479                         ir_node *irn;
480                         struct _arch_env_t *arch_env     = ssenv->cenv->birg->main_env->arch_env;
481                         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
482                         int size = get_mode_size_bytes(arch_register_class_mode(cls));
483                         assert(ss->size == size && "Different sizes for the same spill slot are not allowed.");
484                         for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
485                                 /* use values_interfere here, because it uses the dominance check,
486                                          which does work for values in memory */
487                                 assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
488                         }
489                 }
490 #endif /* NDEBUG */
491         }
492
493         pset_insert_ptr(ss->members, spill);
494 }
495
496 /**
497  * qsort compare function, sort spill slots by size.
498  */
499 static int ss_sorter(const void *v1, const void *v2) {
500         const spill_slot_t *ss1 = v1;
501         const spill_slot_t *ss2 = v2;
502         return ((int) ss2->size) - ((int) ss1->size);
503 }
504
505
506 /**
507  * This function should optimize the spill slots.
508  *  - Coalescing of multiple slots
509  *  - Ordering the slots
510  *
511  * Input slots are in @p ssenv->slots
512  * @p size The count of initial spill slots in @p ssenv->slots
513  *         This also is the size of the preallocated array @p ass
514  *
515  * @return An array of spill slots @p ass in specific order
516  **/
517 static void optimize_slots(ss_env_t *ssenv, int size, spill_slot_t **ass) {
518         int i, o, used_slots;
519         pmap_entry *entr;
520
521         i=0;
522         pmap_foreach(ssenv->slots, entr)
523                 ass[i++] = entr->value;
524
525         /* Sort the array to minimize fragmentation and cache footprint.
526            Large slots come first */
527         qsort(ass, size, sizeof(ass[0]), ss_sorter);
528
529         /* For each spill slot:
530                 - assign a new offset to this slot
531             - xor find another slot to coalesce with */
532         used_slots = 0;
533         for (i=0; i<size; ++i) { /* for each spill slot */
534                 ir_node *n1;
535                 int tgt_slot = -1;
536
537                 DBG((ssenv->dbg, LEVEL_1, "Spill slot %d members:\n", i));
538                 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
539                         DBG((ssenv->dbg, LEVEL_1, "  %+F\n", n1));
540
541
542                 for (o=0; o < used_slots && tgt_slot == -1; ++o) { /* for each offset-assigned spill slot */
543                         /* check inter-slot-pairs for interference */
544                         ir_node *n2;
545                         for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
546                                 for(n2 = pset_first(ass[o]->members); n2; n2 = pset_next(ass[o]->members))
547                                         if(values_interfere(n1, n2)) {
548                                                 pset_break(ass[i]->members);
549                                                 pset_break(ass[o]->members);
550                                                 DBG((ssenv->dbg, LEVEL_1, "    Interf %+F -- %+F\n", n1, n2));
551                                                 goto interf_detected;
552                                         }
553
554                         /* if we are here, there is no interference between ass[i] and ass[o] */
555                         tgt_slot = o;
556
557 interf_detected: /*nothing*/ ;
558                 }
559
560                 /* now the members of ass[i] join the members of ass[tgt_slot] */
561
562                 /* do we need a new slot? */
563                 if (tgt_slot == -1) {
564                         tgt_slot = used_slots;
565                         used_slots++;
566
567                         /* init slot */
568                         if (tgt_slot != i) {
569                                 ass[tgt_slot]->size = ass[i]->size;
570                                 del_pset(ass[tgt_slot]->members);
571                                 ass[tgt_slot]->members = pset_new_ptr(8);
572                         }
573                 }
574
575                 /* copy the members to the target pset */
576                 /* NOTE: If src and tgt pset are the same, inserting while iterating is not allowed */
577                 if (tgt_slot != i)
578                         for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
579                                         pset_insert_ptr(ass[tgt_slot]->members, n1);
580         }
581 }
582
583 #define ALIGN_SPILL_AREA 16
584 #define pset_foreach(pset, elm)  for(elm=pset_first(pset); elm; elm=pset_next(pset))
585
586 /**
587  * Returns a spill type for a mode. Keep them in a map to reduce
588  * the number of types.
589  *
590  * @param types  a map containing all created types
591  * @param ss     the spill slot
592  *
593  * Note that type types should are identical for every mode.
594  * This rule might break if two different register classes return the same
595  * mode but different alignments.
596  */
597 static ir_type *get_spill_type(pmap *types, spill_slot_t *ss) {
598   pmap_entry *e = pmap_find(types, ss->largest_mode);
599   ir_type *res;
600
601   if (! e) {
602                 char buf[64];
603     snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(ss->largest_mode));
604     res = new_type_primitive(new_id_from_str(buf), ss->largest_mode);
605                 set_type_alignment_bytes(res, ss->align);
606     pmap_insert(types, ss->largest_mode, res);
607   }
608   else {
609     res = e->value;
610                 assert(get_type_alignment_bytes(res) == (int)ss->align);
611         }
612   return res;
613 }
614
615 /**
616  * Create spill slot entities on the frame type.
617  *
618  * @param ssenv   the spill environment
619  * @param n       number of spill slots
620  * @param ss      array of spill slots
621  */
622 static void assign_entities(ss_env_t *ssenv, int n_slots, spill_slot_t *ss[]) {
623         int i, offset, frame_align;
624         ir_type *frame = get_irg_frame_type(ssenv->cenv->irg);
625
626         /* aligning by increasing frame size */
627         offset = get_type_size_bits(frame) / 8;
628         offset = round_up2(offset, ALIGN_SPILL_AREA);
629         set_type_size_bytes(frame, -1);
630
631         /* create entities and assign offsets according to size and alignment*/
632         for (i = 0; i < n_slots; ++i) {
633                 char buf[64];
634                 ident *name;
635                 entity *spill_ent;
636                 ir_node *irn;
637
638                 /* build entity */
639                 snprintf(buf, sizeof(buf), "spill_slot_%d", i);
640                 name = new_id_from_str(buf);
641
642                 spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]));
643
644                 /* align */
645                 offset = round_up2(offset, ss[i]->align);
646                 /* set */
647                 set_entity_offset_bytes(spill_ent, offset);
648                 /* next possible offset */
649                 offset += round_up2(ss[i]->size, ss[i]->align);
650
651                 pset_foreach(ss[i]->members, irn)
652                         be_set_Spill_entity(irn, spill_ent);
653         }
654
655         /* set final size of stack frame */
656         frame_align = get_type_alignment_bytes(frame);
657         set_type_size_bytes(frame, round_up2(offset, frame_align));
658 }
659
660 void be_compute_spill_offsets(be_chordal_env_t *cenv) {
661         ss_env_t ssenv;
662         spill_slot_t **ss;
663         int ss_size;
664         pmap_entry *pme;
665
666         obstack_init(&ssenv.ob);
667         ssenv.cenv  = cenv;
668         ssenv.slots = pmap_create();
669         ssenv.types = pmap_create();
670         FIRM_DBG_REGISTER(ssenv.dbg, "ir.be.spillslots");
671
672         /* Get initial spill slots */
673         irg_walk_graph(cenv->irg, NULL, compute_spill_slots_walker, &ssenv);
674
675         /* Build an empty array for optimized spill slots */
676         ss_size = pmap_count(ssenv.slots);
677         ss = obstack_alloc(&ssenv.ob, ss_size * sizeof(*ss));
678         optimize_slots(&ssenv, ss_size, ss);
679
680         /* Integrate slots into the stack frame entity */
681         assign_entities(&ssenv, ss_size, ss);
682
683         /* Clean up */
684         pmap_foreach(ssenv.slots, pme)
685         del_pset(((spill_slot_t *)pme->value)->members);
686         pmap_destroy(ssenv.slots);
687         pmap_destroy(ssenv.types);
688         obstack_free(&ssenv.ob, NULL);
689
690         be_copy_entities_to_reloads(cenv->irg);
691 }