2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Beladys spillalgorithm.
23 * @author Daniel Grund, Matthias Braun
32 #include "irprintf_t.h"
38 #include "iredges_t.h"
41 #include "irnodeset.h"
50 #include "bechordal_t.h"
52 #include "beloopana.h"
54 #include "bespillutil.h"
64 #define DBG_WORKSET 128
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
67 #define TIME_UNDEFINED 6666
69 //#define LOOK_AT_LOOPDEPTH
72 * An association between a node and a point in time.
74 typedef struct loc_t {
76 unsigned time; /**< A use time (see beuses.h). */
77 bool spilled; /**< the value was already spilled on this path */
80 typedef struct _workset_t {
81 int len; /**< current length */
82 loc_t vals[0]; /**< inlined array of the values/distances in this working set */
85 static struct obstack obst;
86 static const arch_register_class_t *cls;
87 static const be_lv_t *lv;
88 static be_loopana_t *loop_ana;
90 static workset_t *ws; /**< the main workset used while
91 processing a block. */
92 static be_uses_t *uses; /**< env for the next-use magic */
93 static ir_node *instr; /**< current instruction */
94 static unsigned instr_nr; /**< current instruction number
95 (relative to block start) */
96 static spill_env_t *senv; /**< see bespill.h */
97 static ir_node **blocklist;
99 static int move_spills = true;
100 static int respectloopdepth = true;
101 static int improve_known_preds = true;
102 /* factor to weight the different costs of reloading/rematerializing a node
103 (see bespill.h be_get_reload_costs_no_weight) */
104 static int remat_bonus = 10;
106 static const lc_opt_table_entry_t options[] = {
107 LC_OPT_ENT_BOOL ("movespills", "try to move spills out of loops", &move_spills),
108 LC_OPT_ENT_BOOL ("respectloopdepth", "exprimental (outermost loop cutting)", &respectloopdepth),
109 LC_OPT_ENT_BOOL ("improveknownpreds", "experimental (known preds cutting)", &improve_known_preds),
110 LC_OPT_ENT_INT ("rematbonus", "give bonus to rematerialisable nodes", &remat_bonus),
114 static int loc_compare(const void *a, const void *b)
118 return p->time - q->time;
121 void workset_print(const workset_t *w)
125 for (i = 0; i < w->len; ++i) {
126 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
131 * Alloc a new workset on obstack @p ob with maximum size @p max
133 static workset_t *new_workset(void)
135 return OALLOCFZ(&obst, workset_t, vals, n_regs);
139 * Alloc a new instance on obstack and make it equal to @param workset
141 static workset_t *workset_clone(workset_t *workset)
143 workset_t *res = OALLOCF(&obst, workset_t, vals, n_regs);
144 memcpy(res, workset, sizeof(*res) + n_regs * sizeof(res->vals[0]));
149 * Copy workset @param src to @param tgt
151 static void workset_copy(workset_t *dest, const workset_t *src)
153 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
154 memcpy(dest, src, size);
158 * Overwrites the current content array of @param ws with the
159 * @param count locations given at memory @param locs.
160 * Set the length of @param ws to count.
162 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
164 workset->len = count;
165 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
169 * Inserts the value @p val into the workset, iff it is not
170 * already contained. The workset must not be full.
172 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
176 /* check for current regclass */
177 assert(arch_irn_consider_in_reg_alloc(cls, val));
179 /* check if val is already contained */
180 for (i = 0; i < workset->len; ++i) {
181 loc = &workset->vals[i];
182 if (loc->node == val) {
191 assert(workset->len < n_regs && "Workset already full!");
192 loc = &workset->vals[workset->len];
194 loc->spilled = spilled;
195 loc->time = TIME_UNDEFINED;
200 * Removes all entries from this workset
202 static void workset_clear(workset_t *workset)
208 * Removes the value @p val from the workset if present.
210 static inline void workset_remove(workset_t *workset, ir_node *val)
213 for (i = 0; i < workset->len; ++i) {
214 if (workset->vals[i].node == val) {
215 workset->vals[i] = workset->vals[--workset->len];
221 static inline const loc_t *workset_contains(const workset_t *ws,
226 for (i = 0; i < ws->len; ++i) {
227 if (ws->vals[i].node == val)
235 * Iterates over all values in the working set.
236 * @p ws The workset to iterate
237 * @p v A variable to put the current value in
238 * @p i An integer for internal use
240 #define workset_foreach(ws, v, i) for (i=0; \
241 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
244 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
245 #define workset_get_time(ws, i) (ws)->vals[i].time
246 #define workset_set_length(ws, length) (ws)->len = length
247 #define workset_get_length(ws) ((ws)->len)
248 #define workset_get_val(ws, i) ((ws)->vals[i].node)
249 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
251 typedef struct _block_info_t
253 workset_t *start_workset;
254 workset_t *end_workset;
258 static block_info_t *new_block_info(void)
260 return OALLOCZ(&obst, block_info_t);
263 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
264 #define set_block_info(block, info) set_irn_link(block, info)
267 * @return The distance to the next use or 0 if irn has dont_spill flag set
269 static inline unsigned get_distance(ir_node *from, unsigned from_step,
270 const ir_node *def, int skip_from_uses)
276 assert(!arch_irn_is_ignore(def));
278 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
280 if (USES_IS_INFINITE(time))
281 return USES_INFINITY;
283 /* We have to keep nonspillable nodes in the workingset */
284 if (arch_irn_get_flags(skip_Proj_const(def)) & arch_irn_flags_dont_spill)
287 /* give some bonus to rematerialisable nodes */
288 if (remat_bonus > 0) {
289 costs = be_get_reload_costs_no_weight(senv, def, use.before);
290 assert(costs * remat_bonus < 1000);
291 time += 1000 - (costs * remat_bonus);
298 * Performs the actions necessary to grant the request that:
299 * - new_vals can be held in registers
300 * - as few as possible other values are disposed
301 * - the worst values get disposed
303 * @p is_usage indicates that the values in new_vals are used (not defined)
304 * In this case reloads must be performed
306 static void displace(workset_t *new_vals, int is_usage)
308 ir_node **to_insert = ALLOCAN(ir_node*, n_regs);
309 bool *spilled = ALLOCAN(bool, n_regs);
317 /* 1. Identify the number of needed slots and the values to reload */
319 workset_foreach(new_vals, val, iter) {
320 bool reloaded = false;
322 if (! workset_contains(ws, val)) {
323 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
325 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
326 be_add_reload(senv, val, instr, cls, 1);
330 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
332 /* remove the value from the current workset so it is not accidently
334 workset_remove(ws, val);
336 spilled[demand] = reloaded;
337 to_insert[demand] = val;
341 /* 2. Make room for at least 'demand' slots */
342 len = workset_get_length(ws);
343 spills_needed = len + demand - n_regs;
344 assert(spills_needed <= len);
346 /* Only make more free room if we do not have enough */
347 if (spills_needed > 0) {
348 ir_node *curr_bb = NULL;
349 workset_t *ws_start = NULL;
352 curr_bb = get_nodes_block(instr);
353 ws_start = get_block_info(curr_bb)->start_workset;
356 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
358 /* calculate current next-use distance for live values */
359 for (i = 0; i < len; ++i) {
360 ir_node *val = workset_get_val(ws, i);
361 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
362 workset_set_time(ws, i, dist);
365 /* sort entries by increasing nextuse-distance*/
368 for (i = len - spills_needed; i < len; ++i) {
369 ir_node *val = ws->vals[i].node;
371 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
372 workset_get_time(ws, i)));
375 if (!USES_IS_INFINITE(ws->vals[i].time)
376 && !ws->vals[i].spilled) {
377 ir_node *after_pos = sched_prev(instr);
378 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
380 be_add_spill(senv, val, after_pos);
385 /* kill the last 'demand' entries in the array */
386 workset_set_length(ws, len - spills_needed);
389 /* 3. Insert the new values into the workset */
390 for (i = 0; i < demand; ++i) {
391 ir_node *val = to_insert[i];
393 workset_insert(ws, val, spilled[i]);
398 AVAILABLE_EVERYWHERE,
404 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
405 size_t n_pred_worksets,
406 const ir_node *value, bool is_local_phi)
409 bool avail_everywhere = true;
410 bool avail_nowhere = true;
412 assert(n_pred_worksets > 0);
414 /* value available in all preds? */
415 for (i = 0; i < n_pred_worksets; ++i) {
417 const workset_t *p_workset = pred_worksets[i];
418 int p_len = workset_get_length(p_workset);
420 const ir_node *l_value;
423 assert(is_Phi(value));
424 l_value = get_irn_n(value, i);
429 for (p_i = 0; p_i < p_len; ++p_i) {
430 const loc_t *p_l = &p_workset->vals[p_i];
431 if (p_l->node != l_value)
439 avail_nowhere = false;
441 avail_everywhere = false;
445 if (avail_everywhere) {
446 assert(!avail_nowhere);
447 return AVAILABLE_EVERYWHERE;
448 } else if (avail_nowhere) {
449 return AVAILABLE_NOWHERE;
451 return AVAILABLE_PARTLY;
455 /** Decides whether a specific node should be in the start workset or not
457 * @param env belady environment
459 * @param node the node to test
460 * @param loop the loop of the node
462 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
463 ir_loop *loop, unsigned available)
465 be_next_use_t next_use;
468 loc.time = USES_INFINITY;
472 if (!arch_irn_consider_in_reg_alloc(cls, node)) {
473 loc.time = USES_INFINITY;
477 /* We have to keep nonspillable nodes in the workingset */
478 if (arch_irn_get_flags(skip_Proj_const(node)) & arch_irn_flags_dont_spill) {
480 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
484 next_use = be_get_next_use(uses, first, 0, node, 0);
485 if (USES_IS_INFINITE(next_use.time)) {
486 // the nodes marked as live in shouldn't be dead, so it must be a phi
487 assert(is_Phi(node));
488 loc.time = USES_INFINITY;
489 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
493 loc.time = next_use.time;
495 if (improve_known_preds) {
496 if (available == AVAILABLE_EVERYWHERE) {
497 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
500 } else if (available == AVAILABLE_NOWHERE) {
501 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
503 loc.time = USES_INFINITY;
508 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
509 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
510 next_use.outermost_loop));
512 loc.time = USES_PENDING;
513 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
514 node, next_use.outermost_loop, get_loop_depth(loop)));
521 * Computes the start-workset for a block with multiple predecessors. We assume
522 * that at least 1 of the predeccesors is a back-edge which means we're at the
523 * beginning of a loop. We try to reload as much values as possible now so they
524 * don't get reloaded inside the loop.
526 static void decide_start_workset(const ir_node *block)
528 ir_loop *loop = get_irn_loop(block);
534 int i, len, ws_count;
535 int free_slots, free_pressure_slots;
538 workset_t **pred_worksets;
539 bool all_preds_known;
541 /* check predecessors */
542 arity = get_irn_arity(block);
543 pred_worksets = ALLOCAN(workset_t*, arity);
544 all_preds_known = true;
545 for (i = 0; i < arity; ++i) {
546 ir_node *pred_block = get_Block_cfgpred_block(block, i);
547 block_info_t *pred_info = get_block_info(pred_block);
549 if (pred_info == NULL) {
550 pred_worksets[i] = NULL;
551 all_preds_known = false;
553 pred_worksets[i] = pred_info->end_workset;
557 /* Collect all values living at start of block */
558 starters = NEW_ARR_F(loc_t, 0);
559 delayed = NEW_ARR_F(loc_t, 0);
561 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
562 first = sched_first(block);
564 /* check all Phis first */
565 sched_foreach(block, node) {
570 if (!arch_irn_consider_in_reg_alloc(cls, node))
573 if (all_preds_known) {
574 available = available_in_all_preds(pred_worksets, arity, node, true);
576 available = AVAILABLE_UNKNOWN;
579 loc = to_take_or_not_to_take(first, node, loop, available);
581 if (! USES_IS_INFINITE(loc.time)) {
582 if (USES_IS_PENDING(loc.time))
583 ARR_APP1(loc_t, delayed, loc);
585 ARR_APP1(loc_t, starters, loc);
587 be_spill_phi(senv, node);
591 /* check all Live-Ins */
592 be_lv_foreach(lv, block, be_lv_state_in, i) {
593 ir_node *node = be_lv_get_irn(lv, block, i);
596 if (all_preds_known) {
597 available = available_in_all_preds(pred_worksets, arity, node, false);
599 available = AVAILABLE_UNKNOWN;
602 loc = to_take_or_not_to_take(first, node, loop, available);
604 if (! USES_IS_INFINITE(loc.time)) {
605 if (USES_IS_PENDING(loc.time))
606 ARR_APP1(loc_t, delayed, loc);
608 ARR_APP1(loc_t, starters, loc);
612 pressure = be_get_loop_pressure(loop_ana, cls, loop);
613 assert(ARR_LEN(delayed) <= (signed)pressure);
614 free_slots = n_regs - ARR_LEN(starters);
615 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
616 free_slots = MIN(free_slots, free_pressure_slots);
618 /* so far we only put nodes into the starters list that are used inside
619 * the loop. If register pressure in the loop is low then we can take some
620 * values and let them live through the loop */
621 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
622 pressure, free_slots));
623 if (free_slots > 0) {
624 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
626 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
628 loc_t *loc = & delayed[i];
630 if (!is_Phi(loc->node)) {
631 /* don't use values which are dead in a known predecessors
632 * to not induce unnecessary reloads */
633 arity = get_irn_arity(block);
634 for (p = 0; p < arity; ++p) {
635 ir_node *pred_block = get_Block_cfgpred_block(block, p);
636 block_info_t *pred_info = get_block_info(pred_block);
638 if (pred_info == NULL)
641 if (!workset_contains(pred_info->end_workset, loc->node)) {
643 " delayed %+F not live at pred %+F\n", loc->node,
650 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
651 ARR_APP1(loc_t, starters, *loc);
659 /* spill phis (the actual phis not just their values) that are in this block
660 * but not in the start workset */
661 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
662 ir_node *node = delayed[i].node;
663 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
666 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
667 be_spill_phi(senv, node);
671 /* Sort start values by first use */
672 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
674 /* Copy the best ones from starters to start workset */
675 ws_count = MIN(ARR_LEN(starters), n_regs);
677 workset_bulk_fill(ws, ws_count, starters);
679 /* spill phis (the actual phis not just their values) that are in this block
680 * but not in the start workset */
681 len = ARR_LEN(starters);
682 for (i = ws_count; i < len; ++i) {
683 ir_node *node = starters[i].node;
684 if (! is_Phi(node) || get_nodes_block(node) != block)
687 DB((dbg, DBG_START, " spilling phi %+F\n", node));
688 be_spill_phi(senv, node);
693 /* determine spill status of the values: If there's 1 pred block (which
694 * is no backedge) where the value is spilled then we must set it to
696 for (i = 0; i < ws_count; ++i) {
697 loc_t *loc = &ws->vals[i];
698 ir_node *value = loc->node;
702 /* phis from this block aren't spilled */
703 if (get_nodes_block(value) == block) {
704 assert(is_Phi(value));
705 loc->spilled = false;
709 /* determine if value was spilled on any predecessor */
711 for (n = 0; n < arity; ++n) {
712 workset_t *pred_workset = pred_worksets[n];
716 if (pred_workset == NULL)
719 p_len = workset_get_length(pred_workset);
720 for (p = 0; p < p_len; ++p) {
721 loc_t *l = &pred_workset->vals[p];
723 if (l->node != value)
733 loc->spilled = spilled;
738 * For the given block @p block, decide for each values
739 * whether it is used from a register or is reloaded
742 static void process_block(ir_node *block)
747 block_info_t *block_info;
750 /* no need to process a block twice */
751 assert(get_block_info(block) == NULL);
753 /* construct start workset */
754 arity = get_Block_n_cfgpreds(block);
756 /* no predecessor -> empty set */
758 } else if (arity == 1) {
759 /* one predecessor, copy it's end workset */
760 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
761 block_info_t *pred_info = get_block_info(pred_block);
763 assert(pred_info != NULL);
764 workset_copy(ws, pred_info->end_workset);
766 /* multiple predecessors, do more advanced magic :) */
767 decide_start_workset(block);
770 DB((dbg, DBG_DECIDE, "\n"));
771 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
773 block_info = new_block_info();
774 set_block_info(block, block_info);
776 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
777 workset_foreach(ws, irn, iter) {
778 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
779 workset_get_time(ws, iter)));
782 block_info->start_workset = workset_clone(ws);
784 /* process the block from start to end */
785 DB((dbg, DBG_WSETS, "Processing...\n"));
787 /* TODO: this leaks (into the obstack)... */
788 new_vals = new_workset();
790 sched_foreach(block, irn) {
792 assert(workset_get_length(ws) <= n_regs);
794 /* Phis are no real instr (see insert_starters()) */
798 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
800 /* set instruction in the workset */
803 /* allocate all values _used_ by this instruction */
804 workset_clear(new_vals);
805 for (i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
806 ir_node *in = get_irn_n(irn, i);
807 if (!arch_irn_consider_in_reg_alloc(cls, in))
810 /* (note that "spilled" is irrelevant here) */
811 workset_insert(new_vals, in, false);
813 displace(new_vals, 1);
815 /* allocate all values _defined_ by this instruction */
816 workset_clear(new_vals);
817 if (get_irn_mode(irn) == mode_T) {
818 const ir_edge_t *edge;
820 foreach_out_edge(irn, edge) {
821 ir_node *proj = get_edge_src_irn(edge);
822 if (!arch_irn_consider_in_reg_alloc(cls, proj))
824 workset_insert(new_vals, proj, false);
827 if (!arch_irn_consider_in_reg_alloc(cls, irn))
829 workset_insert(new_vals, irn, false);
831 displace(new_vals, 0);
836 /* Remember end-workset for this block */
837 block_info->end_workset = workset_clone(ws);
838 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
839 workset_foreach(ws, irn, iter)
840 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
841 workset_get_time(ws, iter)));
845 * 'decide' is block-local and makes assumptions
846 * about the set of live-ins. Thus we must adapt the
847 * live-outs to the live-ins at each block-border.
849 static void fix_block_borders(ir_node *block, void *data)
851 workset_t *start_workset;
857 DB((dbg, DBG_FIX, "\n"));
858 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
860 arity = get_irn_arity(block);
861 /* can happen for endless loops */
865 start_workset = get_block_info(block)->start_workset;
867 /* process all pred blocks */
868 for (i = 0; i < arity; ++i) {
869 ir_node *pred = get_Block_cfgpred_block(block, i);
870 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
873 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
875 /* spill all values not used anymore */
876 workset_foreach(pred_end_workset, node, iter) {
880 workset_foreach(start_workset, n2, iter2) {
885 /* note that we do not look at phi inputs, becuase the values
886 * will be either live-end and need no spill or
887 * they have other users in which must be somewhere else in the
894 if (move_spills && be_is_live_in(lv, block, node)
895 && !pred_end_workset->vals[iter].spilled) {
896 ir_node *insert_point;
898 insert_point = be_get_end_of_block_insertion_point(pred);
899 insert_point = sched_prev(insert_point);
901 insert_point = block;
903 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
905 be_add_spill(senv, node, insert_point);
909 /* reload missing values in predecessors, add missing spills */
910 workset_foreach(start_workset, node, iter) {
911 const loc_t *l = &start_workset->vals[iter];
912 const loc_t *pred_loc;
914 /* if node is a phi of the current block we reload
915 * the corresponding argument, else node itself */
916 if (is_Phi(node) && get_nodes_block(node) == block) {
917 node = get_irn_n(node, i);
920 /* we might have unknowns as argument for the phi */
921 if (!arch_irn_consider_in_reg_alloc(cls, node))
925 /* check if node is in a register at end of pred */
926 pred_loc = workset_contains(pred_end_workset, node);
927 if (pred_loc != NULL) {
928 /* we might have to spill value on this path */
929 if (move_spills && !pred_loc->spilled && l->spilled) {
930 ir_node *insert_point
931 = be_get_end_of_block_insertion_point(pred);
932 insert_point = sched_prev(insert_point);
933 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
935 be_add_spill(senv, node, insert_point);
938 /* node is not in register at the end of pred -> reload it */
939 DB((dbg, DBG_FIX, " reload %+F\n", node));
940 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
941 be_add_reload_on_edge(senv, node, block, i, cls, 1);
947 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
950 ir_graph *irg = be_get_birg_irg(birg);
952 be_liveness_assure_sets(be_assure_liveness(birg));
955 /* construct control flow loop tree */
956 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
957 construct_cf_backedges(irg);
959 stat_ev_tim_pop("belady_time_backedges");
963 stat_ev_tim_pop("belady_time_clear_links");
965 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
967 /* init belady env */
971 lv = be_get_birg_liveness(birg);
972 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
974 uses = be_begin_uses(irg, lv);
975 loop_ana = be_new_loop_pressure(birg, cls);
976 senv = be_new_spill_env(birg);
977 blocklist = be_get_cfgpostorder(irg);
978 stat_ev_tim_pop("belady_time_init");
981 /* walk blocks in reverse postorder */
982 for (i = ARR_LEN(blocklist) - 1; i >= 0; --i) {
983 process_block(blocklist[i]);
985 DEL_ARR_F(blocklist);
986 stat_ev_tim_pop("belady_time_belady");
989 /* belady was block-local, fix the global flow by adding reloads on the
991 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
992 stat_ev_tim_pop("belady_time_fix_borders");
994 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
996 /* Insert spill/reload nodes into the graph and fix usages */
997 be_insert_spills_reloads(senv);
1000 be_delete_spill_env(senv);
1002 be_free_loop_pressure(loop_ana);
1003 obstack_free(&obst, NULL);
1006 void be_init_spillbelady(void)
1008 static be_spiller_t belady_spiller = {
1011 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1012 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1013 lc_opt_add_table(belady_group, options);
1015 be_register_spiller("belady", &belady_spiller);
1016 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1019 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);