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