2 * Author: Daniel Grund, Sebastian Hack
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 typedef struct _reloader_t reloader_t;
18 typedef struct _spill_info_t spill_info_t;
25 struct _spill_info_t {
26 ir_node *spilled_node;
27 reloader_t *reloaders;
30 typedef struct _spill_ctx_t {
31 ir_node *spilled; /**< The spilled node. */
32 ir_node *user; /**< The node this spill is for. */
33 ir_node *spill; /**< The spill itself. */
37 firm_dbg_module_t *dbg;
38 const arch_register_class_t *cls;
39 const be_main_session_env_t *session;
42 set *spills; /**< all spill_info_t's, which must be placed */
45 static int cmp_spillctx(const void *a, const void *b, size_t n) {
46 const spill_ctx_t *p = a;
47 const spill_ctx_t *q = b;
48 return !(p->user == q->user && p->spilled == q->spilled);
51 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
52 const spill_info_t *xx = x;
53 const spill_info_t *yy = y;
54 return ! (xx->spilled_node == yy->spilled_node);
57 spill_env_t *be_new_spill_env(firm_dbg_module_t *dbg,
58 const be_main_session_env_t *session, const arch_register_class_t *cls) {
59 spill_env_t *env = malloc(sizeof(env[0]));
60 env->spill_ctxs = new_set(cmp_spillctx, 1024);
61 env->spills = new_set(cmp_spillinfo, 1024);
62 env->session = session;
65 obstack_init(&env->obst);
69 void be_delete_spill_env(spill_env_t *senv) {
70 del_set(senv->spill_ctxs);
71 del_set(senv->spills);
72 obstack_free(&senv->obst, NULL);
76 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
79 templ.spilled = to_spill;
83 return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
86 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
88 DBG((senv->dbg, LEVEL_1, "spill_irn %+F\n", irn));
90 ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
92 const be_main_env_t *env = senv->session->main_env;
93 ctx->spill = be_spill(env->node_factory, env->arch_env, irn);
100 * If the first usage of a phi result would be out of memory
101 * there is no sense in allocating a register for it.
102 * Thus we spill it and all its operands to the same spill slot.
103 * Therefore the phi/dataB becomes a phi/Memory
105 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn,
107 int i, n = get_irn_arity(phi);
108 ir_node **ins, *bl = get_nodes_block(phi);
109 ir_graph *irg = senv->session->irg;
113 DBG((senv->dbg, LEVEL_1, "spill_phi %+F\n", phi));
115 /* search an existing spill for this context */
116 ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
118 /* if not found spill the phi */
120 /* build a new PhiM with dummy in-array */
121 ins = malloc(n * sizeof(ins[0]));
123 ins[i] = new_r_Unknown(irg, mode_M);
124 ctx->spill = new_r_Phi(senv->session->irg, bl, n, ins, mode_M);
127 /* re-wire the phiM */
129 ir_node *arg = get_irn_n(phi, i);
132 if(is_Phi(arg) && pset_find_ptr(mem_phis, arg))
133 sub_res = be_spill_phi(senv, arg, ctx_irn, mem_phis);
135 sub_res = be_spill_irn(senv, arg, ctx_irn);
137 set_irn_n(ctx->spill, i, sub_res);
143 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill, pset *mem_phis) {
145 if (pset_find_ptr(mem_phis, to_spill))
146 res = be_spill_phi(senv, to_spill, to_spill, mem_phis);
148 res = be_spill_irn(senv, to_spill, to_spill);
153 void be_insert_spills_reloads(spill_env_t *senv, pset *reload_set, decide_irn_t is_mem_phi, void *data) {
154 ir_graph *irg = senv->session->irg;
158 pset *mem_phis = pset_new_ptr_default();
162 /* get all special spilled phis */
163 for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
164 irn = si->spilled_node;
165 if (is_Phi(irn) && is_mem_phi(irn, data))
166 pset_insert_ptr(mem_phis, irn);
169 /* process each spilled node */
170 for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
174 ir_mode *mode = get_irn_mode(si->spilled_node);
176 /* go through all reloads for this spill */
177 for(rld = si->reloaders; rld; rld = rld->next) {
178 /* the spill for this reloader */
179 ir_node *spill = be_spill_node(senv, si->spilled_node, mem_phis);
182 ir_node *bl = is_Block(rld->reloader) ? rld->reloader : get_nodes_block(rld->reloader);
183 ir_node *reload = new_Reload(senv->session->main_env->node_factory,
184 senv->cls, irg, bl, mode, spill);
186 DBG((senv->dbg, LEVEL_2, " RELOADER %+F Reload %+F of %+F\n",
187 rld->reloader, reload, si->spilled_node));
189 pset_insert_ptr(reload_set, reload);
191 /* remember the reaload */
192 obstack_ptr_grow(&ob, reload);
193 sched_add_before(rld->reloader, reload);
197 assert(n_reloads > 0);
198 reloads = obstack_finish(&ob);
199 be_introduce_copies_ignore(senv->session->dom_front, si->spilled_node,
200 n_reloads, reloads, mem_phis);
201 obstack_free(&ob, reloads);
204 obstack_free(&ob, NULL);
206 for(irn = pset_first(mem_phis); irn; irn = pset_next(mem_phis)) {
208 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
209 set_irn_n(irn, i, new_r_Bad(senv->session->irg));
216 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
217 spill_info_t templ, *res;
220 templ.spilled_node = to_spill;
221 templ.reloaders = NULL;
222 res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
224 rel = obstack_alloc(&senv->obst, sizeof(rel[0]));
225 rel->reloader = before;
226 rel->next = res->reloaders;
227 res->reloaders = rel;
230 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
231 ir_node *insert_bl = get_irn_arity(bl) == 1
232 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
233 be_add_reload(senv, to_spill, insert_bl);