Some more boolopt tests.
[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      Sebastian Hack, Matthias Braun, Daniel Grund
24  * @date        01.08.2007
25  * @version     $Id$
26  *
27  * The main differences to the original Belady are:
28  * - The workset is empty at the start of a block
29  *   There is no attempt to fill it with variables which
30  *   are not used in the block.
31  * - There is a global pass which tries to use the remaining
32  *   capacity of the blocks to let global variables live through
33  *   them.
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <math.h>
40 #include <limits.h>
41
42 #include "obst.h"
43 #include "irnodeset.h"
44 #include "irbitset.h"
45 #include "irprintf_t.h"
46 #include "irgraph.h"
47 #include "irnode.h"
48 #include "irmode.h"
49 #include "irgwalk.h"
50 #include "irloop.h"
51 #include "iredges_t.h"
52 #include "irphase_t.h"
53 #include "ircons_t.h"
54 #include "irprintf.h"
55 #include "execfreq.h"
56 #include "dfs_t.h"
57 #include "xmalloc.h"
58
59 #include "beutil.h"
60 #include "bearch_t.h"
61 #include "bespillbelady.h"
62 #include "besched_t.h"
63 #include "beirgmod.h"
64 #include "belive_t.h"
65 #include "benode_t.h"
66 #include "bechordal_t.h"
67 #include "bespilloptions.h"
68 #include "beloopana.h"
69 #include "beirg_t.h"
70 #include "bemodule.h"
71
72 #include <libcore/lc_opts.h>
73 #include <libcore/lc_opts_enum.h>
74 #include <libcore/lc_timing.h>
75
76 #define DBG_SPILL     1
77 #define DBG_WSETS     2
78 #define DBG_FIX       4
79 #define DBG_DECIDE    8
80 #define DBG_START    16
81 #define DBG_SLOTS    32
82 #define DBG_TRACE    64
83 #define DBG_WORKSET 128
84 #define DBG_GLOBAL  256
85
86 #define ALREADY_SPILLED_FACTOR 2
87
88 #define DEAD       UINT_MAX
89 #define LIVE_END   (DEAD-1)
90 #define REMAT_DIST (DEAD-2)
91
92 static int already_spilled_factor = 2;
93 static int remat_live_range_ext   = 1;
94 static int global_pass_enabled    = 1;
95
96 static const lc_opt_table_entry_t options[] = {
97         LC_OPT_ENT_INT           ("asf",    "already spilled factor",                             &already_spilled_factor),
98         LC_OPT_ENT_BOOL          ("remat",  "rematerializable ops get infinite long live ranges", &remat_live_range_ext),
99         LC_OPT_ENT_BOOL          ("global", "enable/disable the global pass",                     &global_pass_enabled),
100         LC_OPT_LAST
101 };
102
103 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
104
105 /**
106  * An association between a node and a point in time.
107  */
108 typedef struct _loc_t {
109   ir_node *irn;        /**< A node. */
110   unsigned time;       /**< A use time.
111                                                  In the global pass this is used
112                                                  as the version number and not as a time.
113                                                  Only to save space...
114                                                 */
115 } loc_t;
116
117 typedef struct _workset_t {
118         int len;                        /**< current length */
119         loc_t vals[0];          /**< inlined array of the values/distances in this working set */
120 } workset_t;
121
122 typedef struct _belady_env_t {
123         struct obstack ob;
124         ir_graph *irg;
125         const dfs_t *dfs;
126         const arch_env_t *arch;
127         const arch_register_class_t *cls;
128         be_lv_t *lv;
129         ir_exec_freq *ef;
130
131         ir_node **blocks;   /**< Array of all blocks. */
132         int n_blocks;       /**< Number of blocks in the graph. */
133         int n_regs;                     /**< number of regs in this reg-class */
134         workset_t *ws;          /**< the main workset used while processing a block. ob-allocated */
135         ir_node *instr;         /**< current instruction */
136         int instr_nr;           /**< current instruction number (relative to block start) */
137
138         spill_env_t *senv;      /**< see bespill.h */
139         bitset_t *spilled;  /**< bitset to keep all the irns which have already been spilled. */
140 } belady_env_t;
141
142
143 static int loc_compare(const void *a, const void *b)
144 {
145         const loc_t *p = a;
146         const loc_t *q = b;
147         return (p->time > q->time) - (p->time < q->time);
148 }
149
150 static INLINE void workset_print(const workset_t *w)
151 {
152         int i;
153
154         for(i = 0; i < w->len; ++i) {
155                 ir_fprintf(stderr, "%+F %d\n", w->vals[i].irn, w->vals[i].time);
156         }
157 }
158
159 /**
160  * Alloc a new workset on obstack @p ob with maximum size @p max
161  */
162 static INLINE workset_t *new_workset(belady_env_t *env, struct obstack *ob) {
163         workset_t *res;
164         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
165         res = obstack_alloc(ob, size);
166         memset(res, 0, size);
167         return res;
168 }
169
170 /**
171  * Alloc a new instance on obstack and make it equal to @param ws
172  */
173 static INLINE workset_t *workset_clone(belady_env_t *env, struct obstack *ob, workset_t *ws) {
174         workset_t *res;
175         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
176         res = obstack_alloc(ob, size);
177         memcpy(res, ws, size);
178         return res;
179 }
180
181 /**
182  * Do NOT alloc anything. Make @param tgt equal to @param src.
183  * returns @param tgt for convenience
184  */
185 static INLINE workset_t *workset_copy(belady_env_t *env, workset_t *tgt, workset_t *src) {
186         size_t size = sizeof(*src) + (env->n_regs)*sizeof(src->vals[0]);
187         memcpy(tgt, src, size);
188         return tgt;
189 }
190
191 /**
192  * Overwrites the current content array of @param ws with the
193  * @param count locations given at memory @param locs.
194  * Set the length of @param ws to count.
195  */
196 static INLINE void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs) {
197         workset->len = count;
198         memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
199 }
200
201 /**
202  * Inserts the value @p val into the workset, iff it is not
203  * already contained. The workset must not be full.
204  */
205 static INLINE void workset_insert(belady_env_t *env, workset_t *ws, ir_node *val) {
206         int i;
207         /* check for current regclass */
208         if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, val)) {
209                 DBG((dbg, DBG_WORKSET, "Skipped %+F\n", val));
210                 return;
211         }
212
213         /* check if val is already contained */
214         for(i=0; i<ws->len; ++i)
215                 if (ws->vals[i].irn == val)
216                         return;
217
218         /* insert val */
219         assert(ws->len < env->n_regs && "Workset already full!");
220         ws->vals[ws->len++].irn = val;
221 }
222
223 /**
224  * Removes all entries from this workset
225  */
226 static INLINE void workset_clear(workset_t *ws) {
227         ws->len = 0;
228 }
229
230 /**
231  * Removes the value @p val from the workset if present.
232  */
233 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
234         int i;
235         for(i=0; i<ws->len; ++i) {
236                 if (ws->vals[i].irn == val) {
237                         ws->vals[i] = ws->vals[--ws->len];
238                         return;
239                 }
240         }
241 }
242
243 static INLINE int workset_get_index(const workset_t *ws, const ir_node *val) {
244         int i;
245         for(i=0; i<ws->len; ++i) {
246                 if (ws->vals[i].irn == val)
247                         return i;
248         }
249
250         return -1;
251 }
252
253 /**
254  * Iterates over all values in the working set.
255  * @p ws The workset to iterate
256  * @p v  A variable to put the current value in
257  * @p i  An integer for internal use
258  */
259 #define workset_foreach(ws, v, i)       for(i=0; \
260                                                                                 v=(i < ws->len) ? ws->vals[i].irn : NULL, i < ws->len; \
261                                                                                 ++i)
262
263 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
264 #define workset_get_time(ws, i) (ws)->vals[i].time
265 #define workset_set_length(ws, length) (ws)->len = length
266 #define workset_get_length(ws) ((ws)->len)
267 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
268 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
269 #define workset_contains(ws, n) (workset_get_index(ws, n) >= 0)
270
271 typedef struct _block_info_t {
272         belady_env_t *bel;
273         const ir_node *bl;
274         workset_t *ws_start, *ws_end;
275         ir_phase next_uses;
276         int id;
277
278         ir_node *first_non_in;   /**< First node in block which is not a phi.  */
279         ir_node *last_ins;       /**< The instruction before which end of
280                                                            block reloads will be inserted. */
281
282         workset_t *entrance_reg; /**< That set will contain all values
283                                                                   transported into the block which
284                                                                   are used before they are displaced.
285                                                                   That means, we later have to care to
286                                                                   bring them into the block in a register
287                                                                   or reload them at the entry of the block. */
288
289         int pressure; /**< The amount of registers which remain free
290                                         in this block. This capacity can be used to let
291                                         global variables, transported into other blocks,
292                                         live through this block. */
293
294         double exec_freq; /**< The execution frequency of this block. */
295 } block_info_t;
296
297 static INLINE void *new_block_info(belady_env_t *bel, int id)
298 {
299         ir_node      *bl  = bel->blocks[id];
300         block_info_t *res = obstack_alloc(&bel->ob, sizeof(*res));
301         memset(res, 0, sizeof(res[0]));
302         res->first_non_in = NULL;
303         res->last_ins = NULL;
304         res->bel = bel;
305         res->bl  = bl;
306         res->id  = id;
307         res->entrance_reg = new_workset(bel, &bel->ob);
308         res->exec_freq    = get_block_execfreq(bel->ef, bl);
309         set_irn_link(bl, res);
310         return res;
311 }
312
313 #define get_block_info(block)        ((block_info_t *)get_irn_link(block))
314 #define set_block_info(block, info)  set_irn_link(block, info)
315
316 static INLINE ir_node *block_info_get_last_ins(block_info_t *bi)
317 {
318         if (!bi->last_ins)
319                 bi->last_ins = be_get_end_of_block_insertion_point(bi->bl);
320
321         return bi->last_ins;
322 }
323
324 typedef struct _next_use_t {
325         unsigned is_first_use : 1; /**< Indicate that this use is the first
326                                                                  in the block. Needed to identify
327                                                                  transport in values for the global
328                                                                  pass. */
329         int step;                  /**< The time step of the use. */
330         ir_node *irn;
331         struct _next_use_t *next;  /**< The next use int this block
332                                                                  or NULL. */
333 } next_use_t;
334
335 static void *next_use_init(ir_phase *phase, ir_node *irn, void *old)
336 {
337         (void) phase;
338         (void) irn;
339         (void) old;
340         return NULL;
341 }
342
343 static void build_next_uses(block_info_t *bi)
344 {
345         ir_node *irn;
346
347         sched_renumber(bi->bl);
348
349         phase_init(&bi->next_uses, "next uses", bi->bel->irg, PHASE_DEFAULT_GROWTH, next_use_init, NULL);
350         sched_foreach_reverse(bi->bl, irn) {
351                 int i;
352
353                 if (is_Phi(irn))
354                         break;
355
356                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
357                         ir_node *op = get_irn_n(irn, i);
358                         next_use_t *curr = phase_get_irn_data(&bi->next_uses, op);
359                         next_use_t *use  = phase_alloc(&bi->next_uses, sizeof(use[0]));
360
361                         use->is_first_use = 1;
362                         use->step         = sched_get_time_step(irn);
363                         use->next         = curr;
364                         use->irn          = irn;
365
366                         if (curr) {
367                                 curr->is_first_use = 0;
368                                 assert(curr->step >= use->step);
369                         }
370
371                         phase_set_irn_data(&bi->next_uses, op, use);
372                 }
373         }
374 }
375
376 #define get_current_use(bi, irn)         phase_get_irn_data(&(bi)->next_uses, (irn))
377
378 static INLINE void advance_current_use(block_info_t *bi, const ir_node *irn)
379 {
380         next_use_t *use = get_current_use(bi, irn);
381
382         assert(use);
383         phase_set_irn_data(&bi->next_uses, irn, use->next);
384 }
385
386 static INLINE unsigned get_curr_distance(block_info_t *bi, const ir_node *irn, int is_usage)
387 {
388         belady_env_t *env = bi->bel;
389         int curr_step     = sched_get_time_step(env->instr);
390         next_use_t *use   = get_current_use(bi, irn);
391         int flags         = arch_irn_get_flags(env->arch, irn);
392
393         assert(!(flags & arch_irn_flags_ignore));
394
395         /* We have to keep nonspillable nodes in the workingset */
396         if(flags & arch_irn_flags_dont_spill)
397                 return 0;
398
399         if (!is_usage && use && use->step == curr_step)
400                 use = use->next;
401
402         if (use) {
403                 unsigned res  = use->step - curr_step;
404
405                 assert(use->step >= curr_step);
406
407                 if (res != 0) {
408                         if (remat_live_range_ext && be_is_rematerializable(env->senv, irn, use->irn))
409                                 res = REMAT_DIST;
410                         else if (bitset_contains_irn(env->spilled, irn))
411                                 res *= already_spilled_factor;
412                 }
413
414                 return res;
415         }
416
417         return be_is_live_end(env->lv, bi->bl, irn) ? LIVE_END : DEAD;
418 }
419
420 static INLINE int is_local_phi(const ir_node *bl, const ir_node *irn)
421 {
422         return is_Phi(irn) && get_nodes_block(irn) == bl;
423 }
424
425 /**
426  * Check, if the value is something that is transported into a block.
427  * That is, the value is defined elsewhere or defined by a Phi in the block.
428  * @param env  The belady environment.
429  * @param bl   The block in question.
430  * @param irn  The node in question.
431  * @return     1, if node is something transported into @p bl, 0 if not.
432  * @note       The function will only give correct answers in the case
433  *             where @p irn is unsed in the block @p bl which is always
434  *             the case in our usage scenario.
435  */
436 static INLINE int is_transport_in(const ir_node *bl, const ir_node *irn)
437 {
438         return is_local_phi(bl, irn) || get_nodes_block(irn) != bl;
439 }
440
441 /**
442  * Performs the actions necessary to grant the request that:
443  * - new_vals can be held in registers
444  * - as few as possible other values are disposed
445  * - the worst values get disposed
446  *
447  * @p is_usage indicates that the values in new_vals are used (not defined)
448  * In this case reloads must be performed
449  */
450 static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
451         belady_env_t *env       = bi->bel;
452         workset_t    *ws        = env->ws;
453         ir_node     **to_insert = alloca(env->n_regs * sizeof(to_insert[0]));
454
455         int i, len, max_allowed, demand, iter;
456         ir_node *val;
457
458         /*
459                 1. Identify the number of needed slots and the values to reload
460         */
461         demand = 0;
462         workset_foreach(new_vals, val, iter) {
463                 /* mark value as used */
464
465                 if (! workset_contains(ws, val)) {
466                         DBG((dbg, DBG_DECIDE, "    insert %+F\n", val));
467                         to_insert[demand++] = val;
468                         if (is_usage) {
469                                 int insert_reload = 1;
470                                 next_use_t *use = get_current_use(bi, val);
471
472                                 /*
473                                  * if we use a value which is transported in this block, i.e. a
474                                  * phi defined here or a live in, for the first time, we check
475                                  * if there is room for that guy to survive from the block's
476                                  * entrance to here or not.
477                                  */
478                                 assert(use);
479                                 assert(sched_get_time_step(env->instr) == use->step);
480                                 if (is_transport_in(bi->bl, val) && use->is_first_use) {
481                                         DBG((dbg, DBG_DECIDE, "entrance node %+F, pressure %d:\n", val, bi->pressure));
482                                         if (bi->pressure < env->n_regs) {
483                                                 workset_insert(env, bi->entrance_reg, val);
484                                                 insert_reload = 0;
485                                                 ++bi->pressure;
486                                                 DBG((dbg, DBG_DECIDE, "... no reload. must be considered at block start\n"));
487                                         }
488                                 }
489
490                                 if (insert_reload) {
491                                         bitset_add_irn(env->spilled, val);
492                                         DBG((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, env->instr));
493                                         be_add_reload(env->senv, val, env->instr, env->cls, 1);
494                                 }
495                         }
496                 } else {
497                         assert(is_usage || "Defined value already in workset?!?");
498                         DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
499                 }
500         }
501         DBG((dbg, DBG_DECIDE, "    demand = %d\n", demand));
502
503         /*
504                 2. Make room for at least 'demand' slots
505         */
506         len         = workset_get_length(ws);
507         max_allowed = env->n_regs - demand;
508
509         /* Only make more free room if we do not have enough */
510         if (len > max_allowed) {
511                 DBG((dbg, DBG_DECIDE, "    disposing %d values\n", len - max_allowed));
512
513                 /* get current next-use distance */
514                 for (i = 0; i < ws->len; ++i) {
515                         ir_node *val  = workset_get_val(ws, i);
516                         unsigned dist = get_curr_distance(bi, val, is_usage);
517                         workset_set_time(ws, i, dist);
518                 }
519
520                 /* sort entries by increasing nextuse-distance*/
521                 workset_sort(ws);
522
523                 /* kill the last 'demand' entries in the array */
524                 workset_set_length(ws, max_allowed);
525         }
526
527         /*
528                 3. Insert the new values into the workset
529                    Also, we update the pressure in the block info.
530                    That is important for the global pass to decide
531                    how many values can live through the block.
532         */
533         for (i = 0; i < demand; ++i)
534                 workset_insert(env, env->ws, to_insert[i]);
535
536         bi->pressure = MAX(bi->pressure, workset_get_length(env->ws));
537 }
538
539 /**
540  * For the given block @p block, decide for each values
541  * whether it is used from a register or is reloaded
542  * before the use.
543  */
544 static void belady(belady_env_t *env, int id) {
545         block_info_t *block_info = new_block_info(env, id);
546         const ir_node *block     = block_info->bl;
547         void *obst_state         = obstack_base(&env->ob);
548
549         workset_t *new_vals;
550         ir_node *irn;
551         int iter;
552
553         DBG((dbg, DBG_WSETS, "Processing %+F...\n", block_info->bl));
554         new_vals = new_workset(env, &env->ob);
555         workset_clear(env->ws);
556
557         /* build the next use information for this block. */
558         build_next_uses(block_info);
559
560         env->instr_nr = 0;
561         block_info->first_non_in = NULL;
562
563         /* process the block from start to end */
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                 if (!block_info->first_non_in)
578                         block_info->first_non_in = irn;
579
580                 /* set instruction in the workset */
581                 env->instr = irn;
582
583                 /* allocate all values _used_ by this instruction */
584                 workset_clear(new_vals);
585                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
586                         workset_insert(env, new_vals, get_irn_n(irn, i));
587                 }
588                 displace(block_info, new_vals, 1);
589
590                 /*
591                  * set all used variables to the next use in their next_use_t list
592                  * Also, kill all dead variables from the workset. They are only
593                  * augmenting the pressure. Note, that a variable is dead
594                  * if it has no further use in this block and is *not* live end
595                  */
596                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
597                         ir_node *op = get_irn_n(irn, i);
598                         next_use_t *use = get_current_use(block_info, op);
599
600                         assert(use);
601                         if (!use->next && !be_is_live_end(env->lv, block, op))
602                                 workset_remove(env->ws, op);
603
604                         advance_current_use(block_info, op);
605                 }
606
607                 /* allocate all values _defined_ by this instruction */
608                 workset_clear(new_vals);
609                 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
610                         const ir_edge_t *edge;
611
612                         foreach_out_edge(irn, edge) {
613                                 ir_node *proj = get_edge_src_irn(edge);
614                                 workset_insert(env, new_vals, proj);
615                         }
616                 } else {
617                         workset_insert(env, new_vals, irn);
618                 }
619                 displace(block_info, new_vals, 0);
620
621                 env->instr_nr++;
622         }
623
624         phase_free(&block_info->next_uses);
625         obstack_free(&env->ob, obst_state);
626
627         /* Remember end-workset for this block */
628         block_info->ws_end = workset_clone(env, &env->ob, env->ws);
629         DBG((dbg, DBG_WSETS, "End workset for %+F:\n", block));
630         workset_foreach(block_info->ws_end, irn, iter)
631                 DBG((dbg, DBG_WSETS, "  %+F (%u)\n", irn, workset_get_time(block_info->ws_end, iter)));
632         DBG((dbg, DBG_WSETS, "Max pressure in block: %d\n", block_info->pressure));
633 }
634
635 /*
636  _____ _                  _       _           _   ____            _
637 |_   _| |__   ___    __ _| | ___ | |__   __ _| | |  _ \ __ _ _ __| |_
638   | | | '_ \ / _ \  / _` | |/ _ \| '_ \ / _` | | | |_) / _` | '__| __|
639   | | | | | |  __/ | (_| | | (_) | |_) | (_| | | |  __/ (_| | |  | |_
640   |_| |_| |_|\___|  \__, |_|\___/|_.__/ \__,_|_| |_|   \__,_|_|   \__|
641                     |___/
642
643 */
644
645 #define workset_set_version(ws, i, t) ((ws)->vals[(i)].time = (t))
646 #define workset_get_version(ws, i)    ((ws)->vals[(i)].time)
647
648 #define ver_oldest                        (0)
649 #define ver_youngest                      ((unsigned) -1)
650 #define ver_make_newer(v)                 ((v) + 1)
651 #define ver_is_older(v, w)                ((v) < (w))
652 #define ver_is_younger(v, w)              ((v) > (w))
653
654 static int block_freq_gt(const void *a, const void *b)
655 {
656         const ir_node * const *p = a;
657         const ir_node * const *q = b;
658         block_info_t *pi = get_block_info(*p);
659         block_info_t *qi = get_block_info(*q);
660         double diff = qi->exec_freq - pi->exec_freq;
661         return (diff > 0) - (diff < 0);
662 }
663
664 static int block_order(const void *a, const void *b)
665 {
666         const ir_node * const *p = a;
667         const ir_node * const *q = b;
668         block_info_t *pi = get_block_info(*p);
669         block_info_t *qi = get_block_info(*q);
670         double diff;
671
672         if (pi->exec_freq > 1.0 && qi->exec_freq > 1.0) {
673                 const dfs_t *dfs = pi->bel->dfs;
674                 int pp = dfs_get_post_num(dfs, pi->bl);
675                 int pq = dfs_get_post_num(dfs, qi->bl);
676                 return pq - pp;
677         }
678
679         diff = qi->exec_freq - pi->exec_freq;
680         return (diff > 0) - (diff < 0);
681 }
682
683 enum {
684         irn_act_none = 0,
685         irn_act_reload,
686         irn_act_live_through
687 };
688
689 typedef struct _block_state_t {
690         struct _block_state_t *next;
691         struct _block_state_t *next_intern;
692         block_info_t *bi;
693         unsigned pressure;
694         workset_t *end_state;
695 } block_state_t;
696
697 typedef struct _irn_action_t {
698         struct _irn_action_t *next;
699         ir_node *irn;
700         const ir_node *bl;
701         int act;
702 } irn_action_t;
703
704 typedef struct _global_end_state_t {
705         belady_env_t *env;
706         bitset_t *succ_phis;
707         bitset_t *committed;
708         struct obstack obst;
709         void *reset_level;
710         unsigned version;
711
712         unsigned       *bs_tops_vers;
713         block_state_t **bs_tops;
714         block_state_t  *bs_top;
715         irn_action_t   *ia_top;
716 } global_end_state_t;
717
718 typedef struct {
719         void          *obst_level;
720         block_state_t *bs_top;
721         irn_action_t  *ia_top;
722 } rollback_info_t;
723
724 static INLINE block_state_t *get_block_state(global_end_state_t *ges, block_info_t *bi)
725 {
726         int id = bi->id;
727         assert(!ver_is_younger(ges->bs_tops_vers[id], ges->version));
728         return ver_is_older(ges->bs_tops_vers[id], ges->version) ? NULL : ges->bs_tops[bi->id];
729 }
730
731 static INLINE const workset_t *get_end_state(global_end_state_t *ges, block_info_t *bi)
732 {
733         block_state_t *bs = get_block_state(ges, bi);
734         return bs ? bs->end_state : bi->ws_end;
735 }
736
737 static block_state_t *new_block_state(global_end_state_t *ges, block_info_t *bi)
738 {
739         block_state_t *bs = get_block_state(ges, bi);
740         block_state_t *nw = obstack_alloc(&ges->obst, sizeof(nw[0]));
741
742         nw->next_intern = bs;
743         nw->next        = ges->bs_top;
744         nw->bi          = bi;
745
746         if (bs) {
747                 nw->pressure  = bs->pressure;
748                 nw->end_state = workset_clone(ges->env, &ges->obst, bs->end_state);
749         }
750         else {
751                 nw->pressure  = bi->pressure;
752                 nw->end_state = workset_clone(ges->env, &ges->obst, bi->ws_end);
753         }
754
755         ges->bs_top               = nw;
756         ges->bs_tops[bi->id]      = nw;
757         ges->bs_tops_vers[bi->id] = ges->version;
758         return nw;
759 }
760
761 static irn_action_t *new_irn_action(global_end_state_t *ges, ir_node *irn, const ir_node *bl)
762 {
763         irn_action_t *ia = obstack_alloc(&ges->obst, sizeof(ia[0]));
764
765         ia->irn  = irn;
766         ia->bl   = bl;
767         ia->act  = irn_act_none;
768         ia->next = ges->ia_top;
769         ges->ia_top = ia;
770         return ia;
771 }
772
773 static INLINE rollback_info_t trans_begin(global_end_state_t *ges)
774 {
775         rollback_info_t rb;
776         rb.obst_level = obstack_base(&ges->obst);
777         rb.bs_top     = ges->bs_top;
778         rb.ia_top     = ges->ia_top;
779         return rb;
780 }
781
782 static INLINE void trans_rollback(global_end_state_t *ges, rollback_info_t *rb)
783 {
784         block_state_t *bs;
785
786         /* unwind all the stacks indiced with the block number */
787         for (bs = ges->bs_top; bs != rb->bs_top; bs = bs->next) {
788                 unsigned id = bs->bi->id;
789                 ges->bs_tops[id] = bs->next_intern;
790         }
791
792         ges->ia_top = rb->ia_top;
793         ges->bs_top = rb->bs_top;
794         obstack_free(&ges->obst, rb->obst_level);
795 }
796
797
798 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level);
799
800 static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
801 {
802         block_info_t *bi     = get_block_info(bl);
803         const workset_t *end = get_end_state(ges, bi);
804         double res;
805         int index;
806
807         DBG((dbg, DBG_GLOBAL, "\t%2Dcan make avail %+F at end of %+F\n", level, irn, bl));
808
809         /*
810          * to make the value available at end,
811          * we have several cases here.
812          *
813          * - we already visited that block.
814          * - If the value is in the final end set, return 0.
815          *   somebody else already allocated it there.
816          * - If not and the final end set is already full,
817          *   we cannot make the value available at the end
818          *   of this block. return INFINITY.
819          * - Else (value not in final end set and there is room):
820          *   1) The value is in a register at the end of the local Belady pass.
821          *      Allocate a slot in  the final end set and return 0.
822          *   2) The value is not in the Belady end set:
823          *      If the block's capacity is < k then check what it costs
824          *      to transport the value from upper blocks to this block.
825          *      Compare that against the reload cost in this block. If
826          *      cheaper, do the other thing. If not, reload it here.
827          */
828
829         /* if the end set contains it already, it is in a reg and it costs nothing
830          * to load it to one. */
831         index = workset_get_index(end, irn);
832         if (index >= 0) {
833                 unsigned ver = workset_get_version(end, index);
834                 DBG((dbg, DBG_GLOBAL, "\t%2Dnode is in the end set and is %s fixed\n",
835                                         level, ver_is_older(ver, ges->version) ? "already" : "not yet"));
836
837                 /*
838                  * if the version is older, the value is already fixed
839                  * and cannot be removed from the end set.
840                  *
841                  * If not, we will create a new block state for that block since
842                  * we modify it by giving the end state a new version.
843                  */
844                 if (ver_is_younger(ver, ges->version)) {
845                         block_state_t *bs = new_block_state(ges, bi);
846                         workset_set_version(bs->end_state, index, ges->version);
847                 }
848
849                 res = 0.0;
850                 goto end;
851         }
852
853         /*
854          * Now we have two options:
855          * 1) Reload the value at the end of the block.
856          *    Therefore, perhaps, we have to erase another one from the workset.
857          *    This may only be done if it has not been fixed.
858          *    Since fixed means that a previous pass has decided that that value
859          *    *has* to stay in the end set.
860          * 2) we can try, if the capacity of the block allows it, to let
861          *    the value live through the block and make it available at
862          *    the entrance.
863          *
864          * First, we test the local (reload in this block) alternative
865          * and compare against the other alternative.
866          * Of course, we chose the cheaper one.
867          */
868
869         {
870                 int n_regs = bi->bel->n_regs;
871                 int len  = workset_get_length(end);
872                 int slot = -1;
873                 int i;
874
875                 res = HUGE_VAL;
876
877                 /*
878                  * look if there is room in the end array
879                  * for the variable. Note that this does not
880                  * mean that the var can live through the block.
881                  * There is just room at the *end*
882                  */
883                 if (len < n_regs) {
884                         DBG((dbg, DBG_GLOBAL, "\t%2Dthe end set has %d free slots\n", level, n_regs - len));
885                         slot = len;
886                 } else {
887                         for (i = 0; i < len; ++i) {
888                                 unsigned ver = workset_get_version(end, i);
889                                 if (ver_is_younger(ver, ges->version))
890                                         break;
891                         }
892
893                         if (i < len) {
894                                 DBG((dbg, DBG_GLOBAL, "\t%2D%+F (slot %d) can be erased from the end set\n",
895                                                         level, end->vals[i].irn, i));
896                                 slot = i;
897                         }
898                 }
899
900                 /*
901                  * finally there is some room. we can at least reload the value.
902                  * but we will try to let ot live through anyhow.
903                  */
904                 if (slot >= 0) {
905                         irn_action_t *vs    = new_irn_action(ges, irn, bi->bl);
906                         block_state_t *bs   = new_block_state(ges, bi);
907                         workset_t *end      = bs->end_state;
908                         ir_node *ins_before = block_info_get_last_ins(bi);
909                         double reload_here  = be_get_reload_costs(bi->bel->senv, irn, ins_before);
910                         int pressure_ok     = bs->pressure < (unsigned) n_regs;
911
912                         /*
913                          * No matter what we do, the value will be in the end set
914                          * if the block from now on (of course only regarding the
915                          * current state). Enter it and set the new length
916                          * appropriately.
917                          */
918                         end->vals[slot].irn     = irn;
919                         workset_set_version(end, slot, ges->version);
920                         workset_set_length(end, MAX(workset_get_length(end), slot + 1));
921
922                         vs->act = irn_act_reload;
923                         res     = reload_here;
924
925                         DBG((dbg, DBG_GLOBAL, "\t%2Dthere is a free slot. capacity=%d, reload here=%f, pressure %s\n",
926                                                 level, n_regs - bs->pressure, reload_here, pressure_ok ? "ok" : "insufficient"));
927
928                         /* look if we can bring the value in. */
929                         if (pressure_ok) {
930                                 rollback_info_t rb = trans_begin(ges);
931                                 double new_limit   = MIN(reload_here, limit);
932
933                                 vs->act = irn_act_live_through;
934                                 bs->pressure += 1;
935                                 res = can_bring_in(ges, bl, irn, new_limit, level + 1);
936
937                                 /*
938                                  * if bring in is too expensive re-adjust the pressure
939                                  * and roll back the state
940                                  */
941                                 if (res >= reload_here) {
942                                         bs->pressure -= 1;
943                                         vs->act = irn_act_reload;
944                                         trans_rollback(ges, &rb);
945                                         res = reload_here;
946                                 }
947                         }
948
949                         DBG((dbg, DBG_GLOBAL, "\t%2D%s\n", level,
950                                                 vs->act == irn_act_reload ? "reloading" : "bringing in"));
951                 }
952         }
953
954 end:
955         DBG((dbg, DBG_GLOBAL, "\t%2D-> %f\n", level, res));
956         return res;
957 }
958
959 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
960 {
961         belady_env_t *env = ges->env;
962         double glob_costs = HUGE_VAL;
963
964         DBG((dbg, DBG_GLOBAL, "\t%2Dcan bring in (max %f) for %+F at block %+F\n", level, limit, irn, bl));
965
966         if (is_transport_in(bl, irn)) {
967                 int i, n           = get_irn_arity(bl);
968                 ir_node **nodes    = alloca(get_irn_arity(bl) * sizeof(nodes[0]));
969                 rollback_info_t rb = trans_begin(ges);
970
971
972                 glob_costs = 0.0;
973                 for (i = 0; i < n; ++i) {
974                         ir_node *pr = get_Block_cfgpred_block(bl, i);
975                         ir_node *op = is_local_phi(bl, irn) ? get_irn_n(irn, i) : irn;
976                         double c;
977
978                         /*
979                          * there might by unknwons as operands of phis in that case
980                          * we set the costs to zero, since they won't get spilled.
981                          */
982                         if (arch_irn_consider_in_reg_alloc(env->arch, env->cls, op))
983                                 c = can_make_available_at_end(ges, pr, op, limit - glob_costs, level + 1);
984                         else
985                                 c = 0.0;
986
987                         glob_costs += c;
988
989                         if (glob_costs >= limit) {
990                                 glob_costs = HUGE_VAL;
991                                 trans_rollback(ges, &rb);
992                                 goto end;
993                         }
994                 }
995         }
996
997 end:
998         DBG((dbg, DBG_GLOBAL, "\t%2D-> %f\n", level, glob_costs));
999         return glob_costs;
1000 }
1001
1002 static void materialize_and_commit_end_state(global_end_state_t *ges)
1003 {
1004         belady_env_t *env = ges->env;
1005         irn_action_t *ia;
1006         block_state_t *bs;
1007
1008         DBG((dbg, DBG_GLOBAL, "\tmaterializing\n"));
1009
1010         /*
1011          * Perform all the variable actions.
1012          */
1013         for (ia = ges->ia_top; ia != NULL; ia = ia->next) {
1014                 switch (ia->act) {
1015                         case irn_act_live_through:
1016                                 if (is_local_phi(ia->bl, ia->irn)) {
1017                                         bitset_add_irn(ges->succ_phis, ia->irn);
1018                                         DBG((dbg, DBG_GLOBAL, "\t\tlive through phi kept alive: %+F\n", ia->irn));
1019                                 }
1020                                 break;
1021                         case irn_act_reload:
1022                                 be_add_reload_at_end(env->senv, ia->irn, ia->bl, env->cls, 1);
1023                                 DBG((dbg, DBG_GLOBAL, "\t\tadding reload of %+F at end of %+F\n", ia->irn, ia->bl));
1024                                 break;
1025                         default:
1026                                 DBG((dbg, DBG_GLOBAL, "\t\t%+F is in the end set of %+F\n", ia->irn, ia->bl));
1027                 }
1028         }
1029
1030         /*
1031          * Commit the block end states
1032          */
1033         for (bs = ges->bs_top; bs != NULL; bs = bs->next) {
1034                 block_info_t *bi = bs->bi;
1035
1036                 if (!bitset_is_set(ges->committed, bi->id)) {
1037                         DBG((dbg, DBG_GLOBAL, "\t\tcommiting workset of %+F with version %x\n", bi->bl, ges->version));
1038                         // bes->bs->end_state->vals[idx].version = ges->version;
1039                         workset_copy(env, bi->ws_end, bs->end_state);
1040                         DBG((dbg, DBG_GLOBAL, "\t\told pressure: %d, new pressure: %d, end length: %d\n",
1041                                                 bi->pressure, bs->pressure, workset_get_length(bs->end_state)));
1042                         bi->pressure = bs->pressure;
1043                         bitset_set(ges->committed, bi->id);
1044                 }
1045         }
1046
1047         /* clear the committed bitset. the next call is expecting it. */
1048         bitset_clear_all(ges->committed);
1049 }
1050
1051 /**
1052  * Examine all irns which shall be in regs at the beginning of the
1053  * block.
1054  */
1055 static void fix_block_borders(global_end_state_t *ges, ir_node *block) {
1056         block_info_t *bi  = get_block_info(block);
1057         belady_env_t *env = ges->env;
1058         void *reset_level = obstack_base(&ges->obst);
1059
1060         ir_node *irn;
1061         int i;
1062
1063         for (i = workset_get_length(bi->ws_end) - 1; i >= 0; --i)
1064                 workset_set_version(bi->ws_end, i, ver_youngest);
1065
1066         DBG((dbg, DBG_GLOBAL, "fixing block borders at %+F (%f)\n", block, bi->exec_freq));
1067
1068         /* process all variables which shall be in a reg at
1069          * the beginning of the block in the order of the next use. */
1070         workset_foreach(bi->entrance_reg, irn, i) {
1071                 double local_costs = be_get_reload_costs(env->senv, irn, bi->first_non_in);
1072                 double bring_in_costs;
1073
1074                 /* reset the lists */
1075                 ges->bs_top  = NULL;
1076                 ges->ia_top  = NULL;
1077
1078                 DBG((dbg, DBG_GLOBAL, "\ttrans in var %+F, version %x\n", irn, ges->version));
1079
1080                 bring_in_costs = can_bring_in(ges, block, irn, local_costs, 1);
1081
1082                 DBG((dbg, DBG_GLOBAL, "\tbring in: %f, local: %f", bring_in_costs, local_costs));
1083
1084                 /*
1085                  * we were not able to let the value arrive
1086                  * in a register at the entrance of the block
1087                  * or it is too costly, so we have to do the reload locally
1088                  */
1089                 if (bring_in_costs >= local_costs) {
1090                         DBG((dbg, DBG_GLOBAL, " -> do local reload\n"));
1091                         be_add_reload(env->senv, irn, bi->first_non_in, env->cls, 1);
1092                 } else {
1093                         /*
1094                          * if the transport-in was a phi (that is actually used in block)
1095                          * it will no longer remain and we have to spill it completely.
1096                          */
1097                         if (is_local_phi(block, irn))
1098                                 bitset_add_irn(ges->succ_phis, irn);
1099
1100                         DBG((dbg, DBG_GLOBAL, " -> bring it in\n"));
1101                         materialize_and_commit_end_state(ges);
1102                 }
1103
1104                 DBG((dbg, DBG_GLOBAL, "\n"));
1105
1106                 /* reset the obstack and create a new version. */
1107                 obstack_free(&ges->obst, reset_level);
1108                 ges->version = ver_make_newer(ges->version);
1109         }
1110 }
1111
1112 static void global_assign(belady_env_t *env)
1113 {
1114         global_end_state_t ges;
1115         int i;
1116
1117         /*
1118          * sort the blocks according to execution frequency.
1119          * That's not necessary for belady() but for the global pass later on.
1120          */
1121         qsort(env->blocks, env->n_blocks, sizeof(env->blocks[0]), block_order);
1122
1123         memset(&ges, 0, sizeof(ges));
1124         obstack_init(&ges.obst);
1125         ges.env          = env;
1126         ges.version      = ver_make_newer(ver_oldest);
1127         ges.succ_phis    = bitset_irg_obstack_alloc(&ges.obst, env->irg);
1128         ges.committed    = bitset_obstack_alloc(&ges.obst, env->n_blocks);
1129         ges.bs_tops      = obstack_alloc(&ges.obst, sizeof(ges.bs_tops[0])      * env->n_blocks);
1130         ges.bs_tops_vers = obstack_alloc(&ges.obst, sizeof(ges.bs_tops_vers[0]) * env->n_blocks);
1131
1132         for (i = 0; i < env->n_blocks; ++i)
1133                 ges.bs_tops_vers[i] = ver_oldest;
1134
1135         for (i = 0; i < env->n_blocks; ++i)
1136                 fix_block_borders(&ges, env->blocks[i]);
1137
1138         /*
1139          * Now we spill phis which cannot be kept since they were replaced
1140          * by reloads at the block entrances.
1141          */
1142         for (i = 0; i < env->n_blocks; ++i) {
1143                 ir_node *bl = env->blocks[i];
1144                 ir_node *irn;
1145
1146                 sched_foreach(bl, irn) {
1147                         if (!is_Phi(irn))
1148                                 break;
1149
1150                         if (arch_irn_consider_in_reg_alloc(env->arch, env->cls, irn)
1151                                         && !bitset_contains_irn(ges.succ_phis, irn))
1152                                 be_spill_phi(env->senv, irn);
1153                 }
1154         }
1155 }
1156
1157 static void collect_blocks(ir_node *bl, void *data)
1158 {
1159         belady_env_t *env = data;
1160         ++env->n_blocks;
1161         obstack_ptr_grow(&env->ob, bl);
1162 }
1163
1164 /**
1165  * Do spilling for a register class on a graph using the belady heuristic.
1166  * In the transformed graph, the register pressure never exceeds the number
1167  * of available registers.
1168  *
1169  * @param birg  The backend graph
1170  * @param cls   The register class to spill
1171  */
1172 void be_spill_belady(be_irg_t *birg, const arch_register_class_t *cls)
1173 {
1174         ir_graph *irg = be_get_birg_irg(birg);
1175         belady_env_t env;
1176         int i, n_regs;
1177
1178         /* some special classes contain only ignore regs, nothing to do then */
1179         n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1180         if(n_regs == 0)
1181                 return;
1182
1183         be_clear_links(irg);
1184
1185         /* init belady env */
1186         obstack_init(&env.ob);
1187         env.irg       = irg;
1188         env.arch      = be_get_birg_arch_env(birg);
1189         env.cls       = cls;
1190         env.lv        = be_get_birg_liveness(birg);
1191         env.dfs       = env.lv->dfs;
1192         env.n_regs    = n_regs;
1193         env.ws        = new_workset(&env, &env.ob);
1194         env.senv      = be_new_spill_env(birg);
1195         env.ef        = be_get_birg_exec_freq(birg);
1196         env.spilled   = bitset_irg_obstack_alloc(&env.ob, irg);
1197         env.n_blocks  = 0;
1198
1199         irg_block_walk_graph(irg, NULL, collect_blocks, &env);
1200         obstack_ptr_grow(&env.ob, NULL);
1201         env.blocks = obstack_finish(&env.ob);
1202
1203         /* Fix high register pressure in blocks with belady algorithm */
1204         for (i = 0; i < env.n_blocks; ++i)
1205                 belady(&env, i);
1206
1207         global_assign(&env);
1208
1209         /* Insert spill/reload nodes into the graph and fix usages */
1210         be_insert_spills_reloads(env.senv);
1211
1212         /* clean up */
1213         be_delete_spill_env(env.senv);
1214
1215         obstack_free(&env.ob, NULL);
1216 }
1217
1218 void be_init_spillbelady2(void)
1219 {
1220         lc_opt_entry_t *be_grp    = lc_opt_get_grp(firm_opt_get_root(), "be");
1221         lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
1222         lc_opt_entry_t *bel2_grp  = lc_opt_get_grp(spill_grp, "belady2");
1223
1224         static be_spiller_t belady_spiller = {
1225                 be_spill_belady
1226         };
1227
1228         lc_opt_add_table(bel2_grp, options);
1229         be_register_spiller("belady2", &belady_spiller);
1230         FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady2");
1231 }
1232
1233 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady2);