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);
297 if (USES_IS_INFINITE(time))
298 return USES_INFINITY;
300 /* We have to keep nonspillable nodes in the workingset */
301 if (flags & arch_irn_flags_dont_spill)
304 /* give some bonus to rematerialisable nodes */
305 if (remat_bonus > 0) {
306 costs = be_get_reload_costs_no_weight(senv, def, use.before);
307 assert(costs * remat_bonus < 1000);
308 time += 1000 - (costs * remat_bonus);
315 * Performs the actions necessary to grant the request that:
316 * - new_vals can be held in registers
317 * - as few as possible other values are disposed
318 * - the worst values get disposed
320 * @p is_usage indicates that the values in new_vals are used (not defined)
321 * In this case reloads must be performed
323 static void displace(workset_t *new_vals, int is_usage)
325 ir_node **to_insert = alloca(n_regs * sizeof(to_insert[0]));
326 bool *spilled = alloca(n_regs * sizeof(spilled[0]));
334 /* 1. Identify the number of needed slots and the values to reload */
336 workset_foreach(new_vals, val, iter) {
337 bool reloaded = false;
339 /* mark value as used */
341 ir_nodeset_insert(&used, val);
343 if (! workset_contains(ws, val)) {
344 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
346 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
347 be_add_reload(senv, val, instr, cls, 1);
351 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
353 /* remove the value from the current workset so it is not accidently
355 workset_remove(ws, val);
357 spilled[demand] = reloaded;
358 to_insert[demand] = val;
362 /* 2. Make room for at least 'demand' slots */
363 len = workset_get_length(ws);
364 spills_needed = len + demand - n_regs;
365 assert(spills_needed <= len);
367 /* Only make more free room if we do not have enough */
368 if (spills_needed > 0) {
369 ir_node *curr_bb = NULL;
370 workset_t *ws_start = NULL;
373 curr_bb = get_nodes_block(instr);
374 ws_start = get_block_info(curr_bb)->start_workset;
377 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
379 /* calculate current next-use distance for live values */
380 for (i = 0; i < len; ++i) {
381 ir_node *val = workset_get_val(ws, i);
382 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
383 workset_set_time(ws, i, dist);
386 /* sort entries by increasing nextuse-distance*/
389 for (i = len - spills_needed; i < len; ++i) {
390 ir_node *val = ws->vals[i].node;
392 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
393 workset_get_time(ws, i)));
396 if (!USES_IS_INFINITE(ws->vals[i].time)
397 && !ws->vals[i].spilled) {
398 ir_node *after_pos = sched_prev(instr);
399 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
401 be_add_spill(senv, val, after_pos);
404 /* Logic for not needed live-ins: If a value is disposed
405 * before its first use, remove it from start workset
406 * We don't do this for phis though */
407 if (!is_Phi(val) && ! ir_nodeset_contains(&used, val)) {
408 workset_remove(ws_start, val);
409 DB((dbg, DBG_DECIDE, " (and removing %+F from start workset)\n", val));
414 /* kill the last 'demand' entries in the array */
415 workset_set_length(ws, len - spills_needed);
418 /* 3. Insert the new values into the workset */
419 for (i = 0; i < demand; ++i) {
420 ir_node *val = to_insert[i];
422 workset_insert(ws, val, spilled[i]);
427 AVAILABLE_EVERYWHERE,
433 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
434 size_t n_pred_worksets,
435 const ir_node *value, bool is_local_phi)
438 bool avail_everywhere = true;
439 bool avail_nowhere = true;
441 assert(n_pred_worksets > 0);
443 /* value available in all preds? */
444 for (i = 0; i < n_pred_worksets; ++i) {
446 const workset_t *p_workset = pred_worksets[i];
447 int p_len = workset_get_length(p_workset);
449 const ir_node *l_value;
452 assert(is_Phi(value));
453 l_value = get_irn_n(value, i);
458 for (p_i = 0; p_i < p_len; ++p_i) {
459 const loc_t *p_l = &p_workset->vals[p_i];
460 if (p_l->node != l_value)
468 avail_nowhere = false;
470 avail_everywhere = false;
474 if (avail_everywhere) {
475 assert(!avail_nowhere);
476 return AVAILABLE_EVERYWHERE;
477 } else if (avail_nowhere) {
478 return AVAILABLE_NOWHERE;
480 return AVAILABLE_PARTLY;
484 /** Decides whether a specific node should be in the start workset or not
486 * @param env belady environment
488 * @param node the node to test
489 * @param loop the loop of the node
491 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
492 ir_loop *loop, unsigned available)
494 be_next_use_t next_use;
497 loc.time = USES_INFINITY;
501 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
502 loc.time = USES_INFINITY;
506 /* We have to keep nonspillable nodes in the workingset */
507 if (arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
509 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
513 next_use = be_get_next_use(uses, first, 0, node, 0);
514 if (USES_IS_INFINITE(next_use.time)) {
515 // the nodes marked as live in shouldn't be dead, so it must be a phi
516 assert(is_Phi(node));
517 loc.time = USES_INFINITY;
518 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
522 loc.time = next_use.time;
524 if (improve_known_preds) {
525 if (available == AVAILABLE_EVERYWHERE) {
526 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
529 } else if(available == AVAILABLE_NOWHERE) {
530 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
532 loc.time = USES_INFINITY;
537 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
538 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
539 next_use.outermost_loop));
541 loc.time = USES_PENDING;
542 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
543 node, next_use.outermost_loop, get_loop_depth(loop)));
550 * Computes the start-workset for a block with multiple predecessors. We assume
551 * that at least 1 of the predeccesors is a back-edge which means we're at the
552 * beginning of a loop. We try to reload as much values as possible now so they
553 * don't get reloaded inside the loop.
555 static void decide_start_workset(const ir_node *block)
557 ir_loop *loop = get_irn_loop(block);
563 int i, len, ws_count;
564 int free_slots, free_pressure_slots;
567 workset_t **pred_worksets;
568 bool all_preds_known;
570 /* check predecessors */
571 arity = get_irn_arity(block);
572 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
573 all_preds_known = true;
574 for(i = 0; i < arity; ++i) {
575 ir_node *pred_block = get_Block_cfgpred_block(block, i);
576 block_info_t *pred_info = get_block_info(pred_block);
578 if (pred_info == NULL) {
579 pred_worksets[i] = NULL;
580 all_preds_known = false;
582 pred_worksets[i] = pred_info->end_workset;
586 /* Collect all values living at start of block */
587 starters = NEW_ARR_F(loc_t, 0);
588 delayed = NEW_ARR_F(loc_t, 0);
590 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
591 first = sched_first(block);
593 /* check all Phis first */
594 sched_foreach(block, node) {
599 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
602 if (all_preds_known) {
603 available = available_in_all_preds(pred_worksets, arity, node, true);
605 available = AVAILABLE_UNKNOWN;
608 loc = to_take_or_not_to_take(first, node, loop, available);
610 if (! USES_IS_INFINITE(loc.time)) {
611 if (USES_IS_PENDING(loc.time))
612 ARR_APP1(loc_t, delayed, loc);
614 ARR_APP1(loc_t, starters, loc);
616 be_spill_phi(senv, node);
620 /* check all Live-Ins */
621 be_lv_foreach(lv, block, be_lv_state_in, i) {
622 ir_node *node = be_lv_get_irn(lv, block, i);
625 if (all_preds_known) {
626 available = available_in_all_preds(pred_worksets, arity, node, false);
628 available = AVAILABLE_UNKNOWN;
631 loc = to_take_or_not_to_take(first, node, loop, available);
633 if (! USES_IS_INFINITE(loc.time)) {
634 if (USES_IS_PENDING(loc.time))
635 ARR_APP1(loc_t, delayed, loc);
637 ARR_APP1(loc_t, starters, loc);
641 pressure = be_get_loop_pressure(loop_ana, cls, loop);
642 assert(ARR_LEN(delayed) <= (signed)pressure);
643 free_slots = n_regs - ARR_LEN(starters);
644 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
645 free_slots = MIN(free_slots, free_pressure_slots);
647 /* so far we only put nodes into the starters list that are used inside
648 * the loop. If register pressure in the loop is low then we can take some
649 * values and let them live through the loop */
650 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
651 pressure, free_slots));
652 if (free_slots > 0) {
653 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
655 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
657 loc_t *loc = & delayed[i];
659 /* don't use values which are dead in a known predecessors
660 * to not induce unnecessary reloads */
661 arity = get_irn_arity(block);
662 for (p = 0; p < arity; ++p) {
663 ir_node *pred_block = get_Block_cfgpred_block(block, p);
664 block_info_t *pred_info = get_block_info(pred_block);
666 if (pred_info == NULL)
669 if (!workset_contains(pred_info->end_workset, loc->node)) {
671 " delayed %+F not live at pred %+F\n", loc->node,
677 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
678 ARR_APP1(loc_t, starters, *loc);
686 /* spill phis (the actual phis not just their values) that are in this block
687 * but not in the start workset */
688 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
689 ir_node *node = delayed[i].node;
690 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
693 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
694 be_spill_phi(senv, node);
698 /* Sort start values by first use */
699 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
701 /* Copy the best ones from starters to start workset */
702 ws_count = MIN(ARR_LEN(starters), n_regs);
704 workset_bulk_fill(ws, ws_count, starters);
706 /* spill phis (the actual phis not just their values) that are in this block
707 * but not in the start workset */
708 len = ARR_LEN(starters);
709 for (i = ws_count; i < len; ++i) {
710 ir_node *node = starters[i].node;
711 if (! is_Phi(node) || get_nodes_block(node) != block)
714 DB((dbg, DBG_START, " spilling phi %+F\n", node));
715 be_spill_phi(senv, node);
720 /* determine spill status of the values: If there's 1 pred block (which
721 * is no backedge) where the value is spilled then we must set it to
723 for(i = 0; i < ws_count; ++i) {
724 loc_t *loc = &ws->vals[i];
725 ir_node *value = loc->node;
729 /* phis from this block aren't spilled */
730 if (get_nodes_block(value) == block) {
731 assert(is_Phi(value));
732 loc->spilled = false;
736 /* determine if value was spilled on any predecessor */
738 for(n = 0; n < arity; ++n) {
739 workset_t *pred_workset = pred_worksets[n];
743 if (pred_workset == NULL)
746 p_len = workset_get_length(pred_workset);
747 for(p = 0; p < p_len; ++p) {
748 loc_t *l = &pred_workset->vals[p];
750 if (l->node != value)
760 loc->spilled = spilled;
765 * For the given block @p block, decide for each values
766 * whether it is used from a register or is reloaded
769 static void belady(ir_node *block)
774 block_info_t *block_info;
776 int has_backedges = 0;
778 const ir_edge_t *edge;
780 /* no need to process a block twice */
781 if (get_block_info(block) != NULL) {
785 /* check if all predecessor blocks are processed yet (though for backedges
786 * we have to make an exception as we can't process them first) */
787 arity = get_Block_n_cfgpreds(block);
788 for(i = 0; i < arity; ++i) {
789 ir_node *pred_block = get_Block_cfgpred_block(block, i);
790 block_info_t *pred_info = get_block_info(pred_block);
792 if (pred_info == NULL) {
793 /* process predecessor first (it will be in the queue already) */
794 if (!is_backedge(block, i)) {
800 (void) has_backedges;
803 } else if (arity == 1) {
804 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
805 block_info_t *pred_info = get_block_info(pred_block);
807 assert(pred_info != NULL);
808 workset_copy(ws, pred_info->end_workset);
810 /* we need 2 heuristics here, for the case when all predecessor blocks
811 * are known and when some are backedges (and therefore can't be known
813 decide_start_workset(block);
816 DB((dbg, DBG_DECIDE, "\n"));
817 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
819 block_info = new_block_info();
820 set_block_info(block, block_info);
822 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
823 workset_foreach(ws, irn, iter) {
824 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
825 workset_get_time(ws, iter)));
828 block_info->start_workset = workset_clone(ws);
830 /* process the block from start to end */
831 DB((dbg, DBG_WSETS, "Processing...\n"));
832 ir_nodeset_init(&used);
834 /* TODO: this leaks (into the obstack)... */
835 new_vals = new_workset();
837 sched_foreach(block, irn) {
839 assert(workset_get_length(ws) <= n_regs);
841 /* Phis are no real instr (see insert_starters()) */
845 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
847 /* set instruction in the workset */
850 /* allocate all values _used_ by this instruction */
851 workset_clear(new_vals);
852 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
853 ir_node *in = get_irn_n(irn, i);
854 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, in))
857 /* (note that "spilled" is irrelevant here) */
858 workset_insert(new_vals, in, false);
860 displace(new_vals, 1);
862 /* allocate all values _defined_ by this instruction */
863 workset_clear(new_vals);
864 if (get_irn_mode(irn) == mode_T) {
865 const ir_edge_t *edge;
867 foreach_out_edge(irn, edge) {
868 ir_node *proj = get_edge_src_irn(edge);
869 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, proj))
871 workset_insert(new_vals, proj, false);
874 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
876 workset_insert(new_vals, irn, false);
878 displace(new_vals, 0);
882 ir_nodeset_destroy(&used);
884 /* Remember end-workset for this block */
885 block_info->end_workset = workset_clone(ws);
886 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
887 workset_foreach(ws, irn, iter)
888 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
889 workset_get_time(ws, iter)));
891 /* add successor blocks into worklist */
892 foreach_block_succ(block, edge) {
893 ir_node *succ = get_edge_src_irn(edge);
894 pdeq_putr(worklist, succ);
899 * 'decide' is block-local and makes assumptions
900 * about the set of live-ins. Thus we must adapt the
901 * live-outs to the live-ins at each block-border.
903 static void fix_block_borders(ir_node *block, void *data)
905 workset_t *start_workset;
911 DB((dbg, DBG_FIX, "\n"));
912 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
914 start_workset = get_block_info(block)->start_workset;
916 /* process all pred blocks */
917 arity = get_irn_arity(block);
918 for (i = 0; i < arity; ++i) {
919 ir_node *pred = get_Block_cfgpred_block(block, i);
920 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
923 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
925 /* spill all values not used anymore */
926 workset_foreach(pred_end_workset, node, iter) {
930 workset_foreach(start_workset, n2, iter2) {
935 /* note that we do not look at phi inputs, becuase the values
936 * will be either live-end and need no spill or
937 * they have other users in which must be somewhere else in the
944 if (move_spills && be_is_live_in(lv, block, node)
945 && !pred_end_workset->vals[iter].spilled) {
946 ir_node *insert_point;
948 insert_point = be_get_end_of_block_insertion_point(pred);
949 insert_point = sched_prev(insert_point);
951 insert_point = block;
953 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
955 be_add_spill(senv, node, insert_point);
959 /* reload missing values in predecessors, add missing spills */
960 workset_foreach(start_workset, node, iter) {
961 const loc_t *l = &start_workset->vals[iter];
962 const loc_t *pred_loc;
964 /* if node is a phi of the current block we reload
965 * the corresponding argument, else node itself */
966 if (is_Phi(node) && get_nodes_block(node) == block) {
967 node = get_irn_n(node, i);
970 /* we might have unknowns as argument for the phi */
971 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
975 /* check if node is in a register at end of pred */
976 pred_loc = workset_contains(pred_end_workset, node);
977 if (pred_loc != NULL) {
978 /* we might have to spill value on this path */
979 if (move_spills && !pred_loc->spilled && l->spilled) {
980 ir_node *insert_point
981 = be_get_end_of_block_insertion_point(pred);
982 insert_point = sched_prev(insert_point);
983 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
985 be_add_spill(senv, node, insert_point);
988 /* node is not in register at the end of pred -> reload it */
989 DB((dbg, DBG_FIX, " reload %+F\n", node));
990 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
991 be_add_reload_on_edge(senv, node, block, i, cls, 1);
997 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
999 ir_graph *irg = be_get_birg_irg(birg);
1001 be_liveness_assure_sets(be_assure_liveness(birg));
1003 /* construct control flow loop tree */
1004 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
1005 construct_cf_backedges(irg);
1008 be_clear_links(irg);
1010 /* init belady env */
1011 obstack_init(&obst);
1012 arch_env = birg->main_env->arch_env;
1014 lv = be_get_birg_liveness(birg);
1015 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
1017 uses = be_begin_uses(irg, lv);
1018 loop_ana = be_new_loop_pressure(birg);
1019 senv = be_new_spill_env(birg);
1020 worklist = new_pdeq();
1022 pdeq_putr(worklist, get_irg_start_block(irg));
1024 while(!pdeq_empty(worklist)) {
1025 ir_node *block = pdeq_getl(worklist);
1028 /* end block might not be reachable in endless loops */
1029 belady(get_irg_end_block(irg));
1033 /* belady was block-local, fix the global flow by adding reloads on the
1035 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1037 /* Insert spill/reload nodes into the graph and fix usages */
1038 be_insert_spills_reloads(senv);
1041 be_delete_spill_env(senv);
1043 be_free_loop_pressure(loop_ana);
1044 obstack_free(&obst, NULL);
1047 void be_init_spillbelady(void)
1049 static be_spiller_t belady_spiller = {
1052 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1053 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1054 lc_opt_add_table(belady_group, options);
1056 be_register_spiller("belady", &belady_spiller);
1057 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1060 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);