adapted (some parts) to abi changes
[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
23 #include "besched.h"
24 #include "bespill.h"
25 #include "benode_t.h"
26 #include "bechordal_t.h"
27
28 typedef struct _reloader_t reloader_t;
29 typedef struct _spill_info_t spill_info_t;
30
31 struct _reloader_t {
32         reloader_t *next;
33         ir_node *reloader;
34 };
35
36 struct _spill_info_t {
37         ir_node *spilled_node;
38         reloader_t *reloaders;
39 };
40
41 typedef struct _spill_ctx_t {
42         ir_node *spilled;  /**< The spilled node. */
43         ir_node *user;     /**< The node this spill is for. */
44         ir_node *spill;    /**< The spill itself. */
45 } spill_ctx_t;
46
47 struct _spill_env_t {
48         firm_dbg_module_t *dbg;
49         const arch_register_class_t *cls;
50         const be_chordal_env_t *chordal_env;
51         struct obstack obst;
52         set *spill_ctxs;
53         set *spills;                            /**< all spill_info_t's, which must be placed */
54         pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed seperately */
55         decide_irn_t is_mem_phi;        /**< callback func to decide if a phi needs special spilling */
56         void *data;                                     /**< data passed to all callbacks */
57 };
58
59 static int cmp_spillctx(const void *a, const void *b, size_t n) {
60         const spill_ctx_t *p = a;
61         const spill_ctx_t *q = b;
62         return !(p->user == q->user && p->spilled == q->spilled);
63 }
64
65 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
66         const spill_info_t *xx = x;
67         const spill_info_t *yy = y;
68         return ! (xx->spilled_node == yy->spilled_node);
69 }
70
71 spill_env_t *be_new_spill_env(firm_dbg_module_t *dbg,
72                                                           const be_chordal_env_t *chordal_env,
73                                                           decide_irn_t is_mem_phi, void *data) {
74
75         spill_env_t *env = malloc(sizeof(env[0]));
76         env->spill_ctxs  = new_set(cmp_spillctx, 1024);
77         env->spills      = new_set(cmp_spillinfo, 1024);
78         env->cls         = chordal_env->cls;
79         env->dbg         = dbg;
80         env->is_mem_phi  = is_mem_phi;
81         env->data        = data;
82         env->chordal_env = chordal_env;
83         obstack_init(&env->obst);
84         return env;
85 }
86
87 void be_delete_spill_env(spill_env_t *senv) {
88         del_set(senv->spill_ctxs);
89         del_set(senv->spills);
90         obstack_free(&senv->obst, NULL);
91         free(senv);
92 }
93
94 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
95         spill_ctx_t templ;
96
97         templ.spilled = to_spill;
98         templ.user    = ctx_irn;
99         templ.spill   = NULL;
100
101         return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
102 }
103
104 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
105         spill_ctx_t *ctx;
106         DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", irn, ctx_irn));
107
108         ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
109         if(!ctx->spill) {
110                 const be_main_env_t *env = senv->chordal_env->birg->main_env;
111                 ctx->spill = be_spill(env->arch_env, irn, ctx_irn);
112         }
113
114         return ctx->spill;
115 }
116
117 /**
118  * If the first usage of a phi result would be out of memory
119  * there is no sense in allocating a register for it.
120  * Thus we spill it and all its operands to the same spill slot.
121  * Therefore the phi/dataB becomes a phi/Memory
122  */
123 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn) {
124         int i, n = get_irn_arity(phi);
125         ir_node **ins, *bl = get_nodes_block(phi);
126         ir_graph *irg = senv->chordal_env->irg;
127         spill_ctx_t *ctx;
128
129         assert(is_Phi(phi));
130         DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", phi, ctx_irn));
131
132         /* search an existing spill for this context */
133         ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
134
135         /* if not found spill the phi */
136         if(!ctx->spill) {
137                 /* build a new PhiM with dummy in-array */
138                 ins  = malloc(n * sizeof(ins[0]));
139                 for(i=0; i<n; ++i)
140                         ins[i] = new_r_Unknown(irg, mode_M);
141                 ctx->spill = new_r_Phi(senv->chordal_env->irg, bl, n, ins, mode_M);
142                 free(ins);
143
144                 /* re-wire the phiM */
145                 for(i=0; i<n; ++i) {
146                         ir_node *arg = get_irn_n(phi, i);
147                         ir_node *sub_res;
148
149                         if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg))
150                                 sub_res = be_spill_phi(senv, arg, ctx_irn);
151                         else
152                                 sub_res = be_spill_irn(senv, arg, ctx_irn);
153
154                         set_irn_n(ctx->spill, i, sub_res);
155                 }
156         }
157         return ctx->spill;
158 }
159
160 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
161         ir_node *res;
162         if (pset_find_ptr(senv->mem_phis, to_spill))
163                 res = be_spill_phi(senv, to_spill, to_spill);
164         else
165                 res = be_spill_irn(senv, to_spill, to_spill);
166
167         return res;
168 }
169
170 static void phi_walker(ir_node *irn, void *env) {
171         spill_env_t *senv = env;
172         const arch_env_t *arch = senv->chordal_env->birg->main_env->arch_env;
173
174         if (is_Phi(irn) && arch_irn_has_reg_class(arch, irn, 0, senv->cls)
175                         && senv->is_mem_phi(irn, senv->data)) {
176                 DBG((senv->dbg, LEVEL_1, "  %+F\n", irn));
177                 pset_insert_ptr(senv->mem_phis, irn);
178         }
179 }
180
181 void be_insert_spills_reloads(spill_env_t *senv, pset *reload_set) {
182         const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
183         ir_graph *irg          = senv->chordal_env->irg;
184         ir_node *irn;
185         spill_info_t *si;
186         struct obstack ob;
187
188         obstack_init(&ob);
189
190         /* get all special spilled phis */
191         DBG((senv->dbg, LEVEL_1, "Mem-phis:\n"));
192         senv->mem_phis = pset_new_ptr_default();
193         irg_walk_graph(senv->chordal_env->irg, phi_walker, NULL, senv);
194
195         /* Add reloads for mem_phis */
196         /* BETTER: These reloads (1) should only be inserted, if they are really needed */
197         DBG((senv->dbg, LEVEL_1, "Reloads for mem-phis:\n"));
198         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
199                 const ir_edge_t *e;
200                 DBG((senv->dbg, LEVEL_1, " Mem-phi %+F\n", irn));
201                 foreach_out_edge(irn, e) {
202                         ir_node *user = e->src;
203                         if (is_Phi(user) && !pset_find_ptr(senv->mem_phis, user)) {
204                                         ir_node *use_bl = get_nodes_block(user);
205                                         DBG((senv->dbg, LEVEL_1, " non-mem-phi user %+F\n", user));
206                                         be_add_reload_on_edge(senv, irn, use_bl, e->pos); /* (1) */
207                         }
208                 }
209         }
210
211         /* process each spilled node */
212         DBG((senv->dbg, LEVEL_1, "Insert spills and reloads:\n"));
213         for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
214                 reloader_t *rld;
215                 ir_node **reloads;
216                 int n_reloads = 0;
217                 ir_mode *mode = get_irn_mode(si->spilled_node);
218
219                 /* go through all reloads for this spill */
220                 for(rld = si->reloaders; rld; rld = rld->next) {
221                         /* the spill for this reloader */
222                         ir_node *spill   = be_spill_node(senv, si->spilled_node);
223
224                         /* the reload */
225                         ir_node *reload  = be_reload(aenv, senv->cls, rld->reloader, mode, spill);
226
227                         DBG((senv->dbg, LEVEL_1, " %+F of %+F before %+F\n", reload, si->spilled_node, rld->reloader));
228                         if(reload_set)
229                                 pset_insert_ptr(reload_set, reload);
230
231                         /* remember the reload */
232                         obstack_ptr_grow(&ob, reload);
233                         n_reloads++;
234                 }
235
236                 assert(n_reloads > 0);
237                 obstack_ptr_grow(&ob, si->spilled_node);
238                 reloads = obstack_finish(&ob);
239                 be_ssa_constr_ignore(senv->chordal_env->dom_front, n_reloads + 1, reloads, senv->mem_phis);
240                 obstack_free(&ob, reloads);
241         }
242
243         obstack_free(&ob, NULL);
244
245         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
246                 int i, n;
247                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
248                         set_irn_n(irn, i, new_r_Bad(senv->chordal_env->irg));
249                 sched_remove(irn);
250         }
251
252         del_pset(senv->mem_phis);
253 }
254
255 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
256         spill_info_t templ, *res;
257         reloader_t *rel;
258
259         templ.spilled_node = to_spill;
260         templ.reloaders    = NULL;
261         res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
262
263         rel           = obstack_alloc(&senv->obst, sizeof(rel[0]));
264         rel->reloader = before;
265         rel->next     = res->reloaders;
266         res->reloaders = rel;
267 }
268
269 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
270         ir_node *insert_bl = get_irn_arity(bl) == 1 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
271         be_add_reload(senv, to_spill, insert_bl);
272 }
273
274
275
276 /****************************************
277
278         SPILL SLOT MANAGEMENT AND OPTS
279
280 ****************************************/
281
282 typedef struct _spill_slot_t {
283         unsigned size;
284         unsigned align;
285         pset *members;
286         ir_mode *largest_mode;  /* the mode of all members with largest size */
287 } spill_slot_t;
288
289 typedef struct _ss_env_t {
290         firm_dbg_module_t *dbg;
291         struct obstack ob;
292         be_chordal_env_t *cenv;
293         pmap *slots;            /* maps spill_contexts to spill_slots */
294   pmap *types;    /* maps modes to types */
295 } ss_env_t;
296
297
298 static void compute_spill_slots_walker(ir_node *spill, void *env) {
299         ss_env_t *ssenv = env;
300         ir_node *ctx;
301         pmap_entry *entry;
302         spill_slot_t *ss;
303
304         if (!be_is_Spill(spill))
305                 return;
306
307         /* check, if this spill is for a context already known */
308         ctx = be_get_Spill_context(spill);
309         entry = pmap_find(ssenv->slots, ctx);
310
311         if (!entry) {
312                 /* this is a new spill context */
313                 ss = obstack_alloc(&ssenv->ob, sizeof(*ss));
314                 ss->members = pset_new_ptr(8);
315                 ss->largest_mode = get_irn_mode(get_irn_n(spill, be_pos_Spill_val));
316                 ss->size = get_mode_size_bytes(ss->largest_mode);
317                 ss->align = ss->size; /* TODO Assumed for now */
318                 pmap_insert(ssenv->slots, ctx, ss);
319         } else {
320                 ir_node *irn;
321                 /* values with the same spill_ctx must go into the same spill slot */
322                 ss = entry->value;
323                 assert(ss->size == (unsigned)get_mode_size_bytes(get_irn_mode(get_irn_n(spill, be_pos_Spill_val))) && "Different sizes for the same spill slot are not allowed yet.");
324                 for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
325                         /* use values_interfere here, because it uses the dominance check,
326                            which does work for values in memory */
327                         assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
328                 }
329         }
330
331         pset_insert_ptr(ss->members, spill);
332 }
333
334 static int ss_sorter(const void *v1, const void *v2) {
335         const spill_slot_t *ss1 = v1;
336         const spill_slot_t *ss2 = v2;
337         return ((int) ss2->size) - ((int) ss1->size);
338 }
339
340
341 /**
342  * This function should optimize the spill slots.
343  *  - Coalescing of multiple slots
344  *  - Ordering the slots
345  *
346  * Input slots are in @p ssenv->slots
347  * @p size The count of initial spill slots in @p ssenv->slots
348  *         This also is the size of the preallocated array @p ass
349  *
350  * @return An array of spill slots @p ass in specific order
351  **/
352 static void optimize_slots(ss_env_t *ssenv, int size, spill_slot_t **ass) {
353         int i, o, used_slots;
354         pmap_entry *entr;
355
356         i=0;
357         pmap_foreach(ssenv->slots, entr)
358                 ass[i++] = entr->value;
359
360         /* Sort the array to minimize fragmentation and cache footprint.
361            Large slots come first */
362         qsort(ass, size, sizeof(ass[0]), ss_sorter);
363
364         /* For each spill slot:
365                 - assign a new offset to this slot
366             - xor find another slot to coalesce with */
367         used_slots = 0;
368         for (i=0; i<size; ++i) { /* for each spill slot */
369                 ir_node *n1;
370                 int tgt_slot = -1;
371
372                 DBG((ssenv->dbg, LEVEL_1, "Spill slot %d members:\n", i));
373                 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
374                         DBG((ssenv->dbg, LEVEL_1, "  %+F\n", n1));
375
376
377                 for (o=0; o < used_slots && tgt_slot == -1; ++o) { /* for each offset-assigned spill slot */
378                         /* check inter-slot-pairs for interference */
379                         ir_node *n2;
380                         for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
381                                 for(n2 = pset_first(ass[o]->members); n2; n2 = pset_next(ass[o]->members))
382                                         if(values_interfere(n1, n2)) {
383                                                 pset_break(ass[i]->members);
384                                                 pset_break(ass[o]->members);
385                                                 DBG((ssenv->dbg, LEVEL_1, "    Interf %+F -- %+F\n", n1, n2));
386                                                 goto interf_detected;
387                                         }
388
389                         /* if we are here, there is no interference between ass[i] and ass[o] */
390                         tgt_slot = o;
391
392 interf_detected: /*nothing*/ ;
393                 }
394
395                 /* now the members of ass[i] join the members of ass[tgt_slot] */
396
397                 /* do we need a new slot? */
398                 if (tgt_slot == -1) {
399                         tgt_slot = used_slots;
400                         used_slots++;
401
402                         /* init slot */
403                         if (tgt_slot != i) {
404                                 ass[tgt_slot]->size = ass[i]->size;
405                                 del_pset(ass[tgt_slot]->members);
406                                 ass[tgt_slot]->members = pset_new_ptr(8);
407                         }
408                 }
409
410                 /* copy the members to the target pset */
411                 /* NOTE: If src and tgt pset are the same, inserting while iterating is not allowed */
412                 if (tgt_slot != i)
413                         for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
414                                         pset_insert_ptr(ass[tgt_slot]->members, n1);
415         }
416 }
417
418 #define ALIGN_SPILL_AREA 16
419 #define pset_foreach(pset, elm)  for(elm=pset_first(pset); elm; elm=pset_next(pset))
420
421 /**
422  * Returns a spill type for a mode. Keep them in a map to reduce
423  * the number of types.
424  */
425 static ir_type *get_spill_type(pmap *types, ir_mode *mode) {
426   pmap_entry *e = pmap_find(types, mode);
427   ir_type *res;
428
429   if (! e) {
430                 char buf[64];
431     snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(mode));
432     res = new_type_primitive(new_id_from_str(buf), mode);
433     pmap_insert(types, mode, res);
434   }
435   else
436     res = e->value;
437   return res;
438 }
439
440 static void assign_entities(ss_env_t *ssenv, int n, spill_slot_t **ss) {
441         int i, offset;
442         ir_type *frame = get_irg_frame_type(ssenv->cenv->irg);
443
444         /* aligning by increasing frame size */
445         offset = get_type_size_bits(frame) / 8;
446         offset = round_up2(offset, ALIGN_SPILL_AREA);
447         set_type_size_bytes(frame, -1);
448
449         /* create entities and assign offsets according to size and alignment*/
450         for (i=0; i<n; ++i) {
451                 char buf[64];
452                 ident *name;
453                 entity *spill_ent;
454                 ir_node *irn;
455
456                 /* build entity */
457                 snprintf(buf, sizeof(buf), "spill_slot_%d", i);
458                 name = new_id_from_str(buf);
459
460                 spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]->largest_mode));
461
462                 /* align */
463                 offset = round_up2(offset, ss[i]->align);
464                 /* set */
465                 set_entity_offset_bytes(spill_ent, offset);
466                 /* next possible offset */
467                 offset += ss[i]->size;
468
469                 pset_foreach(ss[i]->members, irn)
470                         be_set_Spill_entity(irn, spill_ent);
471         }
472
473         /* set final size of stack frame */
474         set_type_size_bytes(frame, offset);
475 }
476
477 void be_compute_spill_offsets(be_chordal_env_t *cenv) {
478         ss_env_t ssenv;
479         spill_slot_t **ss;
480         int ss_size;
481         pmap_entry *pme;
482
483         obstack_init(&ssenv.ob);
484         ssenv.cenv  = cenv;
485         ssenv.slots = pmap_create();
486         ssenv.types = pmap_create();
487         ssenv.dbg   = firm_dbg_register("ir.be.spillslots");
488
489         /* Get initial spill slots */
490         irg_walk_graph(cenv->irg, NULL, compute_spill_slots_walker, &ssenv);
491
492         /* Build an empty array for optimized spill slots */
493         ss_size = pmap_count(ssenv.slots);
494         ss = obstack_alloc(&ssenv.ob, ss_size * sizeof(*ss));
495         optimize_slots(&ssenv, ss_size, ss);
496
497         /* Integrate slots into the stack frame entity */
498         assign_entities(&ssenv, ss_size, ss);
499
500         /* Clean up */
501         pmap_foreach(ssenv.slots, pme)
502         del_pset(((spill_slot_t *)pme->value)->members);
503         pmap_destroy(ssenv.slots);
504         pmap_destroy(ssenv.types);
505         obstack_free(&ssenv.ob, NULL);
506
507         be_copy_entities_to_reloads(cenv->irg);
508 }