Changed to the new infrastructure
[libfirm] / ir / be / bespill.c
1 /**
2  * Author:      Daniel Grund
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 "debug.h"
12
13 #include "besched.h"
14 #include "bespill.h"
15 #include "benode_t.h"
16
17 typedef struct _reloader_t reloader_t;
18 typedef struct _spill_info_t spill_info_t;
19
20 struct _reloader_t {
21         reloader_t *next;
22         ir_node *reloader;
23 };
24
25 struct _spill_info_t {
26         ir_node *spilled_node;
27         reloader_t *reloaders;
28 };
29
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. */
34 } spill_ctx_t;
35
36 struct _spill_env_t {
37         firm_dbg_module_t *dbg;
38         const arch_register_class_t *cls;
39         const be_main_session_env_t *session;
40         struct obstack obst;
41         set *spill_ctxs;
42         set *spills;            /**< all spill_info_t's, which must be placed */
43 };
44
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);
49 }
50
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);
55 }
56
57 spill_env_t *be_new_spill_env(
58                 firm_dbg_module_t *dbg,
59                 const be_main_session_env_t *session,
60                 const arch_register_class_t *cls)
61 {
62         spill_env_t *env = malloc(sizeof(env[0]));
63         env->spill_ctxs = new_set(cmp_spillctx, 1024);
64         env->spills     = new_set(cmp_spillinfo, 1024);
65         env->session    = session;
66         env->cls        = cls;
67         env->dbg        = dbg;
68         obstack_init(&env->obst);
69         return env;
70 }
71
72 void be_delete_spill_env(spill_env_t *senv)
73 {
74         del_set(senv->spill_ctxs);
75         del_set(senv->spills);
76         obstack_free(&senv->obst, NULL);
77         free(senv);
78 }
79
80 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
81         spill_ctx_t templ;
82
83         templ.spilled = to_spill;
84         templ.user    = ctx_irn;
85         templ.spill   = NULL;
86
87         return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
88 }
89
90 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
91         spill_ctx_t *ctx;
92 //      DBG((dbg, DBG_SPILL, "spill_irn %+F\n", irn));
93
94         ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
95         if(!ctx->spill) {
96                 const be_main_env_t *env = senv->session->main_env;
97                 ctx->spill = be_spill(env->node_factory, env->arch_env, irn);
98         }
99
100         return ctx->spill;
101 }
102
103 /**
104  * If the first usage of a phi result would be out of memory
105  * there is no sense in allocating a register for it.
106  * Thus we spill it and all its operands to the same spill slot.
107  * Therefore the phi/dataB becomes a phi/Memory
108  */
109 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn,
110                 pset *mem_phis) {
111         int i, n = get_irn_arity(phi);
112         ir_node **ins, *bl = get_nodes_block(phi);
113         ir_graph *irg = senv->session->irg;
114         spill_ctx_t *ctx;
115
116         assert(is_Phi(phi));
117 //      DBG((dbg, DBG_SPILL, "spill_phi %+F\n", phi));
118
119         /* search an existing spill for this context */
120         ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
121
122         /* if not found spill the phi */
123         if(!ctx->spill) {
124                 /* build a new PhiM with dummy in-array */
125                 ins  = malloc(n * sizeof(ins[0]));
126                 for(i=0; i<n; ++i)
127                         ins[i] = new_r_Unknown(irg, mode_M);
128                 ctx->spill = new_r_Phi(senv->session->irg, bl, n, ins, mode_M);
129                 free(ins);
130
131                 /* re-wire the phiM */
132                 for(i=0; i<n; ++i) {
133                         ir_node *arg = get_irn_n(phi, i);
134                         ir_node *sub_res;
135
136                         if(is_Phi(arg) && pset_find_ptr(mem_phis, arg))
137                                 sub_res = be_spill_phi(senv, arg, ctx_irn, mem_phis);
138                         else
139                                 sub_res = be_spill_irn(senv, arg, ctx_irn);
140
141                         set_irn_n(ctx->spill, i, sub_res);
142                 }
143         }
144         return ctx->spill;
145 }
146
147 ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill, pset *mem_phis) {
148         ir_node *res;
149         if (pset_find_ptr(mem_phis, to_spill))
150                 res = be_spill_phi(senv, to_spill, to_spill, mem_phis);
151         else
152                 res = be_spill_irn(senv, to_spill, to_spill);
153
154         return res;
155 }
156
157 void be_insert_spills_reloads(spill_env_t *senv, pset *mem_phis, pset *reload_set)
158 {
159         ir_graph *irg = senv->session->irg;
160         ir_node *irn;
161         spill_info_t *si;
162         struct obstack ob;
163
164         obstack_init(&ob);
165
166         /* process each spilled node */
167         for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
168                 reloader_t *rld;
169                 ir_node **reloads;
170                 int n_reloads = 0;
171                 ir_mode *mode = get_irn_mode(si->spilled_node);
172
173                 /* go through all reloads for this spill */
174                 for(rld = si->reloaders; rld; rld = rld->next) {
175                         /* the spill for this reloader */
176                         ir_node *spill   = be_spill_node(senv, si->spilled_node, mem_phis);
177
178                         /* the reload */
179                         ir_node *bl      = is_Block(rld->reloader) ? rld->reloader : get_nodes_block(rld->reloader);
180                         ir_node *reload  = new_Reload(senv->session->main_env->node_factory,
181                                                                                                                 senv->cls, irg, bl, mode, spill);
182
183                         DBG((senv->dbg, LEVEL_2, " RELOADER %+F   Reload %+F of %+F\n",
184                                                 rld->reloader, reload, si->spilled_node));
185                         if(reload_set)
186                                 pset_insert_ptr(reload_set, reload);
187
188                         /* remember the reaload */
189                         obstack_ptr_grow(&ob, reload);
190                         sched_add_before(rld->reloader, reload);
191                         n_reloads++;
192                 }
193
194                 assert(n_reloads > 0);
195                 reloads = obstack_finish(&ob);
196                 be_introduce_copies_ignore(senv->session->dom_front, si->spilled_node,
197                                 n_reloads, reloads, mem_phis);
198                 obstack_free(&ob, reloads);
199         }
200
201         obstack_free(&ob, NULL);
202
203         for(irn = pset_first(mem_phis); irn; irn = pset_next(mem_phis)) {
204                 int i, n;
205                 for(i = 0, n = get_irn_arity(irn); i < n; ++i)
206                         set_irn_n(irn, i, new_r_Bad(senv->session->irg));
207                 sched_remove(irn);
208         }
209 }
210
211 void be_add_spill(spill_env_t *senv, ir_node *to_spill, ir_node *before)
212 {
213         spill_info_t templ, *res;
214         reloader_t *rel;
215
216         templ.spilled_node = to_spill;
217         templ.reloaders    = NULL;
218         res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
219
220         rel           = obstack_alloc(&senv->obst, sizeof(rel[0]));
221         rel->reloader = before;
222         rel->next     = res->reloaders;
223         res->reloaders = rel;
224 }
225
226 void be_add_spill_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos)
227 {
228         ir_node *insert_bl = get_irn_arity(bl) == 1
229                 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
230         be_add_spill(senv, to_spill, insert_bl);
231 }