fixed indents
[libfirm] / ir / be / bespillbelady.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                20.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  */
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #ifdef HAVE_ALLOCA_H
13 #include <alloca.h>
14 #endif
15
16 #ifdef HAVE_MALLOC_H
17 #include <malloc.h>
18 #endif
19
20 #include "obst.h"
21 #include "set.h"
22 #include "pset.h"
23 #include "irprintf_t.h"
24 #include "irgraph.h"
25 #include "irnode.h"
26 #include "irmode.h"
27 #include "irgwalk.h"
28 #include "iredges_t.h"
29 #include "ircons_t.h"
30 #include "irprintf.h"
31
32 #include "beutil.h"
33 #include "bearch.h"
34 #include "bespillbelady.h"
35 #include "beuses_t.h"
36 #include "besched_t.h"
37 #include "beirgmod.h"
38 #include "belive_t.h"
39 #include "benode_t.h"
40 #include "bechordal_t.h"
41
42 #define DBG_SPILL   1
43 #define DBG_WSETS   2
44 #define DBG_FIX     4
45 #define DBG_DECIDE  8
46 #define DBG_START  16
47 #define DBG_SLOTS  32
48 #define DBG_TRACE  64
49 #define DEBUG_LVL 0 //(DBG_START | DBG_DECIDE | DBG_WSETS | DBG_FIX | DBG_SPILL)
50 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51
52 #define MIN(a,b) (((a)<(b))?(a):(b))
53
54 typedef struct _workset_t workset_t;
55
56 typedef struct _belady_env_t {
57         struct obstack ob;
58         const arch_env_t *arch;
59         const arch_register_class_t *cls;
60         int n_regs;                     /** number of regs in this reg-class */
61
62         workset_t *ws;          /**< the main workset used while processing a block. ob-allocated */
63         be_uses_t *uses;        /**< env for the next-use magic */
64         ir_node *instr;         /**< current instruction */
65         unsigned instr_nr;      /**< current instruction number (relative to block start) */
66         pset *used;                     /**< holds the values used (so far) in the current BB */
67         pset *copies;           /**< holds all copies placed due to phi-spilling */
68
69         spill_env_t *senv;      /* see bespill.h */
70         pset *reloads;          /**< all reload nodes placed */
71 } belady_env_t;
72
73 struct _workset_t {
74         belady_env_t *bel;
75         int len;                        /**< current length */
76         loc_t vals[1];          /**< inlined array of the values/distances in this working set */
77 };
78
79 void workset_print(const workset_t *w)
80 {
81         int i;
82
83         for(i = 0; i < w->len; ++i) {
84                 ir_printf("%+F %d\n", w->vals[i].irn, w->vals[i].time);
85         }
86 }
87
88 /**
89  * Alloc a new workset on obstack @p ob with maximum size @p max
90  */
91 static INLINE workset_t *new_workset(struct obstack *ob, belady_env_t *bel) {
92         workset_t *res;
93         size_t size = sizeof(*res) + (bel->n_regs-1)*sizeof(res->vals[0]);
94         res = obstack_alloc(ob, size);
95         memset(res, 0, size);
96         res->bel = bel;
97         return res;
98 }
99
100 /**
101  * Alloc a new instance on obstack and make it equal to @param ws
102  */
103 static INLINE workset_t *workset_clone(struct obstack *ob, workset_t *ws) {
104         workset_t *res;
105         size_t size = sizeof(*res) + (ws->bel->n_regs-1)*sizeof(res->vals[0]);
106         res = obstack_alloc(ob, size);
107         memcpy(res, ws, size);
108         return res;
109 }
110
111 /**
112  * Do NOT alloc anything. Make @param tgt equal to @param src.
113  * returns @param tgt for convinience
114  */
115 static INLINE workset_t *workset_copy(workset_t *tgt, workset_t *src) {
116         size_t size = sizeof(*src) + (src->bel->n_regs-1)*sizeof(src->vals[0]);
117         memcpy(tgt, src, size);
118         return tgt;
119 }
120
121 /**
122  * Overwrites the current content array of @param ws with the
123  * @param count locations given at memory @param locs.
124  * Set the length of @param ws to count.
125  */
126 #define workset_bulk_fill(ws, count, locs) memcpy(&(ws)->vals[0], locs, ((ws)->len=count)*sizeof(locs[0]));
127
128
129 /**
130  * Inserts the value @p val into the workset, iff it is not
131  * already contained. The workset must not be full.
132  */
133 static INLINE void workset_insert(workset_t *ws, ir_node *val) {
134         int i;
135         /* check for current regclass */
136         if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
137                 DBG((dbg, DBG_DECIDE, "Dropped %+F\n", val));
138                 return;
139         }
140
141         /* check if val is already contained */
142         for(i=0; i<ws->len; ++i)
143                 if (ws->vals[i].irn == val)
144                         return;
145
146         /* insert val */
147         assert(ws->len < ws->bel->n_regs && "Workset already full!");
148         ws->vals[ws->len++].irn = val;
149 }
150
151 /**
152  * Inserts all values in array @p vals of length @p cnt
153  * into the workset. There must be enough space for the
154  * entries.
155  */
156 static INLINE void workset_bulk_insert(workset_t *ws, int cnt, ir_node **vals) {
157         int i, o;
158
159         for(o=0; o<cnt; ++o) {
160                 ir_node *val = vals[o];
161                 DBG((dbg, DBG_TRACE, "Bulk insert %+F\n", val));
162                 /* check for current regclass */
163                 if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
164                         DBG((dbg, DBG_TRACE, "Wrong reg class\n"));
165                         goto no_insert;
166                 }
167
168                 /* check if val is already contained */
169                 for(i=0; i<ws->len; ++i)
170                         if (ws->vals[i].irn == val) {
171                                 DBG((dbg, DBG_TRACE, "Already contained\n"));
172                                 goto no_insert;
173                         }
174
175                 /* insert val */
176                 assert(ws->len < ws->bel->n_regs && "Workset does not have enough room!");
177                 ws->vals[ws->len++].irn = val;
178                 DBG((dbg, DBG_TRACE, "Inserted\n"));
179
180 no_insert:
181                 /*epsilon statement :)*/;
182         }
183 }
184
185 /**
186  * Removes all entries from this workset
187  */
188 #define workset_clear(ws) (ws)->len = 0;
189
190 /**
191  * Removes the value @p val from the workset if present.
192  */
193 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
194         int i;
195         for(i=0; i<ws->len; ++i)
196                 if (ws->vals[i].irn == val) {
197                         ws->vals[i] = ws->vals[--ws->len];
198                         return;
199                 }
200 }
201
202 static INLINE int workset_contains(const workset_t *ws, const ir_node *val) {
203         int i;
204         for(i=0; i<ws->len; ++i)
205                 if (ws->vals[i].irn == val)
206                         return 1;
207         return 0;
208 }
209
210 /**
211  * Iterates over all values in the working set.
212  * @p ws The workset to iterate
213  * @p v  A variable to put the current value in
214  * @p i  An integer for internal use
215  */
216 #define workset_foreach(ws, v, i)       for(i=0; \
217                                                                                 v=(i < ws->len) ? ws->vals[i].irn : NULL, i < ws->len; \
218                                                                                 ++i)
219
220 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
221 #define workset_set_length(ws, length) (ws)->len = length
222 #define workset_get_length(ws) ((ws)->len)
223 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
224 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
225
226 typedef struct _block_info_t {
227         workset_t *ws_start, *ws_end;
228 } block_info_t;
229
230
231 static INLINE void *new_block_info(struct obstack *ob) {
232         block_info_t *res = obstack_alloc(ob, sizeof(*res));
233         res->ws_start = NULL;
234         res->ws_end = NULL;
235
236         return res;
237 }
238
239 #define get_block_info(blk)                     ((block_info_t *)get_irn_link(blk))
240 #define set_block_info(blk, info)       set_irn_link(blk, info)
241
242 static int is_mem_phi(const ir_node *irn, void *data) {
243         workset_t *sws;
244         ir_node *blk = get_nodes_block(irn);
245
246         DBG((dbg, DBG_SPILL, "Is %+F a mem-phi?\n", irn));
247         sws = get_block_info(blk)->ws_start;
248         DBG((dbg, DBG_SPILL, "  %d\n", !workset_contains(sws, irn)));
249         return !workset_contains(sws, irn);
250 }
251
252 /**
253  * @return The distance to the next use
254  *         Or 0 if irn is an ignore node
255  */
256
257 static INLINE unsigned get_distance(belady_env_t *bel, const ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
258 {
259         arch_irn_flags_t fl = arch_irn_get_flags(bel->arch, def);
260         unsigned dist = be_get_next_use(bel->uses, from, from_step, def, skip_from_uses);
261
262         if(!USES_IS_INIFINITE(dist) && (fl & (arch_irn_flags_ignore | arch_irn_flags_dont_spill)) != 0)
263                 return 0;
264
265         return dist;
266 }
267
268 /**
269  * Performs the actions necessary to grant the request that:
270  * - new_vals can be held in registers
271  * - as few as possible other values are disposed
272  * - the worst values get disposed
273  *
274  * @p is_usage indicates that the values in new_vals are used (not defined)
275  * In this case reloads must be performed
276  */
277 static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
278         ir_node *val;
279         int i, len, max_allowed, demand, iter;
280         workset_t *ws = bel->ws;
281         ir_node **to_insert = alloca(bel->n_regs * sizeof(*to_insert));
282
283         /*
284          * 1. Identify the number of needed slots and the values to reload
285          */
286         demand = 0;
287         workset_foreach(new_vals, val, iter) {
288                 /* mark value as used */
289                 if (is_usage)
290                         pset_insert_ptr(bel->used, val);
291
292                 if (!workset_contains(ws, val)) {
293                         DBG((dbg, DBG_DECIDE, "    insert %+F\n", val));
294                         to_insert[demand++] = val;
295                         if (is_usage)
296                                 be_add_reload(bel->senv, val, bel->instr);
297                 } else
298                         DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
299         }
300         DBG((dbg, DBG_DECIDE, "    demand = %d\n", demand));
301
302         /*
303          * 2. Make room for at least 'demand' slots
304          */
305         len = workset_get_length(ws);
306         max_allowed = bel->n_regs - demand;
307
308         /* Only make more free room if we do not have enough */
309         if (len > max_allowed) {
310                 /* get current next-use distance */
311                 for (i=0; i<ws->len; ++i)
312                         workset_set_time(ws, i, get_distance(bel, bel->instr, bel->instr_nr, workset_get_val(ws, i), !is_usage));
313
314                 /* sort entries by increasing nextuse-distance*/
315                 workset_sort(ws);
316
317                 /* Logic for not needed live-ins: If a value is disposed
318                    before its first usage, remove it from start workset */
319                 for (i=max_allowed; i<ws->len; ++i) {
320                         ir_node *irn = ws->vals[i].irn;
321
322                         if (!pset_find_ptr(bel->used, irn)) {
323                                 ir_node *curr_bb = get_nodes_block(bel->instr);
324                                 workset_t *ws_start = get_block_info(curr_bb)->ws_start;
325                                 workset_remove(ws_start, irn);
326
327                                 DBG((dbg, DBG_DECIDE, "    dispose %+F dumb\n", irn));
328                         } else
329                                 DBG((dbg, DBG_DECIDE, "    dispose %+F\n", irn));
330                 }
331
332                 /* kill the last 'demand' entries in the array */
333                 workset_set_length(ws, max_allowed);
334         }
335
336         /*
337          * 3. Insert the new values into the workset
338          */
339         workset_bulk_insert(bel->ws, demand, to_insert);
340 }
341
342 static void belady(ir_node *blk, void *env);
343
344 /**
345  * Collects all values live-in at block @p blk and all phi results in this block.
346  * Then it adds the best values (at most n_regs) to the blocks start_workset.
347  * The phis among the remaining values get spilled: Introduce psudo-copies of
348  *  their args to break interference and make it possible to spill them to the
349  *  same spill slot.
350  */
351 static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
352         belady_env_t *bel = env;
353         ir_node *irn, *first;
354         irn_live_t *li;
355         int i, count, ws_count;
356         loc_t loc, *starters;
357         ir_graph *irg = get_irn_irg(blk);
358         struct obstack ob;
359         block_info_t *res = get_block_info(blk);
360
361         /* Have we seen this block before? */
362         if (res)
363                 return res;
364
365         /* Create the block info for this block. */
366         res = new_block_info(&bel->ob);
367         set_block_info(blk, res);
368
369
370         /* Get all values living at the block start sorted by next use*/
371         obstack_init(&ob);
372
373         DBG((dbg, DBG_START, "Living at start of %+F:\n", blk));
374         first = sched_first(blk);
375         count = 0;
376         sched_foreach(blk, irn)
377                 if (is_Phi(irn) && arch_get_irn_reg_class(bel->arch, irn, -1) == bel->cls) {
378                         loc.irn = irn;
379                         loc.time = get_distance(bel, first, 0, irn, 0);
380                         obstack_grow(&ob, &loc, sizeof(loc));
381                         DBG((dbg, DBG_START, "    %+F:\n", irn));
382                         count++;
383                 } else
384                         break;
385
386         live_foreach(blk, li)
387                 if (live_is_in(li) && arch_get_irn_reg_class(bel->arch, li->irn, -1) == bel->cls) {
388                         loc.irn = (ir_node *)li->irn;
389                         loc.time = get_distance(bel, first, 0, li->irn, 0);
390                         obstack_grow(&ob, &loc, sizeof(loc));
391                         DBG((dbg, DBG_START, "    %+F:\n", irn));
392                         count++;
393                 }
394
395         starters = obstack_finish(&ob);
396         qsort(starters, count, sizeof(starters[0]), loc_compare);
397
398
399         /* If we have only one predecessor, we want the start_set of blk to be the end_set of pred */
400         if (get_Block_n_cfgpreds(blk) == 1 && blk != get_irg_start_block(get_irn_irg(blk))) {
401                 ir_node *pred_blk       = get_Block_cfgpred_block(blk, 0);
402                 block_info_t *pred_info = get_block_info(pred_blk);
403
404                 /* if pred block has not been processed yet, do it now */
405                 if (! pred_info) {
406                         belady(pred_blk, bel);
407                         pred_info = get_block_info(pred_blk);
408                 }
409
410                 /* now we have an end_set of pred */
411                 assert(pred_info->ws_end && "The recursive call (above) is supposed to compute an end_set");
412                 res->ws_start = workset_clone(&bel->ob, pred_info->ws_end);
413
414         } else
415
416         /* Else we want the start_set to be the values used 'the closest' */
417         {
418                 /* Copy the best ones from starters to start workset */
419                 ws_count = MIN(count, bel->n_regs);
420                 res->ws_start = new_workset(&bel->ob, bel);
421                 workset_bulk_fill(res->ws_start, ws_count, starters);
422         }
423
424
425         /* The phis of this block which are not in the start set have to be spilled later.
426          * Therefore we add temporary copies in the pred_blocks so the spills can spill
427          * into the same spill slot.
428          * After spilling these copies get deleted. */
429         for (i=workset_get_length(res->ws_start); i<count; ++i) {
430                 int o, max;
431
432                 irn = starters[i].irn;
433                 if (!is_Phi(irn) || get_nodes_block(irn) != blk)
434                         continue;
435
436                 DBG((dbg, DBG_START, "For %+F:\n", irn));
437
438                 for (max=get_irn_arity(irn), o=0; o<max; ++o) {
439                         ir_node *arg = get_irn_n(irn, o);
440                         ir_node *pred_block = get_Block_cfgpred_block(get_nodes_block(irn), o);
441                         ir_node *cpy = be_new_Copy(bel->cls, irg, pred_block, arg);
442                         pset_insert_ptr(bel->copies, cpy);
443                         DBG((dbg, DBG_START, "    place a %+F of %+F in %+F\n", cpy, arg, pred_block));
444                         sched_add_before(pred_block, cpy);
445                         set_irn_n(irn, o, cpy);
446                 }
447         }
448
449         obstack_free(&ob, NULL);
450         return res;
451 }
452
453
454 /**
455  * For the given block @p blk, decide for each values
456  * whether it is used from a register or is reloaded
457  * before the use.
458  */
459 static void belady(ir_node *blk, void *env) {
460         belady_env_t *bel = env;
461         workset_t *new_vals;
462         ir_node *irn;
463         int iter;
464         block_info_t *blk_info;
465
466         /* Don't do a block twice */
467         if (get_block_info(blk))
468                 return;
469
470         /* get the starting workset for this block */
471         blk_info = compute_block_start_info(blk, bel);
472
473         DBG((dbg, DBG_DECIDE, "\n"));
474         DBG((dbg, DBG_DECIDE, "Decide for %+F\n", blk));
475
476         workset_copy(bel->ws, blk_info->ws_start);
477         DBG((dbg, DBG_WSETS, "Start workset for %+F:\n", blk));
478         workset_foreach(bel->ws, irn, iter)
479                 DBG((dbg, DBG_WSETS, "  %+F\n", irn));
480
481         /* process the block from start to end */
482         DBG((dbg, DBG_WSETS, "Processing...\n"));
483         bel->used = pset_new_ptr(32);
484         bel->instr_nr = 0;
485         new_vals = new_workset(&bel->ob, bel);
486         sched_foreach(blk, irn) {
487                 assert(workset_get_length(bel->ws) <= bel->n_regs && "Too much values in workset!");
488
489
490                 /* projs are handled with the tuple value.
491                  * Phis are no real instr (see insert_starters())
492                  * instr_nr does not increase */
493                 if (is_Proj(irn) || is_Phi(irn)) {
494                         DBG((dbg, DBG_DECIDE, "  ...%+F skipped\n", irn));
495                         continue;
496                 }
497                 DBG((dbg, DBG_DECIDE, "  ...%+F\n", irn));
498
499                 /* set instruction in the workset */
500                 bel->instr = irn;
501
502                 /* allocate all values _used_ by this instruction */
503                 workset_clear(new_vals);
504                 workset_bulk_insert(new_vals, get_irn_arity(irn)+1, get_irn_in(irn));
505                 displace(bel, new_vals, 1);
506
507                 /* allocate all values _defined_ by this instruction */
508                 workset_clear(new_vals);
509                 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
510                         ir_node *proj;
511                         for(proj=sched_next(irn); is_Proj(proj); proj=sched_next(proj))
512                                 workset_insert(new_vals, proj);
513                 } else {
514                         workset_insert(new_vals, irn);
515                 }
516                 displace(bel, new_vals, 0);
517
518                 bel->instr_nr++;
519         }
520         del_pset(bel->used);
521
522         /* Remember end-workset for this block */
523         blk_info->ws_end = workset_clone(&bel->ob, bel->ws);
524         DBG((dbg, DBG_WSETS, "End workset for %+F:\n", blk));
525         workset_foreach(blk_info->ws_end, irn, iter)
526                 DBG((dbg, DBG_WSETS, "  %+F\n", irn));
527 }
528
529 /**
530  * 'decide' is block-local and makes assumptions
531  * about the set of live-ins. Thus we must adapt the
532  * live-outs to the live-ins at each block-border.
533  */
534 static void fix_block_borders(ir_node *blk, void *env) {
535         workset_t *wsb;
536         belady_env_t *bel = env;
537         int i, max, iter, iter2;
538
539         DBG((dbg, DBG_FIX, "\n"));
540         DBG((dbg, DBG_FIX, "Fixing %+F\n", blk));
541
542         wsb = get_block_info(blk)->ws_start;
543
544         /* process all pred blocks */
545         for (i=0, max=get_irn_arity(blk); i<max; ++i) {
546                 ir_node *irnb, *irnp, *pred = get_Block_cfgpred_block(blk, i);
547                 workset_t *wsp = get_block_info(pred)->ws_end;
548
549                 DBG((dbg, DBG_FIX, "  Pred %+F\n", pred));
550
551                 workset_foreach(wsb, irnb, iter) {
552                         /* if irnb is a phi of the current block we reload
553                          * the corresponding argument, else irnb itself */
554                         if(is_Phi(irnb) && blk == get_nodes_block(irnb))
555                                 irnb = get_irn_n(irnb, i);
556
557                         /* Unknowns are available everywhere */
558                         if(get_irn_opcode(irnb) == iro_Unknown)
559                                 continue;
560
561                         /* check if irnb is in a register at end of pred */
562                         workset_foreach(wsp, irnp, iter2)
563                                 if (irnb == irnp)
564                                         goto next_value;
565
566                         /* irnb is in memory at the end of pred, so we have to reload it */
567                         DBG((dbg, DBG_FIX, "    reload %+F\n", irnb));
568                         be_add_reload_on_edge(bel->senv, irnb, blk, i);
569
570 next_value:
571                         /*epsilon statement :)*/;
572                 }
573         }
574 }
575
576 /**
577  * Removes all used reloads from bel->reloads.
578  * The remaining nodes in bel->reloads will be removed from the graph.
579  */
580 static void rescue_used_reloads(ir_node *irn, void *env) {
581         pset *rlds = (pset *)env;
582         if (pset_find_ptr(rlds, irn))
583                 pset_remove_ptr(rlds, irn);
584 }
585
586 /**
587  * Removes all copies introduced for phi-spills
588  */
589 static void remove_copies(belady_env_t *bel) {
590         ir_node *irn;
591
592         for (irn = pset_first(bel->copies); irn; irn = pset_next(bel->copies)) {
593                 ir_node *src, *user;
594
595                 assert(be_is_Copy(irn));
596                 assert(get_irn_n_edges(irn) == 1 && "This is not a copy introduced in 'compute_block_start_info()'. Who created it?");
597
598                 user = get_irn_edge(get_irn_irg(irn), irn, 0)->src;
599
600                 src = be_get_Copy_op(irn);
601                 set_irn_n(user, 0, src);
602         }
603 }
604
605 /**
606  * Finds all unused reloads and remove them from the schedule
607  * Also removes spills if they are not used anymore after removing reloads
608  */
609 static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
610         ir_node *irn;
611
612         irg_walk_graph(irg, rescue_used_reloads, NULL, bel->reloads);
613         for(irn = pset_first(bel->reloads); irn; irn = pset_next(bel->reloads)) {
614                 ir_node *spill;
615                 DBG((dbg, DBG_SPILL, "Removing %+F before %+F in %+F\n", irn, sched_next(irn), get_nodes_block(irn)));
616
617                 if (be_is_Reload(irn))
618                         spill = get_irn_n(irn, be_pos_Reload_mem);
619
620                 /* remove reload */
621                 set_irn_n(irn, 0, new_Bad());
622                 sched_remove(irn);
623
624                 if (be_is_Reload(irn)) {
625                         /* if spill not used anymore, remove it too
626                          * test of regclass is necessary since spill may be a phi-M */
627                         if (get_irn_n_edges(spill) == 0 && bel->cls == arch_get_irn_reg_class(bel->arch, spill, -1)) {
628                                 set_irn_n(spill, 0, new_Bad());
629                                 sched_remove(spill);
630                         }
631                 }
632         }
633 }
634
635 void be_spill_belady(const be_chordal_env_t *chordal_env) {
636         belady_env_t bel;
637
638         FIRM_DBG_REGISTER(dbg, "ir.be.spillbelady");
639
640         /* init belady env */
641         obstack_init(&bel.ob);
642         bel.arch      = chordal_env->birg->main_env->arch_env;
643         bel.cls       = chordal_env->cls;
644         bel.n_regs    = arch_register_class_n_regs(bel.cls);
645         bel.ws        = new_workset(&bel.ob, &bel);
646         bel.uses      = be_begin_uses(chordal_env->irg, chordal_env->birg->main_env->arch_env, bel.cls);
647         bel.senv      = be_new_spill_env(chordal_env, is_mem_phi, NULL);
648         DEBUG_ONLY(be_set_spill_env_dbg_module(bel.senv, dbg);)
649         bel.reloads   = pset_new_ptr_default();
650         bel.copies    = pset_new_ptr_default();
651
652         DBG((dbg, LEVEL_1, "running on register class: %s\n", bel.cls->name));
653
654         /* do the work */
655         be_clear_links(chordal_env->irg);
656         irg_block_walk_graph(chordal_env->irg, NULL, belady, &bel);
657         irg_block_walk_graph(chordal_env->irg, fix_block_borders, NULL, &bel);
658         be_insert_spills_reloads(bel.senv, bel.reloads);
659         remove_unused_reloads(chordal_env->irg, &bel);
660         remove_copies(&bel);
661
662         /* clean up */
663         del_pset(bel.reloads);
664         be_delete_spill_env(bel.senv);
665         be_end_uses(bel.uses);
666         obstack_free(&bel.ob, NULL);
667 }