Add some Processor specific instruction selections
[libfirm] / ir / be / bespillbelady.c
index 71d0965..dc8532a 100644 (file)
 #include "obst.h"
 #include "set.h"
 #include "pset.h"
+#include "irprintf_t.h"
 #include "irgraph.h"
 #include "irnode.h"
 #include "irmode.h"
 #include "irgwalk.h"
 #include "iredges_t.h"
 #include "ircons_t.h"
+#include "irprintf.h"
 
 #include "beutil.h"
 #include "bearch.h"
@@ -45,7 +47,7 @@
 #define DBG_SLOTS  32
 #define DBG_TRACE  64
 #define DEBUG_LVL 0 //(DBG_START | DBG_DECIDE | DBG_WSETS | DBG_FIX | DBG_SPILL)
-static firm_dbg_module_t *dbg = NULL;
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 #define MIN(a,b) (((a)<(b))?(a):(b))
 
@@ -255,10 +257,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;
 }
 
 /**
@@ -586,16 +590,15 @@ 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, *spill;
+               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?");
 
-               spill = get_irn_edge(get_irn_irg(irn), irn, 0)->src;
-               assert(be_is_Spill(spill) && "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, 0);
-               set_irn_n(spill, 0, src);
+               src = be_get_Copy_op(irn);
+               set_irn_n(user, 0, src);
        }
 }
 
@@ -611,17 +614,20 @@ static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
                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, 0);
+               if (be_is_Reload(irn))
+                       spill = get_irn_n(irn, be_pos_Reload_mem);
 
                /* remove reload */
                set_irn_n(irn, 0, new_Bad());
                sched_remove(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);
+               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);
+                       }
                }
        }
 }
@@ -629,18 +635,19 @@ static void remove_unused_reloads(ir_graph *irg, belady_env_t *bel) {
 void be_spill_belady(const be_chordal_env_t *chordal_env) {
        belady_env_t bel;
 
-       dbg = firm_dbg_register("ir.be.spillbelady");
+       FIRM_DBG_REGISTER(dbg, "ir.be.spillbelady");
 
        /* 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);
+       bel.senv      = be_new_spill_env(chordal_env, 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();
 
        DBG((dbg, LEVEL_1, "running on register class: %s\n", bel.cls->name));