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