fixed constraint for SubC
[libfirm] / ir / be / bespillbelady.c
index 1a31dfd..b47f16c 100644 (file)
@@ -54,6 +54,7 @@ typedef struct _workset_t workset_t;
 
 typedef struct _belady_env_t {
        struct obstack ob;
+       const be_chordal_env_t *cenv;
        const arch_env_t *arch;
        const arch_register_class_t *cls;
        int n_regs;                     /** number of regs in this reg-class */
@@ -105,7 +106,7 @@ static INLINE workset_t *workset_clone(belady_env_t *env, struct obstack *ob, wo
 
 /**
  * Do NOT alloc anything. Make @param tgt equal to @param src.
- * returns @param tgt for convinience
+ * returns @param tgt for convenience
  */
 static INLINE workset_t *workset_copy(belady_env_t *env, workset_t *tgt, workset_t *src) {
        size_t size = sizeof(*src) + (env->n_regs)*sizeof(src->vals[0]);
@@ -285,8 +286,8 @@ static void displace(belady_env_t *env, workset_t *new_vals, int is_usage) {
                for (i=max_allowed; i<ws->len; ++i) {
                        ir_node *irn = ws->vals[i].irn;
 
-                        if(is_Phi(irn))
-                            continue;
+            if(is_Phi(irn))
+                continue;
 
                        if (!pset_find_ptr(env->used, irn)) {
                                ir_node *curr_bb = get_nodes_block(env->instr);
@@ -313,13 +314,12 @@ static void displace(belady_env_t *env, workset_t *new_vals, int is_usage) {
 static void belady(ir_node *blk, void *env);
 
 /*
- * Computes set of live-ins for each block with multiple predecessors and
- * places copies in the predecessors when phis get spilled
+ * Computes set of live-ins for each block with multiple predecessors
+ * and notifies spill algorithm which phis need to be spilled
  */
-static void place_copy_walker(ir_node *block, void *data) {
+static void spill_phi_walker(ir_node *block, void *data) {
        belady_env_t *env = data;
        block_info_t *block_info;
-       irn_live_t *li;
        ir_node *first, *irn;
        loc_t loc, *starters;
        int i, len, ws_count;
@@ -347,14 +347,15 @@ static void place_copy_walker(ir_node *block, void *data) {
                DBG((dbg, DBG_START, "    %+F:\n", irn));
        }
 
-       live_foreach(block, li) {
-               if (!live_is_in(li) || !arch_irn_consider_in_reg_alloc(env->arch, env->cls, li->irn))
+       be_lv_foreach(env->cenv->lv, block, be_lv_state_in, i) {
+               ir_node *irn = be_lv_get_irn(env->cenv->lv, block, i);
+               if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, irn))
                        continue;
 
-               loc.irn = (ir_node *)li->irn;
-               loc.time = get_distance(env, first, 0, li->irn, 0);
+               loc.irn = irn;
+               loc.time = get_distance(env, first, 0, irn, 0);
                ARR_APP1(loc_t, starters, loc);
-               DBG((dbg, DBG_START, "    %+F:\n", li->irn));
+               DBG((dbg, DBG_START, "    %+F:\n", irn));
        }
 
        // Sort start values by first use
@@ -558,14 +559,16 @@ void be_spill_belady_spill_env(const be_chordal_env_t *chordal_env, spill_env_t
        belady_env_t env;
 
        FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
+       //firm_dbg_set_mask(dbg, DBG_WSETS);
 
        /* init belady env */
        obstack_init(&env.ob);
+       env.cenv      = chordal_env;
        env.arch      = chordal_env->birg->main_env->arch_env;
        env.cls       = chordal_env->cls;
        env.n_regs    = arch_count_non_ignore_regs(env.arch, env.cls);
        env.ws        = new_workset(&env, &env.ob);
-       env.uses      = be_begin_uses(chordal_env->irg, chordal_env->birg->main_env->arch_env, env.cls);
+       env.uses      = be_begin_uses(chordal_env->irg, chordal_env->lv, chordal_env->birg->main_env->arch_env, env.cls);
        if(spill_env == NULL) {
                env.senv = be_new_spill_env(chordal_env);
        } else {
@@ -575,14 +578,18 @@ void be_spill_belady_spill_env(const be_chordal_env_t *chordal_env, spill_env_t
 
        DBG((dbg, LEVEL_1, "running on register class: %s\n", env.cls->name));
 
-       /* do the work */
        be_clear_links(chordal_env->irg);
-       irg_block_walk_graph(chordal_env->irg, place_copy_walker, NULL, &env);
+       /* Decide which phi nodes will be spilled and place copies for them into the graph */
+       irg_block_walk_graph(chordal_env->irg, spill_phi_walker, NULL, &env);
+       /* Fix high register pressure with belady algorithm */
        irg_block_walk_graph(chordal_env->irg, NULL, belady, &env);
+       /* belady was block-local, fix the global flow by adding reloads on the edges */
        irg_block_walk_graph(chordal_env->irg, fix_block_borders, NULL, &env);
+       /* Insert spill/reload nodes into the graph and fix usages */
        be_insert_spills_reloads(env.senv);
 
        be_remove_dead_nodes_from_schedule(chordal_env->irg);
+       be_liveness_recompute(chordal_env->lv);
 
        /* clean up */
        if(spill_env == NULL)