cd1acec35d6347d1ca5018e3564de4c8a669b7a0
[libfirm] / ir / be / bespillbelady.c
1 /**
2  * Author:      Daniel Grund, Matthias Braun
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 "irloop.h"
29 #include "iredges_t.h"
30 #include "ircons_t.h"
31 #include "irprintf.h"
32
33 #include "beutil.h"
34 #include "bearch.h"
35 #include "bespillbelady.h"
36 #include "beuses_t.h"
37 #include "besched_t.h"
38 #include "beirgmod.h"
39 #include "belive_t.h"
40 #include "benode_t.h"
41 #include "bechordal_t.h"
42 #include "bespilloptions.h"
43
44 #define DBG_SPILL   1
45 #define DBG_WSETS   2
46 #define DBG_FIX     4
47 #define DBG_DECIDE  8
48 #define DBG_START  16
49 #define DBG_SLOTS  32
50 #define DBG_TRACE  64
51 #define DBG_WORKSET 128
52 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
53
54 /**
55  * An association between a node and a point in time.
56  */
57 typedef struct _loc_t {
58   ir_node *irn;        /**< A node. */
59   unsigned time;       /**< A use time (see beuses.h). */
60 } loc_t;
61
62 typedef struct _workset_t {
63         int len;                        /**< current length */
64         loc_t vals[0];          /**< inlined array of the values/distances in this working set */
65 } workset_t;
66
67 typedef struct _belady_env_t {
68         struct obstack ob;
69         const arch_env_t *arch;
70         const arch_register_class_t *cls;
71         const be_lv_t *lv;
72         int n_regs;                     /** number of regs in this reg-class */
73
74         workset_t *ws;          /**< the main workset used while processing a block. ob-allocated */
75         be_uses_t *uses;        /**< env for the next-use magic */
76         ir_node *instr;         /**< current instruction */
77         unsigned instr_nr;      /**< current instruction number (relative to block start) */
78         pset *used;
79
80         spill_env_t *senv;      /**< see bespill.h */
81 } belady_env_t;
82
83 static int loc_compare(const void *a, const void *b)
84 {
85         const loc_t *p = a;
86         const loc_t *q = b;
87         return p->time - q->time;
88 }
89
90 void workset_print(const workset_t *w)
91 {
92         int i;
93
94         for(i = 0; i < w->len; ++i) {
95                 ir_fprintf(stderr, "%+F %d\n", w->vals[i].irn, w->vals[i].time);
96         }
97 }
98
99 /**
100  * Alloc a new workset on obstack @p ob with maximum size @p max
101  */
102 static INLINE workset_t *new_workset(belady_env_t *env, struct obstack *ob) {
103         workset_t *res;
104         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
105         res = obstack_alloc(ob, size);
106         memset(res, 0, size);
107         return res;
108 }
109
110 /**
111  * Alloc a new instance on obstack and make it equal to @param ws
112  */
113 static INLINE workset_t *workset_clone(belady_env_t *env, struct obstack *ob, workset_t *ws) {
114         workset_t *res;
115         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
116         res = obstack_alloc(ob, size);
117         memcpy(res, ws, size);
118         return res;
119 }
120
121 /**
122  * Do NOT alloc anything. Make @param tgt equal to @param src.
123  * returns @param tgt for convenience
124  */
125 static INLINE workset_t *workset_copy(belady_env_t *env, workset_t *tgt, workset_t *src) {
126         size_t size = sizeof(*src) + (env->n_regs)*sizeof(src->vals[0]);
127         memcpy(tgt, src, size);
128         return tgt;
129 }
130
131 /**
132  * Overwrites the current content array of @param ws with the
133  * @param count locations given at memory @param locs.
134  * Set the length of @param ws to count.
135  */
136 static INLINE void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs) {
137         workset->len = count;
138         memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
139 }
140
141 /**
142  * Inserts the value @p val into the workset, iff it is not
143  * already contained. The workset must not be full.
144  */
145 static INLINE void workset_insert(belady_env_t *env, workset_t *ws, ir_node *val) {
146         int i;
147         /* check for current regclass */
148         if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, val)) {
149                 DBG((dbg, DBG_WORKSET, "Skipped %+F\n", val));
150                 return;
151         }
152
153         /* check if val is already contained */
154         for(i=0; i<ws->len; ++i)
155                 if (ws->vals[i].irn == val)
156                         return;
157
158         /* insert val */
159         assert(ws->len < env->n_regs && "Workset already full!");
160         ws->vals[ws->len++].irn = val;
161 }
162
163 /**
164  * Removes all entries from this workset
165  */
166 static INLINE void workset_clear(workset_t *ws) {
167         ws->len = 0;
168 }
169
170 /**
171  * Removes the value @p val from the workset if present.
172  */
173 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
174         int i;
175         for(i=0; i<ws->len; ++i) {
176                 if (ws->vals[i].irn == val) {
177                         ws->vals[i] = ws->vals[--ws->len];
178                         return;
179                 }
180         }
181 }
182
183 static INLINE int workset_contains(const workset_t *ws, const ir_node *val) {
184         int i;
185         for(i=0; i<ws->len; ++i) {
186                 if (ws->vals[i].irn == val)
187                         return 1;
188         }
189
190         return 0;
191 }
192
193 /**
194  * Iterates over all values in the working set.
195  * @p ws The workset to iterate
196  * @p v  A variable to put the current value in
197  * @p i  An integer for internal use
198  */
199 #define workset_foreach(ws, v, i)       for(i=0; \
200                                                                                 v=(i < ws->len) ? ws->vals[i].irn : NULL, i < ws->len; \
201                                                                                 ++i)
202
203 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
204 #define workset_get_time(ws, i) (ws)->vals[i].time
205 #define workset_set_length(ws, length) (ws)->len = length
206 #define workset_get_length(ws) ((ws)->len)
207 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
208 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
209
210 typedef struct _block_info_t {
211         workset_t *ws_start, *ws_end;
212         int processed;
213 } block_info_t;
214
215
216 static INLINE void *new_block_info(struct obstack *ob) {
217         block_info_t *res = obstack_alloc(ob, sizeof(*res));
218         res->ws_start = NULL;
219         res->ws_end = NULL;
220         res->processed = 0;
221
222         return res;
223 }
224
225 #define get_block_info(block)        ((block_info_t *)get_irn_link(block))
226 #define set_block_info(block, info)  set_irn_link(block, info)
227
228 /**
229  * @return The distance to the next use or 0 if irn has dont_spill flag set
230  */
231 static INLINE unsigned get_distance(belady_env_t *env, ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
232 {
233         be_next_use_t use;
234         int flags = arch_irn_get_flags(env->arch, def);
235
236         assert(! (flags & arch_irn_flags_ignore));
237
238         use = be_get_next_use(env->uses, from, from_step, def, skip_from_uses);
239         if(USES_IS_INFINITE(use.time))
240                 return USES_INFINITY;
241
242         /* We have to keep nonspillable nodes in the workingset */
243         if(flags & arch_irn_flags_dont_spill)
244                 return 0;
245
246         return use.time;
247 }
248
249 #if 0
250 /**
251  * Fix to remove dead nodes (especially don't spill nodes) from workset.
252  */
253 static void fix_dead_values(workset_t *ws, ir_node *irn) {
254         int idx;
255         ir_node *node;
256         ir_node *block = get_nodes_block(irn);
257
258         DBG((dbg, DBG_DECIDE, "fixing dead values at %+F:\n", irn));
259
260         workset_foreach(ws, node, idx) {
261                 const ir_edge_t *edge;
262                 int             fixme = 1;
263
264                 /* skip already fixed nodes */
265                 if (workset_get_time(ws, idx) == INT_MAX)
266                         continue;
267
268                 /* check all users */
269                 foreach_out_edge(node, edge) {
270                         ir_node *user = get_edge_src_irn(edge);
271
272                         if ((get_nodes_block(user) != block)                           ||  /* user is in a different block */
273                                 (sched_is_scheduled(user) && sched_comes_after(irn, user)) ||  /* user is scheduled after irn */
274                                 user == irn)                                                   /* irn is the user */
275                         {                                                                  /* => don't fix distance */
276                                 fixme = 0;
277                                 break;
278                         }
279                 }
280
281                 /* all users scheduled prior to current irn in in same block as irn -> fix */
282                 if (fixme) {
283                         workset_set_time(ws, idx, INT_MAX);
284                         DBG((dbg, DBG_DECIDE, "\tfixing time for %+F to INT_MAX\n", node));
285                 }
286         }
287
288 }
289 #endif
290
291 /**
292  * Performs the actions necessary to grant the request that:
293  * - new_vals can be held in registers
294  * - as few as possible other values are disposed
295  * - the worst values get disposed
296  *
297  * @p is_usage indicates that the values in new_vals are used (not defined)
298  * In this case reloads must be performed
299  */
300 static void displace(belady_env_t *env, workset_t *new_vals, int is_usage) {
301         ir_node *val;
302         int     i, len, max_allowed, demand, iter;
303
304         workset_t *ws         = env->ws;
305         ir_node   **to_insert = alloca(env->n_regs * sizeof(*to_insert));
306
307         /*
308                 1. Identify the number of needed slots and the values to reload
309         */
310         demand = 0;
311         workset_foreach(new_vals, val, iter) {
312                 /* mark value as used */
313                 if (is_usage)
314                         pset_insert_ptr(env->used, val);
315
316                 if (! workset_contains(ws, val)) {
317                         DBG((dbg, DBG_DECIDE, "    insert %+F\n", val));
318                         to_insert[demand++] = val;
319                         if (is_usage) {
320                                 DBG((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, env->instr));
321                                 be_add_reload(env->senv, val, env->instr, env->cls);
322                         }
323                 }
324                 else {
325                         assert(is_usage || "Defined value already in workset?!?");
326                         DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
327                 }
328         }
329         DBG((dbg, DBG_DECIDE, "    demand = %d\n", demand));
330
331         /*
332                 2. Make room for at least 'demand' slots
333         */
334         len         = workset_get_length(ws);
335         max_allowed = env->n_regs - demand;
336
337         DBG((dbg, DBG_DECIDE, "    disposing %d values\n", ws->len - max_allowed));
338
339         /* Only make more free room if we do not have enough */
340         if (len > max_allowed) {
341                 /* get current next-use distance */
342                 for (i = 0; i < ws->len; ++i) {
343                         unsigned dist = get_distance(env, env->instr, env->instr_nr, workset_get_val(ws, i), !is_usage);
344                         workset_set_time(ws, i, dist);
345                 }
346
347 #if 0
348                 /*
349                         FIX for don't spill nodes:
350                         Problem is that get_distance always returns 0 for those nodes even if they are not
351                         needed anymore (all their usages have already been visited).
352                         Even if we change this behavior, get_distance doesn't distinguish between not
353                         used anymore (dead) and live out of block.
354                         Solution: Set distances of all nodes having all their usages in schedule prior to
355                         current instruction to MAX_INT.
356                 */
357                 fix_dead_values(ws, env->instr);
358 #endif
359
360                 /* sort entries by increasing nextuse-distance*/
361                 workset_sort(ws);
362
363                 /*
364                         Logic for not needed live-ins: If a value is disposed
365                         before its first usage, remove it from start workset
366                         We don't do this for phis though
367                 */
368                 for (i = max_allowed; i < ws->len; ++i) {
369                         ir_node *irn = ws->vals[i].irn;
370
371                         DBG((dbg, DBG_DECIDE, "    disposing %+F (%u)\n", irn, workset_get_time(ws, i)));
372
373             if (is_Phi(irn))
374                 continue;
375
376                         if (! pset_find_ptr(env->used, irn)) {
377                                 ir_node   *curr_bb  = get_nodes_block(env->instr);
378                                 workset_t *ws_start = get_block_info(curr_bb)->ws_start;
379                                 workset_remove(ws_start, irn);
380
381                                 DBG((dbg, DBG_DECIDE, "    (and removing %+F from start workset)\n", irn));
382                         }
383                 }
384
385                 /* kill the last 'demand' entries in the array */
386                 workset_set_length(ws, max_allowed);
387         }
388
389         /*
390                 3. Insert the new values into the workset
391         */
392         for (i = 0; i < demand; ++i)
393                 workset_insert(env, env->ws, to_insert[i]);
394 }
395
396 static void belady(ir_node *block, void *env);
397
398 static loc_t to_take_or_not_to_take(belady_env_t *env, ir_node* first, ir_node *node, ir_node *block, ir_loop *loop) {
399         be_next_use_t next_use;
400         loc_t loc;
401         loc.time = USES_INFINITY;
402
403         if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, node)) {
404                 loc.time = USES_INFINITY;
405                 return loc;
406         }
407
408         loc.irn = node;
409
410         /* We have to keep nonspillable nodes in the workingset */
411         if(arch_irn_get_flags(env->arch, node) & arch_irn_flags_dont_spill) {
412                 loc.time = 0;
413                 DBG((dbg, DBG_START, "    %+F taken (dontspill node)\n", node, loc.time));
414                 return loc;
415         }
416
417         next_use = be_get_next_use(env->uses, first, 0, node, 0);
418         if(USES_IS_INFINITE(next_use.time)) {
419                 // the nodes marked as live in shouldn't be dead, so it must be a phi
420                 loc.time = USES_INFINITY;
421                 DBG((dbg, DBG_START, "    %+F not taken (dead)\n", node));
422                 assert(is_Phi(node));
423                 return loc;
424         }
425
426         loc.time = next_use.time;
427
428         if(next_use.outermost_loop >= get_loop_depth(loop)) {
429                 DBG((dbg, DBG_START, "    %+F taken (%u, loop %d)\n", node, loc.time, next_use.outermost_loop));
430                 return loc;
431                 // ARR_APP1(loc_t, starters, loc);
432         } else {
433                 loc.time = USES_INFINITY;
434                 DBG((dbg, DBG_START, "    %+F not taken (outerloopdepth %d < loopdetph %d)\n", node, next_use.outermost_loop, get_loop_depth(loop)));
435                 return loc;
436         }
437 }
438
439 /*
440  * Computes set of live-ins for each block with multiple predecessors
441  * and notifies spill algorithm which phis need to be spilled
442  */
443 static void compute_live_ins(ir_node *block, void *data) {
444         belady_env_t *env = data;
445         block_info_t *block_info;
446         ir_node *first, *irn;
447         loc_t loc, *starters;
448         int i, len, ws_count;
449         ir_loop *loop = get_irn_loop(block);
450         const be_lv_t *lv = env->lv;
451
452         if(get_Block_n_cfgpreds(block) == 1 && get_irg_start_block(get_irn_irg(block)) != block)
453                 return;
454
455         block_info = new_block_info(&env->ob);
456         set_block_info(block, block_info);
457
458         /* Collect all values living at start of block */
459         starters = NEW_ARR_F(loc_t, 0);
460
461         /* rebuild schedule time information, because it seems to be broken */
462         // Matze: is this still true?
463         //sched_renumber(block);
464
465         DBG((dbg, DBG_START, "Living at start of %+F:\n", block));
466         first = sched_first(block);
467
468         sched_foreach(block, irn) {
469                 if(!is_Phi(irn))
470                         break;
471
472                 loc = to_take_or_not_to_take(env, first, irn, block, loop);
473
474                 if(!USES_IS_INFINITE(loc.time)) {
475                         ARR_APP1(loc_t, starters, loc);
476                 } else {
477                         be_spill_phi(env->senv, irn);
478                 }
479         }
480
481         be_lv_foreach(lv, block, be_lv_state_in, i) {
482                 ir_node *node = be_lv_get_irn(lv, block, i);
483
484                 loc = to_take_or_not_to_take(env, first, node, block, loop);
485
486                 if(!USES_IS_INFINITE(loc.time)) {
487                         ARR_APP1(loc_t, starters, loc);
488                 }
489         }
490
491         // Sort start values by first use
492         qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
493
494         /* Copy the best ones from starters to start workset */
495         ws_count = MIN(ARR_LEN(starters), env->n_regs);
496         block_info->ws_start = new_workset(env, &env->ob);
497         workset_bulk_fill(block_info->ws_start, ws_count, starters);
498
499         /* The phis of this block which are not in the start set have to be spilled later. */
500         len = ARR_LEN(starters);
501         for (i = ws_count; i < len; ++i) {
502                 irn = starters[i].irn;
503                 if (!is_Phi(irn) || get_nodes_block(irn) != block)
504                         continue;
505
506                 be_spill_phi(env->senv, irn);
507         }
508
509         DEL_ARR_F(starters);
510 }
511
512 /**
513  * Collects all values live-in at block @p block and all phi results in this block.
514  * Then it adds the best values (at most n_regs) to the blocks start_workset.
515  * The phis among the remaining values get spilled: Introduce psudo-copies of
516  *  their args to break interference and make it possible to spill them to the
517  *  same spill slot.
518  */
519 static block_info_t *compute_block_start_info(belady_env_t *env, ir_node *block) {
520         ir_node *pred_block;
521         block_info_t *res, *pred_info;
522
523         /* Have we seen this block before? */
524         res = get_block_info(block);
525         if (res)
526                 return res;
527
528         /* Create the block info for this block. */
529         res = new_block_info(&env->ob);
530         set_block_info(block, res);
531
532         /* Use endset of predecessor block as startset */
533         assert(get_Block_n_cfgpreds(block) == 1 && block != get_irg_start_block(get_irn_irg(block)));
534         pred_block = get_Block_cfgpred_block(block, 0);
535         pred_info = get_block_info(pred_block);
536
537         /* if pred block has not been processed yet, do it now */
538         if (pred_info == NULL || pred_info->processed == 0) {
539                 belady(pred_block, env);
540                 pred_info = get_block_info(pred_block);
541         }
542
543         /* now we have an end_set of pred */
544         assert(pred_info->ws_end && "The recursive call (above) is supposed to compute an end_set");
545         res->ws_start = workset_clone(env, &env->ob, pred_info->ws_end);
546
547         return res;
548 }
549
550
551 /**
552  * For the given block @p block, decide for each values
553  * whether it is used from a register or is reloaded
554  * before the use.
555  */
556 static void belady(ir_node *block, void *data) {
557         belady_env_t *env = data;
558         workset_t *new_vals;
559         ir_node *irn;
560         int iter;
561         block_info_t *block_info;
562
563         /* make sure we have blockinfo (with startset) */
564         block_info = get_block_info(block);
565         if (block_info == NULL)
566                 block_info = compute_block_start_info(env, block);
567
568         /* Don't do a block twice */
569         if(block_info->processed)
570                 return;
571
572         /* get the starting workset for this block */
573         DBG((dbg, DBG_DECIDE, "\n"));
574         DBG((dbg, DBG_DECIDE, "Decide for %+F\n", block));
575
576         workset_copy(env, env->ws, block_info->ws_start);
577         DBG((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
578         workset_foreach(env->ws, irn, iter)
579                 DBG((dbg, DBG_WSETS, "  %+F (%u)\n", irn, workset_get_time(env->ws, iter)));
580
581         /* process the block from start to end */
582         DBG((dbg, DBG_WSETS, "Processing...\n"));
583         env->used = pset_new_ptr_default();
584         env->instr_nr = 0;
585         new_vals = new_workset(env, &env->ob);
586         sched_foreach(block, irn) {
587                 int i, arity;
588                 assert(workset_get_length(env->ws) <= env->n_regs && "Too much values in workset!");
589
590                 /* projs are handled with the tuple value.
591                  * Phis are no real instr (see insert_starters())
592                  * instr_nr does not increase */
593                 if (is_Proj(irn) || is_Phi(irn)) {
594                         DBG((dbg, DBG_DECIDE, "  ...%+F skipped\n", irn));
595                         continue;
596                 }
597                 DBG((dbg, DBG_DECIDE, "  ...%+F\n", irn));
598
599                 /* set instruction in the workset */
600                 env->instr = irn;
601
602                 /* allocate all values _used_ by this instruction */
603                 workset_clear(new_vals);
604                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
605                         workset_insert(env, new_vals, get_irn_n(irn, i));
606                 }
607                 displace(env, new_vals, 1);
608
609                 /* allocate all values _defined_ by this instruction */
610                 workset_clear(new_vals);
611                 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
612                         ir_node *proj;
613                         for(proj=sched_next(irn); is_Proj(proj); proj=sched_next(proj))
614                                 workset_insert(env, new_vals, proj);
615                 } else {
616                         workset_insert(env, new_vals, irn);
617                 }
618                 displace(env, new_vals, 0);
619
620                 env->instr_nr++;
621         }
622         del_pset(env->used);
623
624         /* Remember end-workset for this block */
625         block_info->ws_end = workset_clone(env, &env->ob, env->ws);
626         block_info->processed = 1;
627         DBG((dbg, DBG_WSETS, "End workset for %+F:\n", block));
628         workset_foreach(block_info->ws_end, irn, iter)
629                 DBG((dbg, DBG_WSETS, "  %+F (%u)\n", irn, workset_get_time(block_info->ws_end, iter)));
630 }
631
632 /**
633  * 'decide' is block-local and makes assumptions
634  * about the set of live-ins. Thus we must adapt the
635  * live-outs to the live-ins at each block-border.
636  */
637 static void fix_block_borders(ir_node *block, void *data) {
638         belady_env_t *env = data;
639         workset_t *wsb;
640         ir_graph *irg = get_irn_irg(block);
641         ir_node *startblock = get_irg_start_block(irg);
642         int i, max, iter, iter2;
643
644         if(block == startblock)
645             return;
646
647         DBG((dbg, DBG_FIX, "\n"));
648         DBG((dbg, DBG_FIX, "Fixing %+F\n", block));
649
650         wsb = get_block_info(block)->ws_start;
651
652         /* process all pred blocks */
653         for (i=0, max=get_irn_arity(block); i<max; ++i) {
654                 ir_node *irnb, *irnp, *pred = get_Block_cfgpred_block(block, i);
655                 workset_t *wsp = get_block_info(pred)->ws_end;
656
657                 DBG((dbg, DBG_FIX, "  Pred %+F\n", pred));
658
659                 workset_foreach(wsb, irnb, iter) {
660                         /* if irnb is a phi of the current block we reload
661                          * the corresponding argument, else irnb itself */
662                         if(is_Phi(irnb) && block == get_nodes_block(irnb)) {
663                                 irnb = get_irn_n(irnb, i);
664
665                                 // we might have unknowns as argument for the phi
666                                 if(!arch_irn_consider_in_reg_alloc(env->arch, env->cls, irnb))
667                                         continue;
668                         }
669
670                         /* Unknowns are available everywhere */
671                         if(get_irn_opcode(irnb) == iro_Unknown)
672                                 continue;
673
674                         /* check if irnb is in a register at end of pred */
675                         workset_foreach(wsp, irnp, iter2) {
676                                 if (irnb == irnp)
677                                         goto next_value;
678                         }
679
680                         /* irnb is not in memory at the end of pred, so we have to reload it */
681                         DBG((dbg, DBG_FIX, "    reload %+F\n", irnb));
682                         DBG((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", irnb, block, i));
683                         be_add_reload_on_edge(env->senv, irnb, block, i, env->cls);
684
685 next_value:
686                         /*epsilon statement :)*/;
687                 }
688         }
689 }
690
691 void be_spill_belady(be_irg_t *birg, const arch_register_class_t *cls) {
692         be_spill_belady_spill_env(birg, cls, NULL);
693 }
694
695 void be_spill_belady_spill_env(be_irg_t *birg, const arch_register_class_t *cls, spill_env_t *spill_env) {
696         belady_env_t env;
697         ir_graph *irg = be_get_birg_irg(birg);
698
699         FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
700         //firm_dbg_set_mask(dbg, DBG_SPILL);
701
702         be_assure_liveness(birg);
703
704         /* init belady env */
705         obstack_init(&env.ob);
706         env.arch      = birg->main_env->arch_env;
707         env.cls       = cls;
708         env.lv        = be_get_birg_liveness(birg);
709         env.n_regs    = env.cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
710         env.ws        = new_workset(&env, &env.ob);
711         env.uses      = be_begin_uses(irg, env.lv);
712         if(spill_env == NULL) {
713                 env.senv = be_new_spill_env(birg);
714         } else {
715                 env.senv = spill_env;
716         }
717         DEBUG_ONLY(be_set_spill_env_dbg_module(env.senv, dbg);)
718
719         be_clear_links(irg);
720         /* Decide which phi nodes will be spilled and place copies for them into the graph */
721         irg_block_walk_graph(irg, compute_live_ins, NULL, &env);
722         /* Fix high register pressure with belady algorithm */
723         irg_block_walk_graph(irg, NULL, belady, &env);
724         /* belady was block-local, fix the global flow by adding reloads on the edges */
725         irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
726         /* Insert spill/reload nodes into the graph and fix usages */
727         be_insert_spills_reloads(env.senv);
728
729         /* clean up */
730         if(spill_env == NULL)
731                 be_delete_spill_env(env.senv);
732         be_end_uses(env.uses);
733         obstack_free(&env.ob, NULL);
734 }
735
736 static void be_spill_belady_oldinterface(const be_chordal_env_t *chordal_env)
737 {
738         be_spill_belady(chordal_env->birg, chordal_env->cls);
739 }
740
741 void be_init_spillbelady(void)
742 {
743         static be_spiller_t belady_spiller = {
744                 be_spill_belady_oldinterface
745         };
746
747         be_register_spiller("belady", &belady_spiller);
748 }
749
750 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);