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