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