Fixed a bug concerning recursion.
[libfirm] / ir / be / bespillbelady2.c
index eba5a1e..0f5ac51 100644 (file)
@@ -78,7 +78,9 @@
 #define DBG_WORKSET 128
 #define DBG_GLOBAL  256
 
-#define DEAD UINT_MAX
+#define DEAD     UINT_MAX
+#define LIVE_END (DEAD-1)
+
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 /**
@@ -89,8 +91,7 @@ typedef struct _loc_t {
   unsigned time;       /**< A use time (see beuses.h). */
   unsigned version;    /**< That is used in the global pass below.
                                                 For usage see the comments below.
-                                                In the local belady pass, this is not
-                                                important. */
+                                                In the local belady pass, this is not important. */
 } loc_t;
 
 typedef struct _workset_t {
@@ -121,7 +122,7 @@ static int loc_compare(const void *a, const void *b)
 {
        const loc_t *p = a;
        const loc_t *q = b;
-       return (int) p->time - (int) q->time;
+       return (p->time > q->time) - (p->time < q->time);
 }
 
 static INLINE void workset_print(const workset_t *w)
@@ -351,6 +352,29 @@ static INLINE void advance_current_use(block_info_t *bi, const ir_node *irn)
        phase_set_irn_data(&bi->next_uses, irn, use->next);
 }
 
+static INLINE unsigned get_curr_distance(block_info_t *bi, const ir_node *irn, int is_usage)
+{
+       belady_env_t *env = bi->bel;
+       next_use_t *use   = get_current_use(bi, irn);
+       int curr_step     = sched_get_time_step(env->instr);
+       int flags         = arch_irn_get_flags(env->arch, irn);
+
+       assert(!(flags & arch_irn_flags_ignore));
+
+       /* We have to keep nonspillable nodes in the workingset */
+       if(flags & arch_irn_flags_dont_spill)
+               return 0;
+
+       if (!is_usage && use && use->step == curr_step)
+               use = use->next;
+
+       if (use) {
+               assert(use->step >= curr_step);
+               return use->step - curr_step;
+       }
+
+       return be_is_live_end(env->lv, bi->bl, irn) ? LIVE_END : DEAD;
+}
 
 static INLINE int is_local_phi(const ir_node *bl, const ir_node *irn)
 {
@@ -427,8 +451,7 @@ static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
                                        be_add_reload(env->senv, val, env->instr, env->cls, 1);
                                }
                        }
-               }
-               else {
+               } else {
                        assert(is_usage || "Defined value already in workset?!?");
                        DBG((dbg, DBG_DECIDE, "    skip %+F\n", val));
                }
@@ -443,20 +466,15 @@ static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
 
        /* Only make more free room if we do not have enough */
        if (len > max_allowed) {
-               int curr_step = sched_get_time_step(env->instr);
+               // int curr_step = sched_get_time_step(env->instr);
 
                DBG((dbg, DBG_DECIDE, "    disposing %d values\n", len - max_allowed));
 
                /* get current next-use distance */
                for (i = 0; i < ws->len; ++i) {
-                       ir_node *val = workset_get_val(ws, i);
-                       next_use_t *use = phase_get_irn_data(&bi->next_uses, val);
-                       assert(use == NULL || use->step >= curr_step);
-
-                       if (!is_usage && use)
-                               use = use->next;
-
-                       workset_set_time(ws, i, use ? (unsigned) (use->step - curr_step) : DEAD);
+                       ir_node *val  = workset_get_val(ws, i);
+                       unsigned dist = get_curr_distance(bi, val, is_usage);
+                       workset_set_time(ws, i, dist);
                }
 
                /* sort entries by increasing nextuse-distance*/
@@ -487,6 +505,7 @@ static void displace(block_info_t *bi, workset_t *new_vals, int is_usage) {
 static void belady(ir_node *block, void *data) {
        belady_env_t *env        = data;
        block_info_t *block_info = new_block_info(env, block);
+       void *obst_state         = obstack_base(&env->ob);
 
        workset_t *new_vals;
        ir_node *irn;
@@ -564,6 +583,7 @@ static void belady(ir_node *block, void *data) {
        }
 
        phase_free(&block_info->next_uses);
+       obstack_free(&env->ob, obst_state);
 
        /* Remember end-workset for this block */
        block_info->ws_end = workset_clone(env, &env->ob, env->ws);
@@ -611,20 +631,41 @@ typedef struct _global_end_state_t {
        unsigned version;
 } global_end_state_t;
 
-static block_end_state_t *get_block_end_state(global_end_state_t *state, ir_node *bl, ir_node *irn)
+typedef struct {
+       void *obst_level;
+       unsigned gauge;
+} rollback_info_t;
+
+static INLINE rollback_info_t trans_begin(global_end_state_t *ges)
+{
+       rollback_info_t rb;
+       rb.obst_level = obstack_base(&ges->obst);
+       rb.gauge      = ges->gauge;
+       return rb;
+}
+
+static INLINE void trans_rollback(global_end_state_t *ges, rollback_info_t *rb)
+{
+       ges->gauge = rb->gauge;
+       obstack_free(&ges->obst, rb->obst_level);
+}
+
+static unsigned get_block_end_state(global_end_state_t *state, ir_node *bl, ir_node *irn)
 {
        unsigned i;
 
        for (i = 0; i < state->gauge; ++i) {
                block_end_state_t *bei = &state->end_info[i];
                if (bei->bl == bl && bei->irn == irn)
-                       return bei;
+                       return i;
        }
 
        {
                block_info_t *bi = get_block_info(bl);
                block_end_state_t *curr;
 
+               i = state->gauge;
+
                /* make sure we have room in the array */
                ARR_EXTO(block_end_state_t, state->end_info, (int) state->gauge);
 
@@ -636,15 +677,16 @@ static block_end_state_t *get_block_end_state(global_end_state_t *state, ir_node
                curr->end_state = workset_clone(state->env, &state->obst, bi->ws_end);
                curr->costs = -1.0;
                ++state->gauge;
-               return curr;
+               return i;
        }
 }
 
-static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level);
+static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level);
 
-static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level)
+static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
 {
-       block_end_state_t *bes = get_block_end_state(ges, bl, irn);
+       unsigned bes_index     = get_block_end_state(ges, bl, irn);
+       block_end_state_t *bes = &ges->end_info[bes_index];
        workset_t *end         = bes->end_state;
        block_info_t *bi       = get_block_info(bl);
        int n_regs             = bi->bel->n_regs;
@@ -735,8 +777,7 @@ static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir
                        DBG((dbg, DBG_GLOBAL, "\t%2Dthe end set has %d free slots\n",
                                                level, n_regs - len));
                        slot = len;
-               }
-               else {
+               } else {
                        for (i = 0; i < len; ++i)
                                if (end->vals[i].version < ges->version)
                                        break;
@@ -749,11 +790,28 @@ static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir
                }
 
                if (slot >= 0) {
-                       int gauge           = ges->gauge;
+                       rollback_info_t rb  = trans_begin(ges);
                        ir_node *ins_before = block_info_get_last_ins(bi);
                        double reload_here  = be_get_reload_costs(bi->bel->senv, irn, ins_before);
-                       double bring_in     = bi->pressure < n_regs ? can_bring_in(ges, bl, irn, level + 1) : HUGE_VAL;
+                       double bring_in     = HUGE_VAL;
 
+                       /* allocate a slot before recursively descending. */
+                       end->vals[slot].irn     = irn;
+                       end->vals[slot].version = ges->version;
+                       end->len = MAX(end->len, slot + 1);
+
+                       /* look if we can bring the value in. */
+                       if (bi->pressure < n_regs) {
+                               double new_limit = MIN(reload_here, limit);
+                               bring_in = can_bring_in(ges, bl, irn, new_limit, level + 1);
+                       }
+
+                       /*
+                        * re-read the bes descriptor since meanwhile, the
+                        * array could have been displaced by recursive calls
+                        */
+                       assert(bes_index < ges->gauge);
+                       bes = &ges->end_info[bes_index];
                        DBG((dbg, DBG_GLOBAL, "\t%2Dthere is a free slot. capacity=%d, reload here=%f, bring in=%f\n",
                                                level, n_regs - bi->pressure, reload_here, bring_in));
 
@@ -763,23 +821,16 @@ static double can_make_available_at_end(global_end_state_t *ges, ir_node *bl, ir
                         * the gauge.
                         */
                        if (reload_here <= bring_in) {
-                               ges->gauge = gauge;
+                               trans_rollback(ges, &rb);
                                bes->costs = reload_here;
                                bes->reload_at_end = 1;
-                       }
-
-                       else {
+                               DBG((dbg, DBG_GLOBAL, "\t%2Ddoing a reload %p\n", level, bes));
+                       } else {
                                bes->live_through = 1;
                                bes->costs = bring_in;
                        }
 
-                       end->vals[slot].irn     = irn;
-                       end->vals[slot].version = ges->version;
-                       end->len = MAX(end->len, slot + 1);
                }
-
-               else
-                       ges->gauge -= 1;
        }
 
 end:
@@ -787,31 +838,41 @@ end:
        return bes->costs;
 }
 
-static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, int level)
+static double can_bring_in(global_end_state_t *ges, ir_node *bl, ir_node *irn, double limit, int level)
 {
+       belady_env_t *env = ges->env;
        double glob_costs = HUGE_VAL;
 
-       DBG((dbg, DBG_GLOBAL, "\t%2Dcan bring in for %+F at block %+F\n", level, irn, bl));
+       DBG((dbg, DBG_GLOBAL, "\t%2Dcan bring in (max %f) for %+F at block %+F\n", level, limit, irn, bl));
 
        if (is_transport_in(bl, irn)) {
                int i, n           = get_irn_arity(bl);
                ir_node **nodes    = alloca(get_irn_arity(bl) * sizeof(nodes[0]));
+               rollback_info_t rb = trans_begin(ges);
 
-               int gauge_begin    = ges->gauge;
 
                glob_costs = 0.0;
                for (i = 0; i < n; ++i) {
                        ir_node *pr = get_Block_cfgpred_block(bl, i);
                        ir_node *op = is_local_phi(bl, irn) ? get_irn_n(irn, i) : irn;
-                       double c    = can_make_available_at_end(ges, pr, op, level + 1);
+                       double c;
+
+                       /*
+                        * there might by unknwons as operands of phis in that case
+                        * we set the costs to zero, since they won't get spilled.
+                        */
+                       if (arch_irn_consider_in_reg_alloc(env->arch, env->cls, op))
+                               c = can_make_available_at_end(ges, pr, op, limit - glob_costs, level + 1);
+                       else
+                               c = 0.0;
+
+                       glob_costs += c;
 
-                       if (c >= HUGE_VAL) {
-                               ges->gauge = gauge_begin;
+                       if (glob_costs >= limit) {
                                glob_costs = HUGE_VAL;
+                               trans_rollback(ges, &rb);
                                goto end;
                        }
-
-                       glob_costs += c;
                }
        }
 
@@ -831,8 +892,8 @@ static void materialize_and_commit_end_state(global_end_state_t *ges)
                block_info_t *bi       = get_block_info(bes->bl);
                int idx, end_pressure;
 
-               DBG((dbg, DBG_GLOBAL, "\t\t%+F in %+F, cost %f through: %d, rel: %d\n",
-                               bes->irn, bes->bl, bes->costs, bes->live_through, bes->reload_at_end));
+               DBG((dbg, DBG_GLOBAL, "\t\t%+F in %+F, cost: %f through: %d, rel: %d %p\n",
+                               bes->irn, bes->bl, bes->costs, bes->live_through, bes->reload_at_end, bes));
 
                /* insert the reload if the val was reloaded at the block's end */
                if (bes->reload_at_end) {
@@ -840,23 +901,6 @@ static void materialize_and_commit_end_state(global_end_state_t *ges)
                        DBG((dbg, DBG_GLOBAL, "\t\tadding reload of %+F at end of %+F\n", bes->irn, bes->bl));
                }
 
-               end_pressure = 0;
-               for (idx = workset_get_length(bes->end_state) - 1; idx >= 0; --idx)
-                       if (bes->end_state->vals[idx].version >= ges->version)
-                               end_pressure += 1;
-
-               /*
-                * if the variable is live through the block,
-                * update the pressure indicator.
-                */
-               DBG((dbg, DBG_GLOBAL, "\t\told pressure %d, ", bi->pressure));
-
-               bi->pressure = MAX(bi->pressure + bes->live_through, end_pressure);
-
-               DBG((dbg, DBG_GLOBAL, "new pressure: %d, end pressure: %d, end length: %d\n",
-                                       bi->pressure, end_pressure, workset_get_length(bes->end_state)));
-
-//             workset_print(bes->end_state);
                idx = workset_get_index(bes->end_state, bes->irn);
 
                if (is_local_phi(bes->bl, bes->irn) && bes->live_through)
@@ -875,6 +919,23 @@ static void materialize_and_commit_end_state(global_end_state_t *ges)
                        bes->end_state->vals[idx].version = ges->version;
                        workset_copy(env, bi->ws_end, bes->end_state);
                }
+
+               end_pressure = 0;
+               for (idx = workset_get_length(bes->end_state) - 1; idx >= 0; --idx)
+                       if (bes->end_state->vals[idx].version >= ges->version)
+                               end_pressure += 1;
+
+               /*
+                * if the variable is live through the block,
+                * update the pressure indicator.
+                */
+               DBG((dbg, DBG_GLOBAL, "\t\told pressure %d, ", bi->pressure));
+
+               bi->pressure = MAX(bi->pressure + bes->live_through, end_pressure);
+
+               DBG((dbg, DBG_GLOBAL, "new pressure: %d, end pressure: %d, end length: %d\n",
+                                       bi->pressure, end_pressure, workset_get_length(bes->end_state)));
+
        }
 }
 
@@ -903,7 +964,7 @@ static void fix_block_borders(global_end_state_t *ges, ir_node *block) {
 
                DBG((dbg, DBG_GLOBAL, "\ttrans in var %+F, version %x\n", irn, ges->version));
 
-               bring_in_costs = can_bring_in(ges, block, irn, 1);
+               bring_in_costs = can_bring_in(ges, block, irn, local_costs, 1);
 
                DBG((dbg, DBG_GLOBAL, "\tbring in: %f, local: %f", bring_in_costs, local_costs));
 
@@ -912,13 +973,10 @@ static void fix_block_borders(global_end_state_t *ges, ir_node *block) {
                 * in a register at the entrance of the block
                 * or it is too costly, so we have to do the reload locally
                 */
-               if (bring_in_costs > local_costs) {
-
+               if (bring_in_costs >= local_costs) {
                        DBG((dbg, DBG_GLOBAL, " -> do local reload\n"));
                        be_add_reload(env->senv, irn, bi->first_non_in, env->cls, 1);
-               }
-
-               else  {
+               } else {
                        /*
                         * if the transport-in was a phi (that is actually used in block)
                         * it will no longer remain and we have to spill it completely.
@@ -973,6 +1031,7 @@ static void global_assign(belady_env_t *env)
                }
        }
 
+       DEL_ARR_F(ges.end_info);
 }
 
 static void collect_blocks(ir_node *bl, void *data)