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"
49 #include "besched_t.h"
53 #include "bechordal_t.h"
54 #include "bespilloptions.h"
55 #include "beloopana.h"
67 #define DBG_WORKSET 128
68 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
70 #define TIME_UNDEFINED 6666
72 //#define LOOK_AT_LOOPDEPTH
75 * An association between a node and a point in time.
77 typedef struct loc_t {
79 unsigned time; /**< A use time (see beuses.h). */
80 bool spilled; /**< the value was already spilled on this path */
83 typedef struct _workset_t {
84 int len; /**< current length */
85 loc_t vals[0]; /**< inlined array of the values/distances in this working set */
88 static struct obstack obst;
89 static const arch_env_t *arch_env;
90 static const arch_register_class_t *cls;
91 static const be_lv_t *lv;
92 static be_loopana_t *loop_ana;
94 static workset_t *ws; /**< the main workset used while
95 processing a block. */
96 static be_uses_t *uses; /**< env for the next-use magic */
97 static ir_node *instr; /**< current instruction */
98 static unsigned instr_nr; /**< current instruction number
99 (relative to block start) */
100 static spill_env_t *senv; /**< see bespill.h */
101 static ir_node **blocklist;
103 static bool move_spills = true;
104 static bool respectloopdepth = true;
105 static bool improve_known_preds = true;
106 /* factor to weight the different costs of reloading/rematerializing a node
107 (see bespill.h be_get_reload_costs_no_weight) */
108 static int remat_bonus = 10;
110 static const lc_opt_table_entry_t options[] = {
111 LC_OPT_ENT_BOOL ("movespills", "try to move spills out of loops", &move_spills),
112 LC_OPT_ENT_BOOL ("respectloopdepth", "exprimental (outermost loop cutting)", &respectloopdepth),
113 LC_OPT_ENT_BOOL ("improveknownpreds", "experimental (known preds cutting)", &improve_known_preds),
114 LC_OPT_ENT_INT ("rematbonus", "give bonus to rematerialisable nodes", &remat_bonus),
118 static int loc_compare(const void *a, const void *b)
122 return p->time - q->time;
125 void workset_print(const workset_t *w)
129 for(i = 0; i < w->len; ++i) {
130 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
135 * Alloc a new workset on obstack @p ob with maximum size @p max
137 static workset_t *new_workset(void)
140 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
142 res = obstack_alloc(&obst, size);
143 memset(res, 0, size);
148 * Alloc a new instance on obstack and make it equal to @param workset
150 static workset_t *workset_clone(workset_t *workset)
153 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
154 res = obstack_alloc(&obst, size);
155 memcpy(res, workset, size);
160 * Copy workset @param src to @param tgt
162 static void workset_copy(workset_t *dest, const workset_t *src)
164 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
165 memcpy(dest, src, size);
169 * Overwrites the current content array of @param ws with the
170 * @param count locations given at memory @param locs.
171 * Set the length of @param ws to count.
173 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
175 workset->len = count;
176 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
180 * Inserts the value @p val into the workset, iff it is not
181 * already contained. The workset must not be full.
183 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
187 /* check for current regclass */
188 assert(arch_irn_consider_in_reg_alloc(arch_env, cls, val));
190 /* check if val is already contained */
191 for (i = 0; i < workset->len; ++i) {
192 loc = &workset->vals[i];
193 if (loc->node == val) {
202 assert(workset->len < n_regs && "Workset already full!");
203 loc = &workset->vals[workset->len];
205 loc->spilled = spilled;
206 loc->time = TIME_UNDEFINED;
211 * Removes all entries from this workset
213 static void workset_clear(workset_t *workset)
219 * Removes the value @p val from the workset if present.
221 static INLINE void workset_remove(workset_t *workset, ir_node *val)
224 for(i = 0; i < workset->len; ++i) {
225 if (workset->vals[i].node == val) {
226 workset->vals[i] = workset->vals[--workset->len];
232 static INLINE const loc_t *workset_contains(const workset_t *ws,
237 for (i = 0; i < ws->len; ++i) {
238 if (ws->vals[i].node == val)
246 * Iterates over all values in the working set.
247 * @p ws The workset to iterate
248 * @p v A variable to put the current value in
249 * @p i An integer for internal use
251 #define workset_foreach(ws, v, i) for(i=0; \
252 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
255 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
256 #define workset_get_time(ws, i) (ws)->vals[i].time
257 #define workset_set_length(ws, length) (ws)->len = length
258 #define workset_get_length(ws) ((ws)->len)
259 #define workset_get_val(ws, i) ((ws)->vals[i].node)
260 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
262 typedef struct _block_info_t
264 workset_t *start_workset;
265 workset_t *end_workset;
269 static void *new_block_info(void)
271 block_info_t *res = obstack_alloc(&obst, sizeof(res[0]));
272 memset(res, 0, sizeof(res[0]));
277 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
278 #define set_block_info(block, info) set_irn_link(block, info)
281 * @return The distance to the next use or 0 if irn has dont_spill flag set
283 static INLINE unsigned get_distance(ir_node *from, unsigned from_step,
284 const ir_node *def, int skip_from_uses)
287 int flags = arch_irn_get_flags(arch_env, def);
291 assert(! (flags & arch_irn_flags_ignore));
293 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
295 if (USES_IS_INFINITE(time))
296 return USES_INFINITY;
298 /* We have to keep nonspillable nodes in the workingset */
299 if (flags & arch_irn_flags_dont_spill)
302 /* give some bonus to rematerialisable nodes */
303 if (remat_bonus > 0) {
304 costs = be_get_reload_costs_no_weight(senv, def, use.before);
305 assert(costs * remat_bonus < 1000);
306 time += 1000 - (costs * remat_bonus);
313 * Performs the actions necessary to grant the request that:
314 * - new_vals can be held in registers
315 * - as few as possible other values are disposed
316 * - the worst values get disposed
318 * @p is_usage indicates that the values in new_vals are used (not defined)
319 * In this case reloads must be performed
321 static void displace(workset_t *new_vals, int is_usage)
323 ir_node **to_insert = alloca(n_regs * sizeof(to_insert[0]));
324 bool *spilled = alloca(n_regs * sizeof(spilled[0]));
332 /* 1. Identify the number of needed slots and the values to reload */
334 workset_foreach(new_vals, val, iter) {
335 bool reloaded = false;
337 if (! workset_contains(ws, val)) {
338 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
340 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
341 be_add_reload(senv, val, instr, cls, 1);
345 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
347 /* remove the value from the current workset so it is not accidently
349 workset_remove(ws, val);
351 spilled[demand] = reloaded;
352 to_insert[demand] = val;
356 /* 2. Make room for at least 'demand' slots */
357 len = workset_get_length(ws);
358 spills_needed = len + demand - n_regs;
359 assert(spills_needed <= len);
361 /* Only make more free room if we do not have enough */
362 if (spills_needed > 0) {
363 ir_node *curr_bb = NULL;
364 workset_t *ws_start = NULL;
367 curr_bb = get_nodes_block(instr);
368 ws_start = get_block_info(curr_bb)->start_workset;
371 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
373 /* calculate current next-use distance for live values */
374 for (i = 0; i < len; ++i) {
375 ir_node *val = workset_get_val(ws, i);
376 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
377 workset_set_time(ws, i, dist);
380 /* sort entries by increasing nextuse-distance*/
383 for (i = len - spills_needed; i < len; ++i) {
384 ir_node *val = ws->vals[i].node;
386 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
387 workset_get_time(ws, i)));
390 if (!USES_IS_INFINITE(ws->vals[i].time)
391 && !ws->vals[i].spilled) {
392 ir_node *after_pos = sched_prev(instr);
393 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
395 be_add_spill(senv, val, after_pos);
400 /* kill the last 'demand' entries in the array */
401 workset_set_length(ws, len - spills_needed);
404 /* 3. Insert the new values into the workset */
405 for (i = 0; i < demand; ++i) {
406 ir_node *val = to_insert[i];
408 workset_insert(ws, val, spilled[i]);
413 AVAILABLE_EVERYWHERE,
419 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
420 size_t n_pred_worksets,
421 const ir_node *value, bool is_local_phi)
424 bool avail_everywhere = true;
425 bool avail_nowhere = true;
427 assert(n_pred_worksets > 0);
429 /* value available in all preds? */
430 for (i = 0; i < n_pred_worksets; ++i) {
432 const workset_t *p_workset = pred_worksets[i];
433 int p_len = workset_get_length(p_workset);
435 const ir_node *l_value;
438 assert(is_Phi(value));
439 l_value = get_irn_n(value, i);
444 for (p_i = 0; p_i < p_len; ++p_i) {
445 const loc_t *p_l = &p_workset->vals[p_i];
446 if (p_l->node != l_value)
454 avail_nowhere = false;
456 avail_everywhere = false;
460 if (avail_everywhere) {
461 assert(!avail_nowhere);
462 return AVAILABLE_EVERYWHERE;
463 } else if (avail_nowhere) {
464 return AVAILABLE_NOWHERE;
466 return AVAILABLE_PARTLY;
470 /** Decides whether a specific node should be in the start workset or not
472 * @param env belady environment
474 * @param node the node to test
475 * @param loop the loop of the node
477 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
478 ir_loop *loop, unsigned available)
480 be_next_use_t next_use;
483 loc.time = USES_INFINITY;
487 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
488 loc.time = USES_INFINITY;
492 /* We have to keep nonspillable nodes in the workingset */
493 if (arch_irn_get_flags(arch_env, node) & arch_irn_flags_dont_spill) {
495 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
499 next_use = be_get_next_use(uses, first, 0, node, 0);
500 if (USES_IS_INFINITE(next_use.time)) {
501 // the nodes marked as live in shouldn't be dead, so it must be a phi
502 assert(is_Phi(node));
503 loc.time = USES_INFINITY;
504 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
508 loc.time = next_use.time;
510 if (improve_known_preds) {
511 if (available == AVAILABLE_EVERYWHERE) {
512 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
515 } else if(available == AVAILABLE_NOWHERE) {
516 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
518 loc.time = USES_INFINITY;
523 if (!respectloopdepth || next_use.outermost_loop >= get_loop_depth(loop)) {
524 DB((dbg, DBG_START, " %+F taken (%u, loop %d)\n", node, loc.time,
525 next_use.outermost_loop));
527 loc.time = USES_PENDING;
528 DB((dbg, DBG_START, " %+F delayed (outerdepth %d < loopdepth %d)\n",
529 node, next_use.outermost_loop, get_loop_depth(loop)));
536 * Computes the start-workset for a block with multiple predecessors. We assume
537 * that at least 1 of the predeccesors is a back-edge which means we're at the
538 * beginning of a loop. We try to reload as much values as possible now so they
539 * don't get reloaded inside the loop.
541 static void decide_start_workset(const ir_node *block)
543 ir_loop *loop = get_irn_loop(block);
549 int i, len, ws_count;
550 int free_slots, free_pressure_slots;
553 workset_t **pred_worksets;
554 bool all_preds_known;
556 /* check predecessors */
557 arity = get_irn_arity(block);
558 pred_worksets = alloca(sizeof(pred_worksets[0]) * arity);
559 all_preds_known = true;
560 for(i = 0; i < arity; ++i) {
561 ir_node *pred_block = get_Block_cfgpred_block(block, i);
562 block_info_t *pred_info = get_block_info(pred_block);
564 if (pred_info == NULL) {
565 pred_worksets[i] = NULL;
566 all_preds_known = false;
568 pred_worksets[i] = pred_info->end_workset;
572 /* Collect all values living at start of block */
573 starters = NEW_ARR_F(loc_t, 0);
574 delayed = NEW_ARR_F(loc_t, 0);
576 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
577 first = sched_first(block);
579 /* check all Phis first */
580 sched_foreach(block, node) {
585 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
588 if (all_preds_known) {
589 available = available_in_all_preds(pred_worksets, arity, node, true);
591 available = AVAILABLE_UNKNOWN;
594 loc = to_take_or_not_to_take(first, node, loop, available);
596 if (! USES_IS_INFINITE(loc.time)) {
597 if (USES_IS_PENDING(loc.time))
598 ARR_APP1(loc_t, delayed, loc);
600 ARR_APP1(loc_t, starters, loc);
602 be_spill_phi(senv, node);
606 /* check all Live-Ins */
607 be_lv_foreach(lv, block, be_lv_state_in, i) {
608 ir_node *node = be_lv_get_irn(lv, block, i);
611 if (all_preds_known) {
612 available = available_in_all_preds(pred_worksets, arity, node, false);
614 available = AVAILABLE_UNKNOWN;
617 loc = to_take_or_not_to_take(first, node, loop, available);
619 if (! USES_IS_INFINITE(loc.time)) {
620 if (USES_IS_PENDING(loc.time))
621 ARR_APP1(loc_t, delayed, loc);
623 ARR_APP1(loc_t, starters, loc);
627 pressure = be_get_loop_pressure(loop_ana, cls, loop);
628 assert(ARR_LEN(delayed) <= (signed)pressure);
629 free_slots = n_regs - ARR_LEN(starters);
630 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
631 free_slots = MIN(free_slots, free_pressure_slots);
633 /* so far we only put nodes into the starters list that are used inside
634 * the loop. If register pressure in the loop is low then we can take some
635 * values and let them live through the loop */
636 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
637 pressure, free_slots));
638 if (free_slots > 0) {
639 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
641 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
643 loc_t *loc = & delayed[i];
645 /* don't use values which are dead in a known predecessors
646 * to not induce unnecessary reloads */
647 arity = get_irn_arity(block);
648 for (p = 0; p < arity; ++p) {
649 ir_node *pred_block = get_Block_cfgpred_block(block, p);
650 block_info_t *pred_info = get_block_info(pred_block);
652 if (pred_info == NULL)
655 if (!workset_contains(pred_info->end_workset, loc->node)) {
657 " delayed %+F not live at pred %+F\n", loc->node,
663 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
664 ARR_APP1(loc_t, starters, *loc);
672 /* spill phis (the actual phis not just their values) that are in this block
673 * but not in the start workset */
674 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
675 ir_node *node = delayed[i].node;
676 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
679 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
680 be_spill_phi(senv, node);
684 /* Sort start values by first use */
685 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
687 /* Copy the best ones from starters to start workset */
688 ws_count = MIN(ARR_LEN(starters), n_regs);
690 workset_bulk_fill(ws, ws_count, starters);
692 /* spill phis (the actual phis not just their values) that are in this block
693 * but not in the start workset */
694 len = ARR_LEN(starters);
695 for (i = ws_count; i < len; ++i) {
696 ir_node *node = starters[i].node;
697 if (! is_Phi(node) || get_nodes_block(node) != block)
700 DB((dbg, DBG_START, " spilling phi %+F\n", node));
701 be_spill_phi(senv, node);
706 /* determine spill status of the values: If there's 1 pred block (which
707 * is no backedge) where the value is spilled then we must set it to
709 for(i = 0; i < ws_count; ++i) {
710 loc_t *loc = &ws->vals[i];
711 ir_node *value = loc->node;
715 /* phis from this block aren't spilled */
716 if (get_nodes_block(value) == block) {
717 assert(is_Phi(value));
718 loc->spilled = false;
722 /* determine if value was spilled on any predecessor */
724 for(n = 0; n < arity; ++n) {
725 workset_t *pred_workset = pred_worksets[n];
729 if (pred_workset == NULL)
732 p_len = workset_get_length(pred_workset);
733 for(p = 0; p < p_len; ++p) {
734 loc_t *l = &pred_workset->vals[p];
736 if (l->node != value)
746 loc->spilled = spilled;
751 * For the given block @p block, decide for each values
752 * whether it is used from a register or is reloaded
755 static void process_block(ir_node *block)
760 block_info_t *block_info;
763 /* no need to process a block twice */
764 assert(get_block_info(block) == NULL);
766 /* construct start workset */
767 arity = get_Block_n_cfgpreds(block);
769 /* no predecessor -> empty set */
771 } else if (arity == 1) {
772 /* one predecessor, copy it's end workset */
773 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
774 block_info_t *pred_info = get_block_info(pred_block);
776 assert(pred_info != NULL);
777 workset_copy(ws, pred_info->end_workset);
779 /* multiple predecessors, do more advanced magic :) */
780 decide_start_workset(block);
783 DB((dbg, DBG_DECIDE, "\n"));
784 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
786 block_info = new_block_info();
787 set_block_info(block, block_info);
789 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
790 workset_foreach(ws, irn, iter) {
791 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
792 workset_get_time(ws, iter)));
795 block_info->start_workset = workset_clone(ws);
797 /* process the block from start to end */
798 DB((dbg, DBG_WSETS, "Processing...\n"));
800 /* TODO: this leaks (into the obstack)... */
801 new_vals = new_workset();
803 sched_foreach(block, irn) {
805 assert(workset_get_length(ws) <= n_regs);
807 /* Phis are no real instr (see insert_starters()) */
811 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
813 /* set instruction in the workset */
816 /* allocate all values _used_ by this instruction */
817 workset_clear(new_vals);
818 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
819 ir_node *in = get_irn_n(irn, i);
820 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, in))
823 /* (note that "spilled" is irrelevant here) */
824 workset_insert(new_vals, in, false);
826 displace(new_vals, 1);
828 /* allocate all values _defined_ by this instruction */
829 workset_clear(new_vals);
830 if (get_irn_mode(irn) == mode_T) {
831 const ir_edge_t *edge;
833 foreach_out_edge(irn, edge) {
834 ir_node *proj = get_edge_src_irn(edge);
835 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, proj))
837 workset_insert(new_vals, proj, false);
840 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
842 workset_insert(new_vals, irn, false);
844 displace(new_vals, 0);
849 /* Remember end-workset for this block */
850 block_info->end_workset = workset_clone(ws);
851 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
852 workset_foreach(ws, irn, iter)
853 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
854 workset_get_time(ws, iter)));
858 * 'decide' is block-local and makes assumptions
859 * about the set of live-ins. Thus we must adapt the
860 * live-outs to the live-ins at each block-border.
862 static void fix_block_borders(ir_node *block, void *data)
864 workset_t *start_workset;
870 DB((dbg, DBG_FIX, "\n"));
871 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
873 arity = get_irn_arity(block);
874 /* can happen for endless loops */
878 start_workset = get_block_info(block)->start_workset;
880 /* process all pred blocks */
881 for (i = 0; i < arity; ++i) {
882 ir_node *pred = get_Block_cfgpred_block(block, i);
883 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
886 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
888 /* spill all values not used anymore */
889 workset_foreach(pred_end_workset, node, iter) {
893 workset_foreach(start_workset, n2, iter2) {
898 /* note that we do not look at phi inputs, becuase the values
899 * will be either live-end and need no spill or
900 * they have other users in which must be somewhere else in the
907 if (move_spills && be_is_live_in(lv, block, node)
908 && !pred_end_workset->vals[iter].spilled) {
909 ir_node *insert_point;
911 insert_point = be_get_end_of_block_insertion_point(pred);
912 insert_point = sched_prev(insert_point);
914 insert_point = block;
916 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
918 be_add_spill(senv, node, insert_point);
922 /* reload missing values in predecessors, add missing spills */
923 workset_foreach(start_workset, node, iter) {
924 const loc_t *l = &start_workset->vals[iter];
925 const loc_t *pred_loc;
927 /* if node is a phi of the current block we reload
928 * the corresponding argument, else node itself */
929 if (is_Phi(node) && get_nodes_block(node) == block) {
930 node = get_irn_n(node, i);
933 /* we might have unknowns as argument for the phi */
934 if (!arch_irn_consider_in_reg_alloc(arch_env, cls, node))
938 /* check if node is in a register at end of pred */
939 pred_loc = workset_contains(pred_end_workset, node);
940 if (pred_loc != NULL) {
941 /* we might have to spill value on this path */
942 if (move_spills && !pred_loc->spilled && l->spilled) {
943 ir_node *insert_point
944 = be_get_end_of_block_insertion_point(pred);
945 insert_point = sched_prev(insert_point);
946 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
948 be_add_spill(senv, node, insert_point);
951 /* node is not in register at the end of pred -> reload it */
952 DB((dbg, DBG_FIX, " reload %+F\n", node));
953 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
954 be_add_reload_on_edge(senv, node, block, i, cls, 1);
960 static void add_block(ir_node *block, void *data)
963 ARR_APP1(ir_node*, blocklist, block);
966 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
969 ir_graph *irg = be_get_birg_irg(birg);
971 be_liveness_assure_sets(be_assure_liveness(birg));
974 /* construct control flow loop tree */
975 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
976 construct_cf_backedges(irg);
978 stat_ev_tim_pop("belady_time_backedges");
982 stat_ev_tim_pop("belady_time_clear_links");
984 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
986 /* init belady env */
989 arch_env = birg->main_env->arch_env;
991 lv = be_get_birg_liveness(birg);
992 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
994 uses = be_begin_uses(irg, lv);
995 loop_ana = be_new_loop_pressure(birg, cls);
996 senv = be_new_spill_env(birg);
997 blocklist = NEW_ARR_F(ir_node*, 0);
998 irg_block_edges_walk(get_irg_start_block(irg), NULL, add_block, NULL);
999 stat_ev_tim_pop("belady_time_init");
1002 /* walk blocks in reverse postorder */
1003 for (i = ARR_LEN(blocklist) - 1; i >= 0; --i) {
1004 process_block(blocklist[i]);
1006 DEL_ARR_F(blocklist);
1007 stat_ev_tim_pop("belady_time_belady");
1010 /* belady was block-local, fix the global flow by adding reloads on the
1012 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1013 stat_ev_tim_pop("belady_time_fix_borders");
1015 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1017 /* Insert spill/reload nodes into the graph and fix usages */
1018 be_insert_spills_reloads(senv);
1021 be_delete_spill_env(senv);
1023 be_free_loop_pressure(loop_ana);
1024 obstack_free(&obst, NULL);
1027 void be_init_spillbelady(void)
1029 static be_spiller_t belady_spiller = {
1032 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1033 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1034 lc_opt_add_table(belady_group, options);
1036 be_register_spiller("belady", &belady_spiller);
1037 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1040 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);