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 /* factor to weight the different costs of reloading/rematerializing a node
72 (see bespill.h be_get_reload_costs_no_weight) */
73 #define RELOAD_COST_FACTOR 10
75 #define TIME_UNDEFINED 6666
80 * An association between a node and a point in time.
82 typedef struct loc_t {
84 unsigned time; /**< A use time (see beuses.h). */
85 bool spilled; /**< the value was already spilled on this path */
88 typedef struct _workset_t {
89 int len; /**< current length */
90 loc_t vals[0]; /**< inlined array of the values/distances in this working set */
93 static struct obstack obst;
94 static const arch_env_t *arch_env;
95 static const arch_register_class_t *cls;
96 static const be_lv_t *lv;
97 static be_loopana_t *loop_ana;
99 static workset_t *ws; /**< the main workset used while
100 processing a block. */
101 static be_uses_t *uses; /**< env for the next-use magic */
102 static ir_node *instr; /**< current instruction */
103 static unsigned instr_nr; /**< current instruction number
104 (relative to block start) */
105 static ir_nodeset_t used;
106 static spill_env_t *senv; /**< see bespill.h */
107 static pdeq *worklist;
109 static int loc_compare(const void *a, const void *b)
113 return p->time - q->time;
116 void workset_print(const workset_t *w)
120 for(i = 0; i < w->len; ++i) {
121 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
126 * Alloc a new workset on obstack @p ob with maximum size @p max
128 static workset_t *new_workset(void)
131 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
133 res = obstack_alloc(&obst, size);
134 memset(res, 0, size);
139 * Alloc a new instance on obstack and make it equal to @param workset
141 static workset_t *workset_clone(workset_t *workset)
144 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
145 res = obstack_alloc(&obst, size);
146 memcpy(res, workset, size);
151 * Copy workset @param src to @param tgt
153 static void workset_copy(workset_t *dest, const workset_t *src)
155 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
156 memcpy(dest, src, size);
160 * Overwrites the current content array of @param ws with the
161 * @param count locations given at memory @param locs.
162 * Set the length of @param ws to count.
164 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
166 workset->len = count;
167 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
171 * Inserts the value @p val into the workset, iff it is not
172 * already contained. The workset must not be full.
174 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
178 /* check for current regclass */
179 assert(arch_irn_consider_in_reg_alloc(arch_env, cls, val));
181 /* check if val is already contained */
182 for (i = 0; i < workset->len; ++i) {
183 loc = &workset->vals[i];
184 if (loc->node == val) {
193 assert(workset->len < n_regs && "Workset already full!");
194 loc = &workset->vals[workset->len];
196 loc->spilled = spilled;
197 loc->time = TIME_UNDEFINED;
202 * Removes all entries from this workset
204 static void workset_clear(workset_t *workset)
210 * Removes the value @p val from the workset if present.
212 static INLINE void workset_remove(workset_t *workset, ir_node *val)
215 for(i = 0; i < workset->len; ++i) {
216 if (workset->vals[i].node == val) {
217 workset->vals[i] = workset->vals[--workset->len];
223 static INLINE const loc_t *workset_contains(const workset_t *ws,
228 for (i = 0; i < ws->len; ++i) {
229 if (ws->vals[i].node == val)
237 * Iterates over all values in the working set.
238 * @p ws The workset to iterate
239 * @p v A variable to put the current value in
240 * @p i An integer for internal use
242 #define workset_foreach(ws, v, i) for(i=0; \
243 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
246 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
247 #define workset_get_time(ws, i) (ws)->vals[i].time
248 #define workset_set_length(ws, length) (ws)->len = length
249 #define workset_get_length(ws) ((ws)->len)
250 #define workset_get_val(ws, i) ((ws)->vals[i].node)
251 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
253 typedef struct _block_info_t
255 workset_t *start_workset;
256 workset_t *end_workset;
260 static void *new_block_info(void)
262 block_info_t *res = obstack_alloc(&obst, sizeof(res[0]));
263 memset(res, 0, sizeof(res[0]));
268 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
269 #define set_block_info(block, info) set_irn_link(block, info)
272 * @return The distance to the next use or 0 if irn has dont_spill flag set
274 static INLINE unsigned get_distance(ir_node *from, unsigned from_step,
275 const ir_node *def, int skip_from_uses)
278 int flags = arch_irn_get_flags(arch_env, def);
282 assert(! (flags & arch_irn_flags_ignore));
284 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
285 if (USES_IS_INFINITE(use.time))
286 return USES_INFINITY;
288 /* We have to keep nonspillable nodes in the workingset */
289 if (flags & arch_irn_flags_dont_spill)
292 costs = be_get_reload_costs_no_weight(senv, def, use.before);
293 assert(costs * RELOAD_COST_FACTOR < 1000);
294 time = use.time + 1000 - (costs * RELOAD_COST_FACTOR);
300 * Performs the actions necessary to grant the request that:
301 * - new_vals can be held in registers
302 * - as few as possible other values are disposed
303 * - the worst values get disposed
305 * @p is_usage indicates that the values in new_vals are used (not defined)
306 * In this case reloads must be performed
308 static void displace(workset_t *new_vals, int is_usage)
310 ir_node **to_insert = alloca(n_regs * sizeof(to_insert[0]));
311 bool *spilled = alloca(n_regs * sizeof(spilled[0]));
319 /* 1. Identify the number of needed slots and the values to reload */
321 workset_foreach(new_vals, val, iter) {
322 bool reloaded = false;
324 /* mark value as used */
326 ir_nodeset_insert(&used, val);
328 if (! workset_contains(ws, val)) {
329 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
331 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
332 be_add_reload(senv, val, instr, cls, 1);
336 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
338 /* remove the value from the current workset so it is not accidently
340 workset_remove(ws, val);
342 spilled[demand] = reloaded;
343 to_insert[demand] = val;
347 /* 2. Make room for at least 'demand' slots */
348 len = workset_get_length(ws);
349 spills_needed = len + demand - n_regs;
350 assert(spills_needed <= len);
352 /* Only make more free room if we do not have enough */
353 if (spills_needed > 0) {
355 ir_node *curr_bb = get_nodes_block(instr);
356 workset_t *ws_start = get_block_info(curr_bb)->start_workset;
359 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
361 /* calculate current next-use distance for live values */
362 for (i = 0; i < len; ++i) {
363 ir_node *val = workset_get_val(ws, i);
364 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
365 workset_set_time(ws, i, dist);
368 /* sort entries by increasing nextuse-distance*/
371 for (i = len - spills_needed; i < len; ++i) {
372 ir_node *val = ws->vals[i].node;
374 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
375 workset_get_time(ws, i)));
378 if (!USES_IS_INFINITE(ws->vals[i].time) && !ws->vals[i].spilled) {
379 ir_node *after_pos = sched_prev(instr);
380 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
382 be_add_spill(senv, val, after_pos);
387 /* Logic for not needed live-ins: If a value is disposed
388 * before its first use, remove it from start workset
389 * We don't do this for phis though */
390 if (!is_Phi(val) && ! ir_nodeset_contains(&used, val)) {
391 workset_remove(ws_start, val);
392 DB((dbg, DBG_DECIDE, " (and removing %+F from start workset)\n", val));
397 /* kill the last 'demand' entries in the array */
398 workset_set_length(ws, len - spills_needed);
401 /* 3. Insert the new values into the workset */
402 for (i = 0; i < demand; ++i) {
403 ir_node *val = to_insert[i];
405 workset_insert(ws, val, spilled[i]);
410 AVAILABLE_EVERYWHERE,
416 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
417 size_t n_pred_worksets,
418 const ir_node *value, bool is_local_phi)
421 bool avail_everywhere = true;
422 bool avail_nowhere = true;
424 assert(n_pred_worksets > 0);
426 /* value available in all preds? */
427 for (i = 0; i < n_pred_worksets; ++i) {
429 const workset_t *p_workset = pred_worksets[i];
430 int p_len = workset_get_length(p_workset);
432 const ir_node *l_value;
435 assert(is_Phi(value));
436 l_value = get_irn_n(value, i);
441 for (p_i = 0; p_i < p_len; ++p_i) {
442 const loc_t *p_l = &p_workset->vals[p_i];
443 if (p_l->node != l_value)
451 avail_nowhere = false;
453 avail_everywhere = false;
457 if (avail_everywhere) {
458 assert(!avail_nowhere);
459 return AVAILABLE_EVERYWHERE;
460 } else if (avail_nowhere) {
461 return AVAILABLE_NOWHERE;
463 return AVAILABLE_PARTLY;
467 /** Decides whether a specific node should be in the start workset or not
469 * @param env belady environment
471 * @param node the node to test
472 * @param loop the loop of the node
474 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
475 ir_loop *loop, unsigned available)
477 be_next_use_t next_use;
480 loc.time = USES_INFINITY;
484 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
485 loc.time = USES_INFINITY;
489 /* We have to keep nonspillable nodes in the workingset */
490 if (arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
492 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
496 next_use = be_get_next_use(uses, first, 0, node, 0);
497 if (USES_IS_INFINITE(next_use.time)) {
498 // the nodes marked as live in shouldn't be dead, so it must be a phi
499 assert(is_Phi(node));
500 loc.time = USES_INFINITY;
501 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
505 loc.time = next_use.time;
507 if (available == AVAILABLE_EVERYWHERE) {
508 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n", node,
511 } else if(available == AVAILABLE_NOWHERE) {
512 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n", node,
514 loc.time = USES_INFINITY;
518 if (next_use.outermost_loop >= get_loop_depth(loop)) {
519 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
520 next_use.outermost_loop));
522 loc.time = USES_PENDING;
523 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
524 node, next_use.outermost_loop, get_loop_depth(loop)));
530 * Computes the start-workset for a block with multiple predecessors. We assume
531 * that at least 1 of the predeccesors is a back-edge which means we're at the
532 * beginning of a loop. We try to reload as much values as possible now so they
533 * don't get reloaded inside the loop.
535 static void decide_start_workset(const ir_node *block)
537 ir_loop *loop = get_irn_loop(block);
543 int i, len, ws_count;
544 int free_slots, free_pressure_slots;
547 workset_t **pred_worksets;
548 bool all_preds_known;
550 /* check predecessors */
551 arity = get_irn_arity(block);
552 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
553 all_preds_known = true;
554 for(i = 0; i < arity; ++i) {
555 ir_node *pred_block = get_Block_cfgpred_block(block, i);
556 block_info_t *pred_info = get_block_info(pred_block);
558 if (pred_info == NULL) {
559 pred_worksets[i] = NULL;
560 all_preds_known = false;
562 pred_worksets[i] = pred_info->end_workset;
566 /* Collect all values living at start of block */
567 starters = NEW_ARR_F(loc_t, 0);
568 delayed = NEW_ARR_F(loc_t, 0);
570 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
571 first = sched_first(block);
573 /* check all Phis first */
574 sched_foreach(block, node) {
579 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
582 if (all_preds_known) {
583 available = available_in_all_preds(pred_worksets, arity, node, true);
585 available = AVAILABLE_UNKNOWN;
588 loc = to_take_or_not_to_take(first, node, loop, available);
590 if (! USES_IS_INFINITE(loc.time)) {
591 if (USES_IS_PENDING(loc.time))
592 ARR_APP1(loc_t, delayed, loc);
594 ARR_APP1(loc_t, starters, loc);
596 be_spill_phi(senv, node);
600 /* check all Live-Ins */
601 be_lv_foreach(lv, block, be_lv_state_in, i) {
602 ir_node *node = be_lv_get_irn(lv, block, i);
605 if (all_preds_known) {
606 available = available_in_all_preds(pred_worksets, arity, node, false);
608 available = AVAILABLE_UNKNOWN;
611 loc = to_take_or_not_to_take(first, node, loop, available);
613 if (! USES_IS_INFINITE(loc.time)) {
614 if (USES_IS_PENDING(loc.time))
615 ARR_APP1(loc_t, delayed, loc);
617 ARR_APP1(loc_t, starters, loc);
621 pressure = be_get_loop_pressure(loop_ana, cls, loop);
622 assert(ARR_LEN(delayed) <= (signed)pressure);
623 free_slots = n_regs - ARR_LEN(starters);
624 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
625 free_slots = MIN(free_slots, free_pressure_slots);
627 /* so far we only put nodes into the starters list that are used inside
628 * the loop. If register pressure in the loop is low then we can take some
629 * values and let them live through the loop */
630 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
631 pressure, free_slots));
632 if (free_slots > 0) {
633 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
635 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
637 loc_t *loc = & delayed[i];
639 /* don't use values which are dead in a known predecessors
640 * to not induce unnecessary reloads */
641 arity = get_irn_arity(block);
642 for (p = 0; p < arity; ++p) {
643 ir_node *pred_block = get_Block_cfgpred_block(block, p);
644 block_info_t *pred_info = get_block_info(pred_block);
646 if (pred_info == NULL)
649 if (!workset_contains(pred_info->end_workset, loc->node)) {
651 " delayed %+F not live at pred %+F\n", loc->node,
657 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
658 ARR_APP1(loc_t, starters, *loc);
666 /* spill phis (the actual phis not just their values) that are in this block
667 * but not in the start workset */
668 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
669 ir_node *node = delayed[i].node;
670 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
673 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
674 be_spill_phi(senv, node);
678 /* Sort start values by first use */
679 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
681 /* Copy the best ones from starters to start workset */
682 ws_count = MIN(ARR_LEN(starters), n_regs);
684 workset_bulk_fill(ws, ws_count, starters);
686 /* spill phis (the actual phis not just their values) that are in this block
687 * but not in the start workset */
688 len = ARR_LEN(starters);
689 for (i = ws_count; i < len; ++i) {
690 ir_node *node = starters[i].node;
691 if (! is_Phi(node) || get_nodes_block(node) != block)
694 DB((dbg, DBG_START, " spilling phi %+F\n", node));
695 be_spill_phi(senv, node);
700 /* determine spill status of the values: If there's 1 pred block (which
701 * is no backedge) where the value is spilled then we must set it to
703 for(i = 0; i < ws_count; ++i) {
704 loc_t *loc = &ws->vals[i];
705 ir_node *value = loc->node;
709 /* phis from this block aren't spilled */
710 if (get_nodes_block(value) == block) {
711 assert(is_Phi(value));
712 loc->spilled = false;
716 /* determine if value was spilled on any predecessor */
718 for(n = 0; n < arity; ++n) {
719 workset_t *pred_workset = pred_worksets[n];
723 if (pred_workset == NULL)
726 p_len = workset_get_length(pred_workset);
727 for(p = 0; p < p_len; ++p) {
728 loc_t *l = &pred_workset->vals[p];
730 if (l->node != value)
740 loc->spilled = spilled;
745 static void decide_start_workset2(const ir_node *block)
748 workset_t **pred_worksets;
753 /* check if all predecessors are known */
754 arity = get_irn_arity(block);
755 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
756 for (i = 0; i < arity; ++i) {
757 ir_node *pred_block = get_Block_cfgpred_block(block, i);
758 block_info_t *pred_info = get_block_info(pred_block);
760 if (pred_info == NULL) {
761 /* not all predecessors known, use decide_start_workset */
762 decide_start_workset(block);
766 pred_worksets[i] = pred_info->end_workset;
769 /* we construct a new workset */
772 /* take values live in all pred blocks */
773 len = workset_get_length(pred_worksets[0]);
774 for (p = 0; p < len; ++p) {
775 const loc_t *l = &pred_worksets[0]->vals[p];
777 bool spilled = false;
779 if (USES_IS_INFINITE(l->time))
782 /* value available in all preds? */
784 for (i = 1; i < arity; ++i) {
786 workset_t *p_workset = pred_worksets[i];
787 int p_len = workset_get_length(p_workset);
790 for (p_i = 0; p_i < p_len; ++p_i) {
791 const loc_t *p_l = &p_workset->vals[p_i];
792 if (p_l->node != value)
805 /* it was available in all preds */
807 workset_insert(ws, value, spilled);
811 /* Copy the best ones from starters to start workset */
812 ws_count = MIN(ARR_LEN(starters), n_regs);
813 workset_bulk_fill(ws, ws_count, starters);
818 * For the given block @p block, decide for each values
819 * whether it is used from a register or is reloaded
822 static void belady(ir_node *block)
827 block_info_t *block_info;
829 int has_backedges = 0;
831 const ir_edge_t *edge;
833 /* no need to process a block twice */
834 if (get_block_info(block) != NULL) {
838 /* check if all predecessor blocks are processed yet (though for backedges
839 * we have to make an exception as we can't process them first) */
840 arity = get_Block_n_cfgpreds(block);
841 for(i = 0; i < arity; ++i) {
842 ir_node *pred_block = get_Block_cfgpred_block(block, i);
843 block_info_t *pred_info = get_block_info(pred_block);
845 if (pred_info == NULL) {
846 /* process predecessor first (it will be in the queue already) */
847 if (!is_backedge(block, i)) {
853 (void) has_backedges;
856 } else if (arity == 1) {
857 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
858 block_info_t *pred_info = get_block_info(pred_block);
860 assert(pred_info != NULL);
861 workset_copy(ws, pred_info->end_workset);
863 /* we need 2 heuristics here, for the case when all predecessor blocks
864 * are known and when some are backedges (and therefore can't be known
866 decide_start_workset(block);
869 DB((dbg, DBG_DECIDE, "\n"));
870 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
872 block_info = new_block_info();
873 set_block_info(block, block_info);
875 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
876 workset_foreach(ws, irn, iter) {
877 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
878 workset_get_time(ws, iter)));
881 block_info->start_workset = workset_clone(ws);
883 /* process the block from start to end */
884 DB((dbg, DBG_WSETS, "Processing...\n"));
885 ir_nodeset_init(&used);
887 /* TODO: this leaks (into the obstack)... */
888 new_vals = new_workset();
890 sched_foreach(block, irn) {
892 assert(workset_get_length(ws) <= n_regs);
894 /* Phis are no real instr (see insert_starters()) */
898 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
900 /* set instruction in the workset */
903 /* allocate all values _used_ by this instruction */
904 workset_clear(new_vals);
905 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
906 ir_node *in = get_irn_n(irn, i);
907 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, in))
910 /* (note that "spilled" is irrelevant here) */
911 workset_insert(new_vals, in, false);
913 displace(new_vals, 1);
915 /* allocate all values _defined_ by this instruction */
916 workset_clear(new_vals);
917 if (get_irn_mode(irn) == mode_T) {
918 const ir_edge_t *edge;
920 foreach_out_edge(irn, edge) {
921 ir_node *proj = get_edge_src_irn(edge);
922 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, proj))
924 workset_insert(new_vals, proj, false);
927 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
929 workset_insert(new_vals, irn, false);
931 displace(new_vals, 0);
935 ir_nodeset_destroy(&used);
937 /* Remember end-workset for this block */
938 block_info->end_workset = workset_clone(ws);
939 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
940 workset_foreach(ws, irn, iter)
941 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
942 workset_get_time(ws, iter)));
944 /* add successor blocks into worklist */
945 foreach_block_succ(block, edge) {
946 ir_node *succ = get_edge_src_irn(edge);
947 pdeq_putr(worklist, succ);
952 * 'decide' is block-local and makes assumptions
953 * about the set of live-ins. Thus we must adapt the
954 * live-outs to the live-ins at each block-border.
956 static void fix_block_borders(ir_node *block, void *data)
958 workset_t *start_workset;
964 DB((dbg, DBG_FIX, "\n"));
965 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
967 start_workset = get_block_info(block)->start_workset;
969 /* process all pred blocks */
970 arity = get_irn_arity(block);
971 for (i = 0; i < arity; ++i) {
972 ir_node *pred = get_Block_cfgpred_block(block, i);
973 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
976 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
978 /* spill all values not used anymore */
979 workset_foreach(pred_end_workset, node, iter) {
983 workset_foreach(start_workset, n2, iter2) {
988 /* note that we do not look at phi inputs, becuase the values
989 * will be either live-end and need no spill or
990 * they have other users in which must be somewhere else in the
998 if (be_is_live_in(lv, block, node)
999 && !pred_end_workset->vals[iter].spilled) {
1000 ir_node *insert_point;
1002 insert_point = be_get_end_of_block_insertion_point(pred);
1003 insert_point = sched_prev(insert_point);
1005 insert_point = block;
1007 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
1009 be_add_spill(senv, node, insert_point);
1014 /* reload missing values in predecessors, add missing spills */
1015 workset_foreach(start_workset, node, iter) {
1016 const loc_t *l = &start_workset->vals[iter];
1017 const loc_t *pred_loc;
1019 /* if node is a phi of the current block we reload
1020 * the corresponding argument, else node itself */
1021 if (is_Phi(node) && get_nodes_block(node) == block) {
1022 node = get_irn_n(node, i);
1023 assert(!l->spilled);
1025 /* we might have unknowns as argument for the phi */
1026 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
1030 /* check if node is in a register at end of pred */
1031 pred_loc = workset_contains(pred_end_workset, node);
1032 if (pred_loc != NULL) {
1034 /* we might have to spill value on this path */
1035 if (!pred_loc->spilled && l->spilled) {
1036 ir_node *insert_point
1037 = be_get_end_of_block_insertion_point(pred);
1038 insert_point = sched_prev(insert_point);
1039 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
1041 be_add_spill(senv, node, insert_point);
1045 /* node is not in register at the end of pred -> reload it */
1046 DB((dbg, DBG_FIX, " reload %+F\n", node));
1047 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
1048 be_add_reload_on_edge(senv, node, block, i, cls, 1);
1054 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
1056 ir_graph *irg = be_get_birg_irg(birg);
1058 be_liveness_assure_sets(be_assure_liveness(birg));
1060 /* construct control flow loop tree */
1061 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
1062 construct_cf_backedges(irg);
1065 be_clear_links(irg);
1067 /* init belady env */
1068 obstack_init(&obst);
1069 arch_env = birg->main_env->arch_env;
1071 lv = be_get_birg_liveness(birg);
1072 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1074 uses = be_begin_uses(irg, lv);
1075 loop_ana = be_new_loop_pressure(birg);
1076 senv = be_new_spill_env(birg);
1077 worklist = new_pdeq();
1079 pdeq_putr(worklist, get_irg_start_block(irg));
1081 while(!pdeq_empty(worklist)) {
1082 ir_node *block = pdeq_getl(worklist);
1085 /* end block might not be reachable in endless loops */
1086 belady(get_irg_end_block(irg));
1090 /* belady was block-local, fix the global flow by adding reloads on the
1092 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1094 /* Insert spill/reload nodes into the graph and fix usages */
1095 be_insert_spills_reloads(senv);
1098 be_delete_spill_env(senv);
1100 be_free_loop_pressure(loop_ana);
1101 obstack_free(&obst, NULL);
1104 void be_init_spillbelady(void)
1106 static be_spiller_t belady_spiller = {
1110 be_register_spiller("belady", &belady_spiller);
1111 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1114 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);