remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / be / bespillbelady2.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
36
37 #include <math.h>
38 #include <limits.h>
39
40 #include "obst.h"
41 #include "irnodeset.h"
42 #include "irbitset.h"
43 #include "irprintf_t.h"
44 #include "irgraph.h"
45 #include "irnode.h"
46 #include "irmode.h"
47 #include "irgwalk.h"
48 #include "irloop.h"
49 #include "iredges_t.h"
50 #include "irphase_t.h"
51 #include "ircons_t.h"
52 #include "irprintf.h"
53 #include "execfreq.h"
54 #include "dfs_t.h"
55
56 #include "beutil.h"
57 #include "bearch_t.h"
58 #include "besched_t.h"
59 #include "beirgmod.h"
60 #include "belive_t.h"
61 #include "benode_t.h"
62 #include "bechordal_t.h"
63 #include "bespilloptions.h"
64 #include "beloopana.h"
65 #include "beirg_t.h"
66 #include "bemodule.h"
67 #include "bespill.h"
68
69 #include "lc_opts.h"
70 #include "lc_opts_enum.h"
71
72 #define DBG_SPILL     1
73 #define DBG_WSETS     2
74 #define DBG_FIX       4
75 #define DBG_DECIDE    8
76 #define DBG_START    16
77 #define DBG_SLOTS    32
78 #define DBG_TRACE    64
79 #define DBG_WORKSET 128
80 #define DBG_GLOBAL  256
81
82 #define ALREADY_SPILLED_FACTOR 2
83
84 #define DEAD       UINT_MAX
85 #define LIVE_END   (DEAD-1)
86 #define REMAT_DIST (DEAD-2)
87
88 static int already_spilled_factor = 2;
89 static int remat_live_range_ext   = 1;
90 static int global_pass_enabled    = 1;
91
92 static const lc_opt_table_entry_t options[] = {
93         LC_OPT_ENT_INT           ("asf",    "already spilled factor",                             &already_spilled_factor),
94         LC_OPT_ENT_BOOL          ("remat",  "rematerializable ops get infinite long live ranges", &remat_live_range_ext),
95         LC_OPT_ENT_BOOL          ("global", "enable/disable the global pass",                     &global_pass_enabled),
96         LC_OPT_LAST
97 };
98
99 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
100
101 /**
102  * An association between a node and a point in time.
103  */
104 typedef struct _loc_t {
105   ir_node *irn;        /**< A node. */
106   unsigned time;       /**< A use time.
107                                                  In the global pass this is used
108                                                  as the version number and not as a time.
109                                                  Only to save space...
110                                                 */
111 } loc_t;
112
113 typedef struct _workset_t {
114         int len;                        /**< current length */
115         loc_t vals[0];          /**< inlined array of the values/distances in this working set */
116 } workset_t;
117
118 typedef struct _belady_env_t {
119         struct obstack ob;
120         ir_graph *irg;
121         const dfs_t *dfs;
122         const arch_env_t *arch;
123         const arch_register_class_t *cls;
124         be_lv_t *lv;
125         ir_exec_freq *ef;
126
127         ir_node **blocks;            /**< Array of all blocks. */
128         int n_blocks;                /**< Number of blocks in the graph. */
129         int n_regs;                              /**< number of regs in this reg-class */
130         workset_t *ws;                   /**< the main workset used while processing a block. ob-allocated */
131         ir_node *instr;                  /**< current instruction */
132         int instr_nr;                    /**< current instruction number (relative to block start) */
133
134         spill_env_t *senv;               /**< see bespill.h */
135         bitset_t *spilled;           /**< bitset to keep all the irns which have already been spilled. */
136         ir_nodeset_t *extra_spilled; /** All nodes for which a special spill location has been computed. */
137 } belady_env_t;
138
139
140 static int loc_compare(const void *a, const void *b)
141 {
142         const loc_t *p = a;
143         const loc_t *q = b;
144         return (p->time > q->time) - (p->time < q->time);
145 }
146
147 static INLINE void workset_print(const workset_t *w)
148 {
149         int i;
150
151         for(i = 0; i < w->len; ++i) {
152                 ir_fprintf(stderr, "%+F %d\n", w->vals[i].irn, w->vals[i].time);
153         }
154 }
155
156 /**
157  * Alloc a new workset on obstack @p ob with maximum size @p max
158  */
159 static INLINE workset_t *new_workset(belady_env_t *env, struct obstack *ob) {
160         workset_t *res;
161         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
162         res = obstack_alloc(ob, size);
163         memset(res, 0, size);
164         return res;
165 }
166
167 /**
168  * Alloc a new instance on obstack and make it equal to @param ws
169  */
170 static INLINE workset_t *workset_clone(belady_env_t *env, struct obstack *ob, workset_t *ws) {
171         workset_t *res;
172         size_t size = sizeof(*res) + (env->n_regs)*sizeof(res->vals[0]);
173         res = obstack_alloc(ob, size);
174         memcpy(res, ws, size);
175         return res;
176 }
177
178 /**
179  * Do NOT alloc anything. Make @param tgt equal to @param src.
180  * returns @param tgt for convenience
181  */
182 static INLINE workset_t *workset_copy(belady_env_t *env, workset_t *tgt, workset_t *src) {
183         size_t size = sizeof(*src) + (env->n_regs)*sizeof(src->vals[0]);
184         memcpy(tgt, src, size);
185         return tgt;
186 }
187
188 /**
189  * Overwrites the current content array of @param ws with the
190  * @param count locations given at memory @param locs.
191  * Set the length of @param ws to count.
192  */
193 static INLINE void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs) {
194         workset->len = count;
195         memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
196 }
197
198 /**
199  * Inserts the value @p val into the workset, iff it is not
200  * already contained. The workset must not be full.
201  */
202 static INLINE void workset_insert(belady_env_t *env, workset_t *ws, ir_node *val) {
203         int i;
204         /* check for current regclass */
205         if (!arch_irn_consider_in_reg_alloc(env->cls, val)) {
206                 // DBG((dbg, DBG_WORKSET, "Skipped %+F\n", val));
207                 return;
208         }
209
210         /* check if val is already contained */
211         for(i=0; i<ws->len; ++i)
212                 if (ws->vals[i].irn == val)
213                         return;
214
215         /* insert val */
216         assert(ws->len < env->n_regs && "Workset already full!");
217         ws->vals[ws->len++].irn = val;
218 }
219
220 /**
221  * Removes all entries from this workset
222  */
223 static INLINE void workset_clear(workset_t *ws) {
224         ws->len = 0;
225 }
226
227 /**
228  * Removes the value @p val from the workset if present.
229  */
230 static INLINE void workset_remove(workset_t *ws, ir_node *val) {
231         int i;
232         for(i=0; i<ws->len; ++i) {
233                 if (ws->vals[i].irn == val) {
234                         ws->vals[i] = ws->vals[--ws->len];
235                         return;
236                 }
237         }
238 }
239
240 static INLINE int workset_get_index(const workset_t *ws, const ir_node *val) {
241         int i;
242         for(i=0; i<ws->len; ++i) {
243                 if (ws->vals[i].irn == val)
244                         return i;
245         }
246
247         return -1;
248 }
249
250 /**
251  * Iterates over all values in the working set.
252  * @p ws The workset to iterate
253  * @p v  A variable to put the current value in
254  * @p i  An integer for internal use
255  */
256 #define workset_foreach(ws, v, i)       for(i=0; \
257                                                                                 v=(i < ws->len) ? ws->vals[i].irn : NULL, i < ws->len; \
258                                                                                 ++i)
259
260 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
261 #define workset_get_time(ws, i) (ws)->vals[i].time
262 #define workset_set_length(ws, length) (ws)->len = length
263 #define workset_get_length(ws) ((ws)->len)
264 #define workset_get_val(ws, i) ((ws)->vals[i].irn)
265 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
266 #define workset_contains(ws, n) (workset_get_index(ws, n) >= 0)
267
268 typedef struct _bring_in_t bring_in_t;
269
270 typedef struct _block_info_t {
271         belady_env_t *bel;
272         ir_node *bl;
273         int id;
274         ir_phase next_uses;
275         workset_t *ws_end;       /**< The end set after the local belady pass. */
276         double exec_freq;        /**< The execution frequency of this block. */
277
278         double reload_cost;      /**< Cost of a reload in this block. */
279         ir_node *first_non_in;   /**< First node in block which is not a phi.  */
280         ir_node *last_ins;       /**< The instruction before which end of
281                                                            block reloads will be inserted. */
282
283         int pressure;            /**< The amount of registers which remain free
284                                                in this block. This capacity can be used to let
285                                                global variables, transported into other blocks,
286                                                live through this block. */
287
288         int front_pressure;      /**< The pressure right before the first
289                                                            real (non-phi) node. At the beginning
290                                                            of the global pass, this is 0. */
291         struct list_head br_head; /**< List head for all bring_in variables. */
292         int free_at_jump;         /**< registers free at jump. */
293
294 } block_info_t;
295
296 static INLINE void *new_block_info(belady_env_t *bel, int id)
297 {
298         ir_node      *bl  = bel->blocks[id];
299         block_info_t *res = obstack_alloc(&bel->ob, sizeof(*res));
300         memset(res, 0, sizeof(res[0]));
301         res->first_non_in = NULL;
302         res->last_ins = NULL;
303         res->bel = bel;
304         res->bl  = bl;
305         res->id  = id;
306         res->exec_freq    = get_block_execfreq(bel->ef, bl);
307         res->reload_cost  = bel->arch->reload_cost * res->exec_freq;
308         res->free_at_jump = bel->n_regs;
309         INIT_LIST_HEAD(&res->br_head);
310         set_irn_link(bl, res);
311         return res;
312 }
313
314 #define get_block_info(block)        ((block_info_t *)get_irn_link(block))
315 #define set_block_info(block, info)  set_irn_link(block, info)
316
317 static INLINE ir_node *block_info_get_last_ins(block_info_t *bi)
318 {
319         if (!bi->last_ins)
320                 bi->last_ins = be_get_end_of_block_insertion_point(bi->bl);
321
322         return bi->last_ins;
323 }
324
325 typedef struct _next_use_t {
326         unsigned is_first_use : 1; /**< Indicate that this use is the first
327                                                                  in the block. Needed to identify
328                                                                  transport in values for the global
329                                                                  pass. */
330         sched_timestep_t step;     /**< The time step of the use. */
331         ir_node *irn;
332         struct _next_use_t *next;  /**< The next use int this block
333                                                                  or NULL. */
334 } next_use_t;
335
336 static void *next_use_init(ir_phase *phase, const ir_node *irn, void *old)
337 {
338         (void) phase;
339         (void) irn;
340         (void) old;
341         return NULL;
342 }
343
344 static void build_next_uses(block_info_t *bi)
345 {
346         ir_node *irn;
347
348         sched_renumber(bi->bl);
349
350         phase_init(&bi->next_uses, "next uses", bi->bel->irg, PHASE_DEFAULT_GROWTH, next_use_init, NULL);
351         sched_foreach_reverse(bi->bl, irn) {
352                 int i;
353
354                 if (is_Phi(irn))
355                         break;
356
357                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
358                         ir_node *op = get_irn_n(irn, i);
359                         next_use_t *curr = phase_get_irn_data(&bi->next_uses, op);
360                         next_use_t *use  = phase_alloc(&bi->next_uses, sizeof(use[0]));
361
362                         use->is_first_use = 1;
363                         use->step         = sched_get_time_step(irn);
364                         use->next         = curr;
365                         use->irn          = irn;
366
367                         if (curr) {
368                                 curr->is_first_use = 0;
369                                 assert(curr->step >= use->step);
370                         }
371
372                         phase_set_irn_data(&bi->next_uses, op, use);
373                 }
374         }
375 }
376
377 #define get_current_use(bi, irn)         phase_get_irn_data(&(bi)->next_uses, (irn))
378
379 static INLINE void advance_current_use(block_info_t *bi, const ir_node *irn)
380 {
381         next_use_t *use = get_current_use(bi, irn);
382
383         assert(use);
384         phase_set_irn_data(&bi->next_uses, irn, use->next);
385 }
386
387 static __attribute__((unused)) int block_freq_gt(const void *a, const void *b)
388 {
389         const ir_node * const *p = a;
390         const ir_node * const *q = b;
391         block_info_t *pi = get_block_info(*p);
392         block_info_t *qi = get_block_info(*q);
393         double diff = qi->exec_freq - pi->exec_freq;
394         return (diff > 0) - (diff < 0);
395 }
396
397 static int block_freq_dfs_gt(const void *a, const void *b)
398 {
399         const ir_node * const *p = a;
400         const ir_node * const *q = b;
401         block_info_t *pi = get_block_info(*p);
402         block_info_t *qi = get_block_info(*q);
403         double diff;
404
405         if ((pi->exec_freq > 1.0 && qi->exec_freq > 1.0)
406                         || (pi->exec_freq <= 1.0 && qi->exec_freq <= 1.0)) {
407
408                 const dfs_t *dfs = pi->bel->dfs;
409                 int pp = dfs_get_post_num(dfs, pi->bl);
410                 int pq = dfs_get_post_num(dfs, qi->bl);
411                 return pq - pp;
412         }
413
414         diff = qi->exec_freq - pi->exec_freq;
415         return (diff > 0) - (diff < 0);
416 }
417
418 /*
419    ____       _               ___
420   | __ ) _ __(_)_ __   __ _  |_ _|_ __
421   |  _ \| '__| | '_ \ / _` |  | || '_ \
422   | |_) | |  | | | | | (_| |  | || | | |
423   |____/|_|  |_|_| |_|\__, | |___|_| |_|
424                       |___/
425
426   Data structures to represent bring in variables.
427 */
428
429 struct _bring_in_t {
430         ir_node *irn;              /**< The node to bring in. */
431         block_info_t *bi;          /**< The block to which bring in should happen. */
432         int pressure_so_far;       /**< The maximal pressure till the first use of irn in bl. */
433         ir_node *first_use;        /**< The first user of irn in bl. */
434         sched_timestep_t use_step; /**< Schedule sttep of the first use. */
435
436         int is_remat : 1;          /**< Is rematerializable. */
437         int sect_pressure;         /**< Offset to maximum pressure in block. */
438         struct list_head list;
439         struct list_head sect_list;
440         bring_in_t *sect_head;
441 };
442
443 static INLINE bring_in_t *new_bring_in(block_info_t *bi, ir_node *irn, const next_use_t *use)
444 {
445         bring_in_t *br    = obstack_alloc(&bi->bel->ob, sizeof(br[0]));
446
447         br->irn             = irn;
448         br->bi              = bi;
449         br->first_use       = use->irn;
450         br->use_step        = use->step;
451         br->is_remat        = be_is_rematerializable(bi->bel->senv, irn, use->irn);
452         br->pressure_so_far = bi->pressure;
453         br->sect_pressure   = bi->front_pressure;
454         br->sect_head       = br;
455
456         INIT_LIST_HEAD(&br->list);
457         INIT_LIST_HEAD(&br->sect_list);
458         list_add_tail(&br->list, &bi->br_head);
459         return br;
460 }
461
462 static int bring_in_cmp(const void *a, const void *b)
463 {
464         const bring_in_t *p = *(const bring_in_t * const *) a;
465         const bring_in_t *q = *(const bring_in_t * const *) b;
466         double fp, fq;
467
468         /* if one of both is a remat node, it will be done after the other. */
469         if (p->is_remat != q->is_remat)
470                 return p->is_remat - q->is_remat;
471
472         /* in the same block, the one further in the front has to be processed first!
473          * Otherwise the front_pressure 'trick' is not exact. */
474         if (p->bi == q->bi)
475                 return p->use_step - q->use_step;
476
477         fp = p->bi->exec_freq;
478         fq = q->bi->exec_freq;
479
480         /* if both have the same frequency, inspect the frequency of the definition */
481         if (fp == fq) {
482                 double fdp = get_block_info(get_nodes_block(p->irn))->exec_freq;
483                 double fdq = get_block_info(get_nodes_block(q->irn))->exec_freq;
484
485                 /* if the defs of both have the same freq, we go for reverse dfs post order. */
486                 if (fdp == fdq) {
487                         const dfs_t *dfs = p->bi->bel->dfs;
488                         int pp = dfs_get_post_num(dfs, p->bi->bl);
489                         int pq = dfs_get_post_num(dfs, q->bi->bl);
490                         return pq - pp;
491                 }
492
493                 return (fdq > fdp) - (fdq < fdp);
494         }
495
496         return (fq > fp) - (fq < fp);
497 }
498
499 static INLINE unsigned get_curr_distance(block_info_t *bi, const ir_node *irn, int is_usage)
500 {
501         belady_env_t *env          = bi->bel;
502         sched_timestep_t curr_step = sched_get_time_step(env->instr);
503         next_use_t *use            = get_current_use(bi, irn);
504         int flags                  = arch_irn_get_flags(irn);
505
506         assert(!(flags & arch_irn_flags_ignore));
507
508         /* We have to keep nonspillable nodes in the workingset */
509         if(flags & arch_irn_flags_dont_spill)
510                 return 0;
511
512         if (!is_usage && use && use->step == curr_step)
513                 use = use->next;
514
515         if (use) {
516                 unsigned res  = use->step - curr_step;
517
518                 assert(use->step >= curr_step);
519
520                 if (res != 0) {
521                         if (remat_live_range_ext && be_is_rematerializable(env->senv, irn, use->irn))
522                                 res = REMAT_DIST;
523                         else if (bitset_contains_irn(env->spilled, irn))
524                                 res *= already_spilled_factor;
525                 }
526
527                 return res;
528         }
529
530         return be_is_live_end(env->lv, bi->bl, irn) ? LIVE_END : DEAD;
531 }
532
533 static INLINE int is_local_phi(const ir_node *bl, const ir_node *irn)
534 {
535         return is_Phi(irn) && get_nodes_block(irn) == bl;
536 }
537
538 /**
539  * Check, if the value is something that is transported into a block.
540  * That is, the value is defined elsewhere or defined by a Phi in the block.
541  * @param env  The belady environment.
542  * @param bl   The block in question.
543  * @param irn  The node in question.
544  * @return     1, if node is something transported into @p bl, 0 if not.
545  * @note       The function will only give correct answers in the case
546  *             where @p irn is unsed in the block @p bl which is always
547  *             the case in our usage scenario.
548  */
549 static INLINE int is_transport_in(const ir_node *bl, const ir_node *irn)
550 {
551         return get_nodes_block(irn) != bl || is_Phi(irn);
552 }
553
554 /**
555  * Performs the actions necessary to grant the request that:
556  * - new_vals can be held in registers
557  * - as few as possible other values are disposed
558  * - the worst values get disposed
559  *
560  * @p is_usage indicates that the values in new_vals are used (not defined)
561  * In this case reloads must be performed
562  */
563 static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
564         belady_env_t *env       = bi->bel;
565         workset_t    *ws        = env->ws;
566         ir_node     **to_insert = alloca(env->n_regs * sizeof(to_insert[0]));
567
568         int i, len, max_allowed, demand, iter;
569         ir_node *val;
570
571         /*
572                 1. Identify the number of needed slots and the values to reload
573         */
574         demand = 0;
575         workset_foreach(new_vals, val, iter) {
576                 /* mark value as used */
577
578                 if (! workset_contains(ws, val)) {
579                         DBG((dbg, DBG_DECIDE, "\t\tinsert %+F\n", val));
580                         to_insert[demand++] = val;
581                         if (is_usage) {
582                                 next_use_t *use = get_current_use(bi, val);
583
584                                 /*
585                                  * if we use a value which is transported in this block, i.e. a
586                                  * phi defined here or a live in, for the first time, we check
587                                  * if there is room for that guy to survive from the block's
588                                  * entrance to here or not.
589                                  */
590                                 assert(use);
591                                 assert(sched_get_time_step(env->instr) == (int) use->step);
592                                 if (is_transport_in(bi->bl, val) && use->is_first_use) {
593                                         bring_in_t *bri = new_bring_in(bi, val, use);
594                                         bri->first_use = env->instr;
595
596                                         /* reset the section pressure, since a new section starts. */
597                                         bi->front_pressure = 0;
598
599                                         DBG((dbg, DBG_DECIDE, "\t\tbring in node %+F, pressure %d:\n", val, bi->pressure));
600                                         DBG((dbg, DBG_DECIDE, "\t\tno reload. must be considered at block start\n"));
601                                 }
602
603                                 else {
604                                         bitset_add_irn(env->spilled, val);
605                                         DBG((dbg, DBG_SPILL, "\t\tReload %+F before %+F\n", val, env->instr));
606                                         be_add_reload(env->senv, val, env->instr, env->cls, 1);
607                                 }
608                         }
609                 } else {
610                         assert(is_usage && "Defined value already in workset?!?");
611                         DBG((dbg, DBG_DECIDE, "\t\tskip %+F\n", val));
612                 }
613         }
614         DBG((dbg, DBG_DECIDE, "\t\tdemand = %d\n", demand));
615
616         /*
617                 2. Make room for at least 'demand' slots
618         */
619         len         = workset_get_length(ws);
620         max_allowed = env->n_regs - demand;
621
622         /* Only make more free room if we do not have enough */
623         if (len > max_allowed) {
624                 DBG((dbg, DBG_DECIDE, "\t\tdisposing %d values\n", len - max_allowed));
625
626                 /* get current next-use distance */
627                 for (i = 0; i < ws->len; ++i) {
628                         ir_node *val  = workset_get_val(ws, i);
629                         unsigned dist = get_curr_distance(bi, val, is_usage);
630                         workset_set_time(ws, i, dist);
631                 }
632
633                 /* sort entries by increasing nextuse-distance*/
634                 workset_sort(ws);
635
636                 /* kill the last 'demand' entries in the array */
637                 workset_set_length(ws, max_allowed);
638         }
639
640         /*
641                 3. Insert the new values into the workset
642                    Also, we update the pressure in the block info.
643                    That is important for the global pass to decide
644                    how many values can live through the block.
645         */
646         for (i = 0; i < demand; ++i)
647                 workset_insert(env, env->ws, to_insert[i]);
648
649         /* TODO: simplify this expression? */
650         bi->pressure       = MAX(bi->pressure,       workset_get_length(env->ws));
651         bi->front_pressure = MAX(bi->front_pressure, workset_get_length(env->ws));
652 }
653
654 /**
655  * For the given block @p block, decide for each values
656  * whether it is used from a register or is reloaded
657  * before the use.
658  */
659 static void belady(belady_env_t *env, int id) {
660         block_info_t *block_info = new_block_info(env, id);
661         const ir_node *block     = block_info->bl;
662
663         workset_t *new_vals;
664         ir_node *irn;
665         int iter;
666
667         DBG((dbg, DBG_WSETS, "Belady on %+F\n", block_info->bl));
668         new_vals = new_workset(env, &env->ob);
669         workset_clear(env->ws);
670
671         /* build the next use information for this block. */
672         build_next_uses(block_info);
673
674         env->instr_nr = 0;
675         block_info->first_non_in = NULL;
676
677         /* process the block from start to end */
678         sched_foreach(block, irn) {
679                 int i, arity;
680                 assert(workset_get_length(env->ws) <= env->n_regs && "Too much values in workset!");
681
682                 /* projs are handled with the tuple value.
683                  * Phis are no real instr (see insert_starters())
684                  * instr_nr does not increase */
685                 if (is_Proj(irn) || is_Phi(irn))
686                         continue;
687                 DBG((dbg, DBG_DECIDE, "\t%+F\n", irn));
688
689                 if (!block_info->first_non_in)
690                         block_info->first_non_in = irn;
691
692                 /* set instruction in the workset */
693                 env->instr = irn;
694
695                 /* allocate all values _used_ by this instruction */
696                 workset_clear(new_vals);
697                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
698                         workset_insert(env, new_vals, get_irn_n(irn, i));
699                 }
700                 DBG((dbg, DBG_DECIDE, "\t* uses\n"));
701                 displace(block_info, new_vals, 1);
702
703                 /*
704                  * set all used variables to the next use in their next_use_t list
705                  * Also, kill all dead variables from the workset. They are only
706                  * augmenting the pressure. Note, that a variable is dead
707                  * if it has no further use in this block and is *not* live end
708                  */
709                 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
710                         ir_node *op = get_irn_n(irn, i);
711                         next_use_t *use = get_current_use(block_info, op);
712
713                         assert(use);
714                         if (!use->next && !be_is_live_end(env->lv, block, op))
715                                 workset_remove(env->ws, op);
716
717                         advance_current_use(block_info, op);
718                 }
719
720                 /* allocate all values _defined_ by this instruction */
721                 workset_clear(new_vals);
722                 if (get_irn_mode(irn) == mode_T) { /* special handling for tuples and projs */
723                         const ir_edge_t *edge;
724
725                         foreach_out_edge(irn, edge) {
726                                 ir_node *proj = get_edge_src_irn(edge);
727                                 workset_insert(env, new_vals, proj);
728                         }
729                 } else {
730                         workset_insert(env, new_vals, irn);
731                 }
732                 DBG((dbg, DBG_DECIDE, "\t* defs\n"));
733                 displace(block_info, new_vals, 0);
734
735                 if (is_op_forking(get_irn_op(env->instr))) {
736                         for (i = get_irn_arity(env->instr) - 1; i >= 0; --i) {
737                                 ir_node *op = get_irn_n(env->instr, i);
738                                 block_info->free_at_jump -= arch_irn_consider_in_reg_alloc(env->cls, op);
739                         }
740                 }
741
742                 env->instr_nr++;
743         }
744
745         phase_free(&block_info->next_uses);
746
747         /* Remember end-workset for this block */
748         block_info->ws_end = workset_clone(env, &env->ob, env->ws);
749         DBG((dbg, DBG_WSETS, "End workset for %+F:\n", block));
750         workset_foreach(block_info->ws_end, irn, iter)
751                 DBG((dbg, DBG_WSETS, "  %+F (%u)\n", irn, workset_get_time(block_info->ws_end, iter)));
752         DBG((dbg, DBG_WSETS, "Max pressure in block: %d\n", block_info->pressure));
753
754         /* now, initialize the front pressure to 0. */
755         block_info->front_pressure = 0;
756 }
757
758 /*
759  _____ _                  _       _           _   ____            _
760 |_   _| |__   ___    __ _| | ___ | |__   __ _| | |  _ \ __ _ _ __| |_
761   | | | '_ \ / _ \  / _` | |/ _ \| '_ \ / _` | | | |_) / _` | '__| __|
762   | | | | | |  __/ | (_| | | (_) | |_) | (_| | | |  __/ (_| | |  | |_
763   |_| |_| |_|\___|  \__, |_|\___/|_.__/ \__,_|_| |_|   \__,_|_|   \__|
764                     |___/
765
766 */
767
768 #define workset_set_version(ws, i, t) ((ws)->vals[(i)].time = (t))
769 #define workset_get_version(ws, i)    ((ws)->vals[(i)].time)
770
771 #define ver_oldest                        (0)
772 #define ver_youngest                      ((unsigned) -1)
773 #define ver_make_newer(v)                 ((v) + 1)
774 #define ver_is_older(v, w)                ((v) < (w))
775 #define ver_is_younger(v, w)              ((v) > (w))
776
777 enum {
778         irn_act_none = 0,
779         irn_act_reload,
780         irn_act_live_through
781 };
782
783 typedef struct _block_state_t {
784         struct _block_state_t *next;
785         struct _block_state_t *next_intern;
786         block_info_t *bi;
787         int pressure;
788         workset_t *end_state;
789 } block_state_t;
790
791 typedef struct _irn_action_t {
792         struct _irn_action_t *next;
793         ir_node *irn;
794         const ir_node *bl;
795         int act;
796 } irn_action_t;
797
798 typedef struct _global_end_state_t {
799         belady_env_t *env;
800         bitset_t *succ_phis;
801         bitset_t *committed;
802         struct obstack obst;
803         void *reset_level;
804         unsigned version;
805
806         unsigned       *bs_tops_vers;
807         block_state_t **bs_tops;
808         block_state_t  *bs_top;
809         irn_action_t   *ia_top;
810 } global_end_state_t;
811
812 typedef struct {
813         void          *obst_level;
814         block_state_t *bs_top;
815         irn_action_t  *ia_top;
816 } rollback_info_t;
817
818 static INLINE block_state_t *get_block_state(global_end_state_t *ges, const block_info_t *bi)
819 {
820         int id = bi->id;
821         assert(!ver_is_younger(ges->bs_tops_vers[id], ges->version));
822         return ver_is_older(ges->bs_tops_vers[id], ges->version) ? NULL : ges->bs_tops[bi->id];
823 }
824
825 static INLINE const workset_t *get_end_state(global_end_state_t *ges, block_info_t *bi)
826 {
827         block_state_t *bs = get_block_state(ges, bi);
828         return bs ? bs->end_state : bi->ws_end;
829 }
830
831 static block_state_t *new_block_state(global_end_state_t *ges, block_info_t *bi)
832 {
833         block_state_t *bs = get_block_state(ges, bi);
834         block_state_t *nw = obstack_alloc(&ges->obst, sizeof(nw[0]));
835
836         nw->next_intern = bs;
837         nw->next        = ges->bs_top;
838         nw->bi          = bi;
839
840         if (bs) {
841                 nw->pressure  = bs->pressure;
842                 nw->end_state = workset_clone(ges->env, &ges->obst, bs->end_state);
843         }
844         else {
845                 nw->pressure  = bi->pressure;
846                 nw->end_state = workset_clone(ges->env, &ges->obst, bi->ws_end);
847         }
848
849         ges->bs_top               = nw;
850         ges->bs_tops[bi->id]      = nw;
851         ges->bs_tops_vers[bi->id] = ges->version;
852         return nw;
853 }
854
855 static irn_action_t *new_irn_action(global_end_state_t *ges, ir_node *irn, const ir_node *bl)
856 {
857         irn_action_t *ia = obstack_alloc(&ges->obst, sizeof(ia[0]));
858
859         ia->irn  = irn;
860         ia->bl   = bl;
861         ia->act  = irn_act_none;
862         ia->next = ges->ia_top;
863         ges->ia_top = ia;
864         return ia;
865 }
866
867 static INLINE rollback_info_t trans_begin(global_end_state_t *ges)
868 {
869         rollback_info_t rb;
870         rb.obst_level = obstack_base(&ges->obst);
871         rb.bs_top     = ges->bs_top;
872         rb.ia_top     = ges->ia_top;
873         return rb;
874 }
875
876 static INLINE void trans_rollback(global_end_state_t *ges, rollback_info_t *rb)
877 {
878         block_state_t *bs;
879
880         /* unwind all the stacks indiced with the block number */
881         for (bs = ges->bs_top; bs != rb->bs_top; bs = bs->next) {
882                 unsigned id = bs->bi->id;
883                 ges->bs_tops[id] = bs->next_intern;
884         }
885
886         ges->ia_top = rb->ia_top;
887         ges->bs_top = rb->bs_top;
888         obstack_free(&ges->obst, rb->obst_level);
889 }
890
891
892 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level);
893
894 static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
895 {
896         block_info_t *bi     = get_block_info(bl);
897         const workset_t *end = get_end_state(ges, bi);
898         double res;
899         int index;
900
901         DBG((dbg, DBG_GLOBAL, "\t%2Dcan make avail %+F at end of %+F\n", level, irn, bl));
902
903         /*
904          * to make the value available at end,
905          * we have several cases here.
906          *
907          * - we already visited that block.
908          * - If the value is in the final end set, return 0.
909          *   somebody else already allocated it there.
910          * - If not and the final end set is already full,
911          *   we cannot make the value available at the end
912          *   of this block. return INFINITY.
913          * - Else (value not in final end set and there is room):
914          *   1) The value is in a register at the end of the local Belady pass.
915          *      Allocate a slot in  the final end set and return 0.
916          *   2) The value is not in the Belady end set:
917          *      If the block's capacity is < k then check what it costs
918          *      to transport the value from upper blocks to this block.
919          *      Compare that against the reload cost in this block. If
920          *      cheaper, do the other thing. If not, reload it here.
921          */
922
923         /* if the end set contains it already, it is in a reg and it costs nothing
924          * to load it to one. */
925         index = workset_get_index(end, irn);
926         if (index >= 0) {
927                 unsigned ver = workset_get_version(end, index);
928                 DBG((dbg, DBG_GLOBAL, "\t%2Dnode is in the end set and is %s fixed\n",
929                                         level, ver_is_older(ver, ges->version) ? "already" : "not yet"));
930
931                 /*
932                  * if the version is older, the value is already fixed
933                  * and cannot be removed from the end set.
934                  *
935                  * If not, we will create a new block state for that block since
936                  * we modify it by giving the end state a new version.
937                  */
938                 if (ver_is_younger(ver, ges->version)) {
939                         block_state_t *bs = new_block_state(ges, bi);
940                         workset_set_version(bs->end_state, index, ges->version);
941                 }
942
943                 res = 0.0;
944                 goto end;
945         }
946
947         /*
948          * Now we have two options:
949          * 1) Reload the value at the end of the block.
950          *    Therefore, perhaps, we have to erase another one from the workset.
951          *    This may only be done if it has not been fixed.
952          *    Since fixed means that a previous pass has decided that that value
953          *    *has* to stay in the end set.
954          * 2) we can try, if the capacity of the block allows it, to let
955          *    the value live through the block and make it available at
956          *    the entrance.
957          *
958          * First, we test the local (reload in this block) alternative
959          * and compare against the other alternative.
960          * Of course, we chose the cheaper one.
961          */
962
963         {
964                 int n_regs = bi->free_at_jump;
965                 int len  = workset_get_length(end);
966                 int slot = -1;
967                 int i;
968
969                 res = HUGE_VAL;
970
971                 /*
972                  * look if there is room in the end array
973                  * for the variable. Note that this does not
974                  * mean that the var can live through the block.
975                  * There is just room at the *end*
976                  */
977                 if (len < n_regs) {
978                         DBG((dbg, DBG_GLOBAL, "\t%2Dthe end set has %d free slots\n", level, n_regs - len));
979                         slot = len;
980                 } else {
981                         for (i = 0; i < len; ++i) {
982                                 unsigned ver = workset_get_version(end, i);
983                                 if (ver_is_younger(ver, ges->version))
984                                         break;
985                         }
986
987                         if (i < len) {
988                                 DBG((dbg, DBG_GLOBAL, "\t%2D%+F (slot %d) can be erased from the end set\n",
989                                                         level, end->vals[i].irn, i));
990                                 slot = i;
991                         }
992                 }
993
994                 /*
995                  * finally there is some room. we can at least reload the value.
996                  * but we will try to let ot live through anyhow.
997                  */
998                 if (slot >= 0) {
999                         irn_action_t *vs    = new_irn_action(ges, irn, bi->bl);
1000                         block_state_t *bs   = new_block_state(ges, bi);
1001                         workset_t *end      = bs->end_state;
1002                         ir_node *ins_before = block_info_get_last_ins(bi);
1003                         double reload_here  = be_get_reload_costs(bi->bel->senv, irn, ins_before);
1004                         int pressure_ok     = bs->pressure < n_regs;
1005
1006                         if (reload_here < bi->reload_cost)
1007                                 reload_here = 0.0;
1008
1009                         /*
1010                          * No matter what we do, the value will be in the end set
1011                          * if the block from now on (of course only regarding the
1012                          * current state). Enter it and set the new length
1013                          * appropriately.
1014                          */
1015                         end->vals[slot].irn     = irn;
1016                         workset_set_version(end, slot, ges->version);
1017                         workset_set_length(end, MAX(workset_get_length(end), slot + 1));
1018
1019                         vs->act = irn_act_reload;
1020                         res     = reload_here;
1021
1022                         DBG((dbg, DBG_GLOBAL, "\t%2Dthere is a free slot. capacity=%d, reload here=%f, pressure %s\n",
1023                                                 level, n_regs - bs->pressure, reload_here, pressure_ok ? "ok" : "insufficient"));
1024
1025
1026                         /* look if we can bring the value in. */
1027                         if (pressure_ok && reload_here > 0.0) {
1028                                 rollback_info_t rb = trans_begin(ges);
1029                                 double new_limit   = MIN(reload_here, limit);
1030
1031                                 vs->act = irn_act_live_through;
1032                                 bs->pressure += 1;
1033                                 res = can_bring_in(ges, bl, irn, new_limit, level + 1);
1034
1035                                 /*
1036                                  * if bring in is too expensive re-adjust the pressure
1037                                  * and roll back the state
1038                                  */
1039                                 if (res >= reload_here) {
1040                                         bs->pressure -= 1;
1041                                         vs->act = irn_act_reload;
1042                                         trans_rollback(ges, &rb);
1043                                         res = reload_here;
1044                                 }
1045                         }
1046
1047
1048                         DBG((dbg, DBG_GLOBAL, "\t%2D%s\n", level,
1049                                                 vs->act == irn_act_reload ? "reloading" : "bringing in"));
1050                 }
1051         }
1052
1053 end:
1054         DBG((dbg, DBG_GLOBAL, "\t%2D-> %f\n", level, res));
1055         return res;
1056 }
1057
1058 static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
1059 {
1060         belady_env_t *env = ges->env;
1061         double glob_costs = HUGE_VAL;
1062
1063         DBG((dbg, DBG_GLOBAL, "\t%2Dcan bring in (max %f) for %+F at block %+F\n", level, limit, irn, bl));
1064
1065         if (is_transport_in(bl, irn)) {
1066                 int i, n           = get_irn_arity(bl);
1067                 ir_node **nodes    = alloca(get_irn_arity(bl) * sizeof(nodes[0]));
1068                 rollback_info_t rb = trans_begin(ges);
1069
1070                 glob_costs = 0.0;
1071                 for (i = 0; i < n; ++i) {
1072                         ir_node *pr = get_Block_cfgpred_block(bl, i);
1073                         ir_node *op = is_local_phi(bl, irn) ? get_irn_n(irn, i) : irn;
1074                         double c;
1075
1076                         /*
1077                          * there might by unknwons as operands of phis in that case
1078                          * we set the costs to zero, since they won't get spilled.
1079                          */
1080                         if (arch_irn_consider_in_reg_alloc(env->cls, op))
1081                                 c = can_make_available_at_end(ges, pr, op, limit - glob_costs, level + 1);
1082                         else
1083                                 c = 0.0;
1084
1085                         glob_costs += c;
1086
1087                         if (glob_costs >= limit) {
1088                                 glob_costs = HUGE_VAL;
1089                                 trans_rollback(ges, &rb);
1090                                 goto end;
1091                         }
1092                 }
1093         }
1094
1095 end:
1096         DBG((dbg, DBG_GLOBAL, "\t%2D-> %f\n", level, glob_costs));
1097         return glob_costs;
1098 }
1099
1100 static void materialize_and_commit_end_state(global_end_state_t *ges)
1101 {
1102         belady_env_t *env = ges->env;
1103         irn_action_t *ia;
1104         block_state_t *bs;
1105
1106         DBG((dbg, DBG_GLOBAL, "\tmaterializing\n"));
1107
1108         /*
1109          * Perform all the variable actions.
1110          */
1111         for (ia = ges->ia_top; ia != NULL; ia = ia->next) {
1112                 switch (ia->act) {
1113                         case irn_act_live_through:
1114                                 {
1115                                         block_info_t *bi = get_block_info(ia->bl);
1116                                         bring_in_t *iter;
1117
1118                                         if (is_local_phi(ia->bl, ia->irn)) {
1119                                                 bitset_add_irn(ges->succ_phis, ia->irn);
1120                                                 DBG((dbg, DBG_GLOBAL, "\t\tlive through phi kept alive: %+F\n", ia->irn));
1121                                         }
1122
1123                                         list_for_each_entry_reverse(bring_in_t, iter, &bi->br_head, list)
1124                                                 ++iter->sect_pressure;
1125                                         ++bi->front_pressure;
1126                                 }
1127                                 break;
1128                         case irn_act_reload:
1129                                 be_add_reload_at_end(env->senv, ia->irn, ia->bl, env->cls, 1);
1130                                 DBG((dbg, DBG_GLOBAL, "\t\tadding reload of %+F at end of %+F\n", ia->irn, ia->bl));
1131                                 break;
1132                         default:
1133                                 DBG((dbg, DBG_GLOBAL, "\t\t%+F is in the end set of %+F\n", ia->irn, ia->bl));
1134                 }
1135         }
1136
1137         /*
1138          * Commit the block end states
1139          */
1140         for (bs = ges->bs_top; bs != NULL; bs = bs->next) {
1141                 block_info_t *bi = bs->bi;
1142
1143                 if (!bitset_is_set(ges->committed, bi->id)) {
1144                         DBG((dbg, DBG_GLOBAL, "\t\tcommiting workset of %+F with version %x\n", bi->bl, ges->version));
1145                         // bes->bs->end_state->vals[idx].version = ges->version;
1146                         workset_copy(env, bi->ws_end, bs->end_state);
1147                         DBG((dbg, DBG_GLOBAL, "\t\told pressure: %d, new pressure: %d, end length: %d\n",
1148                                                 bi->pressure, bs->pressure, workset_get_length(bs->end_state)));
1149                         bi->pressure = bs->pressure;
1150                         bitset_set(ges->committed, bi->id);
1151                 }
1152         }
1153
1154         /* clear the committed bitset. the next call is expecting it. */
1155         bitset_clear_all(ges->committed);
1156 }
1157
1158 static ir_node *better_spilled_here(const bring_in_t *br)
1159 {
1160         const block_info_t *bi = br->bi;
1161         double spill_ef        = get_block_info(get_nodes_block(br->irn))->exec_freq;
1162
1163         /*
1164          * If the bring in node is a phi in the bring in block,
1165          * we look at all definitions and sum up their execution frequencies,
1166          * since spills will be placed there.
1167          * (except for the case where an operand is also a phi which is spilled :-( )
1168          * If that cost is higher than spilling the phi in that block, we opt for
1169          * bringing the phi into the block and spill it there.
1170          */
1171         if (is_local_phi(bi->bl, br->irn)) {
1172                 ir_node *bl = bi->bl;
1173                 int i;
1174
1175                 spill_ef = 0.0;
1176                 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i)
1177                         spill_ef += get_block_info(get_Block_cfgpred_block(bl, i))->exec_freq;
1178         }
1179
1180         return bi->exec_freq < spill_ef ? sched_prev(bi->first_non_in) : NULL;
1181 }
1182
1183 static int get_max_pressure_so_far(const block_info_t *bi, const bring_in_t *br)
1184 {
1185         const struct list_head *l;
1186         int res = INT_MIN;
1187
1188         assert(br->bi == bi);
1189         for (l = &br->list; l != &bi->br_head; l = l->prev) {
1190                 br  = list_entry(l, bring_in_t, list);
1191                 res = MAX(res, br->sect_pressure);
1192         }
1193
1194         /* finally consider the front pressure distance and add the reference line (the global block pressure) */
1195         return MAX(res, bi->front_pressure);
1196 }
1197
1198 #define block_last_bring_in(bi)  list_entry((bi)->br_head.prev, bring_in_t, list)
1199
1200 #if 0
1201 static int get_block_max_pressure(const block_info_t *bi)
1202 {
1203         int max = get_max_pressure_so_far(bi, block_last_bring_in(bi));
1204         return MAX(bi->pressure, max);
1205 }
1206 #endif
1207
1208 /**
1209  * Try to bring a variable into a block.
1210  * @param ges   The state of all end sets.
1211  * @param block The block.
1212  * @param irn   The variable.
1213  */
1214 static void optimize_variable(global_end_state_t *ges, bring_in_t *br)
1215 {
1216         block_info_t *bi          = br->bi;
1217         ir_node *irn              = br->irn;
1218         ir_node *bl               = bi->bl;
1219         belady_env_t *env         = ges->env;
1220         void *reset_level         = obstack_base(&ges->obst);
1221         int k                     = env->n_regs;
1222         int pressure_upto_use     = get_max_pressure_so_far(bi, br);
1223         int front_pressure        = bi->front_pressure;
1224         ir_node *better_spill_loc = NULL;
1225
1226         assert(front_pressure <= k);
1227         assert(pressure_upto_use <= k);
1228
1229         DBG((dbg, DBG_GLOBAL, "fixing %+F at %+F (%f), front pr: %d, pr to use: %d, first use: %x\n",
1230                                 irn, bl, bi->exec_freq, front_pressure, pressure_upto_use, br->first_use));
1231
1232         // assert(!is_local_phi(bl, irn) || !bitset_contains_irn(ges->succ_phis, irn));
1233
1234         /*
1235          * if we cannot bring the value to the use, let's see ifit would be worthwhile
1236          * to bring the value to the beginning of the block to have a better spill
1237          * location.
1238          *
1239          * better _spilled_here will return a node where the value can be spilled after
1240          * or NULL if this block does not provide a better spill location.
1241          */
1242 #if 1
1243         if (pressure_upto_use >= k && front_pressure < k && !bitset_contains_irn(env->spilled, irn))
1244                 better_spill_loc = better_spilled_here(br);
1245 #endif
1246
1247         /*
1248          * If either we can bring the value to the use or we should try
1249          * to bring it here to do the spill here, let's try to bring it in.
1250          */
1251         if (better_spill_loc || pressure_upto_use < k) {
1252                 block_state_t *bs;
1253                 double bring_in_costs, local_costs;
1254                 rollback_info_t trans;
1255                 int pressure_inc;
1256
1257                 /* process all variables which shall be in a reg at
1258                  * the beginning of the block in the order of the next use. */
1259                 local_costs  = be_get_reload_costs(env->senv, irn, br->first_use);
1260
1261                 /* reset the lists */
1262                 ges->bs_top  = NULL;
1263                 ges->ia_top  = NULL;
1264
1265                 /* if the variable will live into this block, we must adapt the pressure.
1266                  * The new pressure is the MAX of:
1267                  * 1) the total block pressure
1268                  * 2) the pressure so far + the front pressure increase + 1
1269                  *
1270                  * If the second is larger than the first,
1271                  * we have to increment the total block pressure and hence
1272                  * save the old pressure to restire it in case of failing to
1273                  * bring the variable into the block in a register.
1274                  */
1275                 trans = trans_begin(ges);
1276                 bs    = new_block_state(ges, bi);
1277                 pressure_inc = MAX(bs->pressure, better_spill_loc ? front_pressure : pressure_upto_use + 1);
1278                 bs->pressure = pressure_inc;
1279
1280
1281                 assert(bi->pressure <= k);
1282                 DBG((dbg, DBG_GLOBAL, "\ttrans in var %+F, version %x\n", irn, ges->version));
1283                 bring_in_costs = can_bring_in(ges, bl, irn, local_costs, 1);
1284                 DBG((dbg, DBG_GLOBAL, "\tbring in: %f, local: %f\n", bring_in_costs, local_costs));
1285
1286                 /*
1287                  * Following cases can now occur:
1288                  * 1) There is room and costs ok
1289                  * 2) Cannot bring to use but can spill at begin and costs are ok
1290                  * 3) neither of both worked.
1291                  *
1292                  * following actions can be taken:
1293                  * a) commit changes
1294                  * b) mark phi as succeded if node was phi
1295                  * c) insert reload at use location
1296                  * d) give a spill location hint
1297                  *
1298                  * this is the case/action matrix
1299                  *   | abcd
1300                  * --+----------
1301                  * 1 | XX
1302                  * 2 | XXXX
1303                  * 3 |   X
1304                  */
1305
1306                 /* the costs were acceptable... */
1307                 if (bring_in_costs < local_costs) {
1308                         int check = 0;
1309                         bring_in_t *iter;
1310
1311                         /*
1312                          * case 1 and first part of case 2:
1313                          * commit all the changes done. this manifests the bring-in action.
1314                          * if the transport-in was a phi (that is actually used in block)
1315                          * mark it in the succ_phis set to *not* phi spill it.
1316                          */
1317                         materialize_and_commit_end_state(ges);
1318                         if (is_local_phi(bl, irn))
1319                                 bitset_add_irn(ges->succ_phis, irn);
1320
1321                         DBG((dbg, DBG_GLOBAL, "\t-> bring it in.", pressure_inc));
1322
1323                         /* second half of case 2 */
1324                         if (pressure_upto_use >= k) {
1325                                 DBG((dbg, DBG_GLOBAL, "\t-> use blocked. local reload: %+F, try spill at: %+F\n",
1326                                                         br->first_use, better_spill_loc));
1327                                 be_add_reload(env->senv, irn, br->first_use, env->cls, 1);
1328                                 be_add_spill(env->senv, irn, better_spill_loc);
1329                                 ir_nodeset_insert(env->extra_spilled, irn);
1330                         }
1331
1332                         /*
1333                          * go from the last bring in use to the first and add all the variabled
1334                          * which additionally live through the block to their pressure.
1335                          * at the point were the actually treated use is, we have to increase
1336                          * the pressure by one more as the nrought in value starts to count.
1337                          * Finally, adjust the front pressure as well.
1338                          */
1339                         pressure_inc = 0;
1340                         list_for_each_entry_reverse(bring_in_t, iter, &bi->br_head, list) {
1341                                 if (iter == br)
1342                                         pressure_inc += pressure_upto_use < k;
1343                                 iter->sect_pressure += pressure_inc;
1344                                 check = MAX(check, iter->sect_pressure);
1345                                 DBG((dbg, DBG_GLOBAL, "\tinc section pressure of %+F by %d to %d\n", iter->first_use, pressure_inc, iter->sect_pressure));
1346                         }
1347                         bi->front_pressure += pressure_inc;
1348                         assert(MAX(check, bi->front_pressure) <= bi->pressure);
1349                         DBG((dbg, DBG_GLOBAL, "\t-> result: p: %d, fp: %d\n", bi->pressure, bi->front_pressure));
1350                 }
1351
1352                 /* case 3: nothing worked. insert normal reload and rollback. */
1353                 else {
1354                         DBG((dbg, DBG_GLOBAL, "\t-> bring in was too expensive. local reload: %+F\n", br->first_use));
1355                         be_add_reload(env->senv, irn, br->first_use, env->cls, 1);
1356                         bitset_add_irn(env->spilled, irn);
1357                         trans_rollback(ges, &trans);
1358                 }
1359         }
1360
1361         /* there was no opportunity for optimization at all. reload and be sad ...  */
1362         else {
1363                 DBG((dbg, DBG_GLOBAL, "\t-> can\'t do anything but reload before %+F\n", br->first_use));
1364                 be_add_reload(env->senv, irn, br->first_use, env->cls, 1);
1365                 bitset_add_irn(env->spilled, irn);
1366         }
1367
1368         DBG((dbg, DBG_GLOBAL, "\n"));
1369
1370         /* reset the obstack and create a new version. */
1371         obstack_free(&ges->obst, reset_level);
1372         ges->version = ver_make_newer(ges->version);
1373 }
1374
1375 static bring_in_t **determine_global_order(belady_env_t *env)
1376 {
1377         bring_in_t **res;
1378         bring_in_t *elm;
1379         int i, n = 0;
1380
1381         for (i = env->n_blocks - 1; i >= 0; --i) {
1382                 block_info_t *bi = get_block_info(env->blocks[i]);
1383                 list_for_each_entry(bring_in_t, elm, &bi->br_head, list) {
1384                         obstack_ptr_grow(&env->ob, elm);
1385                         n += 1;
1386                 }
1387         }
1388
1389         obstack_ptr_grow(&env->ob, NULL);
1390         res = obstack_finish(&env->ob);
1391         qsort(res, n, sizeof(res[0]), bring_in_cmp);
1392         return res;
1393 }
1394
1395
1396
1397
1398 static void global_assign(belady_env_t *env)
1399 {
1400         ir_nodeset_iterator_t iter;
1401         global_end_state_t ges;
1402         bring_in_t **br;
1403         ir_node *irn;
1404         int i, j;
1405
1406         /*
1407          * sort the blocks according to execution frequency.
1408          * That's not necessary for belady() but for the global pass later on.
1409          */
1410         qsort(env->blocks, env->n_blocks, sizeof(env->blocks[0]), block_freq_dfs_gt);
1411
1412         memset(&ges, 0, sizeof(ges));
1413         obstack_init(&ges.obst);
1414         ges.env          = env;
1415         ges.version      = ver_make_newer(ver_oldest);
1416         ges.succ_phis    = bitset_irg_obstack_alloc(&ges.obst, env->irg);
1417         ges.committed    = bitset_obstack_alloc(&ges.obst, env->n_blocks);
1418         ges.bs_tops      = obstack_alloc(&ges.obst, sizeof(ges.bs_tops[0])      * env->n_blocks);
1419         ges.bs_tops_vers = obstack_alloc(&ges.obst, sizeof(ges.bs_tops_vers[0]) * env->n_blocks);
1420
1421         /* invalidate all state stack pointer versions */
1422         for (i = 0; i < env->n_blocks; ++i) {
1423                 block_info_t *bi = get_block_info(env->blocks[i]);
1424                 ges.bs_tops_vers[i] = ver_oldest;
1425
1426                 /* Set all block end sets entries to the youngest version */
1427                 for (j = workset_get_length(bi->ws_end) - 1; j >= 0; --j)
1428                         workset_set_version(bi->ws_end, j, ver_youngest);
1429         }
1430
1431         /* determine ordeer and optimize them */
1432         for (br = determine_global_order(env); *br; ++br)
1433                 optimize_variable(&ges, *br);
1434
1435         /*
1436          * Now we spill phis which cannot be kept since they were replaced
1437          * by reloads at the block entrances.
1438          */
1439         for (i = 0; i < env->n_blocks; ++i) {
1440                 ir_node *bl = env->blocks[i];
1441                 ir_node *irn;
1442
1443                 sched_foreach(bl, irn) {
1444                         if (!is_Phi(irn))
1445                                 break;
1446
1447                         if (arch_irn_consider_in_reg_alloc(env->cls, irn)
1448                                         && !bitset_contains_irn(ges.succ_phis, irn))
1449                                 be_spill_phi(env->senv, irn);
1450                 }
1451         }
1452
1453         /* check dominance for specially spilled nodes. */
1454         foreach_ir_nodeset (env->extra_spilled, irn, iter)
1455                 make_spill_locations_dominate_irn(env->senv, irn);
1456 }
1457
1458 static void collect_blocks(ir_node *bl, void *data)
1459 {
1460         belady_env_t *env = data;
1461         ++env->n_blocks;
1462         obstack_ptr_grow(&env->ob, bl);
1463 }
1464
1465 /**
1466  * Do spilling for a register class on a graph using the belady heuristic.
1467  * In the transformed graph, the register pressure never exceeds the number
1468  * of available registers.
1469  *
1470  * @param birg  The backend graph
1471  * @param cls   The register class to spill
1472  */
1473 void be_spill_belady(be_irg_t *birg, const arch_register_class_t *cls)
1474 {
1475         ir_graph *irg = be_get_birg_irg(birg);
1476         belady_env_t env;
1477         int i, n_regs;
1478
1479         /* some special classes contain only ignore regs, nothing to do then */
1480         n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1481         if(n_regs == 0)
1482                 return;
1483
1484         be_clear_links(irg);
1485
1486         /* init belady env */
1487         obstack_init(&env.ob);
1488         env.irg        = irg;
1489         env.arch       = be_get_birg_arch_env(birg);
1490         env.cls        = cls;
1491         env.lv         = be_get_birg_liveness(birg);
1492         env.dfs        = env.lv->dfs;
1493         env.n_regs     = n_regs;
1494         env.ws         = new_workset(&env, &env.ob);
1495         env.senv       = be_new_spill_env(birg);
1496         env.ef         = be_get_birg_exec_freq(birg);
1497         env.spilled    = bitset_irg_obstack_alloc(&env.ob, irg);
1498         env.extra_spilled = ir_nodeset_new(64);
1499         env.n_blocks   = 0;
1500
1501         irg_block_walk_graph(irg, NULL, collect_blocks, &env);
1502         obstack_ptr_grow(&env.ob, NULL);
1503         env.blocks = obstack_finish(&env.ob);
1504
1505         /* renumbering in the blocks gives nicer debug output as number are smaller. */
1506 #ifdef DEBUG_libfirm
1507         for (i = 0; i < env.n_blocks; ++i)
1508                 sched_renumber(env.blocks[i]);
1509 #endif
1510
1511         /* Fix high register pressure in blocks with belady algorithm */
1512         for (i = 0; i < env.n_blocks; ++i)
1513                 belady(&env, i);
1514
1515         global_assign(&env);
1516
1517         /* check dominance for specially spilled nodes. */
1518         {
1519                 ir_nodeset_iterator_t iter;
1520                 ir_node *irn;
1521
1522                 foreach_ir_nodeset (env.extra_spilled, irn, iter)
1523                         make_spill_locations_dominate_irn(env.senv, irn);
1524         }
1525
1526         /* Insert spill/reload nodes into the graph and fix usages */
1527         be_insert_spills_reloads(env.senv);
1528
1529         /* clean up */
1530         be_delete_spill_env(env.senv);
1531         ir_nodeset_del(env.extra_spilled);
1532
1533         obstack_free(&env.ob, NULL);
1534 }
1535
1536 void be_init_spillbelady2(void)
1537 {
1538         lc_opt_entry_t *be_grp    = lc_opt_get_grp(firm_opt_get_root(), "be");
1539         lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
1540         lc_opt_entry_t *bel2_grp  = lc_opt_get_grp(spill_grp, "belady2");
1541
1542         static be_spiller_t belady_spiller = {
1543                 be_spill_belady
1544         };
1545
1546         lc_opt_add_table(bel2_grp, options);
1547         be_register_spiller("belady2", &belady_spiller);
1548         FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady2");
1549 }
1550
1551 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady2);