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