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 ir_nodeset_t used;
102 static spill_env_t *senv; /**< see bespill.h */
103 static pdeq *worklist;
105 static bool move_spills = true;
106 static bool respectloopdepth = true;
107 static bool improve_known_preds = true;
108 /* factor to weight the different costs of reloading/rematerializing a node
109 (see bespill.h be_get_reload_costs_no_weight) */
110 static int remat_bonus = 10;
112 static const lc_opt_table_entry_t options[] = {
113 LC_OPT_ENT_BOOL ("movespills", "try to move spills out of loops", &move_spills),
114 LC_OPT_ENT_BOOL ("respectloopdepth", "exprimental (outermost loop cutting)", &respectloopdepth),
115 LC_OPT_ENT_BOOL ("improveknownpreds", "experimental (known preds cutting)", &improve_known_preds),
116 LC_OPT_ENT_INT ("rematbonus", "give bonus to rematerialisable nodes", &remat_bonus),
120 static int loc_compare(const void *a, const void *b)
124 return p->time - q->time;
127 void workset_print(const workset_t *w)
131 for(i = 0; i < w->len; ++i) {
132 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
137 * Alloc a new workset on obstack @p ob with maximum size @p max
139 static workset_t *new_workset(void)
142 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
144 res = obstack_alloc(&obst, size);
145 memset(res, 0, size);
150 * Alloc a new instance on obstack and make it equal to @param workset
152 static workset_t *workset_clone(workset_t *workset)
155 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
156 res = obstack_alloc(&obst, size);
157 memcpy(res, workset, size);
162 * Copy workset @param src to @param tgt
164 static void workset_copy(workset_t *dest, const workset_t *src)
166 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
167 memcpy(dest, src, size);
171 * Overwrites the current content array of @param ws with the
172 * @param count locations given at memory @param locs.
173 * Set the length of @param ws to count.
175 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
177 workset->len = count;
178 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
182 * Inserts the value @p val into the workset, iff it is not
183 * already contained. The workset must not be full.
185 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
189 /* check for current regclass */
190 assert(arch_irn_consider_in_reg_alloc(arch_env, cls, val));
192 /* check if val is already contained */
193 for (i = 0; i < workset->len; ++i) {
194 loc = &workset->vals[i];
195 if (loc->node == val) {
204 assert(workset->len < n_regs && "Workset already full!");
205 loc = &workset->vals[workset->len];
207 loc->spilled = spilled;
208 loc->time = TIME_UNDEFINED;
213 * Removes all entries from this workset
215 static void workset_clear(workset_t *workset)
221 * Removes the value @p val from the workset if present.
223 static INLINE void workset_remove(workset_t *workset, ir_node *val)
226 for(i = 0; i < workset->len; ++i) {
227 if (workset->vals[i].node == val) {
228 workset->vals[i] = workset->vals[--workset->len];
234 static INLINE const loc_t *workset_contains(const workset_t *ws,
239 for (i = 0; i < ws->len; ++i) {
240 if (ws->vals[i].node == val)
248 * Iterates over all values in the working set.
249 * @p ws The workset to iterate
250 * @p v A variable to put the current value in
251 * @p i An integer for internal use
253 #define workset_foreach(ws, v, i) for(i=0; \
254 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
257 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
258 #define workset_get_time(ws, i) (ws)->vals[i].time
259 #define workset_set_length(ws, length) (ws)->len = length
260 #define workset_get_length(ws) ((ws)->len)
261 #define workset_get_val(ws, i) ((ws)->vals[i].node)
262 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
264 typedef struct _block_info_t
266 workset_t *start_workset;
267 workset_t *end_workset;
271 static void *new_block_info(void)
273 block_info_t *res = obstack_alloc(&obst, sizeof(res[0]));
274 memset(res, 0, sizeof(res[0]));
279 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
280 #define set_block_info(block, info) set_irn_link(block, info)
283 * @return The distance to the next use or 0 if irn has dont_spill flag set
285 static INLINE unsigned get_distance(ir_node *from, unsigned from_step,
286 const ir_node *def, int skip_from_uses)
289 int flags = arch_irn_get_flags(arch_env, def);
293 assert(! (flags & arch_irn_flags_ignore));
295 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
296 if (USES_IS_INFINITE(use.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 = use.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 /* mark value as used */
340 ir_nodeset_insert(&used, val);
342 if (! workset_contains(ws, val)) {
343 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
345 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
346 be_add_reload(senv, val, instr, cls, 1);
350 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
352 /* remove the value from the current workset so it is not accidently
354 workset_remove(ws, val);
356 spilled[demand] = reloaded;
357 to_insert[demand] = val;
361 /* 2. Make room for at least 'demand' slots */
362 len = workset_get_length(ws);
363 spills_needed = len + demand - n_regs;
364 assert(spills_needed <= len);
366 /* Only make more free room if we do not have enough */
367 if (spills_needed > 0) {
368 ir_node *curr_bb = NULL;
369 workset_t *ws_start = NULL;
372 curr_bb = get_nodes_block(instr);
373 ws_start = get_block_info(curr_bb)->start_workset;
376 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
378 /* calculate current next-use distance for live values */
379 for (i = 0; i < len; ++i) {
380 ir_node *val = workset_get_val(ws, i);
381 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
382 workset_set_time(ws, i, dist);
385 /* sort entries by increasing nextuse-distance*/
388 for (i = len - spills_needed; i < len; ++i) {
389 ir_node *val = ws->vals[i].node;
391 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
392 workset_get_time(ws, i)));
395 if (!USES_IS_INFINITE(ws->vals[i].time)
396 && !ws->vals[i].spilled) {
397 ir_node *after_pos = sched_prev(instr);
398 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
400 be_add_spill(senv, val, after_pos);
403 /* Logic for not needed live-ins: If a value is disposed
404 * before its first use, remove it from start workset
405 * We don't do this for phis though */
406 if (!is_Phi(val) && ! ir_nodeset_contains(&used, val)) {
407 workset_remove(ws_start, val);
408 DB((dbg, DBG_DECIDE, " (and removing %+F from start workset)\n", val));
413 /* kill the last 'demand' entries in the array */
414 workset_set_length(ws, len - spills_needed);
417 /* 3. Insert the new values into the workset */
418 for (i = 0; i < demand; ++i) {
419 ir_node *val = to_insert[i];
421 workset_insert(ws, val, spilled[i]);
426 AVAILABLE_EVERYWHERE,
432 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
433 size_t n_pred_worksets,
434 const ir_node *value, bool is_local_phi)
437 bool avail_everywhere = true;
438 bool avail_nowhere = true;
440 assert(n_pred_worksets > 0);
442 /* value available in all preds? */
443 for (i = 0; i < n_pred_worksets; ++i) {
445 const workset_t *p_workset = pred_worksets[i];
446 int p_len = workset_get_length(p_workset);
448 const ir_node *l_value;
451 assert(is_Phi(value));
452 l_value = get_irn_n(value, i);
457 for (p_i = 0; p_i < p_len; ++p_i) {
458 const loc_t *p_l = &p_workset->vals[p_i];
459 if (p_l->node != l_value)
467 avail_nowhere = false;
469 avail_everywhere = false;
473 if (avail_everywhere) {
474 assert(!avail_nowhere);
475 return AVAILABLE_EVERYWHERE;
476 } else if (avail_nowhere) {
477 return AVAILABLE_NOWHERE;
479 return AVAILABLE_PARTLY;
483 /** Decides whether a specific node should be in the start workset or not
485 * @param env belady environment
487 * @param node the node to test
488 * @param loop the loop of the node
490 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
491 ir_loop *loop, unsigned available)
493 be_next_use_t next_use;
496 loc.time = USES_INFINITY;
500 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
501 loc.time = USES_INFINITY;
505 /* We have to keep nonspillable nodes in the workingset */
506 if (arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
508 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
512 next_use = be_get_next_use(uses, first, 0, node, 0);
513 if (USES_IS_INFINITE(next_use.time)) {
514 // the nodes marked as live in shouldn't be dead, so it must be a phi
515 assert(is_Phi(node));
516 loc.time = USES_INFINITY;
517 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
521 loc.time = next_use.time;
523 if (improve_known_preds) {
524 if (available == AVAILABLE_EVERYWHERE) {
525 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
528 } else if(available == AVAILABLE_NOWHERE) {
529 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
531 loc.time = USES_INFINITY;
536 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
537 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
538 next_use.outermost_loop));
540 loc.time = USES_PENDING;
541 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
542 node, next_use.outermost_loop, get_loop_depth(loop)));
549 * Computes the start-workset for a block with multiple predecessors. We assume
550 * that at least 1 of the predeccesors is a back-edge which means we're at the
551 * beginning of a loop. We try to reload as much values as possible now so they
552 * don't get reloaded inside the loop.
554 static void decide_start_workset(const ir_node *block)
556 ir_loop *loop = get_irn_loop(block);
562 int i, len, ws_count;
563 int free_slots, free_pressure_slots;
566 workset_t **pred_worksets;
567 bool all_preds_known;
569 /* check predecessors */
570 arity = get_irn_arity(block);
571 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
572 all_preds_known = true;
573 for(i = 0; i < arity; ++i) {
574 ir_node *pred_block = get_Block_cfgpred_block(block, i);
575 block_info_t *pred_info = get_block_info(pred_block);
577 if (pred_info == NULL) {
578 pred_worksets[i] = NULL;
579 all_preds_known = false;
581 pred_worksets[i] = pred_info->end_workset;
585 /* Collect all values living at start of block */
586 starters = NEW_ARR_F(loc_t, 0);
587 delayed = NEW_ARR_F(loc_t, 0);
589 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
590 first = sched_first(block);
592 /* check all Phis first */
593 sched_foreach(block, node) {
598 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
601 if (all_preds_known) {
602 available = available_in_all_preds(pred_worksets, arity, node, true);
604 available = AVAILABLE_UNKNOWN;
607 loc = to_take_or_not_to_take(first, node, loop, available);
609 if (! USES_IS_INFINITE(loc.time)) {
610 if (USES_IS_PENDING(loc.time))
611 ARR_APP1(loc_t, delayed, loc);
613 ARR_APP1(loc_t, starters, loc);
615 be_spill_phi(senv, node);
619 /* check all Live-Ins */
620 be_lv_foreach(lv, block, be_lv_state_in, i) {
621 ir_node *node = be_lv_get_irn(lv, block, i);
624 if (all_preds_known) {
625 available = available_in_all_preds(pred_worksets, arity, node, false);
627 available = AVAILABLE_UNKNOWN;
630 loc = to_take_or_not_to_take(first, node, loop, available);
632 if (! USES_IS_INFINITE(loc.time)) {
633 if (USES_IS_PENDING(loc.time))
634 ARR_APP1(loc_t, delayed, loc);
636 ARR_APP1(loc_t, starters, loc);
640 pressure = be_get_loop_pressure(loop_ana, cls, loop);
641 assert(ARR_LEN(delayed) <= (signed)pressure);
642 free_slots = n_regs - ARR_LEN(starters);
643 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
644 free_slots = MIN(free_slots, free_pressure_slots);
646 /* so far we only put nodes into the starters list that are used inside
647 * the loop. If register pressure in the loop is low then we can take some
648 * values and let them live through the loop */
649 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
650 pressure, free_slots));
651 if (free_slots > 0) {
652 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
654 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
656 loc_t *loc = & delayed[i];
658 /* don't use values which are dead in a known predecessors
659 * to not induce unnecessary reloads */
660 arity = get_irn_arity(block);
661 for (p = 0; p < arity; ++p) {
662 ir_node *pred_block = get_Block_cfgpred_block(block, p);
663 block_info_t *pred_info = get_block_info(pred_block);
665 if (pred_info == NULL)
668 if (!workset_contains(pred_info->end_workset, loc->node)) {
670 " delayed %+F not live at pred %+F\n", loc->node,
676 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
677 ARR_APP1(loc_t, starters, *loc);
685 /* spill phis (the actual phis not just their values) that are in this block
686 * but not in the start workset */
687 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
688 ir_node *node = delayed[i].node;
689 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
692 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
693 be_spill_phi(senv, node);
697 /* Sort start values by first use */
698 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
700 /* Copy the best ones from starters to start workset */
701 ws_count = MIN(ARR_LEN(starters), n_regs);
703 workset_bulk_fill(ws, ws_count, starters);
705 /* spill phis (the actual phis not just their values) that are in this block
706 * but not in the start workset */
707 len = ARR_LEN(starters);
708 for (i = ws_count; i < len; ++i) {
709 ir_node *node = starters[i].node;
710 if (! is_Phi(node) || get_nodes_block(node) != block)
713 DB((dbg, DBG_START, " spilling phi %+F\n", node));
714 be_spill_phi(senv, node);
719 /* determine spill status of the values: If there's 1 pred block (which
720 * is no backedge) where the value is spilled then we must set it to
722 for(i = 0; i < ws_count; ++i) {
723 loc_t *loc = &ws->vals[i];
724 ir_node *value = loc->node;
728 /* phis from this block aren't spilled */
729 if (get_nodes_block(value) == block) {
730 assert(is_Phi(value));
731 loc->spilled = false;
735 /* determine if value was spilled on any predecessor */
737 for(n = 0; n < arity; ++n) {
738 workset_t *pred_workset = pred_worksets[n];
742 if (pred_workset == NULL)
745 p_len = workset_get_length(pred_workset);
746 for(p = 0; p < p_len; ++p) {
747 loc_t *l = &pred_workset->vals[p];
749 if (l->node != value)
759 loc->spilled = spilled;
764 * For the given block @p block, decide for each values
765 * whether it is used from a register or is reloaded
768 static void belady(ir_node *block)
773 block_info_t *block_info;
775 int has_backedges = 0;
777 const ir_edge_t *edge;
779 /* no need to process a block twice */
780 if (get_block_info(block) != NULL) {
784 /* check if all predecessor blocks are processed yet (though for backedges
785 * we have to make an exception as we can't process them first) */
786 arity = get_Block_n_cfgpreds(block);
787 for(i = 0; i < arity; ++i) {
788 ir_node *pred_block = get_Block_cfgpred_block(block, i);
789 block_info_t *pred_info = get_block_info(pred_block);
791 if (pred_info == NULL) {
792 /* process predecessor first (it will be in the queue already) */
793 if (!is_backedge(block, i)) {
799 (void) has_backedges;
802 } else if (arity == 1) {
803 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
804 block_info_t *pred_info = get_block_info(pred_block);
806 assert(pred_info != NULL);
807 workset_copy(ws, pred_info->end_workset);
809 /* we need 2 heuristics here, for the case when all predecessor blocks
810 * are known and when some are backedges (and therefore can't be known
812 decide_start_workset(block);
815 DB((dbg, DBG_DECIDE, "\n"));
816 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
818 block_info = new_block_info();
819 set_block_info(block, block_info);
821 DB((dbg, DBG_WSETS, "Start 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)));
827 block_info->start_workset = workset_clone(ws);
829 /* process the block from start to end */
830 DB((dbg, DBG_WSETS, "Processing...\n"));
831 ir_nodeset_init(&used);
833 /* TODO: this leaks (into the obstack)... */
834 new_vals = new_workset();
836 sched_foreach(block, irn) {
838 assert(workset_get_length(ws) <= n_regs);
840 /* Phis are no real instr (see insert_starters()) */
844 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
846 /* set instruction in the workset */
849 /* allocate all values _used_ by this instruction */
850 workset_clear(new_vals);
851 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
852 ir_node *in = get_irn_n(irn, i);
853 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, in))
856 /* (note that "spilled" is irrelevant here) */
857 workset_insert(new_vals, in, false);
859 displace(new_vals, 1);
861 /* allocate all values _defined_ by this instruction */
862 workset_clear(new_vals);
863 if (get_irn_mode(irn) == mode_T) {
864 const ir_edge_t *edge;
866 foreach_out_edge(irn, edge) {
867 ir_node *proj = get_edge_src_irn(edge);
868 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, proj))
870 workset_insert(new_vals, proj, false);
873 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
875 workset_insert(new_vals, irn, false);
877 displace(new_vals, 0);
881 ir_nodeset_destroy(&used);
883 /* Remember end-workset for this block */
884 block_info->end_workset = workset_clone(ws);
885 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
886 workset_foreach(ws, irn, iter)
887 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
888 workset_get_time(ws, iter)));
890 /* add successor blocks into worklist */
891 foreach_block_succ(block, edge) {
892 ir_node *succ = get_edge_src_irn(edge);
893 pdeq_putr(worklist, succ);
898 * 'decide' is block-local and makes assumptions
899 * about the set of live-ins. Thus we must adapt the
900 * live-outs to the live-ins at each block-border.
902 static void fix_block_borders(ir_node *block, void *data)
904 workset_t *start_workset;
910 DB((dbg, DBG_FIX, "\n"));
911 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
913 start_workset = get_block_info(block)->start_workset;
915 /* process all pred blocks */
916 arity = get_irn_arity(block);
917 for (i = 0; i < arity; ++i) {
918 ir_node *pred = get_Block_cfgpred_block(block, i);
919 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
922 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
924 /* spill all values not used anymore */
925 workset_foreach(pred_end_workset, node, iter) {
929 workset_foreach(start_workset, n2, iter2) {
934 /* note that we do not look at phi inputs, becuase the values
935 * will be either live-end and need no spill or
936 * they have other users in which must be somewhere else in the
943 if (move_spills && be_is_live_in(lv, block, node)
944 && !pred_end_workset->vals[iter].spilled) {
945 ir_node *insert_point;
947 insert_point = be_get_end_of_block_insertion_point(pred);
948 insert_point = sched_prev(insert_point);
950 insert_point = block;
952 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
954 be_add_spill(senv, node, insert_point);
958 /* reload missing values in predecessors, add missing spills */
959 workset_foreach(start_workset, node, iter) {
960 const loc_t *l = &start_workset->vals[iter];
961 const loc_t *pred_loc;
963 /* if node is a phi of the current block we reload
964 * the corresponding argument, else node itself */
965 if (is_Phi(node) && get_nodes_block(node) == block) {
966 node = get_irn_n(node, i);
969 /* we might have unknowns as argument for the phi */
970 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
974 /* check if node is in a register at end of pred */
975 pred_loc = workset_contains(pred_end_workset, node);
976 if (pred_loc != NULL) {
977 /* we might have to spill value on this path */
978 if (move_spills && !pred_loc->spilled && l->spilled) {
979 ir_node *insert_point
980 = be_get_end_of_block_insertion_point(pred);
981 insert_point = sched_prev(insert_point);
982 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
984 be_add_spill(senv, node, insert_point);
987 /* node is not in register at the end of pred -> reload it */
988 DB((dbg, DBG_FIX, " reload %+F\n", node));
989 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
990 be_add_reload_on_edge(senv, node, block, i, cls, 1);
996 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
998 ir_graph *irg = be_get_birg_irg(birg);
1000 be_liveness_assure_sets(be_assure_liveness(birg));
1002 /* construct control flow loop tree */
1003 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
1004 construct_cf_backedges(irg);
1007 be_clear_links(irg);
1009 /* init belady env */
1010 obstack_init(&obst);
1011 arch_env = birg->main_env->arch_env;
1013 lv = be_get_birg_liveness(birg);
1014 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1016 uses = be_begin_uses(irg, lv);
1017 loop_ana = be_new_loop_pressure(birg);
1018 senv = be_new_spill_env(birg);
1019 worklist = new_pdeq();
1021 pdeq_putr(worklist, get_irg_start_block(irg));
1023 while(!pdeq_empty(worklist)) {
1024 ir_node *block = pdeq_getl(worklist);
1027 /* end block might not be reachable in endless loops */
1028 belady(get_irg_end_block(irg));
1032 /* belady was block-local, fix the global flow by adding reloads on the
1034 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1036 /* Insert spill/reload nodes into the graph and fix usages */
1037 be_insert_spills_reloads(senv);
1040 be_delete_spill_env(senv);
1042 be_free_loop_pressure(loop_ana);
1043 obstack_free(&obst, NULL);
1046 void be_init_spillbelady(void)
1048 static be_spiller_t belady_spiller = {
1051 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1052 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1053 lc_opt_add_table(belady_group, options);
1055 be_register_spiller("belady", &belady_spiller);
1056 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1059 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);