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;
122 * Alloc a new workset on obstack @p ob with maximum size @p max
124 static workset_t *new_workset(void)
126 return OALLOCFZ(&obst, workset_t, vals, n_regs);
130 * Alloc a new instance on obstack and make it equal to @param workset
132 static workset_t *workset_clone(workset_t *workset)
134 workset_t *res = OALLOCF(&obst, workset_t, vals, n_regs);
135 memcpy(res, workset, sizeof(*res) + n_regs * sizeof(res->vals[0]));
140 * Copy workset @param src to @param tgt
142 static void workset_copy(workset_t *dest, const workset_t *src)
144 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
145 memcpy(dest, src, size);
149 * Overwrites the current content array of @param ws with the
150 * @param count locations given at memory @param locs.
151 * Set the length of @param ws to count.
153 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
155 workset->len = count;
156 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
160 * Inserts the value @p val into the workset, iff it is not
161 * already contained. The workset must not be full.
163 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
167 /* check for current regclass */
168 assert(arch_irn_consider_in_reg_alloc(cls, val));
170 /* check if val is already contained */
171 for (i = 0; i < workset->len; ++i) {
172 loc = &workset->vals[i];
173 if (loc->node == val) {
182 assert(workset->len < n_regs && "Workset already full!");
183 loc = &workset->vals[workset->len];
185 loc->spilled = spilled;
186 loc->time = TIME_UNDEFINED;
191 * Removes all entries from this workset
193 static void workset_clear(workset_t *workset)
199 * Removes the value @p val from the workset if present.
201 static inline void workset_remove(workset_t *workset, ir_node *val)
204 for (i = 0; i < workset->len; ++i) {
205 if (workset->vals[i].node == val) {
206 workset->vals[i] = workset->vals[--workset->len];
212 static inline const loc_t *workset_contains(const workset_t *ws,
217 for (i = 0; i < ws->len; ++i) {
218 if (ws->vals[i].node == val)
226 * Iterates over all values in the working set.
227 * @p ws The workset to iterate
228 * @p v A variable to put the current value in
229 * @p i An integer for internal use
231 #define workset_foreach(ws, v, i) for (i=0; \
232 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
235 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
236 #define workset_get_time(ws, i) (ws)->vals[i].time
237 #define workset_set_length(ws, length) (ws)->len = length
238 #define workset_get_length(ws) ((ws)->len)
239 #define workset_get_val(ws, i) ((ws)->vals[i].node)
240 #define workset_sort(ws) do { qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare); } while(0)
242 typedef struct _block_info_t
244 workset_t *start_workset;
245 workset_t *end_workset;
249 static block_info_t *new_block_info(void)
251 return OALLOCZ(&obst, block_info_t);
254 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
255 #define set_block_info(block, info) set_irn_link(block, info)
258 * @return The distance to the next use or 0 if irn has dont_spill flag set
260 static inline unsigned get_distance(ir_node *from, unsigned from_step,
261 const ir_node *def, int skip_from_uses)
267 assert(!arch_irn_is_ignore(def));
269 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
271 if (USES_IS_INFINITE(time))
272 return USES_INFINITY;
274 /* We have to keep nonspillable nodes in the workingset */
275 if (arch_irn_get_flags(skip_Proj_const(def)) & arch_irn_flags_dont_spill)
278 /* give some bonus to rematerialisable nodes */
279 if (remat_bonus > 0) {
280 costs = be_get_reload_costs_no_weight(senv, def, use.before);
281 assert(costs * remat_bonus < 1000);
282 time += 1000 - (costs * remat_bonus);
289 * Performs the actions necessary to grant the request that:
290 * - new_vals can be held in registers
291 * - as few as possible other values are disposed
292 * - the worst values get disposed
294 * @p is_usage indicates that the values in new_vals are used (not defined)
295 * In this case reloads must be performed
297 static void displace(workset_t *new_vals, int is_usage)
299 ir_node **to_insert = ALLOCAN(ir_node*, n_regs);
300 bool *spilled = ALLOCAN(bool, n_regs);
308 /* 1. Identify the number of needed slots and the values to reload */
310 workset_foreach(new_vals, val, iter) {
311 bool reloaded = false;
313 if (! workset_contains(ws, val)) {
314 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
316 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
317 be_add_reload(senv, val, instr, cls, 1);
321 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
323 /* remove the value from the current workset so it is not accidently
325 workset_remove(ws, val);
327 spilled[demand] = reloaded;
328 to_insert[demand] = val;
332 /* 2. Make room for at least 'demand' slots */
333 len = workset_get_length(ws);
334 spills_needed = len + demand - n_regs;
335 assert(spills_needed <= len);
337 /* Only make more free room if we do not have enough */
338 if (spills_needed > 0) {
339 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
341 /* calculate current next-use distance for live values */
342 for (i = 0; i < len; ++i) {
343 ir_node *val = workset_get_val(ws, i);
344 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
345 workset_set_time(ws, i, dist);
348 /* sort entries by increasing nextuse-distance*/
351 for (i = len - spills_needed; i < len; ++i) {
352 ir_node *val = ws->vals[i].node;
354 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
355 workset_get_time(ws, i)));
358 if (!USES_IS_INFINITE(ws->vals[i].time)
359 && !ws->vals[i].spilled) {
360 ir_node *after_pos = sched_prev(instr);
361 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
363 be_add_spill(senv, val, after_pos);
368 /* kill the last 'demand' entries in the array */
369 workset_set_length(ws, len - spills_needed);
372 /* 3. Insert the new values into the workset */
373 for (i = 0; i < demand; ++i) {
374 ir_node *val = to_insert[i];
376 workset_insert(ws, val, spilled[i]);
381 AVAILABLE_EVERYWHERE,
387 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
388 size_t n_pred_worksets,
389 const ir_node *value, bool is_local_phi)
392 bool avail_everywhere = true;
393 bool avail_nowhere = true;
395 assert(n_pred_worksets > 0);
397 /* value available in all preds? */
398 for (i = 0; i < n_pred_worksets; ++i) {
400 const workset_t *p_workset = pred_worksets[i];
401 int p_len = workset_get_length(p_workset);
403 const ir_node *l_value;
406 assert(is_Phi(value));
407 l_value = get_irn_n(value, i);
412 for (p_i = 0; p_i < p_len; ++p_i) {
413 const loc_t *p_l = &p_workset->vals[p_i];
414 if (p_l->node != l_value)
422 avail_nowhere = false;
424 avail_everywhere = false;
428 if (avail_everywhere) {
429 assert(!avail_nowhere);
430 return AVAILABLE_EVERYWHERE;
431 } else if (avail_nowhere) {
432 return AVAILABLE_NOWHERE;
434 return AVAILABLE_PARTLY;
438 /** Decides whether a specific node should be in the start workset or not
440 * @param env belady environment
442 * @param node the node to test
443 * @param loop the loop of the node
445 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
446 ir_loop *loop, unsigned available)
448 be_next_use_t next_use;
451 loc.time = USES_INFINITY;
455 if (!arch_irn_consider_in_reg_alloc(cls, node)) {
456 loc.time = USES_INFINITY;
460 /* We have to keep nonspillable nodes in the workingset */
461 if (arch_irn_get_flags(skip_Proj_const(node)) & arch_irn_flags_dont_spill) {
463 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
467 next_use = be_get_next_use(uses, first, 0, node, 0);
468 if (USES_IS_INFINITE(next_use.time)) {
469 // the nodes marked as live in shouldn't be dead, so it must be a phi
470 assert(is_Phi(node));
471 loc.time = USES_INFINITY;
472 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
476 loc.time = next_use.time;
478 if (improve_known_preds) {
479 if (available == AVAILABLE_EVERYWHERE) {
480 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
483 } else if (available == AVAILABLE_NOWHERE) {
484 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
486 loc.time = USES_INFINITY;
491 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
492 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
493 next_use.outermost_loop));
495 loc.time = USES_PENDING;
496 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
497 node, next_use.outermost_loop, get_loop_depth(loop)));
504 * Computes the start-workset for a block with multiple predecessors. We assume
505 * that at least 1 of the predeccesors is a back-edge which means we're at the
506 * beginning of a loop. We try to reload as much values as possible now so they
507 * don't get reloaded inside the loop.
509 static void decide_start_workset(const ir_node *block)
511 ir_loop *loop = get_irn_loop(block);
517 int i, len, ws_count;
518 int free_slots, free_pressure_slots;
521 workset_t **pred_worksets;
522 bool all_preds_known;
524 /* check predecessors */
525 arity = get_irn_arity(block);
526 pred_worksets = ALLOCAN(workset_t*, arity);
527 all_preds_known = true;
528 for (i = 0; i < arity; ++i) {
529 ir_node *pred_block = get_Block_cfgpred_block(block, i);
530 block_info_t *pred_info = get_block_info(pred_block);
532 if (pred_info == NULL) {
533 pred_worksets[i] = NULL;
534 all_preds_known = false;
536 pred_worksets[i] = pred_info->end_workset;
540 /* Collect all values living at start of block */
541 starters = NEW_ARR_F(loc_t, 0);
542 delayed = NEW_ARR_F(loc_t, 0);
544 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
545 first = sched_first(block);
547 /* check all Phis first */
548 sched_foreach(block, node) {
553 if (!arch_irn_consider_in_reg_alloc(cls, node))
556 if (all_preds_known) {
557 available = available_in_all_preds(pred_worksets, arity, node, true);
559 available = AVAILABLE_UNKNOWN;
562 loc = to_take_or_not_to_take(first, node, loop, available);
564 if (! USES_IS_INFINITE(loc.time)) {
565 if (USES_IS_PENDING(loc.time))
566 ARR_APP1(loc_t, delayed, loc);
568 ARR_APP1(loc_t, starters, loc);
570 be_spill_phi(senv, node);
574 /* check all Live-Ins */
575 be_lv_foreach(lv, block, be_lv_state_in, i) {
576 ir_node *node = be_lv_get_irn(lv, block, i);
579 if (all_preds_known) {
580 available = available_in_all_preds(pred_worksets, arity, node, false);
582 available = AVAILABLE_UNKNOWN;
585 loc = to_take_or_not_to_take(first, node, loop, available);
587 if (! USES_IS_INFINITE(loc.time)) {
588 if (USES_IS_PENDING(loc.time))
589 ARR_APP1(loc_t, delayed, loc);
591 ARR_APP1(loc_t, starters, loc);
595 pressure = be_get_loop_pressure(loop_ana, cls, loop);
596 assert(ARR_LEN(delayed) <= (signed)pressure);
597 free_slots = n_regs - ARR_LEN(starters);
598 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
599 free_slots = MIN(free_slots, free_pressure_slots);
601 /* so far we only put nodes into the starters list that are used inside
602 * the loop. If register pressure in the loop is low then we can take some
603 * values and let them live through the loop */
604 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
605 pressure, free_slots));
606 if (free_slots > 0) {
607 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
609 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
611 loc_t *loc = & delayed[i];
613 if (!is_Phi(loc->node)) {
614 /* don't use values which are dead in a known predecessors
615 * to not induce unnecessary reloads */
616 arity = get_irn_arity(block);
617 for (p = 0; p < arity; ++p) {
618 ir_node *pred_block = get_Block_cfgpred_block(block, p);
619 block_info_t *pred_info = get_block_info(pred_block);
621 if (pred_info == NULL)
624 if (!workset_contains(pred_info->end_workset, loc->node)) {
626 " delayed %+F not live at pred %+F\n", loc->node,
633 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
634 ARR_APP1(loc_t, starters, *loc);
642 /* spill phis (the actual phis not just their values) that are in this block
643 * but not in the start workset */
644 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
645 ir_node *node = delayed[i].node;
646 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
649 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
650 be_spill_phi(senv, node);
654 /* Sort start values by first use */
655 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
657 /* Copy the best ones from starters to start workset */
658 ws_count = MIN(ARR_LEN(starters), n_regs);
660 workset_bulk_fill(ws, ws_count, starters);
662 /* spill phis (the actual phis not just their values) that are in this block
663 * but not in the start workset */
664 len = ARR_LEN(starters);
665 for (i = ws_count; i < len; ++i) {
666 ir_node *node = starters[i].node;
667 if (! is_Phi(node) || get_nodes_block(node) != block)
670 DB((dbg, DBG_START, " spilling phi %+F\n", node));
671 be_spill_phi(senv, node);
676 /* determine spill status of the values: If there's 1 pred block (which
677 * is no backedge) where the value is spilled then we must set it to
679 for (i = 0; i < ws_count; ++i) {
680 loc_t *loc = &ws->vals[i];
681 ir_node *value = loc->node;
685 /* phis from this block aren't spilled */
686 if (get_nodes_block(value) == block) {
687 assert(is_Phi(value));
688 loc->spilled = false;
692 /* determine if value was spilled on any predecessor */
694 for (n = 0; n < arity; ++n) {
695 workset_t *pred_workset = pred_worksets[n];
699 if (pred_workset == NULL)
702 p_len = workset_get_length(pred_workset);
703 for (p = 0; p < p_len; ++p) {
704 loc_t *l = &pred_workset->vals[p];
706 if (l->node != value)
716 loc->spilled = spilled;
721 * For the given block @p block, decide for each values
722 * whether it is used from a register or is reloaded
725 static void process_block(ir_node *block)
730 block_info_t *block_info;
733 /* no need to process a block twice */
734 assert(get_block_info(block) == NULL);
736 /* construct start workset */
737 arity = get_Block_n_cfgpreds(block);
739 /* no predecessor -> empty set */
741 } else if (arity == 1) {
742 /* one predecessor, copy it's end workset */
743 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
744 block_info_t *pred_info = get_block_info(pred_block);
746 assert(pred_info != NULL);
747 workset_copy(ws, pred_info->end_workset);
749 /* multiple predecessors, do more advanced magic :) */
750 decide_start_workset(block);
753 DB((dbg, DBG_DECIDE, "\n"));
754 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
756 block_info = new_block_info();
757 set_block_info(block, block_info);
759 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
760 workset_foreach(ws, irn, iter) {
761 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
762 workset_get_time(ws, iter)));
765 block_info->start_workset = workset_clone(ws);
767 /* process the block from start to end */
768 DB((dbg, DBG_WSETS, "Processing...\n"));
770 /* TODO: this leaks (into the obstack)... */
771 new_vals = new_workset();
773 sched_foreach(block, irn) {
775 assert(workset_get_length(ws) <= n_regs);
777 /* Phis are no real instr (see insert_starters()) */
781 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
783 /* set instruction in the workset */
786 /* allocate all values _used_ by this instruction */
787 workset_clear(new_vals);
788 for (i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
789 ir_node *in = get_irn_n(irn, i);
790 if (!arch_irn_consider_in_reg_alloc(cls, in))
793 /* (note that "spilled" is irrelevant here) */
794 workset_insert(new_vals, in, false);
796 displace(new_vals, 1);
798 /* allocate all values _defined_ by this instruction */
799 workset_clear(new_vals);
800 if (get_irn_mode(irn) == mode_T) {
801 const ir_edge_t *edge;
803 foreach_out_edge(irn, edge) {
804 ir_node *proj = get_edge_src_irn(edge);
805 if (!arch_irn_consider_in_reg_alloc(cls, proj))
807 workset_insert(new_vals, proj, false);
810 if (!arch_irn_consider_in_reg_alloc(cls, irn))
812 workset_insert(new_vals, irn, false);
814 displace(new_vals, 0);
819 /* Remember end-workset for this block */
820 block_info->end_workset = workset_clone(ws);
821 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
822 workset_foreach(ws, irn, iter)
823 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
824 workset_get_time(ws, iter)));
828 * 'decide' is block-local and makes assumptions
829 * about the set of live-ins. Thus we must adapt the
830 * live-outs to the live-ins at each block-border.
832 static void fix_block_borders(ir_node *block, void *data)
834 workset_t *start_workset;
840 DB((dbg, DBG_FIX, "\n"));
841 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
843 arity = get_irn_arity(block);
844 /* can happen for endless loops */
848 start_workset = get_block_info(block)->start_workset;
850 /* process all pred blocks */
851 for (i = 0; i < arity; ++i) {
852 ir_node *pred = get_Block_cfgpred_block(block, i);
853 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
856 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
858 /* spill all values not used anymore */
859 workset_foreach(pred_end_workset, node, iter) {
863 workset_foreach(start_workset, n2, iter2) {
868 /* note that we do not look at phi inputs, becuase the values
869 * will be either live-end and need no spill or
870 * they have other users in which must be somewhere else in the
877 if (move_spills && be_is_live_in(lv, block, node)
878 && !pred_end_workset->vals[iter].spilled) {
879 ir_node *insert_point;
881 insert_point = be_get_end_of_block_insertion_point(pred);
882 insert_point = sched_prev(insert_point);
884 insert_point = block;
886 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
888 be_add_spill(senv, node, insert_point);
892 /* reload missing values in predecessors, add missing spills */
893 workset_foreach(start_workset, node, iter) {
894 const loc_t *l = &start_workset->vals[iter];
895 const loc_t *pred_loc;
897 /* if node is a phi of the current block we reload
898 * the corresponding argument, else node itself */
899 if (is_Phi(node) && get_nodes_block(node) == block) {
900 node = get_irn_n(node, i);
903 /* we might have unknowns as argument for the phi */
904 if (!arch_irn_consider_in_reg_alloc(cls, node))
908 /* check if node is in a register at end of pred */
909 pred_loc = workset_contains(pred_end_workset, node);
910 if (pred_loc != NULL) {
911 /* we might have to spill value on this path */
912 if (move_spills && !pred_loc->spilled && l->spilled) {
913 ir_node *insert_point
914 = be_get_end_of_block_insertion_point(pred);
915 insert_point = sched_prev(insert_point);
916 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
918 be_add_spill(senv, node, insert_point);
921 /* node is not in register at the end of pred -> reload it */
922 DB((dbg, DBG_FIX, " reload %+F\n", node));
923 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
924 be_add_reload_on_edge(senv, node, block, i, cls, 1);
930 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
933 ir_graph *irg = be_get_birg_irg(birg);
935 be_liveness_assure_sets(be_assure_liveness(birg));
938 /* construct control flow loop tree */
939 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
940 construct_cf_backedges(irg);
942 stat_ev_tim_pop("belady_time_backedges");
946 stat_ev_tim_pop("belady_time_clear_links");
948 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
950 /* init belady env */
954 lv = be_get_birg_liveness(birg);
955 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
957 uses = be_begin_uses(irg, lv);
958 loop_ana = be_new_loop_pressure(birg, cls);
959 senv = be_new_spill_env(birg);
960 blocklist = be_get_cfgpostorder(irg);
961 stat_ev_tim_pop("belady_time_init");
964 /* walk blocks in reverse postorder */
965 for (i = ARR_LEN(blocklist) - 1; i >= 0; --i) {
966 process_block(blocklist[i]);
968 DEL_ARR_F(blocklist);
969 stat_ev_tim_pop("belady_time_belady");
972 /* belady was block-local, fix the global flow by adding reloads on the
974 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
975 stat_ev_tim_pop("belady_time_fix_borders");
977 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
979 /* Insert spill/reload nodes into the graph and fix usages */
980 be_insert_spills_reloads(senv);
983 be_delete_spill_env(senv);
985 be_free_loop_pressure(loop_ana);
986 obstack_free(&obst, NULL);
989 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);
990 void be_init_spillbelady(void)
992 static be_spiller_t belady_spiller = {
995 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
996 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
997 lc_opt_add_table(belady_group, options);
999 be_register_spiller("belady", &belady_spiller);
1000 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");