values may die at every use
[libfirm] / ir / be / bespillmorgan.c
index 069c4cf..1276581 100644 (file)
 
 #include "bespillmorgan.h"
 
-#include "bechordal.h"
 #include "bechordal_t.h"
 #include "bespill.h"
-#include "belive.h"
 #include "belive_t.h"
-#include "beinsn_t.h"
 #include "irgwalk.h"
 #include "besched.h"
 #include "beutil.h"
-#include "beuses.h"
-#include "interval_analysis.h"
-#include "irloop.h"
 #include "irloop_t.h"
-#include "irgraph.h"
 #include "irgraph_t.h"
-#include "irphase.h"
 #include "irphase_t.h"
 #include "irprintf.h"
 
@@ -45,9 +37,7 @@ typedef struct _morgan_env_t {
        // maximum safe register pressure
        int registers_available;
 
-       be_insn_env_t insn_env;
        spill_env_t *senv;
-       be_uses_t *uses;
 
        set *loop_attr_set;
        set *block_attr_set;
@@ -139,13 +129,15 @@ static INLINE block_attr_t *get_block_attr(morgan_env_t *env, ir_node *block) {
        return res;
 }
 
-static int is_mem_phi(const ir_node *irn, void *data) {
-       // TODO what is this for?
-       return 0;
-}
-
 //---------------------------------------------------------------------------
 
+static INLINE int consider_for_spilling(const arch_env_t *env, const arch_register_class_t *cls, const ir_node *node) {
+       if(!arch_irn_has_reg_class(env, node, -1, cls))
+               return 0;
+
+       return !(arch_irn_get_flags(env, node) & (arch_irn_flags_ignore | arch_irn_flags_dont_spill));
+}
+
 /**
  * Determine edges going out of a loop (= edges that go to a block that is not inside
  * the loop or one of its subloops)
@@ -171,26 +163,42 @@ static INLINE void construct_loop_out_edges(ir_node* block, void* e) {
        }
 }
 
+static void free_loop_out_edges(morgan_env_t *env) {
+       loop_attr_t *l_attr;
+
+       for(l_attr = set_first(env->loop_attr_set); l_attr != NULL; l_attr = set_next(env->loop_attr_set)) {
+               del_set(l_attr->out_edges);
+       }
+}
+
 /**
- * Construct the livethrough unused information for a block
+ * Debugging help, shows all nodes in a (node-)bitset
  */
-static bitset_t *construct_block_livethrough_unused(morgan_env_t* env, ir_node* block) {
+static void show_nodebitset(ir_graph* irg, bitset_t* bitset) {
        int i;
-       int node_idx;
-       ir_node *irn;
-       block_attr_t *block_attr = get_block_attr(env, block);
 
-       /*
-        * This is the first block in a sequence, all variables that are livethrough this block are potential
-        * candidates for livethrough_unused
-        */
+       bitset_foreach(bitset, i) {
+               ir_node* node = get_idx_irn(irg, i);
+               DBG((dbg, DBG_LIVE, "\t%+F\n", node));
+       }
+}
+
+/**
+ * Construct the livethrough unused set for a block
+ */
+static bitset_t *construct_block_livethrough_unused(morgan_env_t* env, ir_node* block) {
+       block_attr_t *block_attr = get_block_attr(env, block);
        irn_live_t *li;
+       ir_node *node;
 
+       DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block)));
        // copy all live-outs into the livethrough_unused set
        live_foreach(block, li) {
+               int node_idx;
+
                if(!live_is_in(li) || !live_is_out(li))
                        continue;
-               if(!arch_irn_consider_in_reg_alloc(env->arch, env->cls, li->irn))
+               if(!consider_for_spilling(env->arch, env->cls, li->irn))
                        continue;
 
                node_idx = get_irn_idx(li->irn);
@@ -201,31 +209,22 @@ static bitset_t *construct_block_livethrough_unused(morgan_env_t* env, ir_node*
         * All values that are used within the block are not unused (and therefore not
         * livethrough_unused)
         */
-       sched_foreach(block, irn) {
-               be_insn_t *insn = be_scan_insn(&env->insn_env, irn);
+       sched_foreach(block, node) {
+               int i, arity;
 
-               for(i = insn->use_start; i < insn->n_ops; ++i) {
-                       const be_operand_t *op = &insn->ops[i];
-                       int idx = get_irn_idx(op->irn);
+               for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
+                       int idx = get_irn_idx(get_irn_n(node, i));
                        bitset_clear(block_attr->livethrough_unused, idx);
                }
        }
 
+       show_nodebitset(env->irg, block_attr->livethrough_unused);
        return block_attr->livethrough_unused;
 }
 
 /**
- * Debugging help, shows all nodes in a (node-)bitset
+ * Construct the livethrough unused set for a loop (and all its subloops+blocks)
  */
-static void show_nodebitset(ir_graph* irg, bitset_t* bitset) {
-       int i;
-
-       bitset_foreach(bitset, i) {
-               ir_node* node = get_idx_irn(irg, i);
-               DBG((dbg, DBG_LIVE, "\t%+F\n", node));
-       }
-}
-
 static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, ir_loop *loop) {
        int i;
        loop_attr_t* loop_attr = get_loop_attr(env, loop);
@@ -262,6 +261,7 @@ static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, ir_loop *l
                        break;
                }
     }
+       DBG((dbg, DBG_LIVE, "Done with loop %d\n", loop->loop_nr));
 
        // remove all unused livethroughs that are remembered for this loop from child loops and blocks
        for(i = 0; i < get_loop_n_elements(loop); ++i) {
@@ -310,7 +310,7 @@ static int reduce_register_pressure_in_block(morgan_env_t *env, ir_node* block,
         */
        sched_foreach_reverse(block, irn) {
                // do we need more spills than possible with unused libethroughs?
-               int spills_needed = pressure - unused_spills_possible - env->registers_available;
+               int spills_needed = pressure - env->registers_available - unused_spills_possible;
                if(spills_needed > 0) {
                        DBG((dbg, DBG_PRESSURE, "\tWARNING %d more spills needed at %+F\n", spills_needed, irn));
                        // TODO further spills needed
@@ -327,14 +327,12 @@ static int reduce_register_pressure_in_block(morgan_env_t *env, ir_node* block,
                        break;
 
                // update pressure
-               {
-                       int pressure_old = pressure;
-                       be_liveness_transfer(env->arch, env->cls, irn, live_nodes);
-                       pressure = pset_count(live_nodes);
-                       DBG((dbg, DBG_PRESSURE, "\tPressure at %+F - before: %d after: %d\n", irn, pressure_old, pressure));
-               }
+               be_liveness_transfer(env->arch, env->cls, irn, live_nodes);
+               pressure = pset_count(live_nodes);
        }
 
+       DBG((dbg, DBG_PRESSURE, "\tMax Pressure in %+F: %d\n", block, max_pressure));
+
        /*
         * Calculate number of spills from loop_unused_spills_possible that we want to use,
         * and spill unused livethroughs from the block if we still don't have enough registers
@@ -368,6 +366,7 @@ static int reduce_register_pressure_in_block(morgan_env_t *env, ir_node* block,
                                DBG((dbg, DBG_PRESSURE, "Spilling node %+F around block %+F\n", to_spill, block));
                                be_add_reload_on_edge(env->senv, to_spill, edge->src, edge->pos);
                        }
+                       spills++;
                }
        } else {
                loop_unused_spills_needed = spills_needed;
@@ -451,16 +450,13 @@ void be_spill_morgan(const be_chordal_env_t *chordal_env) {
        env.arch = chordal_env->birg->main_env->arch_env;
        env.irg = chordal_env->irg;
        env.cls = chordal_env->cls;
-       env.senv = be_new_spill_env(chordal_env, is_mem_phi, NULL);
+       env.senv = be_new_spill_env(chordal_env);
        DEBUG_ONLY(be_set_spill_env_dbg_module(env.senv, dbg);)
-       env.uses = be_begin_uses(env.irg, env.arch, env.cls);
 
        phase_init(&env.phase, "spillmorgan", env.irg, PHASE_DEFAULT_GROWTH, init_phase_data);
 
        env.registers_available = arch_count_non_ignore_regs(env.arch, env.cls);
 
-       be_insn_env_init(&env.insn_env, chordal_env->birg, chordal_env->cls, &env.phase.obst);
-
        env.loop_attr_set = new_set(loop_attr_cmp, 5);
        env.block_attr_set = new_set(block_attr_cmp, 20);
 
@@ -480,16 +476,24 @@ void be_spill_morgan(const be_chordal_env_t *chordal_env) {
        reduce_register_pressure_in_loop(&env, get_irg_loop(env.irg), 0);
 
        be_insert_spills_reloads(env.senv);
-       assert(be_verify_schedule(env.irg));
+       if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
+               be_verify_schedule(env.irg);
+       } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
+               assert(be_verify_schedule(env.irg));
+       }
+
+       // we have to remove dead nodes from schedule to not confuse liveness calculation
+       be_remove_dead_nodes_from_schedule(env.irg);
 
        // cleanup
-       be_end_uses(env.uses);
-       be_dump(env.irg, "-spillmorgan", dump_ir_block_graph_sched);
+       if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPILL)
+               be_dump(env.irg, "-spillmorgan", dump_ir_block_graph_sched);
+
+       free_loop_out_edges(&env);
        del_set(env.loop_attr_set);
        del_set(env.block_attr_set);
 
        // fix the remaining places with too high register pressure with beladies algorithm
-       be_remove_dead_nodes_from_schedule(env.irg);
        be_liveness(env.irg);
        be_spill_belady_spill_env(chordal_env, env.senv);