Added proper phi spilling
[libfirm] / ir / be / bespillbelady2.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Beladys spillalgorithm version 2.
23  * @author      Daniel Grund, Matthias Braun, Sebastian Hack
24  * @date        20.09.2005
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <math.h>
32
33 #include "obst.h"
34 #include "irnodeset.h"
35 #include "irbitset.h"
36 #include "irprintf_t.h"
37 #include "irgraph.h"
38 #include "irnode.h"
39 #include "irmode.h"
40 #include "irgwalk.h"
41 #include "irloop.h"
42 #include "iredges_t.h"
43 #include "ircons_t.h"
44 #include "irprintf.h"
45 #include "execfreq.h"
46 #include "xmalloc.h"
47
48 #include "beutil.h"
49 #include "bearch_t.h"
50 #include "bespillbelady.h"
51 #include "beuses.h"
52 #include "besched_t.h"
53 #include "beirgmod.h"
54 #include "belive_t.h"
55 #include "benode_t.h"
56 #include "bechordal_t.h"
57 #include "bespilloptions.h"
58 #include "beloopana.h"
59 #include "beirg_t.h"
60 #include "bemodule.h"
61
62 #define DBG_SPILL     1
63 #define DBG_WSETS     2
64 #define DBG_FIX       4
65 #define DBG_DECIDE    8
66 #define DBG_START    16
67 #define DBG_SLOTS    32
68 #define DBG_TRACE    64
69 #define DBG_WORKSET 128
70 #define DBG_GLOBAL  256
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
72
73
74 /**
75  * An association between a node and a point in time.
76  */
77 typedef struct _loc_t {
78   ir_node *irn;        /**< A node. */
79   unsigned time;       /**< A use time (see beuses.h). */
80   unsigned version;    /**< That is used in the global pass below.
81                                                  For usage see the comments below.
82                                                  In the local belady pass, this is not
83                                                  important. */
84 } loc_t;
85
86 typedef struct _workset_t {
87         int len;                        /**< current length */
88         loc_t vals[0];          /**< inlined array of the values/distances in this working set */
89 } workset_t;
90
91 typedef struct _belady_env_t {
92         struct obstack ob;
93         ir_graph *irg;
94         const arch_env_t *arch;
95         const arch_register_class_t *cls;
96         be_lv_t *lv;
97         ir_exec_freq *ef;
98
99         ir_node **blocks;   /**< Array of all blocks. */
100         int n_blocks;       /**< Number of blocks in the graph. */
101         int n_regs;                     /**< number of regs in this reg-class */
102         workset_t *ws;          /**< the main workset used while processing a block. ob-allocated */
103         be_uses_t *uses;        /**< env for the next-use magic */
104         ir_node *instr;         /**< current instruction */
105         unsigned instr_nr;      /**< current instruction number (relative to block start) */
106
107         spill_env_t *senv;      /**< see bespill.h */
108 } belady_env_t;
109
110
111 static int loc_compare_idx(const void *a, const void *b)
112 {
113         const loc_t *p = a;
114         const loc_t *q = b;
115         return (int) get_irn_idx(p->irn) - (int) get_irn_idx(q->irn);
116 }
117 static int loc_compare(const void *a, const void *b)
118 {
119         const loc_t *p = a;
120         const loc_t *q = b;
121         int diff  = (int) p->time - (int) q->time;
122         return diff != 0 ? diff : loc_compare_idx(a, b);
123 }
124
125 static INLINE void workset_print(const workset_t *w)
126 {
127         int i;
128
129         for(i = 0; i < w->len; ++i) {
130                 ir_fprintf(stderr, "%+F %d\n", w->vals[i].irn, w->vals[i].time);
131         }
132 }
133
134 /**
135  * Alloc a new workset on obstack @p ob with maximum size @p max
136  */
137 static INLINE workset_t *new_workset(belady_env_t *env, struct obstack *ob) {
138         workset_t *res;
139         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
140         res = obstack_alloc(ob, size);
141         memset(res, 0, size);
142         return res;
143 }
144
145 /**
146  * Alloc a new instance on obstack and make it equal to @param ws
147  */
148 static INLINE workset_t *workset_clone(belady_env_t *env, struct obstack *ob, workset_t *ws) {
149         workset_t *res;
150         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
151         res = obstack_alloc(ob, size);
152         memcpy(res, ws, size);
153         return res;
154 }
155
156 /**
157  * Do NOT alloc anything. Make @param tgt equal to @param src.
158  * returns @param tgt for convenience
159  */
160 static INLINE workset_t *workset_copy(belady_env_t *env, workset_t *tgt, workset_t *src) {
161         size_t size = sizeof(*src) + (env->n_regs)*sizeof(src->vals[0]);
162         memcpy(tgt, src, size);
163         return tgt;
164 }
165
166 /**
167  * Overwrites the current content array of @param ws with the
168  * @param count locations given at memory @param locs.
169  * Set the length of @param ws to count.
170  */
171 static INLINE void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs) {
172         workset->len = count;
173         memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
174 }
175
176 /**
177  * Inserts the value @p val into the workset, iff it is not
178  * already contained. The workset must not be full.
179  */
180 static INLINE void workset_insert(belady_env_t *env, workset_t *ws, ir_node *val) {
181         int i;
182         /* check for current regclass */
183         if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, val)) {
184                 DBG((dbg, DBG_WORKSET, "Skipped %+F\n", val));
185                 return;
186         }
187
188         /* check if val is already contained */
189         for(i=0; i<ws->len; ++i)
190                 if (ws->vals[i].irn == val)
191                         return;
192
193         /* insert val */
194         assert(ws->len < env->n_regs && "Workset already full!");
195         ws->vals[ws->len++].irn = val;
196 }
197
198 /**
199  * Removes all entries from this workset
200  */
201 static INLINE void workset_clear(workset_t *ws) {
202         ws->len = 0;
203 }
204
205 /**
206  * Removes the value @p val from the workset if present.
207  */
208 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
209         int i;
210         for(i=0; i<ws->len; ++i) {
211                 if (ws->vals[i].irn == val) {
212                         ws->vals[i] = ws->vals[--ws->len];
213                         return;
214                 }
215         }
216 }
217
218 static INLINE int workset_get_index(const workset_t *ws, const ir_node *val) {
219         int i;
220         for(i=0; i<ws->len; ++i) {
221                 if (ws->vals[i].irn == val)
222                         return i;
223         }
224
225         return -1;
226 }
227
228 /**
229  * Iterates over all values in the working set.
230  * @p ws The workset to iterate
231  * @p v  A variable to put the current value in
232  * @p i  An integer for internal use
233  */
234 #define workset_foreach(ws, v, i)       for(i=0; \
235                                                                                 v=(i < ws->len) ? ws->vals[i].irn : NULL, i < ws->len; \
236                                                                                 ++i)
237
238 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
239 #define workset_get_time(ws, i) (ws)->vals[i].time
240 #define workset_set_length(ws, length) (ws)->len = length
241 #define workset_get_length(ws) ((ws)->len)
242 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
243 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
244 #define workset_contains(ws, n) (workset_get_index(ws, n) >= 0)
245
246 typedef struct _block_use_t {
247         struct _block_use_t *next;
248         ir_node *insn;
249         int pos, tick;
250 } block_use_t;
251
252 typedef struct _block_info_t {
253         belady_env_t *bel;
254         const ir_node *bl;
255         ir_node *first_non_in;   /**< First node in block which is not a phi.  */
256         workset_t *ws_start, *ws_end;
257
258         workset_t *entrance_reg; /**< That set will contain all values
259                                                                   transported into the block which
260                                                                   are used before they are displaced.
261                                                                   That means, we later have to care to
262                                                                   bring them into the block in a register
263                                                                   or reload them at the entry of the block. */
264
265         workset_t * entrance_mem; /**< That set will contain all variables
266                                                                   (transported into the block)
267                                                                   which are in the memory upon entering
268                                                                   the block:
269                                                                   entrance_reg U entrance_mem = live-in + local phis. */
270
271         int pressure; /**< The amount of registers which remain free
272                                         in this block. This capacity can be used to let
273                                         global variables, transported into other blocks,
274                                         live through this block. */
275
276         double exec_freq; /**< The execution frequency of this block. */
277 } block_info_t;
278
279
280 static INLINE void *new_block_info(belady_env_t *bel, ir_node *bl) {
281         block_info_t *res = obstack_alloc(&bel->ob, sizeof(*res));
282         memset(res, 0, sizeof(res[0]));
283         res->bel = bel;
284         res->bl  = bl;
285         res->entrance_reg = new_workset(bel, &bel->ob);
286         res->entrance_mem = new_workset(bel, &bel->ob);
287         res->exec_freq    = get_block_execfreq(bel->ef, bl);
288         set_irn_link(bl, res);
289         return res;
290 }
291
292 #define get_block_info(block)        ((block_info_t *)get_irn_link(block))
293 #define set_block_info(block, info)  set_irn_link(block, info)
294
295 /**
296  * @return The distance to the next use or 0 if irn has dont_spill flag set
297  */
298 static INLINE unsigned get_distance(belady_env_t *env, ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
299 {
300         be_next_use_t use;
301         int flags = arch_irn_get_flags(env->arch, def);
302
303         assert(! (flags & arch_irn_flags_ignore));
304
305         use = be_get_next_use(env->uses, from, from_step, def, skip_from_uses);
306         if(USES_IS_INFINITE(use.time))
307                 return USES_INFINITY;
308
309         /* We have to keep nonspillable nodes in the workingset */
310         if(flags & arch_irn_flags_dont_spill)
311                 return 0;
312
313         return use.time;
314 }
315
316 /**
317  * Check, if the value is something that is transported into a block.
318  * That is, the value is live in or defined by a Phi in the block.
319  * @param env  The belady environment.
320  * @param bl   The block in question.
321  * @param irn  The node in question.
322  * @return     1, if node is something transported into @p bl, 0 if not.
323  */
324 static INLINE int is_transport_in(belady_env_t *env, const ir_node *bl, const ir_node *irn)
325 {
326         return (is_Phi(irn) && get_nodes_block(irn) == bl) || be_is_live_in(env->lv, bl, irn);
327 }
328
329 /**
330  * Performs the actions necessary to grant the request that:
331  * - new_vals can be held in registers
332  * - as few as possible other values are disposed
333  * - the worst values get disposed
334  *
335  * TODO Sebastian:
336  * Actually, we should displace a value immediately after it was used.
337  * If we don't, the cardinality of the workset does not reflect the register pressure.
338  * That might be necessary to determine the capacity left in the block.
339  *
340  * @p is_usage indicates that the values in new_vals are used (not defined)
341  * In this case reloads must be performed
342  */
343 static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
344         belady_env_t *env = bi->bel;
345         ir_node *val;
346         int     i, len, max_allowed, demand, iter;
347
348         workset_t *ws         = env->ws;
349         ir_node   **to_insert = alloca(env->n_regs * sizeof(*to_insert));
350
351         /*
352                 1. Identify the number of needed slots and the values to reload
353         */
354         demand = 0;
355         workset_foreach(new_vals, val, iter) {
356                 /* mark value as used */
357
358                 if (! workset_contains(ws, val)) {
359                         DBG((dbg, DBG_DECIDE, "    insert %+F\n", val));
360                         to_insert[demand++] = val;
361                         if (is_usage) {
362                                 int insert_reload = 1;
363
364                                 /*
365                                  * if we use a value which is transported in this block,
366                                  * i.e. a phi defined here or a live in, we check if there
367                                  * is room for that guy to survive from the block's entrance
368                                  * to here or not.
369                                  */
370                                 if (is_transport_in(env, bi->bl, val)) {
371                                         DBG((dbg, DBG_SPILL, "entrance node %+F, capacity %d:\n", val, bi->pressure));
372                                         if (bi->pressure < env->n_regs) {
373                                                 ++bi->pressure;
374                                                 workset_insert(env, bi->entrance_reg, val);
375                                                 insert_reload = 0;
376                                                 DBG((dbg, DBG_SPILL, "... no reload. must be considered at block start\n"));
377                                         }
378
379                                         /*
380                                          * if the value on't survive in a register, note that
381                                          * it will be in the memory so that we can spill phis
382                                          * properly later on.
383                                          */
384                                         else
385                                                 workset_insert(env, bi->entrance_mem, val);
386                                 }
387
388                                 if (insert_reload) {
389                                         DBG((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, env->instr));
390                                         be_add_reload(env->senv, val, env->instr, env->cls, 1);
391                                 }
392                         }
393                 }
394                 else {
395                         assert(is_usage || "Defined value already in workset?!?");
396                         DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
397                 }
398         }
399         DBG((dbg, DBG_DECIDE, "    demand = %d\n", demand));
400
401         /*
402                 2. Make room for at least 'demand' slots
403         */
404         len         = workset_get_length(ws);
405         max_allowed = env->n_regs - demand;
406
407         DBG((dbg, DBG_DECIDE, "    disposing %d values\n", ws->len - max_allowed));
408
409         /* Only make more free room if we do not have enough */
410         if (len > max_allowed) {
411                 /* get current next-use distance */
412                 for (i = 0; i < ws->len; ++i) {
413                         unsigned dist = get_distance(env, env->instr, env->instr_nr, workset_get_val(ws, i), !is_usage);
414                         workset_set_time(ws, i, dist);
415                 }
416
417                 /* sort entries by increasing nextuse-distance*/
418                 workset_sort(ws);
419
420                 /* kill the last 'demand' entries in the array */
421                 workset_set_length(ws, max_allowed);
422         }
423
424         /*
425                 3. Insert the new values into the workset
426         */
427         for (i = 0; i < demand; ++i)
428                 workset_insert(env, env->ws, to_insert[i]);
429 }
430
431 /**
432  * For the given block @p block, decide for each values
433  * whether it is used from a register or is reloaded
434  * before the use.
435  */
436 static void belady(ir_node *block, void *data) {
437         belady_env_t *env        = data;
438         block_info_t *block_info = new_block_info(env, block);
439         workset_t *new_vals;
440         ir_node *irn;
441         int iter;
442
443         /* process the block from start to end */
444         DBG((dbg, DBG_WSETS, "Processing...\n"));
445         env->instr_nr = 0;
446         new_vals = new_workset(env, &env->ob);
447         block_info->first_non_in = NULL;
448
449         sched_foreach(block, irn) {
450                 int i, arity;
451                 assert(workset_get_length(env->ws) <= env->n_regs && "Too much values in workset!");
452
453                 /* projs are handled with the tuple value.
454                  * Phis are no real instr (see insert_starters())
455                  * instr_nr does not increase */
456                 if (is_Proj(irn) || is_Phi(irn)) {
457                         DBG((dbg, DBG_DECIDE, "  ...%+F skipped\n", irn));
458                         continue;
459                 }
460                 DBG((dbg, DBG_DECIDE, "  ...%+F\n", irn));
461
462                 if (!block_info->first_non_in)
463                         block_info->first_non_in = irn;
464
465                 /* set instruction in the workset */
466                 env->instr = irn;
467
468                 /* allocate all values _used_ by this instruction */
469                 workset_clear(new_vals);
470                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
471                         workset_insert(env, new_vals, get_irn_n(irn, i));
472                 }
473                 displace(block_info, new_vals, 1);
474
475                 /* allocate all values _defined_ by this instruction */
476                 workset_clear(new_vals);
477                 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
478                         const ir_edge_t *edge;
479
480                         foreach_out_edge(irn, edge) {
481                                 ir_node *proj = get_edge_src_irn(edge);
482                                 workset_insert(env, new_vals, proj);
483                         }
484                 } else {
485                         workset_insert(env, new_vals, irn);
486                 }
487                 displace(block_info, new_vals, 0);
488
489                 block_info->pressure = MAX(block_info->pressure, workset_get_length(env->ws));
490                 env->instr_nr++;
491         }
492
493         /* Remember end-workset for this block */
494         block_info->ws_end = workset_clone(env, &env->ob, env->ws);
495         DBG((dbg, DBG_WSETS, "End workset for %+F:\n", block));
496         workset_foreach(block_info->ws_end, irn, iter)
497                 DBG((dbg, DBG_WSETS, "  %+F (%u)\n", irn, workset_get_time(block_info->ws_end, iter)));
498 }
499
500 /*
501  _____ _                  _       _           _   ____            _
502 |_   _| |__   ___    __ _| | ___ | |__   __ _| | |  _ \ __ _ _ __| |_
503   | | | '_ \ / _ \  / _` | |/ _ \| '_ \ / _` | | | |_) / _` | '__| __|
504   | | | | | |  __/ | (_| | | (_) | |_) | (_| | | |  __/ (_| | |  | |_
505   |_| |_| |_|\___|  \__, |_|\___/|_.__/ \__,_|_| |_|   \__,_|_|   \__|
506                     |___/
507
508 */
509
510 static int block_freq_gt(const void *a, const void *b)
511 {
512         const ir_node * const *p = a;
513         const ir_node * const *q = b;
514         block_info_t *pi = get_block_info(*p);
515         block_info_t *qi = get_block_info(*q);
516         return qi->exec_freq - pi->exec_freq;
517 }
518
519 typedef struct _block_end_state_t {
520         ir_node *bl;
521         ir_node *irn;
522         double costs;
523         workset_t *end_state;
524         unsigned reload_at_end : 1;
525         unsigned live_through  : 1;
526 } block_end_state_t;
527
528 typedef struct _global_end_state_t {
529         belady_env_t *env;
530         bitset_t *succ_phis;
531         struct obstack obst;
532         block_end_state_t *end_info;
533         unsigned gauge;
534         unsigned version;
535 } global_end_state_t;
536
537 static block_end_state_t *get_block_end_state(global_end_state_t *state, ir_node *bl, ir_node *irn)
538 {
539         unsigned i;
540
541         for (i = 0; i < state->gauge; ++i) {
542                 block_end_state_t *bei = &state->end_info[i];
543                 if (bei->bl == bl && bei->irn == irn)
544                         return bei;
545         }
546
547         {
548                 block_info_t *bi = get_block_info(bl);
549                 block_end_state_t *curr;
550
551                 /* make sure we have room in the array */
552                 ARR_EXTO(block_end_state_t, state->end_info, (int) state->gauge);
553
554                 curr = &state->end_info[state->gauge];
555
556                 memset(curr, 0, sizeof(curr[0]));
557                 curr->bl  = bl;
558                 curr->irn = irn;
559                 curr->end_state = workset_clone(state->env, &state->obst, bi->ws_end);
560                 curr->costs = -1.0;
561                 ++state->gauge;
562                 return curr;
563         }
564 }
565
566 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level);
567
568 static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level)
569 {
570         block_end_state_t *bes = get_block_end_state(ges, bl, irn);
571         workset_t *end         = bes->end_state;
572         block_info_t *bi       = get_block_info(bl);
573         int n_regs             = bi->bel->n_regs;
574         int index;
575
576         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}can make avail %+F at end of %+F (pressure %d)\n",
577                                 level, irn, bl, bi->pressure));
578
579         /*
580          * to make the value available at end,
581          * we have several cases here.
582          *
583          * - we already visited that block.
584          * - If the value is in the final end set, return 0.
585          *   somebody else already allocated it there.
586          * - If not and the final end set is already full,
587          *   we cannot make the value available at the end
588          *   of this block. return INFINITY.
589          * - Else (value not in final end set and there is room):
590          *   1) The value is in a register at the end of the local Belady pass.
591          *      Allocate a slot in  the final end set and return 0.
592          *   2) The value is not in the Belady end set:
593          *      If the block's capacity is < k then check what it costs
594          *      to transport the value from upper blocks to this block.
595          *      Compare that against the reload cost in this block. If
596          *      cheaper, do the other thing. If not, reload it here.
597          */
598
599         /*
600          * we have been here before and already figured out some costs.
601          * so we can exit safely.
602          */
603         if (bes->costs >= 0.0) {
604                 DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}we\'ve been here before\n", level));
605                 goto end;
606         }
607
608         /* if the end set contains it already, it is in a reg and it costs nothing
609          * to load it to one. */
610         index = workset_get_index(end, irn);
611         if (index >= 0) {
612                 unsigned ver = end->vals[index].version;
613                 DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}node is in the end set and is %s fixed\n",
614                                         level, ver > ges->version ? "" : "not"));
615
616                 /*
617                  * if the version is older, the value is already fixed
618                  * and cannot be removed from the end set. If not,
619                  * we fix it here by giving it our version.
620                  */
621                 if (ver < ges->version)
622                         end->vals[index].version = ges->version;
623
624                 bes->costs = 0.0;
625                 goto end;
626         }
627
628         /*
629          * Now we have two options:
630          * 1) Reload the value at the end of the block.
631          *    Therefore, perhaps, we have to erase another one from the workset.
632          *    This may only be done if it has not been fixed.
633          *    Since fixed means that a previous pass has decided that that value
634          *    *has* to stay in the end set.
635          * 2) we can try, if the capacity of the block allows it, to let
636          *    the value live through the block and make it available at
637          *    the entrance.
638          *
639          * First, we test the local (reload in this block) alternative
640          * and compare against the other alternative.
641          * Of course, we chose the cheaper one.
642          */
643
644         {
645                 int len = workset_get_length(end);
646                 int slot = -1;
647                 int i;
648
649                 bes->costs = HUGE_VAL;
650
651                 /*
652                  * look if there is room in the end array
653                  * for the variable. Note that this does not
654                  * means that the var is living through the block.
655                  * There is just room at the *end*
656                  */
657                 if (len < n_regs) {
658                         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}the end set has %d free slots\n",
659                                                 level, n_regs - len));
660                         slot = len;
661                 }
662                 else {
663                         for (i = 0; i < len; ++i)
664                                 if (end->vals[i].version < ges->version)
665                                         break;
666
667                         if (i < len) {
668                                 DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}%+F (slot %d) can be erased from the end set\n",
669                                                         level, end->vals[i].irn, i));
670                                 slot = i;
671                         }
672                 }
673
674                 if (slot > 0) {
675                         int gauge          = ges->gauge;
676                         double reload_here = bi->exec_freq;
677                         double bring_in    = bi->pressure < n_regs ? can_bring_in(ges, bl, irn, level + 1) : HUGE_VAL;
678
679                         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}there is a free slot. capacity=%d, reload here=%f, bring in=%f\n",
680                                                 level, n_regs - bi->pressure, reload_here, bring_in));
681
682                         /*
683                          * reloading here pays off; bringing the value in from elsewhere
684                          * is too expensive, hence we drop that search by resetting
685                          * the gauge.
686                          */
687                         if (reload_here <= bring_in) {
688                                 ges->gauge = gauge;
689                                 bes->costs = reload_here;
690                                 bes->reload_at_end = 1;
691                         }
692
693                         else {
694                                 bes->live_through = 1;
695                                 bes->costs = bring_in;
696                         }
697
698                         end->vals[slot].irn     = irn;
699                         end->vals[slot].version = ges->version;
700                 }
701         }
702
703 end:
704         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}-> %f\n", level, bes->costs));
705         return bes->costs;
706 }
707
708 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level)
709 {
710         double glob_costs = HUGE_VAL;
711         int def_block     = bl == get_nodes_block(irn);
712         int phi           = is_Phi(irn);
713
714         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}can bring in for %+F at block %+F\n", level, irn, bl));
715
716         if (phi || !def_block) {
717                 int i, n           = get_irn_arity(bl);
718                 ir_node **nodes    = alloca(get_irn_arity(bl) * sizeof(nodes[0]));
719
720                 int gauge_begin    = ges->gauge;
721
722                 glob_costs = 0.0;
723                 for (i = 0; i < n; ++i) {
724                         ir_node *pr = get_Block_cfgpred_block(bl, i);
725                         ir_node *op = phi && def_block ? get_irn_n(irn, i) : irn;
726                         double c    = can_make_available_at_end(ges, pr, op, level + 1);
727
728                         if (c >= HUGE_VAL) {
729                                 ges->gauge = gauge_begin;
730                                 glob_costs = HUGE_VAL;
731                                 goto end;
732                         }
733
734                         glob_costs += c;
735                 }
736         }
737
738 end:
739         DBG((dbg, DBG_GLOBAL, "\t%{firm:indent}-> %f\n", level, glob_costs));
740         return glob_costs;
741 }
742
743 static void materialize_and_commit_end_state(global_end_state_t *ges)
744 {
745         belady_env_t *env = ges->env;
746         unsigned i;
747
748         DBG((dbg, DBG_GLOBAL, "\tmaterializing\n"));
749         for (i = 0; i < ges->gauge; ++i) {
750                 block_end_state_t *bes = &ges->end_info[i];
751                 block_info_t *bi       = get_block_info(bes->bl);
752                 int idx;
753
754                 /* insert the reload if the val was reloaded at the block's end */
755                 if (bes->reload_at_end) {
756                         be_add_reload(env->senv, bes->irn, bes->bl, env->cls, 1);
757                         DBG((dbg, DBG_GLOBAL, "\t\tadding reload of %+F at end of %+F\n", bes->irn, bes->bl));
758                 }
759
760                 /*
761                  * if the variable is live through the block,
762                  * update the pressure indicator.
763                  */
764                 bi->pressure = MAX(bi->pressure + bes->live_through, workset_get_length(bes->end_state));
765
766                 idx = workset_get_index(bes->end_state, bes->irn);
767
768                 /*
769                  * set the version number in the workset.
770                  * That will mark this value as fixed in the end set
771                  * and will prevent further investigations from removing
772                  * it from there.
773                  * Also "commit" the workset;
774                  * by copying it back to the block's end workset.
775                  */
776                 if (idx >= 0) {
777                         DBG((dbg, DBG_GLOBAL, "\t\tcommiting workset of %+F with version %x\n", bes->bl, ges->version));
778                         bes->end_state->vals[idx].version = ges->version;
779                         workset_copy(env, bi->ws_end, bes->end_state);
780                 }
781         }
782 }
783
784 /**
785  * Examine all irns which shall be in regs at the beginning of the
786  * block.
787  */
788 static void fix_block_borders(global_end_state_t *ges, ir_node *block) {
789         block_info_t *bi  = get_block_info(block);
790         belady_env_t *env = ges->env;
791
792         ir_node *irn;
793         int i;
794
795         DBG((dbg, DBG_GLOBAL, "fixing block borders at %+F (%fHz)\n", block, bi->exec_freq));
796
797         /* process all variables which shall be in a reg at
798          * the beginning of the block in the order of the next use. */
799         workset_foreach(bi->entrance_reg, irn, i) {
800                 int is_entrance_phi = is_Phi(irn) && get_nodes_block(irn) == block;
801                 double bring_in_costs;
802
803                 /* reset the gauge and begin the search */
804                 ges->gauge = 0;
805                 --ges->version;
806
807                 DBG((dbg, DBG_GLOBAL, "\ttrans in var %+F, version %x\n", irn, ges->version));
808
809                 bring_in_costs = can_bring_in(ges, block, irn, 0);
810
811                 /* we were not able to let the value arrive
812                  * in a register at the entrance of the block
813                  * so we have to do the reload locally */
814                 if (bring_in_costs > bi->exec_freq) {
815                         DBG((dbg, DBG_GLOBAL, "\tbring in: %f, local: %f -> doing reload at beginning\n",
816                                                 bring_in_costs, bi->exec_freq));
817
818                         be_add_reload(env->senv, irn, bi->first_non_in, env->cls, 1);
819                 }
820
821                 else {
822                         /*
823                          * Mark this phi as succeeded.
824                          * It was not replaced by a reload at the block's entrance
825                          * and thus is not phi_spilled.
826                          */
827                         if (is_entrance_phi)
828                                 bitset_add_irn(ges->succ_phis, irn);
829
830                         materialize_and_commit_end_state(ges);
831                 }
832         }
833 }
834
835 static void global_assign(belady_env_t *env)
836 {
837         global_end_state_t ges;
838         int i;
839
840         obstack_init(&ges.obst);
841         ges.gauge     = 0;
842         ges.env       = env;
843         ges.version   = -1;
844         ges.end_info  = NEW_ARR_F(block_end_state_t, env->n_blocks);
845         ges.succ_phis = bitset_irg_obstack_alloc(&env->ob, env->irg);
846
847         /*
848          * sort the blocks according to execution frequency.
849          * That's not necessary for belady() but for the global pass later on.
850          */
851         qsort(env->blocks, env->n_blocks, sizeof(env->blocks[0]), block_freq_gt);
852
853         for (i = 0; i < env->n_blocks; ++i)
854                 fix_block_borders(&ges, env->blocks[i]);
855
856         /*
857          * Now we spill phis which cannot be kept since they were replaced
858          * by reloads at the block entrances.
859          */
860         for (i = 0; i < env->n_blocks; ++i) {
861                 ir_node *bl = env->blocks[i];
862                 ir_node *irn;
863
864                 sched_foreach(bl, irn) {
865                         if (!is_Phi(irn))
866                                 break;
867
868                         if (bitset_contains_irn(ges.succ_phis, irn))
869                                 be_spill_phi(env->senv, irn);
870                 }
871         }
872
873 }
874
875 static void collect_blocks(ir_node *bl, void *data)
876 {
877         belady_env_t *env = data;
878         ++env->n_blocks;
879         obstack_ptr_grow(&env->ob, bl);
880 }
881
882 void be_spill_belady_spill_env2(be_irg_t *birg, const arch_register_class_t *cls, spill_env_t *spill_env) {
883         belady_env_t env;
884         ir_graph *irg = be_get_birg_irg(birg);
885         int i, n_regs;
886
887         /* some special classes contain only ignore regs, nothing to do then */
888         n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
889         if(n_regs == 0)
890                 return;
891
892         be_clear_links(irg);
893
894         /* init belady env */
895         obstack_init(&env.ob);
896         env.irg       = irg;
897         env.arch      = birg->main_env->arch_env;
898         env.cls       = cls;
899         env.lv        = be_get_birg_liveness(birg);
900         env.n_regs    = n_regs;
901         env.ws        = new_workset(&env, &env.ob);
902         env.uses      = be_begin_uses(irg, env.lv);
903         env.senv      = spill_env ? spill_env : be_new_spill_env(birg);
904         env.ef        = be_get_birg_exec_freq(birg);
905         env.n_blocks  = 0;
906
907         irg_block_walk_graph(irg, NULL, collect_blocks, &env);
908         obstack_ptr_grow(&env.ob, NULL);
909         env.blocks = obstack_finish(&env.ob);
910
911         /* Fix high register pressure with belady algorithm */
912         for (i = 0; i < env.n_blocks; ++i)
913                 belady(env.blocks[i], &env);
914
915         global_assign(&env);
916
917         /* Insert spill/reload nodes into the graph and fix usages */
918         be_insert_spills_reloads(env.senv);
919
920         /* clean up */
921         if(spill_env == NULL)
922                 be_delete_spill_env(env.senv);
923         be_end_uses(env.uses);
924         obstack_free(&env.ob, NULL);
925 }
926
927
928 /**
929  * Do spilling for a register class on a graph using the belady heuristic.
930  * In the transformed graph, the register pressure never exceeds the number
931  * of available registers.
932  *
933  * @param birg  The backend graph
934  * @param cls   The register class to spill
935  */
936 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *cls) {
937         be_spill_belady_spill_env2(birg, cls, NULL);
938 }
939
940
941 void be_init_spillbelady2(void)
942 {
943         static be_spiller_t belady_spiller = {
944                 be_spill_belady
945         };
946
947         be_register_spiller("belady2", &belady_spiller);
948         FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady2");
949 }
950
951 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady2);