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