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
32 #include "irprintf_t.h"
38 #include "iredges_t.h"
41 #include "irnodeset.h"
50 #include "bechordal_t.h"
52 #include "beloopana.h"
54 #include "bespillutil.h"
64 #define DBG_WORKSET 128
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
67 #define TIME_UNDEFINED 6666
69 //#define LOOK_AT_LOOPDEPTH
72 * An association between a node and a point in time.
74 typedef struct loc_t {
76 unsigned time; /**< A use time (see beuses.h). */
77 bool spilled; /**< the value was already spilled on this path */
80 typedef struct _workset_t {
81 int len; /**< current length */
82 loc_t vals[0]; /**< inlined array of the values/distances in this working set */
85 static struct obstack obst;
86 static const arch_register_class_t *cls;
87 static const be_lv_t *lv;
88 static be_loopana_t *loop_ana;
90 static workset_t *ws; /**< the main workset used while
91 processing a block. */
92 static be_uses_t *uses; /**< env for the next-use magic */
93 static ir_node *instr; /**< current instruction */
94 static unsigned instr_nr; /**< current instruction number
95 (relative to block start) */
96 static spill_env_t *senv; /**< see bespill.h */
97 static ir_node **blocklist;
99 static bool move_spills = true;
100 static bool respectloopdepth = true;
101 static bool improve_known_preds = true;
102 /* factor to weight the different costs of reloading/rematerializing a node
103 (see bespill.h be_get_reload_costs_no_weight) */
104 static int remat_bonus = 10;
106 static const lc_opt_table_entry_t options[] = {
107 LC_OPT_ENT_BOOL ("movespills", "try to move spills out of loops", &move_spills),
108 LC_OPT_ENT_BOOL ("respectloopdepth", "exprimental (outermost loop cutting)", &respectloopdepth),
109 LC_OPT_ENT_BOOL ("improveknownpreds", "experimental (known preds cutting)", &improve_known_preds),
110 LC_OPT_ENT_INT ("rematbonus", "give bonus to rematerialisable nodes", &remat_bonus),
114 static int loc_compare(const void *a, const void *b)
118 return p->time - q->time;
121 void workset_print(const workset_t *w)
125 for(i = 0; i < w->len; ++i) {
126 ir_fprintf(stderr, "%+F %d\n", w->vals[i].node, w->vals[i].time);
131 * Alloc a new workset on obstack @p ob with maximum size @p max
133 static workset_t *new_workset(void)
136 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
138 res = obstack_alloc(&obst, size);
139 memset(res, 0, size);
144 * Alloc a new instance on obstack and make it equal to @param workset
146 static workset_t *workset_clone(workset_t *workset)
149 size_t size = sizeof(*res) + n_regs * sizeof(res->vals[0]);
150 res = obstack_alloc(&obst, size);
151 memcpy(res, workset, size);
156 * Copy workset @param src to @param tgt
158 static void workset_copy(workset_t *dest, const workset_t *src)
160 size_t size = sizeof(*src) + n_regs * sizeof(src->vals[0]);
161 memcpy(dest, src, size);
165 * Overwrites the current content array of @param ws with the
166 * @param count locations given at memory @param locs.
167 * Set the length of @param ws to count.
169 static void workset_bulk_fill(workset_t *workset, int count, const loc_t *locs)
171 workset->len = count;
172 memcpy(&(workset->vals[0]), locs, count * sizeof(locs[0]));
176 * Inserts the value @p val into the workset, iff it is not
177 * already contained. The workset must not be full.
179 static void workset_insert(workset_t *workset, ir_node *val, bool spilled)
183 /* check for current regclass */
184 assert(arch_irn_consider_in_reg_alloc(cls, val));
186 /* check if val is already contained */
187 for (i = 0; i < workset->len; ++i) {
188 loc = &workset->vals[i];
189 if (loc->node == val) {
198 assert(workset->len < n_regs && "Workset already full!");
199 loc = &workset->vals[workset->len];
201 loc->spilled = spilled;
202 loc->time = TIME_UNDEFINED;
207 * Removes all entries from this workset
209 static void workset_clear(workset_t *workset)
215 * Removes the value @p val from the workset if present.
217 static inline void workset_remove(workset_t *workset, ir_node *val)
220 for(i = 0; i < workset->len; ++i) {
221 if (workset->vals[i].node == val) {
222 workset->vals[i] = workset->vals[--workset->len];
228 static inline const loc_t *workset_contains(const workset_t *ws,
233 for (i = 0; i < ws->len; ++i) {
234 if (ws->vals[i].node == val)
242 * Iterates over all values in the working set.
243 * @p ws The workset to iterate
244 * @p v A variable to put the current value in
245 * @p i An integer for internal use
247 #define workset_foreach(ws, v, i) for(i=0; \
248 v=(i < ws->len) ? ws->vals[i].node : NULL, i < ws->len; \
251 #define workset_set_time(ws, i, t) (ws)->vals[i].time=t
252 #define workset_get_time(ws, i) (ws)->vals[i].time
253 #define workset_set_length(ws, length) (ws)->len = length
254 #define workset_get_length(ws) ((ws)->len)
255 #define workset_get_val(ws, i) ((ws)->vals[i].node)
256 #define workset_sort(ws) qsort((ws)->vals, (ws)->len, sizeof((ws)->vals[0]), loc_compare);
258 typedef struct _block_info_t
260 workset_t *start_workset;
261 workset_t *end_workset;
265 static void *new_block_info(void)
267 block_info_t *res = obstack_alloc(&obst, sizeof(res[0]));
268 memset(res, 0, sizeof(res[0]));
273 #define get_block_info(block) ((block_info_t *)get_irn_link(block))
274 #define set_block_info(block, info) set_irn_link(block, info)
277 * @return The distance to the next use or 0 if irn has dont_spill flag set
279 static inline unsigned get_distance(ir_node *from, unsigned from_step,
280 const ir_node *def, int skip_from_uses)
286 assert(!arch_irn_is_ignore(def));
288 use = be_get_next_use(uses, from, from_step, def, skip_from_uses);
290 if (USES_IS_INFINITE(time))
291 return USES_INFINITY;
293 /* We have to keep nonspillable nodes in the workingset */
294 if (arch_irn_get_flags(skip_Proj_const(def)) & arch_irn_flags_dont_spill)
297 /* give some bonus to rematerialisable nodes */
298 if (remat_bonus > 0) {
299 costs = be_get_reload_costs_no_weight(senv, def, use.before);
300 assert(costs * remat_bonus < 1000);
301 time += 1000 - (costs * remat_bonus);
308 * Performs the actions necessary to grant the request that:
309 * - new_vals can be held in registers
310 * - as few as possible other values are disposed
311 * - the worst values get disposed
313 * @p is_usage indicates that the values in new_vals are used (not defined)
314 * In this case reloads must be performed
316 static void displace(workset_t *new_vals, int is_usage)
318 ir_node **to_insert = ALLOCAN(ir_node*, n_regs);
319 bool *spilled = ALLOCAN(bool, n_regs);
327 /* 1. Identify the number of needed slots and the values to reload */
329 workset_foreach(new_vals, val, iter) {
330 bool reloaded = false;
332 if (! workset_contains(ws, val)) {
333 DB((dbg, DBG_DECIDE, " insert %+F\n", val));
335 DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr));
336 be_add_reload(senv, val, instr, cls, 1);
340 DB((dbg, DBG_DECIDE, " %+F already in workset\n", val));
342 /* remove the value from the current workset so it is not accidently
344 workset_remove(ws, val);
346 spilled[demand] = reloaded;
347 to_insert[demand] = val;
351 /* 2. Make room for at least 'demand' slots */
352 len = workset_get_length(ws);
353 spills_needed = len + demand - n_regs;
354 assert(spills_needed <= len);
356 /* Only make more free room if we do not have enough */
357 if (spills_needed > 0) {
358 ir_node *curr_bb = NULL;
359 workset_t *ws_start = NULL;
362 curr_bb = get_nodes_block(instr);
363 ws_start = get_block_info(curr_bb)->start_workset;
366 DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed));
368 /* calculate current next-use distance for live values */
369 for (i = 0; i < len; ++i) {
370 ir_node *val = workset_get_val(ws, i);
371 unsigned dist = get_distance(instr, instr_nr, val, !is_usage);
372 workset_set_time(ws, i, dist);
375 /* sort entries by increasing nextuse-distance*/
378 for (i = len - spills_needed; i < len; ++i) {
379 ir_node *val = ws->vals[i].node;
381 DB((dbg, DBG_DECIDE, " disposing node %+F (%u)\n", val,
382 workset_get_time(ws, i)));
385 if (!USES_IS_INFINITE(ws->vals[i].time)
386 && !ws->vals[i].spilled) {
387 ir_node *after_pos = sched_prev(instr);
388 DB((dbg, DBG_DECIDE, "Spill %+F after node %+F\n", val,
390 be_add_spill(senv, val, after_pos);
395 /* kill the last 'demand' entries in the array */
396 workset_set_length(ws, len - spills_needed);
399 /* 3. Insert the new values into the workset */
400 for (i = 0; i < demand; ++i) {
401 ir_node *val = to_insert[i];
403 workset_insert(ws, val, spilled[i]);
408 AVAILABLE_EVERYWHERE,
414 static unsigned available_in_all_preds(workset_t* const* pred_worksets,
415 size_t n_pred_worksets,
416 const ir_node *value, bool is_local_phi)
419 bool avail_everywhere = true;
420 bool avail_nowhere = true;
422 assert(n_pred_worksets > 0);
424 /* value available in all preds? */
425 for (i = 0; i < n_pred_worksets; ++i) {
427 const workset_t *p_workset = pred_worksets[i];
428 int p_len = workset_get_length(p_workset);
430 const ir_node *l_value;
433 assert(is_Phi(value));
434 l_value = get_irn_n(value, i);
439 for (p_i = 0; p_i < p_len; ++p_i) {
440 const loc_t *p_l = &p_workset->vals[p_i];
441 if (p_l->node != l_value)
449 avail_nowhere = false;
451 avail_everywhere = false;
455 if (avail_everywhere) {
456 assert(!avail_nowhere);
457 return AVAILABLE_EVERYWHERE;
458 } else if (avail_nowhere) {
459 return AVAILABLE_NOWHERE;
461 return AVAILABLE_PARTLY;
465 /** Decides whether a specific node should be in the start workset or not
467 * @param env belady environment
469 * @param node the node to test
470 * @param loop the loop of the node
472 static loc_t to_take_or_not_to_take(ir_node* first, ir_node *node,
473 ir_loop *loop, unsigned available)
475 be_next_use_t next_use;
478 loc.time = USES_INFINITY;
482 if (!arch_irn_consider_in_reg_alloc(cls, node)) {
483 loc.time = USES_INFINITY;
487 /* We have to keep nonspillable nodes in the workingset */
488 if (arch_irn_get_flags(skip_Proj_const(node)) & arch_irn_flags_dont_spill) {
490 DB((dbg, DBG_START, " %+F taken (dontspill node)\n", node, loc.time));
494 next_use = be_get_next_use(uses, first, 0, node, 0);
495 if (USES_IS_INFINITE(next_use.time)) {
496 // the nodes marked as live in shouldn't be dead, so it must be a phi
497 assert(is_Phi(node));
498 loc.time = USES_INFINITY;
499 DB((dbg, DBG_START, " %+F not taken (dead)\n", node));
503 loc.time = next_use.time;
505 if (improve_known_preds) {
506 if (available == AVAILABLE_EVERYWHERE) {
507 DB((dbg, DBG_START, " %+F taken (%u, live in all preds)\n",
510 } else if(available == AVAILABLE_NOWHERE) {
511 DB((dbg, DBG_START, " %+F not taken (%u, live in no pred)\n",
513 loc.time = USES_INFINITY;
518 if (!respectloopdepth || 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)));
531 * Computes the start-workset for a block with multiple predecessors. We assume
532 * that at least 1 of the predeccesors is a back-edge which means we're at the
533 * beginning of a loop. We try to reload as much values as possible now so they
534 * don't get reloaded inside the loop.
536 static void decide_start_workset(const ir_node *block)
538 ir_loop *loop = get_irn_loop(block);
544 int i, len, ws_count;
545 int free_slots, free_pressure_slots;
548 workset_t **pred_worksets;
549 bool all_preds_known;
551 /* check predecessors */
552 arity = get_irn_arity(block);
553 pred_worksets = ALLOCAN(workset_t*, arity);
554 all_preds_known = true;
555 for(i = 0; i < arity; ++i) {
556 ir_node *pred_block = get_Block_cfgpred_block(block, i);
557 block_info_t *pred_info = get_block_info(pred_block);
559 if (pred_info == NULL) {
560 pred_worksets[i] = NULL;
561 all_preds_known = false;
563 pred_worksets[i] = pred_info->end_workset;
567 /* Collect all values living at start of block */
568 starters = NEW_ARR_F(loc_t, 0);
569 delayed = NEW_ARR_F(loc_t, 0);
571 DB((dbg, DBG_START, "Living at start of %+F:\n", block));
572 first = sched_first(block);
574 /* check all Phis first */
575 sched_foreach(block, node) {
580 if (!arch_irn_consider_in_reg_alloc(cls, node))
583 if (all_preds_known) {
584 available = available_in_all_preds(pred_worksets, arity, node, true);
586 available = AVAILABLE_UNKNOWN;
589 loc = to_take_or_not_to_take(first, node, loop, available);
591 if (! USES_IS_INFINITE(loc.time)) {
592 if (USES_IS_PENDING(loc.time))
593 ARR_APP1(loc_t, delayed, loc);
595 ARR_APP1(loc_t, starters, loc);
597 be_spill_phi(senv, node);
601 /* check all Live-Ins */
602 be_lv_foreach(lv, block, be_lv_state_in, i) {
603 ir_node *node = be_lv_get_irn(lv, block, i);
606 if (all_preds_known) {
607 available = available_in_all_preds(pred_worksets, arity, node, false);
609 available = AVAILABLE_UNKNOWN;
612 loc = to_take_or_not_to_take(first, node, loop, available);
614 if (! USES_IS_INFINITE(loc.time)) {
615 if (USES_IS_PENDING(loc.time))
616 ARR_APP1(loc_t, delayed, loc);
618 ARR_APP1(loc_t, starters, loc);
622 pressure = be_get_loop_pressure(loop_ana, cls, loop);
623 assert(ARR_LEN(delayed) <= (signed)pressure);
624 free_slots = n_regs - ARR_LEN(starters);
625 free_pressure_slots = n_regs - (pressure - ARR_LEN(delayed));
626 free_slots = MIN(free_slots, free_pressure_slots);
628 /* so far we only put nodes into the starters list that are used inside
629 * the loop. If register pressure in the loop is low then we can take some
630 * values and let them live through the loop */
631 DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
632 pressure, free_slots));
633 if (free_slots > 0) {
634 qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
636 for (i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
638 loc_t *loc = & delayed[i];
640 if (!is_Phi(loc->node)) {
641 /* don't use values which are dead in a known predecessors
642 * to not induce unnecessary reloads */
643 arity = get_irn_arity(block);
644 for (p = 0; p < arity; ++p) {
645 ir_node *pred_block = get_Block_cfgpred_block(block, p);
646 block_info_t *pred_info = get_block_info(pred_block);
648 if (pred_info == NULL)
651 if (!workset_contains(pred_info->end_workset, loc->node)) {
653 " delayed %+F not live at pred %+F\n", loc->node,
660 DB((dbg, DBG_START, " delayed %+F taken\n", loc->node));
661 ARR_APP1(loc_t, starters, *loc);
669 /* spill phis (the actual phis not just their values) that are in this block
670 * but not in the start workset */
671 for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
672 ir_node *node = delayed[i].node;
673 if (node == NULL || !is_Phi(node) || get_nodes_block(node) != block)
676 DB((dbg, DBG_START, " spilling delayed phi %+F\n", node));
677 be_spill_phi(senv, node);
681 /* Sort start values by first use */
682 qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
684 /* Copy the best ones from starters to start workset */
685 ws_count = MIN(ARR_LEN(starters), n_regs);
687 workset_bulk_fill(ws, ws_count, starters);
689 /* spill phis (the actual phis not just their values) that are in this block
690 * but not in the start workset */
691 len = ARR_LEN(starters);
692 for (i = ws_count; i < len; ++i) {
693 ir_node *node = starters[i].node;
694 if (! is_Phi(node) || get_nodes_block(node) != block)
697 DB((dbg, DBG_START, " spilling phi %+F\n", node));
698 be_spill_phi(senv, node);
703 /* determine spill status of the values: If there's 1 pred block (which
704 * is no backedge) where the value is spilled then we must set it to
706 for(i = 0; i < ws_count; ++i) {
707 loc_t *loc = &ws->vals[i];
708 ir_node *value = loc->node;
712 /* phis from this block aren't spilled */
713 if (get_nodes_block(value) == block) {
714 assert(is_Phi(value));
715 loc->spilled = false;
719 /* determine if value was spilled on any predecessor */
721 for(n = 0; n < arity; ++n) {
722 workset_t *pred_workset = pred_worksets[n];
726 if (pred_workset == NULL)
729 p_len = workset_get_length(pred_workset);
730 for(p = 0; p < p_len; ++p) {
731 loc_t *l = &pred_workset->vals[p];
733 if (l->node != value)
743 loc->spilled = spilled;
748 * For the given block @p block, decide for each values
749 * whether it is used from a register or is reloaded
752 static void process_block(ir_node *block)
757 block_info_t *block_info;
760 /* no need to process a block twice */
761 assert(get_block_info(block) == NULL);
763 /* construct start workset */
764 arity = get_Block_n_cfgpreds(block);
766 /* no predecessor -> empty set */
768 } else if (arity == 1) {
769 /* one predecessor, copy it's end workset */
770 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
771 block_info_t *pred_info = get_block_info(pred_block);
773 assert(pred_info != NULL);
774 workset_copy(ws, pred_info->end_workset);
776 /* multiple predecessors, do more advanced magic :) */
777 decide_start_workset(block);
780 DB((dbg, DBG_DECIDE, "\n"));
781 DB((dbg, DBG_DECIDE, "Decide for %+F\n", block));
783 block_info = new_block_info();
784 set_block_info(block, block_info);
786 DB((dbg, DBG_WSETS, "Start workset for %+F:\n", block));
787 workset_foreach(ws, irn, iter) {
788 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
789 workset_get_time(ws, iter)));
792 block_info->start_workset = workset_clone(ws);
794 /* process the block from start to end */
795 DB((dbg, DBG_WSETS, "Processing...\n"));
797 /* TODO: this leaks (into the obstack)... */
798 new_vals = new_workset();
800 sched_foreach(block, irn) {
802 assert(workset_get_length(ws) <= n_regs);
804 /* Phis are no real instr (see insert_starters()) */
808 DB((dbg, DBG_DECIDE, " ...%+F\n", irn));
810 /* set instruction in the workset */
813 /* allocate all values _used_ by this instruction */
814 workset_clear(new_vals);
815 for(i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
816 ir_node *in = get_irn_n(irn, i);
817 if (!arch_irn_consider_in_reg_alloc(cls, in))
820 /* (note that "spilled" is irrelevant here) */
821 workset_insert(new_vals, in, false);
823 displace(new_vals, 1);
825 /* allocate all values _defined_ by this instruction */
826 workset_clear(new_vals);
827 if (get_irn_mode(irn) == mode_T) {
828 const ir_edge_t *edge;
830 foreach_out_edge(irn, edge) {
831 ir_node *proj = get_edge_src_irn(edge);
832 if (!arch_irn_consider_in_reg_alloc(cls, proj))
834 workset_insert(new_vals, proj, false);
837 if (!arch_irn_consider_in_reg_alloc(cls, irn))
839 workset_insert(new_vals, irn, false);
841 displace(new_vals, 0);
846 /* Remember end-workset for this block */
847 block_info->end_workset = workset_clone(ws);
848 DB((dbg, DBG_WSETS, "End workset for %+F:\n", block));
849 workset_foreach(ws, irn, iter)
850 DB((dbg, DBG_WSETS, " %+F (%u)\n", irn,
851 workset_get_time(ws, iter)));
855 * 'decide' is block-local and makes assumptions
856 * about the set of live-ins. Thus we must adapt the
857 * live-outs to the live-ins at each block-border.
859 static void fix_block_borders(ir_node *block, void *data)
861 workset_t *start_workset;
867 DB((dbg, DBG_FIX, "\n"));
868 DB((dbg, DBG_FIX, "Fixing %+F\n", block));
870 arity = get_irn_arity(block);
871 /* can happen for endless loops */
875 start_workset = get_block_info(block)->start_workset;
877 /* process all pred blocks */
878 for (i = 0; i < arity; ++i) {
879 ir_node *pred = get_Block_cfgpred_block(block, i);
880 workset_t *pred_end_workset = get_block_info(pred)->end_workset;
883 DB((dbg, DBG_FIX, " Pred %+F\n", pred));
885 /* spill all values not used anymore */
886 workset_foreach(pred_end_workset, node, iter) {
890 workset_foreach(start_workset, n2, iter2) {
895 /* note that we do not look at phi inputs, becuase the values
896 * will be either live-end and need no spill or
897 * they have other users in which must be somewhere else in the
904 if (move_spills && be_is_live_in(lv, block, node)
905 && !pred_end_workset->vals[iter].spilled) {
906 ir_node *insert_point;
908 insert_point = be_get_end_of_block_insertion_point(pred);
909 insert_point = sched_prev(insert_point);
911 insert_point = block;
913 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
915 be_add_spill(senv, node, insert_point);
919 /* reload missing values in predecessors, add missing spills */
920 workset_foreach(start_workset, node, iter) {
921 const loc_t *l = &start_workset->vals[iter];
922 const loc_t *pred_loc;
924 /* if node is a phi of the current block we reload
925 * the corresponding argument, else node itself */
926 if (is_Phi(node) && get_nodes_block(node) == block) {
927 node = get_irn_n(node, i);
930 /* we might have unknowns as argument for the phi */
931 if (!arch_irn_consider_in_reg_alloc(cls, node))
935 /* check if node is in a register at end of pred */
936 pred_loc = workset_contains(pred_end_workset, node);
937 if (pred_loc != NULL) {
938 /* we might have to spill value on this path */
939 if (move_spills && !pred_loc->spilled && l->spilled) {
940 ir_node *insert_point
941 = be_get_end_of_block_insertion_point(pred);
942 insert_point = sched_prev(insert_point);
943 DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node,
945 be_add_spill(senv, node, insert_point);
948 /* node is not in register at the end of pred -> reload it */
949 DB((dbg, DBG_FIX, " reload %+F\n", node));
950 DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i));
951 be_add_reload_on_edge(senv, node, block, i, cls, 1);
957 static void add_block(ir_node *block, void *data)
960 ARR_APP1(ir_node*, blocklist, block);
963 static void be_spill_belady(be_irg_t *birg, const arch_register_class_t *rcls)
966 ir_graph *irg = be_get_birg_irg(birg);
968 be_liveness_assure_sets(be_assure_liveness(birg));
971 /* construct control flow loop tree */
972 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
973 construct_cf_backedges(irg);
975 stat_ev_tim_pop("belady_time_backedges");
979 stat_ev_tim_pop("belady_time_clear_links");
981 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
983 /* init belady env */
987 lv = be_get_birg_liveness(birg);
988 n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
990 uses = be_begin_uses(irg, lv);
991 loop_ana = be_new_loop_pressure(birg, cls);
992 senv = be_new_spill_env(birg);
993 blocklist = NEW_ARR_F(ir_node*, 0);
994 irg_block_edges_walk(get_irg_start_block(irg), NULL, add_block, NULL);
995 stat_ev_tim_pop("belady_time_init");
998 /* walk blocks in reverse postorder */
999 for (i = ARR_LEN(blocklist) - 1; i >= 0; --i) {
1000 process_block(blocklist[i]);
1002 DEL_ARR_F(blocklist);
1003 stat_ev_tim_pop("belady_time_belady");
1006 /* belady was block-local, fix the global flow by adding reloads on the
1008 irg_block_walk_graph(irg, fix_block_borders, NULL, NULL);
1009 stat_ev_tim_pop("belady_time_fix_borders");
1011 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1013 /* Insert spill/reload nodes into the graph and fix usages */
1014 be_insert_spills_reloads(senv);
1017 be_delete_spill_env(senv);
1019 be_free_loop_pressure(loop_ana);
1020 obstack_free(&obst, NULL);
1023 void be_init_spillbelady(void)
1025 static be_spiller_t belady_spiller = {
1028 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1029 lc_opt_entry_t *belady_group = lc_opt_get_grp(be_grp, "belady");
1030 lc_opt_add_table(belady_group, options);
1032 be_register_spiller("belady", &belady_spiller);
1033 FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
1036 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);