- belady spiller places its copy nodes smarter now
[libfirm] / ir / be / bespillbelady.c
index ed734c0..cd9d4b0 100644 (file)
 #define DBG_START  16
 #define DBG_SLOTS  32
 #define DBG_TRACE  64
+#define DBG_WORKSET 128
 #define DEBUG_LVL 0 //(DBG_START | DBG_DECIDE | DBG_WSETS | DBG_FIX | DBG_SPILL)
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
-#define MIN(a,b) (((a)<(b))?(a):(b))
-
 typedef struct _workset_t workset_t;
 
 typedef struct _belady_env_t {
@@ -64,10 +63,9 @@ typedef struct _belady_env_t {
        ir_node *instr;         /**< current instruction */
        unsigned instr_nr;      /**< current instruction number (relative to block start) */
        pset *used;                     /**< holds the values used (so far) in the current BB */
-       pset *copies;           /**< holds all copies placed due to phi-spilling */
+       ir_node **copies;       /**< holds all copies placed due to phi-spilling */
 
-       spill_env_t *senv;      /* see bespill.h */
-       pset *reloads;          /**< all reload nodes placed */
+       spill_env_t *senv;      /**< see bespill.h */
 } belady_env_t;
 
 struct _workset_t {
@@ -134,7 +132,7 @@ static INLINE void workset_insert(workset_t *ws, ir_node *val) {
        int i;
        /* check for current regclass */
        if (arch_get_irn_reg_class(ws->bel->arch, val, -1) != ws->bel->cls) {
-               DBG((dbg, DBG_DECIDE, "Dropped %+F\n", val));
+               DBG((dbg, DBG_WORKSET, "Dropped %+F\n", val));
                return;
        }
 
@@ -294,8 +292,9 @@ static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
                        to_insert[demand++] = val;
                        if (is_usage)
                                be_add_reload(bel->senv, val, bel->instr);
-               } else
+               } else {
                        DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
+               }
        }
        DBG((dbg, DBG_DECIDE, "    demand = %d\n", demand));
 
@@ -305,6 +304,8 @@ static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
        len = workset_get_length(ws);
        max_allowed = bel->n_regs - demand;
 
+       DBG((dbg, DBG_DECIDE, "    disposing %d values\n", ws->len - max_allowed));
+
        /* Only make more free room if we do not have enough */
        if (len > max_allowed) {
                /* get current next-use distance */
@@ -325,8 +326,9 @@ static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
                                workset_remove(ws_start, irn);
 
                                DBG((dbg, DBG_DECIDE, "    dispose %+F dumb\n", irn));
-                       } else
+                       } else {
                                DBG((dbg, DBG_DECIDE, "    dispose %+F\n", irn));
+                       }
                }
 
                /* kill the last 'demand' entries in the array */
@@ -341,6 +343,47 @@ static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
 
 static void belady(ir_node *blk, void *env);
 
+/**
+ * Inserts a spill of a value at the earliest possible location in a block.
+ * That is after the last use of the value or at the beginning of the block if
+ * there is no use
+ */
+static ir_node *insert_copy(belady_env_t *env, ir_node *block, ir_node *value) {
+       ir_node* node;
+       ir_graph *irg = get_irn_irg(block);
+       ir_node *copy = be_new_Copy(env->cls, irg, block, value);
+
+       ARR_APP1(ir_node*, env->copies, copy);
+
+       // walk schedule backwards until we find a usage, or until we have reached the first phi
+       // TODO can we do this faster somehow? This makes insert_copy O(n) in block_size...
+       sched_foreach_reverse(block, node) {
+               int i, arity;
+
+               if(is_Phi(node)) {
+                       sched_add_after(node, copy);
+                       goto placed;
+               }
+               if(value == node) {
+                       sched_add_after(node, copy);
+                       goto placed;
+               }
+               for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
+                       ir_node *arg = get_irn_n(node, i);
+                       if(arg == value) {
+                               sched_add_after(node, copy);
+                               goto placed;
+                       }
+               }
+       }
+       // we didn't find a use or a phi yet, so place the copy at the beginning of the block
+       sched_add_before(sched_first(block), copy);
+
+placed:
+
+       return copy;
+}
+
 /**
  * Collects all values live-in at block @p blk and all phi results in this block.
  * Then it adds the best values (at most n_regs) to the blocks start_workset.
@@ -348,8 +391,8 @@ static void belady(ir_node *blk, void *env);
  *  their args to break interference and make it possible to spill them to the
  *  same spill slot.
  */
-static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
-       belady_env_t *bel = env;
+static block_info_t *compute_block_start_info(ir_node *blk, void *data) {
+       belady_env_t *env = data;
        ir_node *irn, *first;
        irn_live_t *li;
        int i, count, ws_count;
@@ -363,7 +406,7 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
                return res;
 
        /* Create the block info for this block. */
-       res = new_block_info(&bel->ob);
+       res = new_block_info(&env->ob);
        set_block_info(blk, res);
 
 
@@ -373,24 +416,26 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
        DBG((dbg, DBG_START, "Living at start of %+F:\n", blk));
        first = sched_first(blk);
        count = 0;
-       sched_foreach(blk, irn)
-               if (is_Phi(irn) && arch_get_irn_reg_class(bel->arch, irn, -1) == bel->cls) {
+       sched_foreach(blk, irn) {
+               if (is_Phi(irn) && arch_get_irn_reg_class(env->arch, irn, -1) == env->cls) {
                        loc.irn = irn;
-                       loc.time = get_distance(bel, first, 0, irn, 0);
+                       loc.time = get_distance(env, first, 0, irn, 0);
                        obstack_grow(&ob, &loc, sizeof(loc));
                        DBG((dbg, DBG_START, "    %+F:\n", irn));
                        count++;
                } else
                        break;
+       }
 
-       live_foreach(blk, li)
-               if (live_is_in(li) && arch_get_irn_reg_class(bel->arch, li->irn, -1) == bel->cls) {
+       live_foreach(blk, li) {
+               if (live_is_in(li) && arch_get_irn_reg_class(env->arch, li->irn, -1) == env->cls) {
                        loc.irn = (ir_node *)li->irn;
-                       loc.time = get_distance(bel, first, 0, li->irn, 0);
+                       loc.time = get_distance(env, first, 0, li->irn, 0);
                        obstack_grow(&ob, &loc, sizeof(loc));
-                       DBG((dbg, DBG_START, "    %+F:\n", irn));
+                       DBG((dbg, DBG_START, "    %+F:\n", li->irn));
                        count++;
                }
+       }
 
        starters = obstack_finish(&ob);
        qsort(starters, count, sizeof(starters[0]), loc_compare);
@@ -403,25 +448,26 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
 
                /* if pred block has not been processed yet, do it now */
                if (! pred_info) {
-                       belady(pred_blk, bel);
+                       belady(pred_blk, env);
                        pred_info = get_block_info(pred_blk);
                }
 
                /* now we have an end_set of pred */
                assert(pred_info->ws_end && "The recursive call (above) is supposed to compute an end_set");
-               res->ws_start = workset_clone(&bel->ob, pred_info->ws_end);
+               res->ws_start = workset_clone(&env->ob, pred_info->ws_end);
 
        } else
 
        /* Else we want the start_set to be the values used 'the closest' */
        {
                /* Copy the best ones from starters to start workset */
-               ws_count = MIN(count, bel->n_regs);
-               res->ws_start = new_workset(&bel->ob, bel);
+               ws_count = MIN(count, env->n_regs);
+               res->ws_start = new_workset(&env->ob, env);
                workset_bulk_fill(res->ws_start, ws_count, starters);
        }
 
 
+
        /* The phis of this block which are not in the start set have to be spilled later.
         * Therefore we add temporary copies in the pred_blocks so the spills can spill
         * into the same spill slot.
@@ -436,13 +482,11 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
                DBG((dbg, DBG_START, "For %+F:\n", irn));
 
                for (max=get_irn_arity(irn), o=0; o<max; ++o) {
-                       ir_node *arg = get_irn_n(irn, o);
                        ir_node *pred_block = get_Block_cfgpred_block(get_nodes_block(irn), o);
-                       ir_node *cpy = be_new_Copy(bel->cls, irg, pred_block, arg);
-                       pset_insert_ptr(bel->copies, cpy);
-                       DBG((dbg, DBG_START, "    place a %+F of %+F in %+F\n", cpy, arg, pred_block));
-                       sched_add_before(pred_block, cpy);
-                       set_irn_n(irn, o, cpy);
+                       ir_node *arg = get_irn_n(irn, o);
+                       ir_node* copy = insert_copy(env, pred_block, arg);
+
+                       set_irn_n(irn, o, copy);
                }
        }
 
@@ -559,9 +603,10 @@ static void fix_block_borders(ir_node *blk, void *env) {
                                continue;
 
                        /* check if irnb is in a register at end of pred */
-                       workset_foreach(wsp, irnp, iter2)
+                       workset_foreach(wsp, irnp, iter2) {
                                if (irnb == irnp)
                                        goto next_value;
+                       }
 
                        /* irnb is in memory at the end of pred, so we have to reload it */
                        DBG((dbg, DBG_FIX, "    reload %+F\n", irnb));
@@ -573,69 +618,37 @@ next_value:
        }
 }
 
-/**
- * Removes all used reloads from bel->reloads.
- * The remaining nodes in bel->reloads will be removed from the graph.
- */
-static void rescue_used_reloads(ir_node *irn, void *env) {
-       pset *rlds = (pset *)env;
-       if (pset_find_ptr(rlds, irn))
-               pset_remove_ptr(rlds, irn);
-}
-
 /**
  * Removes all copies introduced for phi-spills
  */
-static void remove_copies(belady_env_t *bel) {
-       ir_node *irn;
-
-       for (irn = pset_first(bel->copies); irn; irn = pset_next(bel->copies)) {
-               ir_node *src, *user;
-
-               assert(be_is_Copy(irn));
-               assert(get_irn_n_edges(irn) == 1 && "This is not a copy introduced in 'compute_block_start_info()'. Who created it?");
-
-               user = get_irn_edge(get_irn_irg(irn), irn, 0)->src;
-
-               src = be_get_Copy_op(irn);
-               set_irn_n(user, 0, src);
-       }
-}
-
-/**
- * Finds all unused reloads and remove them from the schedule
- * Also removes spills if they are not used anymore after removing reloads
- */
-static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
-       ir_node *irn;
+static void remove_copies(belady_env_t *env) {
+       int i;
 
-       irg_walk_graph(irg, rescue_used_reloads, NULL, bel->reloads);
-       for(irn = pset_first(bel->reloads); irn; irn = pset_next(bel->reloads)) {
-               ir_node *spill;
-               DBG((dbg, DBG_SPILL, "Removing %+F before %+F in %+F\n", irn, sched_next(irn), get_nodes_block(irn)));
+       for(i = 0; i < ARR_LEN(env->copies); ++i) {
+               ir_node *node = env->copies[i];
+               ir_node *src;
+               const ir_edge_t *edge, *ne;
 
-               if (be_is_Reload(irn))
-                       spill = get_irn_n(irn, be_pos_Reload_mem);
+               assert(be_is_Copy(node));
 
-               /* remove reload */
-               set_irn_n(irn, 0, new_Bad());
-               sched_remove(irn);
+               src = be_get_Copy_op(node);
+               foreach_out_edge_safe(node, edge, ne) {
+                       ir_node *user = get_edge_src_irn(edge);
+                       int user_pos = get_edge_src_pos(edge);
 
-               if (be_is_Reload(irn)) {
-                       /* if spill not used anymore, remove it too
-                        * test of regclass is necessary since spill may be a phi-M */
-                       if (get_irn_n_edges(spill) == 0 && bel->cls == arch_get_irn_reg_class(bel->arch, spill, -1)) {
-                               set_irn_n(spill, 0, new_Bad());
-                               sched_remove(spill);
-                       }
+                       set_irn_n(user, user_pos, src);
                }
        }
 }
 
 void be_spill_belady(const be_chordal_env_t *chordal_env) {
+       be_spill_belady_spill_env(chordal_env, NULL);
+}
+
+void be_spill_belady_spill_env(const be_chordal_env_t *chordal_env, spill_env_t *spill_env) {
        belady_env_t bel;
 
-       FIRM_DBG_REGISTER(dbg, "ir.be.spillbelady");
+       FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
 
        /* init belady env */
        obstack_init(&bel.ob);
@@ -644,10 +657,14 @@ void be_spill_belady(const be_chordal_env_t *chordal_env) {
        bel.n_regs    = arch_register_class_n_regs(bel.cls);
        bel.ws        = new_workset(&bel.ob, &bel);
        bel.uses      = be_begin_uses(chordal_env->irg, chordal_env->birg->main_env->arch_env, bel.cls);
-       bel.senv      = be_new_spill_env(chordal_env, is_mem_phi, NULL);
+       if(spill_env == NULL) {
+               bel.senv = be_new_spill_env(chordal_env, is_mem_phi, NULL);
+       } else {
+               bel.senv = spill_env;
+               be_set_is_spilled_phi(bel.senv, is_mem_phi, NULL);
+       }
        DEBUG_ONLY(be_set_spill_env_dbg_module(bel.senv, dbg);)
-       bel.reloads   = pset_new_ptr_default();
-       bel.copies    = pset_new_ptr_default();
+       bel.copies    = NEW_ARR_F(ir_node*, 0);
 
        DBG((dbg, LEVEL_1, "running on register class: %s\n", bel.cls->name));
 
@@ -655,13 +672,15 @@ void be_spill_belady(const be_chordal_env_t *chordal_env) {
        be_clear_links(chordal_env->irg);
        irg_block_walk_graph(chordal_env->irg, NULL, belady, &bel);
        irg_block_walk_graph(chordal_env->irg, fix_block_borders, NULL, &bel);
-       be_insert_spills_reloads(bel.senv, bel.reloads);
-       remove_unused_reloads(chordal_env->irg, &bel);
+       be_insert_spills_reloads(bel.senv);
        remove_copies(&bel);
+       DEL_ARR_F(bel.copies);
+
+       be_remove_dead_nodes_from_schedule(chordal_env->irg);
 
        /* clean up */
-       del_pset(bel.reloads);
-       be_delete_spill_env(bel.senv);
+       if(spill_env == NULL)
+               be_delete_spill_env(bel.senv);
        be_end_uses(bel.uses);
        obstack_free(&bel.ob, NULL);
 }