4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * NOTE: Comments my be (partially) wrong, since there was a major bug
8 * (spilling of phis, prespill) whose fixing changed a lot.
29 #include "iredges_t.h"
34 #include "bespillbelady.h"
36 #include "besched_t.h"
40 #include "bechordal_t.h"
49 #define DEBUG_LVL 0 //(DBG_START | DBG_DECIDE | DBG_WSETS | DBG_FIX | DBG_SPILL)
50 static firm_dbg_module_t *dbg = NULL;
52 #define MIN(a,b) (((a)<(b))?(a):(b))
53 #undef SINGLE_START_PROJS
55 typedef struct _workset_t workset_t;
57 typedef struct _belady_env_t {
59 const arch_env_t *arch;
60 const arch_register_class_t *cls;
61 int n_regs; /** number of regs in this reg-class */
63 workset_t *ws; /**< the main workset used while processing a block. ob-allocated */
64 be_uses_t *uses; /**< env for the next-use magic */
65 ir_node *instr; /**< current instruction */
66 unsigned instr_nr; /**< current instruction number (relative to block start) */
67 pset *used; /**< holds the values used (so far) in the current BB */
68 pset *copies; /**< holds all copies placed due to phi-spilling */
70 spill_env_t *senv; /* see bespill.h */
71 pset *reloads; /**< all reload nodes placed */
76 int i; /**< used for iteration TODO remove this form the struct */
77 int len; /**< current length */
78 loc_t vals[1]; /**< inlined array of the values/distances in this working set */
81 typedef struct _block_info_t {
82 workset_t *ws_start, *ws_end;
86 * Alloc a new workset on obstack @p ob with maximum size @p max
88 static INLINE workset_t *new_workset(struct obstack *ob, belady_env_t *bel) {
90 size_t size = sizeof(*res) + (bel->n_regs-1)*sizeof(res->vals[0]);
91 res = obstack_alloc(ob, size);
98 * Alloc a new instance on obstack and make it equal to @param ws
100 static INLINE workset_t *workset_clone(struct obstack *ob, workset_t *ws) {
102 size_t size = sizeof(*res) + (ws->bel->n_regs-1)*sizeof(res->vals[0]);
103 res = obstack_alloc(ob, size);
104 memcpy(res, ws, size);
109 * Do NOT alloc anything. Make @param tgt equal to @param src.
110 * returns @param tgt for convinience
112 static INLINE workset_t *workset_copy(workset_t *tgt, workset_t *src) {
113 size_t size = sizeof(*src) + (src->bel->n_regs-1)*sizeof(src->vals[0]);
114 memcpy(tgt, src, size);
119 * Overwrites the current content array of @param ws with the
120 * @param count locations given at memory @param locs.
121 * Set the length of @param ws to count.
123 #define workset_bulk_fill(ws, count, locs) memcpy(&(ws)->vals[0], locs, ((ws)->len=count)*sizeof(locs[0]));
127 * Inserts the value @p val into the workset, iff it is not
128 * already contained. The workset must not be full.
130 static INLINE void workset_insert(workset_t *ws, ir_node *val) {
132 /* check for current regclass */
133 if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
134 DBG((dbg, DBG_DECIDE, "Dropped %+F\n", val));
138 /* check if val is already contained */
139 for(i=0; i<ws->len; ++i)
140 if (ws->vals[i].irn == val)
144 assert(ws->len < ws->bel->n_regs && "Workset already full!");
145 ws->vals[ws->len++].irn = val;
149 * Inserts all values in array @p vals of length @p cnt
150 * into the workset. There must be enough space for the
153 static INLINE void workset_bulk_insert(workset_t *ws, int cnt, ir_node **vals) {
156 for(o=0; o<cnt; ++o) {
157 ir_node *val = vals[o];
158 DBG((dbg, DBG_TRACE, "Bulk insert %+F\n", val));
159 /* check for current regclass */
160 if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
161 DBG((dbg, DBG_TRACE, "Wrong reg class\n"));
165 /* check if val is already contained */
166 for(i=0; i<ws->len; ++i)
167 if (ws->vals[i].irn == val) {
168 DBG((dbg, DBG_TRACE, "Already contained\n"));
173 assert(ws->len < ws->bel->n_regs && "Workset does not have enough room!");
174 ws->vals[ws->len++].irn = val;
175 DBG((dbg, DBG_TRACE, "Inserted\n"));
178 /*epsilon statement :)*/;
183 * Removes all entries from this workset
185 #define workset_clear(ws) (ws)->len = 0;
188 * Removes the value @p val from the workset if present.
190 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
192 for(i=0; i<ws->len; ++i)
193 if (ws->vals[i].irn == val) {
194 ws->vals[i] = ws->vals[--ws->len];
199 static INLINE int workset_contains(const workset_t *ws, const ir_node *val) {
201 for(i=0; i<ws->len; ++i)
202 if (ws->vals[i].irn == val)
207 #define workset_foreach(ws, v) for(ws->i=0; \
208 v=(ws->i < ws->len) ? ws->vals[ws->i].irn : NULL, ws->i < ws->len; \
211 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
212 #define workset_set_length(ws, length) (ws)->len = length
213 #define workset_get_length(ws) ((ws)->len)
214 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
215 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
218 static int is_mem_phi(const ir_node *irn, void *data) {
220 ir_node *blk = get_nodes_block(irn);
222 DBG((dbg, DBG_SPILL, "Is %+F a mem-phi?\n", irn));
223 sws = ((block_info_t *) get_irn_link(blk))->ws_start;
224 DBG((dbg, DBG_SPILL, " %d\n", !workset_contains(sws, irn)));
225 return !workset_contains(sws, irn);
229 * Collects all values live-in at block @p blk and all phi results in this block.
230 * Then it adds the best values (at most n_regs) to the blocks start_workset.
231 * The phis among the remaining values get spilled: Introduce psudo-copies of
232 * their args to break interference and make it possible to spill them to the
235 static void compute_block_start_info(ir_node *blk, void *env) {
236 belady_env_t *bel = env;
237 block_info_t *blk_info;
238 ir_node *irn, *first;
240 int i, count, ws_count;
241 loc_t loc, *starters;
242 ir_graph *irg = get_irn_irg(blk);
247 /* Get all values living at the block start */
248 DBG((dbg, DBG_START, "Living at start of %+F:\n", blk));
249 first = sched_first(blk);
251 sched_foreach(blk, irn)
252 if (is_Phi(irn) && arch_get_irn_reg_class(bel->arch, irn, -1) == bel->cls) {
254 loc.time = be_get_next_use(bel->uses, first, 0, irn, 0);
255 obstack_grow(&ob, &loc, sizeof(loc));
256 DBG((dbg, DBG_START, " %+F:\n", irn));
261 live_foreach(blk, li)
262 if (live_is_in(li) && arch_get_irn_reg_class(bel->arch, li->irn, -1) == bel->cls) {
263 loc.irn = (ir_node *)li->irn;
264 loc.time = be_get_next_use(bel->uses, first, 0, li->irn, 0);
265 obstack_grow(&ob, &loc, sizeof(loc));
266 DBG((dbg, DBG_START, " %+F:\n", irn));
269 starters = obstack_finish(&ob);
271 /* Sort all values */
272 qsort(starters, count, sizeof(starters[0]), loc_compare);
274 /* Create the start workset for this block. Copy the best ones from starters */
275 blk_info = obstack_alloc(&bel->ob, sizeof(*blk_info));
276 set_irn_link(blk, blk_info);
278 ws_count = MIN(count, bel->n_regs);
279 blk_info->ws_start = new_workset(&bel->ob, bel);
280 workset_bulk_fill(blk_info->ws_start, ws_count, starters);
282 /* Spill the phis among the remaining values */
283 for (i=ws_count; i<count; ++i) {
286 irn = starters[i].irn;
287 if (!is_Phi(irn) || get_nodes_block(irn) != blk)
290 DBG((dbg, DBG_START, "For %+F:\n", irn));
292 for (max=get_irn_arity(irn), o=0; o<max; ++o) {
293 ir_node *arg = get_irn_n(irn, o);
294 ir_node *pred_block = get_Block_cfgpred_block(get_nodes_block(irn), o);
295 ir_node *cpy = be_new_Copy(bel->cls, irg, pred_block, arg);
296 pset_insert_ptr(bel->copies, cpy);
297 DBG((dbg, DBG_START, " place a %+F of %+F in %+F\n", cpy, arg, pred_block));
298 sched_add_before(pred_block, cpy);
299 set_irn_n(irn, o, cpy);
303 obstack_free(&ob, NULL);
307 * Performs the actions neccessary to grant the request that:
308 * - new_vals can be held in registers
309 * - as few as possible other values are disposed
310 * - the worst values get disposed
312 * @p is_usage indicates that the values in new_vals are used (not defined)
313 * In this case reloads must be performed
315 static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
317 int i, len, max_allowed, demand;
318 workset_t *ws = bel->ws;
319 ir_node **to_insert = alloca(bel->n_regs * sizeof(*to_insert));
322 * 1. Identify the number of needed slots and the values to reload
325 workset_foreach(new_vals, val) {
326 /* mark value as used */
328 pset_insert_ptr(bel->used, val);
330 if (!workset_contains(ws, val)) {
331 DBG((dbg, DBG_DECIDE, " insert %+F\n", val));
332 to_insert[demand++] = val;
334 be_add_reload(bel->senv, val, bel->instr);
336 DBG((dbg, DBG_DECIDE, " skip %+F\n", val));
338 DBG((dbg, DBG_DECIDE, " demand = %d\n", demand));
342 * 2. Make room for at least 'demand' slots
344 len = workset_get_length(ws);
345 max_allowed = bel->n_regs - demand;
347 /* Only make more free room if we do not have enough */
348 if (len > max_allowed) {
349 /* get current next-use distance */
350 for (i=0; i<ws->len; ++i)
351 workset_set_time(ws, i, be_get_next_use(bel->uses, bel->instr, bel->instr_nr, workset_get_val(ws, i), !is_usage));
353 /* sort entries by increasing nextuse-distance*/
356 /* Logic for not needed live-ins: If a value is disposed
357 before its first usage, remove it from start workset */
358 for (i=max_allowed; i<ws->len; ++i) {
359 ir_node *irn = ws->vals[i].irn;
360 if (!pset_find_ptr(bel->used, irn)) {
361 ir_node *curr_bb = get_nodes_block(bel->instr);
362 workset_t *ws_start = ((block_info_t *) get_irn_link(curr_bb))->ws_start;
363 workset_remove(ws_start, irn);
365 DBG((dbg, DBG_DECIDE, " dispose %+F dumb\n", irn));
367 DBG((dbg, DBG_DECIDE, " dispose %+F\n", irn));
370 /* kill the last 'demand' entries in the array */
371 workset_set_length(ws, max_allowed);
375 * 3. Insert the new values into the workset
377 workset_bulk_insert(bel->ws, demand, to_insert);
381 * For the given block @p blk, decide for each values
382 * whether it is used from a register or is reloaded
385 static void belady(ir_node *blk, void *env) {
386 belady_env_t *bel = env;
389 #ifdef SINGLE_START_PROJS
390 ir_node *start_blk = get_irg_start_block(get_irn_irg(blk));
392 block_info_t *blk_info = get_irn_link(blk);
394 DBG((dbg, DBG_DECIDE, "\n"));
395 DBG((dbg, DBG_DECIDE, "Decide for %+F\n", blk));
397 workset_copy(bel->ws, blk_info->ws_start);
398 DBG((dbg, DBG_WSETS, "Initial start workset for %+F:\n", blk));
399 workset_foreach(bel->ws, irn)
400 DBG((dbg, DBG_WSETS, " %+F\n", irn));
402 /* process the block from start to end */
403 DBG((dbg, DBG_WSETS, "Processing...\n"));
404 bel->used = pset_new_ptr(32);
406 new_vals = new_workset(&bel->ob, bel);
407 sched_foreach(blk, irn) {
408 assert(workset_get_length(bel->ws) <= bel->n_regs && "Too much values in workset!");
411 #ifdef SINGLE_START_PROJS
413 (is_Proj(irn) && blk!=start_blk) ||
414 (get_irn_mode(irn) == mode_T && blk==start_blk)) {
415 DBG((dbg, DBG_DECIDE, " ...%+F skipped\n", irn));
419 /* projs are handled with the tuple value.
420 * Phis are no real instr (see insert_starters)
421 * instr_nr does not increase */
422 if (is_Proj(irn) || is_Phi(irn)) {
423 DBG((dbg, DBG_DECIDE, " ...%+F skipped\n", irn));
427 DBG((dbg, DBG_DECIDE, " ...%+F\n", irn));
429 /* set instruction in the workset */
432 /* allocate all values _used_ by this instruction */
433 workset_clear(new_vals);
434 workset_bulk_insert(new_vals, get_irn_arity(irn)+1, get_irn_in(irn));
435 displace(bel, new_vals, 1);
437 /* allocate all values _defined_ by this instruction */
438 workset_clear(new_vals);
439 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
441 for(proj=sched_next(irn); is_Proj(proj); proj=sched_next(proj))
442 workset_insert(new_vals, proj);
444 workset_insert(new_vals, irn);
446 displace(bel, new_vals, 0);
452 /* Remember end-workset for this block */
453 blk_info->ws_end = workset_clone(&bel->ob, bel->ws);
454 DBG((dbg, DBG_WSETS, "Start workset for %+F:\n", blk));
455 workset_foreach(blk_info->ws_start, irn)
456 DBG((dbg, DBG_WSETS, " %+F\n", irn));
457 DBG((dbg, DBG_WSETS, "End workset for %+F:\n", blk));
458 workset_foreach(blk_info->ws_end, irn)
459 DBG((dbg, DBG_WSETS, " %+F\n", irn));
463 * 'decide' is block-local and makes assumptions
464 * about the set of live-ins. Thus we must adapt the
465 * live-outs to the live-ins at each block-border.
467 static void fix_block_borders(ir_node *blk, void *env) {
469 belady_env_t *bel = env;
472 DBG((dbg, DBG_FIX, "\n"));
473 DBG((dbg, DBG_FIX, "Fixing %+F\n", blk));
475 wsb = ((block_info_t *)get_irn_link(blk))->ws_start;
477 /* process all pred blocks */
478 for (i=0, max=get_irn_arity(blk); i<max; ++i) {
479 ir_node *irnb, *irnp, *pred = get_Block_cfgpred_block(blk, i);
480 workset_t *wsp = ((block_info_t *)get_irn_link(pred))->ws_end;
482 DBG((dbg, DBG_FIX, " Pred %+F\n", pred));
484 workset_foreach(wsb, irnb) {
485 /* if irnb is a phi of the current block we reload
486 * the corresponding argument, else irnb itself */
487 if(is_Phi(irnb) && blk == get_nodes_block(irnb))
488 irnb = get_irn_n(irnb, i);
490 /* Unknowns are available everywhere */
491 if(get_irn_opcode(irnb) == iro_Unknown)
494 /* check if irnb is in a register at end of pred */
495 workset_foreach(wsp, irnp)
499 /* irnb is in memory at the end of pred, so we have to reload it */
500 DBG((dbg, DBG_FIX, " reload %+F\n", irnb));
501 be_add_reload_on_edge(bel->senv, irnb, blk, i);
504 /*epsilon statement :)*/;
510 * Removes all used reloads from bel->reloads.
511 * The remaining nodes in bel->reloads will be removed from the graph.
513 static void rescue_used_reloads(ir_node *irn, void *env) {
514 pset *rlds = (pset *)env;
515 if (pset_find_ptr(rlds, irn))
516 pset_remove_ptr(rlds, irn);
520 * Removes all copies introduced for phi-spills
522 static void remove_copies(belady_env_t *bel) {
525 for (irn = pset_first(bel->copies); irn; irn = pset_next(bel->copies)) {
526 ir_node *src, *spill;
528 assert(be_is_Copy(irn));
529 assert(get_irn_n_edges(irn) == 1 && "This is not a copy introduced in 'compute_block_start_info()'. Who created it?");
531 spill = get_irn_edge(get_irn_irg(irn), irn, 0)->src;
532 assert(be_is_Spill(spill) && "This is not a copy introduced in 'compute_block_start_info()'. Who created it?");
534 src = get_irn_n(irn, 0);
535 set_irn_n(spill, 0, src);
540 * Finds all unused reloads and remove them from the schedule
541 * Also removes spills if they are not used anymore after removing reloads
543 static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
546 irg_walk_graph(irg, rescue_used_reloads, NULL, bel->reloads);
547 for(irn = pset_first(bel->reloads); irn; irn = pset_next(bel->reloads)) {
549 DBG((dbg, DBG_SPILL, "Removing %+F before %+F in %+F\n", irn, sched_next(irn), get_nodes_block(irn)));
551 spill = get_irn_n(irn, 0);
554 set_irn_n(irn, 0, new_Bad());
557 /* if spill not used anymore, remove it too
558 * test of regclass is necessary since spill may be a phi-M */
559 if (get_irn_n_edges(spill) == 0 && bel->cls == arch_get_irn_reg_class(bel->arch, spill, -1)) {
560 set_irn_n(spill, 0, new_Bad());
566 void be_spill_belady(const be_chordal_env_t *chordal_env) {
569 dbg = firm_dbg_register("ir.be.spillbelady");
571 /* init belady env */
572 obstack_init(&bel.ob);
573 bel.arch = chordal_env->main_env->arch_env;
574 bel.cls = chordal_env->cls;
575 bel.n_regs = arch_register_class_n_regs(bel.cls);
576 bel.ws = new_workset(&bel.ob, &bel);
577 bel.uses = be_begin_uses(chordal_env->irg, chordal_env->main_env->arch_env, bel.cls);
578 bel.senv = be_new_spill_env(dbg, chordal_env, is_mem_phi, NULL);
579 bel.reloads = pset_new_ptr_default();
580 bel.copies = pset_new_ptr_default();
582 DBG((dbg, LEVEL_1, "running on register class: %s\n", bel.cls->name));
585 irg_block_walk_graph(chordal_env->irg, compute_block_start_info, NULL, &bel);
586 irg_block_walk_graph(chordal_env->irg, belady, NULL, &bel);
587 irg_block_walk_graph(chordal_env->irg, fix_block_borders, NULL, &bel);
588 be_insert_spills_reloads(bel.senv, bel.reloads);
589 remove_unused_reloads(chordal_env->irg, &bel);
595 del_pset(bel.reloads);
596 be_delete_spill_env(bel.senv);
597 be_end_uses(bel.uses);
598 obstack_free(&bel.ob, NULL);