4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 #include "iredges_t.h"
31 #include "bespillbelady.h"
33 #include "besched_t.h"
37 #include "bechordal_t.h"
45 #define DEBUG_LVL SET_LEVEL_0 //(DBG_START | DBG_DECIDE | DBG_WSETS | DBG_FIX | DBG_SPILL)
46 static firm_dbg_module_t *dbg = NULL;
48 #define MIN(a,b) (((a)<(b))?(a):(b))
49 #define SINGLE_START_PROJS
51 typedef struct _workset_t workset_t;
53 typedef struct _belady_env_t {
55 const be_node_factory_t *factory;
56 const arch_env_t *arch;
57 const arch_register_class_t *cls;
58 int n_regs; /** number of regs in this reg-class */
60 workset_t *ws; /**< the main workset used while processing a block. ob-allocated */
61 be_uses_t *uses; /**< env for the next-use magic */
62 ir_node *instr; /**< current instruction */
63 unsigned instr_nr; /**< current instruction number (relative to block start) */
64 pset *used; /**< holds the values used (so far) in the current BB */
66 spill_env_t *senv; /* see bespill.h */
67 pset *reloads; /**< all reload nodes placed */
72 int i; /**< used for iteration */
73 int len; /**< current length */
74 loc_t vals[1]; /**< inlined array of the values/distances in this working set */
77 typedef struct _block_info_t {
78 workset_t *ws_start, *ws_end;
82 * Alloc a new workset on obstack @p ob with maximum size @p max
84 static INLINE workset_t *new_workset(struct obstack *ob, belady_env_t *bel) {
86 size_t size = sizeof(*res) + (bel->n_regs-1)*sizeof(res->vals[0]);
87 res = obstack_alloc(ob, size);
93 static INLINE workset_t *workset_clone(struct obstack *ob, workset_t *ws) {
95 size_t size = sizeof(*res) + (ws->bel->n_regs-1)*sizeof(res->vals[0]);
96 res = obstack_alloc(ob, size);
97 memcpy(res, ws, size);
102 * Inserts the value @p val into the workset, iff it is not
103 * already contained. The workset must not be full.
105 static INLINE void workset_insert(workset_t *ws, ir_node *val) {
107 /* check for current regclass */
108 if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
109 DBG((dbg, DBG_DECIDE, "Dropped %+F\n", val));
113 /* check if val is already contained */
114 for(i=0; i<ws->len; ++i)
115 if (ws->vals[i].irn == val)
119 assert(ws->len < ws->bel->n_regs && "Workset already full!");
120 ws->vals[ws->len++].irn = val;
124 * Inserts all values in array @p vals of length @p cnt
125 * into the workset. There must be enough space for the
128 static INLINE void workset_bulk_insert(workset_t *ws, int cnt, ir_node **vals) {
131 for(o=0; o<cnt; ++o) {
132 ir_node *val = vals[o];
133 DBG((dbg, DBG_TRACE, "Bulk insert %+F\n", val));
134 /* check for current regclass */
135 if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
136 DBG((dbg, DBG_TRACE, "Wrong reg class\n"));
140 /* check if val is already contained */
141 for(i=0; i<ws->len; ++i)
142 if (ws->vals[i].irn == val) {
143 DBG((dbg, DBG_TRACE, "Already contained\n"));
148 assert(ws->len < ws->bel->n_regs && "Workset does not have enough room!");
149 ws->vals[ws->len++].irn = val;
150 DBG((dbg, DBG_TRACE, "Inserted\n"));
153 /*epsilon statement :)*/;
158 * Overwrites the current contents of @p ws with the
159 * locations given in @p locs
161 #define workset_bulk_fill(ws, count, locs) memcpy(&(ws)->vals[0], locs, ((ws)->len=count)*sizeof(locs[0]));
164 * Removes all entries from this workset
166 #define workset_clear(ws) (ws)->len = 0;
169 * Removes the value @p val from the workset if present.
171 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
173 for(i=0; i<ws->len; ++i)
174 if (ws->vals[i].irn == val) {
175 ws->vals[i] = ws->vals[--ws->len];
180 static INLINE int workset_contains(const workset_t *ws, const ir_node *val) {
182 for(i=0; i<ws->len; ++i)
183 if (ws->vals[i].irn == val)
188 #define workset_foreach(ws, v) for(ws->i=0; \
189 v=(ws->i < ws->len) ? ws->vals[ws->i].irn : NULL, ws->i < ws->len; \
192 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
193 #define workset_set_length(ws, length) (ws)->len = length
194 #define workset_get_length(ws) ((ws)->len)
195 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
196 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
199 static int is_mem_phi(const ir_node *irn, void *data) {
201 ir_node *blk = get_nodes_block(irn);
203 DBG((dbg, DBG_SPILL, "Is %+F a mem-phi?\n", irn));
204 sws = ((block_info_t *) get_irn_link(blk))->ws_start;
205 DBG((dbg, DBG_SPILL, " %d\n", !workset_contains(sws, irn)));
206 return !workset_contains(sws, irn);
210 * Collects all values live-in at block @p blk and all phi results in this block.
211 * Then it adds the best values (at most n_regs) to the ws.
213 static void build_start_set(belady_env_t *bel, ir_node *blk) {
214 workset_t *ws = bel->ws;
215 ir_node *irn, *first;
218 loc_t loc, *starters;
224 first = sched_first(blk);
226 sched_foreach(blk, irn)
227 if (is_Phi(irn) && arch_get_irn_reg_class(bel->arch, irn, -1) == bel->cls) {
229 loc.time = be_get_next_use(bel->uses, first, 0, irn, 0);
230 DBG((dbg, DBG_START, " %+F next-use %d\n", loc.irn, loc.time));
231 obstack_grow(&ob, &loc, sizeof(loc));
236 live_foreach(blk, li)
237 if (live_is_in(li) && arch_get_irn_reg_class(bel->arch, li->irn, -1) == bel->cls) {
238 loc.irn = (ir_node *)li->irn;
239 loc.time = be_get_next_use(bel->uses, first, 0, li->irn, 0);
240 DBG((dbg, DBG_START, " %+F next-use %d\n", loc.irn, loc.time));
241 obstack_grow(&ob, &loc, sizeof(loc));
244 starters = obstack_finish(&ob);
246 /* sort all values */
247 qsort(starters, count, sizeof(starters[0]), loc_compare);
249 /* copy the best ones to the ws */
250 count = MIN(count, ws->bel->n_regs);
251 workset_bulk_fill(ws, count, starters);
253 obstack_free(&ob, NULL);
257 * Performs the actions neccessary to grant the request that:
258 * - new_vals can be held in registers
259 * - as few as possible other values are disposed
260 * - the worst values get disposed
262 * @p is_usage indicates that the values in new_vals are used (not defined)
263 * In this case reloads must be performed
265 static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
267 int i, len, max_allowed, demand;
268 workset_t *ws = bel->ws;
269 ir_node **to_insert = alloca(bel->n_regs * sizeof(*to_insert));
272 * 1. Identify the number of needed slots and the values to reload
275 workset_foreach(new_vals, val) {
276 /* mark value as used */
278 pset_insert_ptr(bel->used, val);
280 if (!workset_contains(ws, val)) {
281 DBG((dbg, DBG_DECIDE, " insert %+F\n", val));
282 to_insert[demand++] = val;
284 be_add_reload(bel->senv, val, bel->instr);
286 DBG((dbg, DBG_DECIDE, " skip %+F\n", val));
288 DBG((dbg, DBG_DECIDE, " demand = %d\n", demand));
292 * 2. Make room for at least 'demand' slots
294 len = workset_get_length(ws);
295 max_allowed = bel->n_regs - demand;
297 /* Only make more free room if we do not have enough */
298 if (len > max_allowed) {
299 /* get current next-use distance */
300 for (i=0; i<ws->len; ++i)
301 workset_set_time(ws, i, be_get_next_use(bel->uses, bel->instr, bel->instr_nr, workset_get_val(ws, i), !is_usage));
303 /* sort entries by increasing nextuse-distance*/
306 /* Logic for not needed live-ins: If a value is disposed
307 before its first usage, remove it from start workset */
308 for (i=max_allowed; i<ws->len; ++i) {
309 ir_node *irn = ws->vals[i].irn;
310 if (!pset_find_ptr(bel->used, irn)) {
311 ir_node *curr_bb = get_nodes_block(bel->instr);
312 workset_t *ws_start = ((block_info_t *) get_irn_link(curr_bb))->ws_start;
313 workset_remove(ws_start, irn);
315 DBG((dbg, DBG_DECIDE, " dispose %+F dumb\n", irn));
317 DBG((dbg, DBG_DECIDE, " dispose %+F\n", irn));
320 /* kill the last 'demand' entries in the array */
321 workset_set_length(ws, max_allowed);
325 * 3. Insert the new values into the workset
327 workset_bulk_insert(bel->ws, demand, to_insert);
331 * For the given block @p blk, decide for each values
332 * whether it is used from a register or is reloaded
335 static void decide(ir_node *blk, void *env) {
336 belady_env_t *bel = env;
339 #ifdef SINGLE_START_PROJS
340 ir_node *start_blk = get_irg_start_block(get_irn_irg(blk));
342 block_info_t *blk_info = obstack_alloc(&bel->ob, sizeof(*blk_info));
343 set_irn_link(blk, blk_info);
345 DBG((dbg, DBG_DECIDE, "\n"));
346 DBG((dbg, DBG_DECIDE, "Decide for %+F\n", blk));
348 /* build starting-workset for this block */
349 build_start_set(bel, blk);
350 blk_info->ws_start = workset_clone(&bel->ob, bel->ws);
351 DBG((dbg, DBG_WSETS, "Initial start workset for %+F:\n", blk));
352 workset_foreach(blk_info->ws_start, irn)
353 DBG((dbg, DBG_WSETS, " %+F\n", irn));
355 /* process the block from start to end */
356 DBG((dbg, DBG_WSETS, "Processing...\n"));
357 bel->used = pset_new_ptr(32);
359 new_vals = new_workset(&bel->ob, bel);
360 sched_foreach(blk, irn) {
361 assert(workset_get_length(bel->ws) <= bel->n_regs && "Too much values in workset!");
364 #ifdef SINGLE_START_PROJS
366 (is_Proj(irn) && blk!=start_blk) ||
367 (get_irn_mode(irn) == mode_T && blk==start_blk)) {
368 DBG((dbg, DBG_DECIDE, " ...%+F skipped\n", irn));
372 /* projs are handled with the tuple value.
373 * Phis are no real instr (see insert_starters)
374 * instr_nr does not increase */
375 if (is_Proj(irn) || is_Phi(irn)) {
376 DBG((dbg, DBG_DECIDE, " ...%+F skipped\n", irn));
380 DBG((dbg, DBG_DECIDE, " ...%+F\n", irn));
382 /* set instruction in the workset */
385 /* allocate all values _used_ by this instruction */
386 workset_clear(new_vals);
387 workset_bulk_insert(new_vals, get_irn_arity(irn)+1, get_irn_in(irn));
388 displace(bel, new_vals, 1);
390 /* allocate all values _defined_ by this instruction */
391 workset_clear(new_vals);
392 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
394 for(proj=sched_next(irn); is_Proj(proj); proj=sched_next(proj))
395 workset_insert(new_vals, proj);
397 workset_insert(new_vals, irn);
399 displace(bel, new_vals, 0);
405 /* Remember end-workset for this block */
406 blk_info->ws_end = workset_clone(&bel->ob, bel->ws);
407 DBG((dbg, DBG_WSETS, "Start workset for %+F:\n", blk));
408 workset_foreach(blk_info->ws_start, irn)
409 DBG((dbg, DBG_WSETS, " %+F\n", irn));
410 DBG((dbg, DBG_WSETS, "End workset for %+F:\n", blk));
411 workset_foreach(blk_info->ws_end, irn)
412 DBG((dbg, DBG_WSETS, " %+F\n", irn));
416 * 'decide' is block-local and makes assumptions
417 * about the set of live-ins. Thus we must adapt the
418 * live-outs to the live-ins at each block-border.
420 static void fix_block_borders(ir_node *blk, void *env) {
422 belady_env_t *bel = env;
425 DBG((dbg, DBG_FIX, "\n"));
426 DBG((dbg, DBG_FIX, "Fixing %+F\n", blk));
428 wsb = ((block_info_t *)get_irn_link(blk))->ws_start;
430 /* process all pred blocks */
431 for (i=0, max=get_irn_arity(blk); i<max; ++i) {
432 ir_node *irnb, *irnp, *pred = get_Block_cfgpred_block(blk, i);
433 workset_t *wsp = ((block_info_t *)get_irn_link(pred))->ws_end;
435 DBG((dbg, DBG_FIX, " Pred %+F\n", pred));
437 workset_foreach(wsb, irnb) {
438 /* if irnb is a phi of the current block we reload
439 * the corresponding argument, else irnb itself */
440 if(is_Phi(irnb) && blk == get_nodes_block(irnb))
441 irnb = get_irn_n(irnb, i);
443 /* Unknowns are available everywhere */
444 if(get_irn_opcode(irnb) == iro_Unknown)
447 /* check if irnb is in a register at end of pred */
448 workset_foreach(wsp, irnp)
452 /* irnb is in memory at the end of pred, so we have to reload it */
453 DBG((dbg, DBG_FIX, " reload %+F\n", irnb));
454 be_add_reload_on_edge(bel->senv, irnb, blk, i);
457 /*epsilon statement :)*/;
463 * Removes all used reloads from bel->reloads.
464 * The remaining nodes in bel->reloads will be removed from the graph.
466 static void rescue_used_reloads(ir_node *irn, void *env) {
467 pset *rlds = (pset *)env;
468 if (pset_find_ptr(rlds, irn))
469 pset_remove_ptr(rlds, irn);
473 * Finds all unused reloads and remove them from the schedule
474 * Also removes spills if they are not used anymore after removing reloads
476 static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
479 irg_walk_graph(irg, rescue_used_reloads, NULL, bel->reloads);
480 for(irn = pset_first(bel->reloads); irn; irn = pset_next(bel->reloads)) {
482 DBG((dbg, DBG_SPILL, "Removing %+F before %+F in %+F\n", irn, sched_next(irn), get_nodes_block(irn)));
484 spill = get_irn_n(irn, 0);
487 set_irn_n(irn, 0, new_Bad());
490 /* if spill not used anymore, remove it too
491 * test of regclass is necessary since spill may be a phi-M */
492 if (get_irn_n_edges(spill) == 0 && bel->cls == arch_get_irn_reg_class(bel->arch, spill, -1)) {
493 set_irn_n(spill, 0, new_Bad());
499 void be_spill_belady(const be_chordal_env_t *chordal_env) {
502 dbg = firm_dbg_register("ir.be.spillbelady");
503 firm_dbg_set_mask(dbg, DEBUG_LVL);
505 /* init belady env */
506 obstack_init(&bel.ob);
507 bel.factory = chordal_env->main_env->node_factory;
508 bel.arch = chordal_env->main_env->arch_env;
509 bel.cls = chordal_env->cls;
510 bel.n_regs = arch_register_class_n_regs(bel.cls);
511 bel.ws = new_workset(&bel.ob, &bel);
512 bel.uses = be_begin_uses(chordal_env->irg, chordal_env->main_env->arch_env, bel.cls);
513 bel.senv = be_new_spill_env(dbg, chordal_env, is_mem_phi, NULL);
514 bel.reloads = pset_new_ptr_default();
517 irg_block_walk_graph(chordal_env->irg, decide, NULL, &bel);
518 irg_block_walk_graph(chordal_env->irg, fix_block_borders, NULL, &bel);
519 be_insert_spills_reloads(bel.senv, bel.reloads);
520 remove_unused_reloads(chordal_env->irg, &bel);
523 del_pset(bel.reloads);
524 be_delete_spill_env(bel.senv);
525 be_end_uses(bel.uses);
526 obstack_free(&bel.ob, NULL);