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
34 #include "irprintf_t.h"
40 #include "iredges_t.h"
43 #include "irnodeset.h"
50 #include "besched_t.h"
54 #include "bechordal_t.h"
55 #include "bespilloptions.h"
56 #include "beloopana.h"
68 #define DBG_WORKSET 128
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
71 #define TIME_UNDEFINED 6666
73 //#define LOOK_AT_LOOPDEPTH
76 * An association between a node and a point in time.
78 typedef struct loc_t {
80 unsigned time; /**< A use time (see beuses.h). */
81 bool spilled; /**< the value was already spilled on this path */
84 typedef struct _workset_t {
85 int len; /**< current length */
86 loc_t vals[0]; /**< inlined array of the values/distances in this working set */
89 static struct obstack obst;
90 static const arch_env_t *arch_env;
91 static const arch_register_class_t *cls;
92 static const be_lv_t *lv;
93 static be_loopana_t *loop_ana;
95 static workset_t *ws; /**< the main workset used while
96 processing a block. */
97 static be_uses_t *uses; /**< env for the next-use magic */
98 static ir_node *instr; /**< current instruction */
99 static unsigned instr_nr; /**< current instruction number
100 (relative to block start) */
101 static spill_env_t *senv; /**< see bespill.h */
102 static pdeq *worklist;
104 static bool move_spills = true;
105 static bool respectloopdepth = true;
106 static bool improve_known_preds = true;
107 /* factor to weight the different costs of reloading/rematerializing a node
108 (see bespill.h be_get_reload_costs_no_weight) */
109 static int remat_bonus = 10;
111 static const lc_opt_table_entry_t options[] = {
112 LC_OPT_ENT_BOOL ("movespills", "try to move spills out of loops", &move_spills),
113 LC_OPT_ENT_BOOL ("respectloopdepth", "exprimental (outermost loop cutting)", &respectloopdepth),
114 LC_OPT_ENT_BOOL ("improveknownpreds", "experimental (known preds cutting)", &improve_known_preds),
115 LC_OPT_ENT_INT ("rematbonus", "give bonus to rematerialisable nodes", &remat_bonus),
119 static int loc_compare(const void *a, const void *b)
123 return p->time - q->time;
126 void workset_print(const workset_t *w)
130 for(i = 0; i < w->len; ++i) {
131 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
136 * Alloc a new workset on obstack @p ob with maximum size @p max
138 static workset_t *new_workset(void)
141 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
143 res = obstack_alloc(&obst, size);
144 memset(res, 0, size);
149 * Alloc a new instance on obstack and make it equal to @param workset
151 static workset_t *workset_clone(workset_t *workset)
154 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
155 res = obstack_alloc(&obst, size);
156 memcpy(res, workset, size);
161 * Copy workset @param src to @param tgt
163 static void workset_copy(workset_t *dest, const workset_t *src)
165 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
166 memcpy(dest, src, size);
170 * Overwrites the current content array of @param ws with the
171 * @param count locations given at memory @param locs.
172 * Set the length of @param ws to count.
174 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
176 workset->len = count;
177 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
181 * Inserts the value @p val into the workset, iff it is not
182 * already contained. The workset must not be full.
184 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
188 /* check for current regclass */
189 assert(arch_irn_consider_in_reg_alloc(arch_env, cls, val));
191 /* check if val is already contained */
192 for (i = 0; i < workset->len; ++i) {
193 loc = &workset->vals[i];
194 if (loc->node == val) {
203 assert(workset->len < n_regs && "Workset already full!");
204 loc = &workset->vals[workset->len];
206 loc->spilled = spilled;
207 loc->time = TIME_UNDEFINED;
212 * Removes all entries from this workset
214 static void workset_clear(workset_t *workset)
220 * Removes the value @p val from the workset if present.
222 static INLINE void workset_remove(workset_t *workset, ir_node *val)
225 for(i = 0; i < workset->len; ++i) {
226 if (workset->vals[i].node == val) {
227 workset->vals[i] = workset->vals[--workset->len];
233 static INLINE const loc_t *workset_contains(const workset_t *ws,
238 for (i = 0; i < ws->len; ++i) {
239 if (ws->vals[i].node == val)
247 * Iterates over all values in the working set.
248 * @p ws The workset to iterate
249 * @p v A variable to put the current value in
250 * @p i An integer for internal use
252 #define workset_foreach(ws, v, i) for(i=0; \
253 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
256 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
257 #define workset_get_time(ws, i) (ws)->vals[i].time
258 #define workset_set_length(ws, length) (ws)->len = length
259 #define workset_get_length(ws) ((ws)->len)
260 #define workset_get_val(ws, i) ((ws)->vals[i].node)
261 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
263 typedef struct _block_info_t
265 workset_t *start_workset;
266 workset_t *end_workset;
270 static void *new_block_info(void)
272 block_info_t *res = obstack_alloc(&obst, sizeof(res[0]));
273 memset(res, 0, sizeof(res[0]));
278 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
279 #define set_block_info(block, info) set_irn_link(block, info)
282 * @return The distance to the next use or 0 if irn has dont_spill flag set
284 static INLINE unsigned get_distance(ir_node *from, unsigned from_step,
285 const ir_node *def, int skip_from_uses)
288 int flags = arch_irn_get_flags(arch_env, def);
292 assert(! (flags & arch_irn_flags_ignore));
294 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
296 if (USES_IS_INFINITE(time))
297 return USES_INFINITY;
299 /* We have to keep nonspillable nodes in the workingset */
300 if (flags & arch_irn_flags_dont_spill)
303 /* give some bonus to rematerialisable nodes */
304 if (remat_bonus > 0) {
305 costs = be_get_reload_costs_no_weight(senv, def, use.before);
306 assert(costs * remat_bonus < 1000);
307 time += 1000 - (costs * remat_bonus);
314 * Performs the actions necessary to grant the request that:
315 * - new_vals can be held in registers
316 * - as few as possible other values are disposed
317 * - the worst values get disposed
319 * @p is_usage indicates that the values in new_vals are used (not defined)
320 * In this case reloads must be performed
322 static void displace(workset_t *new_vals, int is_usage)
324 ir_node **to_insert = alloca(n_regs * sizeof(to_insert[0]));
325 bool *spilled = alloca(n_regs * sizeof(spilled[0]));
333 /* 1. Identify the number of needed slots and the values to reload */
335 workset_foreach(new_vals, val, iter) {
336 bool reloaded = false;
338 if (! workset_contains(ws, val)) {
339 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
341 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
342 be_add_reload(senv, val, instr, cls, 1);
346 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
348 /* remove the value from the current workset so it is not accidently
350 workset_remove(ws, val);
352 spilled[demand] = reloaded;
353 to_insert[demand] = val;
357 /* 2. Make room for at least 'demand' slots */
358 len = workset_get_length(ws);
359 spills_needed = len + demand - n_regs;
360 assert(spills_needed <= len);
362 /* Only make more free room if we do not have enough */
363 if (spills_needed > 0) {
364 ir_node *curr_bb = NULL;
365 workset_t *ws_start = NULL;
368 curr_bb = get_nodes_block(instr);
369 ws_start = get_block_info(curr_bb)->start_workset;
372 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
374 /* calculate current next-use distance for live values */
375 for (i = 0; i < len; ++i) {
376 ir_node *val = workset_get_val(ws, i);
377 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
378 workset_set_time(ws, i, dist);
381 /* sort entries by increasing nextuse-distance*/
384 for (i = len - spills_needed; i < len; ++i) {
385 ir_node *val = ws->vals[i].node;
387 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
388 workset_get_time(ws, i)));
391 if (!USES_IS_INFINITE(ws->vals[i].time)
392 && !ws->vals[i].spilled) {
393 ir_node *after_pos = sched_prev(instr);
394 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
396 be_add_spill(senv, val, after_pos);
401 /* kill the last 'demand' entries in the array */
402 workset_set_length(ws, len - spills_needed);
405 /* 3. Insert the new values into the workset */
406 for (i = 0; i < demand; ++i) {
407 ir_node *val = to_insert[i];
409 workset_insert(ws, val, spilled[i]);
414 AVAILABLE_EVERYWHERE,
420 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
421 size_t n_pred_worksets,
422 const ir_node *value, bool is_local_phi)
425 bool avail_everywhere = true;
426 bool avail_nowhere = true;
428 assert(n_pred_worksets > 0);
430 /* value available in all preds? */
431 for (i = 0; i < n_pred_worksets; ++i) {
433 const workset_t *p_workset = pred_worksets[i];
434 int p_len = workset_get_length(p_workset);
436 const ir_node *l_value;
439 assert(is_Phi(value));
440 l_value = get_irn_n(value, i);
445 for (p_i = 0; p_i < p_len; ++p_i) {
446 const loc_t *p_l = &p_workset->vals[p_i];
447 if (p_l->node != l_value)
455 avail_nowhere = false;
457 avail_everywhere = false;
461 if (avail_everywhere) {
462 assert(!avail_nowhere);
463 return AVAILABLE_EVERYWHERE;
464 } else if (avail_nowhere) {
465 return AVAILABLE_NOWHERE;
467 return AVAILABLE_PARTLY;
471 /** Decides whether a specific node should be in the start workset or not
473 * @param env belady environment
475 * @param node the node to test
476 * @param loop the loop of the node
478 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
479 ir_loop *loop, unsigned available)
481 be_next_use_t next_use;
484 loc.time = USES_INFINITY;
488 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
489 loc.time = USES_INFINITY;
493 /* We have to keep nonspillable nodes in the workingset */
494 if (arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
496 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
500 next_use = be_get_next_use(uses, first, 0, node, 0);
501 if (USES_IS_INFINITE(next_use.time)) {
502 // the nodes marked as live in shouldn't be dead, so it must be a phi
503 assert(is_Phi(node));
504 loc.time = USES_INFINITY;
505 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
509 loc.time = next_use.time;
511 if (improve_known_preds) {
512 if (available == AVAILABLE_EVERYWHERE) {
513 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
516 } else if(available == AVAILABLE_NOWHERE) {
517 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
519 loc.time = USES_INFINITY;
524 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
525 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
526 next_use.outermost_loop));
528 loc.time = USES_PENDING;
529 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
530 node, next_use.outermost_loop, get_loop_depth(loop)));
537 * Computes the start-workset for a block with multiple predecessors. We assume
538 * that at least 1 of the predeccesors is a back-edge which means we're at the
539 * beginning of a loop. We try to reload as much values as possible now so they
540 * don't get reloaded inside the loop.
542 static void decide_start_workset(const ir_node *block)
544 ir_loop *loop = get_irn_loop(block);
550 int i, len, ws_count;
551 int free_slots, free_pressure_slots;
554 workset_t **pred_worksets;
555 bool all_preds_known;
557 /* check predecessors */
558 arity = get_irn_arity(block);
559 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
560 all_preds_known = true;
561 for(i = 0; i < arity; ++i) {
562 ir_node *pred_block = get_Block_cfgpred_block(block, i);
563 block_info_t *pred_info = get_block_info(pred_block);
565 if (pred_info == NULL) {
566 pred_worksets[i] = NULL;
567 all_preds_known = false;
569 pred_worksets[i] = pred_info->end_workset;
573 /* Collect all values living at start of block */
574 starters = NEW_ARR_F(loc_t, 0);
575 delayed = NEW_ARR_F(loc_t, 0);
577 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
578 first = sched_first(block);
580 /* check all Phis first */
581 sched_foreach(block, node) {
586 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
589 if (all_preds_known) {
590 available = available_in_all_preds(pred_worksets, arity, node, true);
592 available = AVAILABLE_UNKNOWN;
595 loc = to_take_or_not_to_take(first, node, loop, available);
597 if (! USES_IS_INFINITE(loc.time)) {
598 if (USES_IS_PENDING(loc.time))
599 ARR_APP1(loc_t, delayed, loc);
601 ARR_APP1(loc_t, starters, loc);
603 be_spill_phi(senv, node);
607 /* check all Live-Ins */
608 be_lv_foreach(lv, block, be_lv_state_in, i) {
609 ir_node *node = be_lv_get_irn(lv, block, i);
612 if (all_preds_known) {
613 available = available_in_all_preds(pred_worksets, arity, node, false);
615 available = AVAILABLE_UNKNOWN;
618 loc = to_take_or_not_to_take(first, node, loop, available);
620 if (! USES_IS_INFINITE(loc.time)) {
621 if (USES_IS_PENDING(loc.time))
622 ARR_APP1(loc_t, delayed, loc);
624 ARR_APP1(loc_t, starters, loc);
628 pressure = be_get_loop_pressure(loop_ana, cls, loop);
629 assert(ARR_LEN(delayed) <= (signed)pressure);
630 free_slots = n_regs - ARR_LEN(starters);
631 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
632 free_slots = MIN(free_slots, free_pressure_slots);
634 /* so far we only put nodes into the starters list that are used inside
635 * the loop. If register pressure in the loop is low then we can take some
636 * values and let them live through the loop */
637 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
638 pressure, free_slots));
639 if (free_slots > 0) {
640 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
642 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
644 loc_t *loc = & delayed[i];
646 /* don't use values which are dead in a known predecessors
647 * to not induce unnecessary reloads */
648 arity = get_irn_arity(block);
649 for (p = 0; p < arity; ++p) {
650 ir_node *pred_block = get_Block_cfgpred_block(block, p);
651 block_info_t *pred_info = get_block_info(pred_block);
653 if (pred_info == NULL)
656 if (!workset_contains(pred_info->end_workset, loc->node)) {
658 " delayed %+F not live at pred %+F\n", loc->node,
664 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
665 ARR_APP1(loc_t, starters, *loc);
673 /* spill phis (the actual phis not just their values) that are in this block
674 * but not in the start workset */
675 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
676 ir_node *node = delayed[i].node;
677 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
680 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
681 be_spill_phi(senv, node);
685 /* Sort start values by first use */
686 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
688 /* Copy the best ones from starters to start workset */
689 ws_count = MIN(ARR_LEN(starters), n_regs);
691 workset_bulk_fill(ws, ws_count, starters);
693 /* spill phis (the actual phis not just their values) that are in this block
694 * but not in the start workset */
695 len = ARR_LEN(starters);
696 for (i = ws_count; i < len; ++i) {
697 ir_node *node = starters[i].node;
698 if (! is_Phi(node) || get_nodes_block(node) != block)
701 DB((dbg, DBG_START, " spilling phi %+F\n", node));
702 be_spill_phi(senv, node);
707 /* determine spill status of the values: If there's 1 pred block (which
708 * is no backedge) where the value is spilled then we must set it to
710 for(i = 0; i < ws_count; ++i) {
711 loc_t *loc = &ws->vals[i];
712 ir_node *value = loc->node;
716 /* phis from this block aren't spilled */
717 if (get_nodes_block(value) == block) {
718 assert(is_Phi(value));
719 loc->spilled = false;
723 /* determine if value was spilled on any predecessor */
725 for(n = 0; n < arity; ++n) {
726 workset_t *pred_workset = pred_worksets[n];
730 if (pred_workset == NULL)
733 p_len = workset_get_length(pred_workset);
734 for(p = 0; p < p_len; ++p) {
735 loc_t *l = &pred_workset->vals[p];
737 if (l->node != value)
747 loc->spilled = spilled;
752 * For the given block @p block, decide for each values
753 * whether it is used from a register or is reloaded
756 static void belady(ir_node *block)
761 block_info_t *block_info;
763 int has_backedges = 0;
765 const ir_edge_t *edge;
767 /* no need to process a block twice */
768 if (get_block_info(block) != NULL) {
772 /* check if all predecessor blocks are processed yet (though for backedges
773 * we have to make an exception as we can't process them first) */
774 arity = get_Block_n_cfgpreds(block);
775 for(i = 0; i < arity; ++i) {
776 ir_node *pred_block = get_Block_cfgpred_block(block, i);
777 block_info_t *pred_info = get_block_info(pred_block);
779 if (pred_info == NULL) {
780 /* process predecessor first (it will be in the queue already) */
781 if (!is_backedge(block, i)) {
787 (void) has_backedges;
790 } else if (arity == 1) {
791 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
792 block_info_t *pred_info = get_block_info(pred_block);
794 assert(pred_info != NULL);
795 workset_copy(ws, pred_info->end_workset);
797 /* we need 2 heuristics here, for the case when all predecessor blocks
798 * are known and when some are backedges (and therefore can't be known
800 decide_start_workset(block);
803 DB((dbg, DBG_DECIDE, "\n"));
804 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
806 block_info = new_block_info();
807 set_block_info(block, block_info);
809 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
810 workset_foreach(ws, irn, iter) {
811 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
812 workset_get_time(ws, iter)));
815 block_info->start_workset = workset_clone(ws);
817 /* process the block from start to end */
818 DB((dbg, DBG_WSETS, "Processing...\n"));
820 /* TODO: this leaks (into the obstack)... */
821 new_vals = new_workset();
823 sched_foreach(block, irn) {
825 assert(workset_get_length(ws) <= n_regs);
827 /* Phis are no real instr (see insert_starters()) */
831 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
833 /* set instruction in the workset */
836 /* allocate all values _used_ by this instruction */
837 workset_clear(new_vals);
838 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
839 ir_node *in = get_irn_n(irn, i);
840 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, in))
843 /* (note that "spilled" is irrelevant here) */
844 workset_insert(new_vals, in, false);
846 displace(new_vals, 1);
848 /* allocate all values _defined_ by this instruction */
849 workset_clear(new_vals);
850 if (get_irn_mode(irn) == mode_T) {
851 const ir_edge_t *edge;
853 foreach_out_edge(irn, edge) {
854 ir_node *proj = get_edge_src_irn(edge);
855 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, proj))
857 workset_insert(new_vals, proj, false);
860 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
862 workset_insert(new_vals, irn, false);
864 displace(new_vals, 0);
869 /* Remember end-workset for this block */
870 block_info->end_workset = workset_clone(ws);
871 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
872 workset_foreach(ws, irn, iter)
873 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
874 workset_get_time(ws, iter)));
876 /* add successor blocks into worklist */
877 foreach_block_succ(block, edge) {
878 ir_node *succ = get_edge_src_irn(edge);
879 pdeq_putr(worklist, succ);
884 * 'decide' is block-local and makes assumptions
885 * about the set of live-ins. Thus we must adapt the
886 * live-outs to the live-ins at each block-border.
888 static void fix_block_borders(ir_node *block, void *data)
890 workset_t *start_workset;
896 DB((dbg, DBG_FIX, "\n"));
897 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
899 start_workset = get_block_info(block)->start_workset;
901 /* process all pred blocks */
902 arity = get_irn_arity(block);
903 for (i = 0; i < arity; ++i) {
904 ir_node *pred = get_Block_cfgpred_block(block, i);
905 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
908 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
910 /* spill all values not used anymore */
911 workset_foreach(pred_end_workset, node, iter) {
915 workset_foreach(start_workset, n2, iter2) {
920 /* note that we do not look at phi inputs, becuase the values
921 * will be either live-end and need no spill or
922 * they have other users in which must be somewhere else in the
929 if (move_spills && be_is_live_in(lv, block, node)
930 && !pred_end_workset->vals[iter].spilled) {
931 ir_node *insert_point;
933 insert_point = be_get_end_of_block_insertion_point(pred);
934 insert_point = sched_prev(insert_point);
936 insert_point = block;
938 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
940 be_add_spill(senv, node, insert_point);
944 /* reload missing values in predecessors, add missing spills */
945 workset_foreach(start_workset, node, iter) {
946 const loc_t *l = &start_workset->vals[iter];
947 const loc_t *pred_loc;
949 /* if node is a phi of the current block we reload
950 * the corresponding argument, else node itself */
951 if (is_Phi(node) && get_nodes_block(node) == block) {
952 node = get_irn_n(node, i);
955 /* we might have unknowns as argument for the phi */
956 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
960 /* check if node is in a register at end of pred */
961 pred_loc = workset_contains(pred_end_workset, node);
962 if (pred_loc != NULL) {
963 /* we might have to spill value on this path */
964 if (move_spills && !pred_loc->spilled && l->spilled) {
965 ir_node *insert_point
966 = be_get_end_of_block_insertion_point(pred);
967 insert_point = sched_prev(insert_point);
968 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
970 be_add_spill(senv, node, insert_point);
973 /* node is not in register at the end of pred -> reload it */
974 DB((dbg, DBG_FIX, " reload %+F\n", node));
975 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
976 be_add_reload_on_edge(senv, node, block, i, cls, 1);
982 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
984 ir_graph *irg = be_get_birg_irg(birg);
986 be_liveness_assure_sets(be_assure_liveness(birg));
988 /* construct control flow loop tree */
989 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
990 construct_cf_backedges(irg);
995 /* init belady env */
997 arch_env = birg->main_env->arch_env;
999 lv = be_get_birg_liveness(birg);
1000 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1002 uses = be_begin_uses(irg, lv);
1003 loop_ana = be_new_loop_pressure(birg);
1004 senv = be_new_spill_env(birg);
1005 worklist = new_pdeq();
1007 pdeq_putr(worklist, get_irg_start_block(irg));
1009 while(!pdeq_empty(worklist)) {
1010 ir_node *block = pdeq_getl(worklist);
1013 /* end block might not be reachable in endless loops */
1014 belady(get_irg_end_block(irg));
1018 /* belady was block-local, fix the global flow by adding reloads on the
1020 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1022 /* Insert spill/reload nodes into the graph and fix usages */
1023 be_insert_spills_reloads(senv);
1026 be_delete_spill_env(senv);
1028 be_free_loop_pressure(loop_ana);
1029 obstack_free(&obst, NULL);
1032 void be_init_spillbelady(void)
1034 static be_spiller_t belady_spiller = {
1037 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1038 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1039 lc_opt_add_table(belady_group, options);
1041 be_register_spiller("belady", &belady_spiller);
1042 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1045 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);