implemented a function to retrieve estimated costs of an op
[libfirm] / ir / be / bespillbelady.c
index c5a021e..99c5a92 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)
-static firm_dbg_module_t *dbg = NULL;
-
-#define MIN(a,b) (((a)<(b))?(a):(b))
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 typedef struct _workset_t workset_t;
 
@@ -66,8 +65,7 @@ typedef struct _belady_env_t {
        pset *used;                     /**< holds the values used (so far) in the current BB */
        pset *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;
        }
 
@@ -257,10 +255,12 @@ static int is_mem_phi(const ir_node *irn, void *data) {
 static INLINE unsigned get_distance(belady_env_t *bel, const ir_node *from, unsigned from_step, const ir_node *def, int skip_from_uses)
 {
        arch_irn_flags_t fl = arch_irn_get_flags(bel->arch, def);
-       if((fl & (arch_irn_flags_ignore | arch_irn_flags_dont_spill)) != 0)
+       unsigned dist = be_get_next_use(bel->uses, from, from_step, def, skip_from_uses);
+
+       if(!USES_IS_INIFINITE(dist) && (fl & (arch_irn_flags_ignore | arch_irn_flags_dont_spill)) != 0)
                return 0;
-       else
-               return be_get_next_use(bel->uses, from, from_step, def, skip_from_uses);
+
+       return dist;
 }
 
 /**
@@ -292,18 +292,20 @@ 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));
 
-
        /*
         * 2. Make room for at least 'demand' slots
         */
        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 */
@@ -317,14 +319,16 @@ static void displace(belady_env_t *bel, workset_t *new_vals, int is_usage) {
                   before its first usage, remove it from start workset */
                for (i=max_allowed; i<ws->len; ++i) {
                        ir_node *irn = ws->vals[i].irn;
+
                        if (!pset_find_ptr(bel->used, irn)) {
                                ir_node *curr_bb = get_nodes_block(bel->instr);
                                workset_t *ws_start = get_block_info(curr_bb)->ws_start;
                                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 */
@@ -371,7 +375,7 @@ 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)
+       sched_foreach(blk, irn) {
                if (is_Phi(irn) && arch_get_irn_reg_class(bel->arch, irn, -1) == bel->cls) {
                        loc.irn = irn;
                        loc.time = get_distance(bel, first, 0, irn, 0);
@@ -380,15 +384,17 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
                        count++;
                } else
                        break;
+       }
 
-       live_foreach(blk, li)
+       live_foreach(blk, li) {
                if (live_is_in(li) && arch_get_irn_reg_class(bel->arch, li->irn, -1) == bel->cls) {
                        loc.irn = (ir_node *)li->irn;
                        loc.time = get_distance(bel, 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);
@@ -439,7 +445,8 @@ static block_info_t *compute_block_start_info(ir_node *blk, void *env) {
                        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);
+                       /* TODO: Place copies before jumps! */
+                       sched_add_before(sched_last(pred_block), cpy);
                        set_irn_n(irn, o, cpy);
                }
        }
@@ -557,9 +564,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));
@@ -571,77 +579,52 @@ 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;
+       foreach_pset(bel->copies, irn) {
+               ir_node *src;
+               const ir_edge_t *edge, *ne;
 
                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 = get_irn_n(irn, be_pos_Copy_orig);
-               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;
-
-       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)));
-
-               spill = get_irn_n(irn, be_pos_Reload_mem);
 
-               /* remove reload */
-               set_irn_n(irn, 0, new_Bad());
-               sched_remove(irn);
+               src = be_get_Copy_op(irn);
+               foreach_out_edge_safe(irn, edge, ne) {
+                       ir_node *user = get_edge_src_irn(edge);
+                       int user_pos = get_edge_src_pos(edge);
 
-               /* 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;
 
-       dbg = firm_dbg_register("ir.be.spillbelady");
+       FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
 
        /* init belady env */
        obstack_init(&bel.ob);
-       bel.arch    = chordal_env->birg->main_env->arch_env;
-       bel.cls     = chordal_env->cls;
-       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(dbg, chordal_env, is_mem_phi, NULL);
-       bel.reloads = pset_new_ptr_default();
-       bel.copies  = pset_new_ptr_default();
+       bel.arch      = chordal_env->birg->main_env->arch_env;
+       bel.cls       = chordal_env->cls;
+       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);
+       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.copies    = pset_new_ptr_default();
 
        DBG((dbg, LEVEL_1, "running on register class: %s\n", bel.cls->name));
 
@@ -649,13 +632,14 @@ 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);
 
+       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);
 }