bugfixes
[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
8 #include "pset.h"
9 #include "irnode_t.h"
10 #include "ircons_t.h"
11 #include "iredges_t.h"
12 #include "debug.h"
13
14 #include "besched.h"
15 #include "bespill.h"
16 #include "benode_t.h"
17
18 typedef struct _reloader_t reloader_t;
19 typedef struct _spill_info_t spill_info_t;
20
21 struct _reloader_t {
22         reloader_t *next;
23         ir_node *reloader;
24 };
25
26 struct _spill_info_t {
27         ir_node *spilled_node;
28         reloader_t *reloaders;
29 };
30
31 typedef struct _spill_ctx_t {
32         ir_node *spilled;  /**< The spilled node. */
33         ir_node *user;     /**< The node this spill is for. */
34         ir_node *spill;    /**< The spill itself. */
35 } spill_ctx_t;
36
37 struct _spill_env_t {
38         firm_dbg_module_t *dbg;
39         const arch_register_class_t *cls;
40         const be_main_session_env_t *session;
41         struct obstack obst;
42         set *spill_ctxs;
43         set *spills;                            /**< all spill_info_t's, which must be placed */
44         pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed seperately */
45         decide_irn_t is_mem_phi;        /**< callback func to decide if a phi needs special spilling */
46         void *data;                                     /**< data passed to all callbacks */
47 };
48
49 static int cmp_spillctx(const void *a, const void *b, size_t n) {
50         const spill_ctx_t *p = a;
51         const spill_ctx_t *q = b;
52         return !(p->user == q->user && p->spilled == q->spilled);
53 }
54
55 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
56         const spill_info_t *xx = x;
57         const spill_info_t *yy = y;
58         return ! (xx->spilled_node == yy->spilled_node);
59 }
60
61 spill_env_t *be_new_spill_env(firm_dbg_module_t *dbg, const be_main_session_env_t *session,
62                 const arch_register_class_t *cls, decide_irn_t is_mem_phi, void *data) {
63         spill_env_t *env = malloc(sizeof(env[0]));
64         env->spill_ctxs = new_set(cmp_spillctx, 1024);
65         env->spills     = new_set(cmp_spillinfo, 1024);
66         env->session    = session;
67         env->cls        = cls;
68         env->dbg        = dbg;
69         env->is_mem_phi = is_mem_phi;
70         env->data       = data;
71         obstack_init(&env->obst);
72         return env;
73 }
74
75 void be_delete_spill_env(spill_env_t *senv) {
76         del_set(senv->spill_ctxs);
77         del_set(senv->spills);
78         obstack_free(&senv->obst, NULL);
79         free(senv);
80 }
81
82 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
83         spill_ctx_t templ;
84
85         templ.spilled = to_spill;
86         templ.user    = ctx_irn;
87         templ.spill   = NULL;
88
89         return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
90 }
91
92 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
93         spill_ctx_t *ctx;
94         DBG((senv->dbg, LEVEL_1, "%+F\n", irn));
95
96         ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
97         if(!ctx->spill) {
98                 const be_main_env_t *env = senv->session->main_env;
99                 ctx->spill = be_spill(env->node_factory, env->arch_env, irn);
100         }
101
102         return ctx->spill;
103 }
104
105 /**
106  * If the first usage of a phi result would be out of memory
107  * there is no sense in allocating a register for it.
108  * Thus we spill it and all its operands to the same spill slot.
109  * Therefore the phi/dataB becomes a phi/Memory
110  */
111 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn) {
112         int i, n = get_irn_arity(phi);
113         ir_node **ins, *bl = get_nodes_block(phi);
114         ir_graph *irg = senv->session->irg;
115         spill_ctx_t *ctx;
116
117         assert(is_Phi(phi));
118         DBG((senv->dbg, LEVEL_1, "%+F\n", phi));
119
120         /* search an existing spill for this context */
121         ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
122
123         /* if not found spill the phi */
124         if(!ctx->spill) {
125                 /* build a new PhiM with dummy in-array */
126                 ins  = malloc(n * sizeof(ins[0]));
127                 for(i=0; i<n; ++i)
128                         ins[i] = new_r_Unknown(irg, mode_M);
129                 ctx->spill = new_r_Phi(senv->session->irg, bl, n, ins, mode_M);
130                 free(ins);
131
132                 /* re-wire the phiM */
133                 for(i=0; i<n; ++i) {
134                         ir_node *arg = get_irn_n(phi, i);
135                         ir_node *sub_res;
136
137                         if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg))
138                                 sub_res = be_spill_phi(senv, arg, ctx_irn);
139                         else
140                                 sub_res = be_spill_irn(senv, arg, ctx_irn);
141
142                         set_irn_n(ctx->spill, i, sub_res);
143                 }
144         }
145         return ctx->spill;
146 }
147
148 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
149         ir_node *res;
150         if (pset_find_ptr(senv->mem_phis, to_spill))
151                 res = be_spill_phi(senv, to_spill, to_spill);
152         else
153                 res = be_spill_irn(senv, to_spill, to_spill);
154
155         return res;
156 }
157
158 static void phi_walker(ir_node *irn, void *env) {
159         spill_env_t *senv = env;
160         const arch_env_t *arch = senv->session->main_env->arch_env;
161
162         if (is_Phi(irn) && arch_irn_has_reg_class(arch, irn, 0, senv->cls)
163                         && senv->is_mem_phi(irn, senv->data)) {
164                 DBG((senv->dbg, LEVEL_1, "  %+F\n", irn));
165                 pset_insert_ptr(senv->mem_phis, irn);
166         }
167 }
168
169 void be_insert_spills_reloads(spill_env_t *senv, pset *reload_set) {
170         ir_graph *irg = senv->session->irg;
171         ir_node *irn;
172         spill_info_t *si;
173         struct obstack ob;
174
175         obstack_init(&ob);
176
177         /* get all special spilled phis */
178         DBG((senv->dbg, LEVEL_1, "Mem-phis:\n"));
179         senv->mem_phis = pset_new_ptr_default();
180         irg_walk_graph(senv->session->irg, phi_walker, NULL, senv);
181
182         /* Add reloads for mem_phis */
183         DBG((senv->dbg, LEVEL_1, "Reloads for mem-phis:\n"));
184         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
185                 const ir_edge_t *e;
186                 DBG((senv->dbg, LEVEL_1, " Mem-phi %+F\n", irn));
187                 foreach_out_edge(irn, e) {
188                         ir_node *user = e->src;
189                         if (is_Phi(user) && !pset_find_ptr(senv->mem_phis, user)) {
190                                         DBG((senv->dbg, LEVEL_1, " non-mem-phi user %+F\n", user));
191                                         ir_node *use_bl = get_nodes_block(user);
192                                         be_add_reload_on_edge(senv, irn, use_bl, e->pos);
193                         }
194                 }
195         }
196
197         /* process each spilled node */
198         DBG((senv->dbg, LEVEL_1, "Insert spills and reloads:\n"));
199         for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
200                 reloader_t *rld;
201                 ir_node **reloads;
202                 int n_reloads = 0;
203                 ir_mode *mode = get_irn_mode(si->spilled_node);
204
205                 /* go through all reloads for this spill */
206                 for(rld = si->reloaders; rld; rld = rld->next) {
207                         /* the spill for this reloader */
208                         ir_node *spill   = be_spill_node(senv, si->spilled_node);
209
210                         /* the reload */
211                         ir_node *bl      = is_Block(rld->reloader) ? rld->reloader : get_nodes_block(rld->reloader);
212                         ir_node *reload  = new_Reload(senv->session->main_env->node_factory,
213                                                                                                                 senv->cls, irg, bl, mode, spill);
214
215                         DBG((senv->dbg, LEVEL_1, " %+F of %+F before %+F\n", reload, si->spilled_node, rld->reloader));
216                         if(reload_set)
217                                 pset_insert_ptr(reload_set, reload);
218
219                         /* remember the reaload */
220                         obstack_ptr_grow(&ob, reload);
221                         sched_add_before(rld->reloader, reload);
222                         n_reloads++;
223                 }
224
225                 assert(n_reloads > 0);
226                 reloads = obstack_finish(&ob);
227                 be_introduce_copies_ignore(senv->session->dom_front, si->spilled_node,
228                                 n_reloads, reloads, senv->mem_phis);
229                 obstack_free(&ob, reloads);
230         }
231
232         obstack_free(&ob, NULL);
233
234         for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
235                 int i, n;
236                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
237                         set_irn_n(irn, i, new_r_Bad(senv->session->irg));
238                 sched_remove(irn);
239         }
240
241         del_pset(senv->mem_phis);
242 }
243
244 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
245         spill_info_t templ, *res;
246         reloader_t *rel;
247
248         assert(get_irn_opcode(to_spill) != iro_Unknown);
249
250         templ.spilled_node = to_spill;
251         templ.reloaders    = NULL;
252         res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
253
254         rel           = obstack_alloc(&senv->obst, sizeof(rel[0]));
255         rel->reloader = before;
256         rel->next     = res->reloaders;
257         res->reloaders = rel;
258 }
259
260 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
261         ir_node *insert_bl = get_irn_arity(bl) == 1 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
262         be_add_reload(senv, to_spill, insert_bl);
263 }