Fixd typo.
[libfirm] / ir / be / bespillutil.c
index 1aeb1a1..c5b3db9 100644 (file)
@@ -21,7 +21,7 @@
  * @file
  * @brief       implementation of the spill/reload placement abstraction layer
  * @author      Daniel Grund, Sebastian Hack, Matthias Braun
- * @date               29.09.2005
+ * @date        29.09.2005
  * @version     $Id$
  */
 #include "config.h"
@@ -57,6 +57,7 @@
 #include "bestatevent.h"
 #include "bessaconstr.h"
 #include "beirg.h"
+#include "beirgmod.h"
 #include "beintlive_t.h"
 #include "bemodule.h"
 #include "be_t.h"
@@ -105,12 +106,10 @@ struct spill_env_t {
        ir_nodeset_t      mem_phis;       /**< set of all spilled phis. */
        ir_exec_freq     *exec_freq;
 
-#ifdef FIRM_STATISTICS
        unsigned          spill_count;
        unsigned          reload_count;
        unsigned          remat_count;
        unsigned          spilled_phi_count;
-#endif
 };
 
 /**
@@ -118,8 +117,8 @@ struct spill_env_t {
  */
 static int cmp_spillinfo(const void *x, const void *y, size_t size)
 {
-       const spill_info_t *xx = x;
-       const spill_info_t *yy = y;
+       const spill_info_t *xx = (const spill_info_t*)x;
+       const spill_info_t *yy = (const spill_info_t*)y;
        (void) size;
 
        return xx->to_spill != yy->to_spill;
@@ -134,14 +133,14 @@ static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
        int hash = hash_irn(value);
 
        info.to_spill = value;
-       res = set_find(env->spills, &info, sizeof(info), hash);
+       res = (spill_info_t*)set_find(env->spills, &info, sizeof(info), hash);
 
        if (res == NULL) {
                info.reloaders   = NULL;
                info.spills      = NULL;
                info.spill_costs = -1;
                info.reload_cls  = NULL;
-               res = set_insert(env->spills, &info, sizeof(info), hash);
+               res = (spill_info_t*)set_insert(env->spills, &info, sizeof(info), hash);
        }
 
        return res;
@@ -152,7 +151,7 @@ spill_env_t *be_new_spill_env(ir_graph *irg)
        const arch_env_t *arch_env = be_get_irg_arch_env(irg);
 
        spill_env_t *env = XMALLOC(spill_env_t);
-       env->spills                     = new_set(cmp_spillinfo, 1024);
+       env->spills         = new_set(cmp_spillinfo, 1024);
        env->irg            = irg;
        env->arch_env       = arch_env;
        ir_nodeset_init(&env->mem_phis);
@@ -161,12 +160,10 @@ spill_env_t *be_new_spill_env(ir_graph *irg)
        env->exec_freq      = be_get_irg_exec_freq(irg);
        obstack_init(&env->obst);
 
-#ifdef FIRM_STATISTICS
        env->spill_count       = 0;
        env->reload_count      = 0;
        env->remat_count       = 0;
        env->spilled_phi_count = 0;
-#endif
 
        return env;
 }
@@ -231,29 +228,6 @@ void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after)
        spill_info->spills = spill;
 }
 
-void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
-                  ir_node *rematted_node)
-{
-       spill_info_t *spill_info;
-       reloader_t *reloader;
-
-       spill_info = get_spillinfo(env, to_spill);
-
-       /* add the remat information */
-       reloader                   = OALLOC(&env->obst, reloader_t);
-       reloader->next             = spill_info->reloaders;
-       reloader->reloader         = before;
-       reloader->rematted_node    = rematted_node;
-       reloader->remat_cost_delta = 0; /* We will never have a cost win over a
-                                          reload since we're not even allowed to
-                                          create a reload */
-
-       spill_info->reloaders  = reloader;
-
-       DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
-               to_spill, before));
-}
-
 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
                ir_node *can_spill_after, const arch_register_class_t *reload_cls,
                int allow_remat)
@@ -316,8 +290,13 @@ ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
        return last;
 }
 
-static ir_node *skip_keeps_phis(ir_node *node)
+/**
+ * determine final spill position: it should be after all phis, keep nodes
+ * and behind nodes marked as prolog
+ */
+static ir_node *determine_spill_point(ir_node *node)
 {
+       node = skip_Proj(node);
        while (true) {
                ir_node *next = sched_next(node);
                if (!is_Phi(next) && !be_is_Keep(next) && !be_is_CopyKeep(next))
@@ -358,7 +337,7 @@ void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
 }
 
 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
-                           int pos,    const arch_register_class_t *reload_cls,
+                           int pos, const arch_register_class_t *reload_cls,
                            int allow_remat)
 {
        ir_node *before = get_block_insertion_point(block, pos);
@@ -387,7 +366,7 @@ void be_spill_phi(spill_env_t *env, ir_node *node)
                        insert = be_get_end_of_block_insertion_point(pred_block);
                        insert = sched_prev(insert);
                } else {
-                       insert = skip_keeps_phis(arg);
+                       insert = determine_spill_point(arg);
                }
 
                be_add_spill(env, arg, insert);
@@ -427,7 +406,8 @@ static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
         * and simply always available. */
        if (!sched_is_scheduled(insn)) {
                /* override spillinfos or create a new one */
-               spillinfo->spills->spill = new_NoMem();
+               ir_graph *irg = get_irn_irg(to_spill);
+               spillinfo->spills->spill = get_irg_no_mem(irg);
                DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
                return;
        }
@@ -438,14 +418,12 @@ static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
                ir_node *after = spill->after;
                ir_node *block = get_block(after);
 
-               after = skip_keeps_phis(after);
+               after = determine_spill_point(after);
 
                spill->spill = be_spill(block, to_spill);
                sched_add_after(skip_Proj(after), spill->spill);
                DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after));
-#ifdef FIRM_STATISTICS
                env->spill_count++;
-#endif
        }
        DBG((dbg, LEVEL_1, "\n"));
 }
@@ -487,14 +465,13 @@ static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
 
        /* override or replace spills list... */
        spill         = OALLOC(&env->obst, spill_t);
-       spill->after  = skip_keeps_phis(phi);
-       spill->spill  = new_r_Phi(block, arity, ins, mode_M);
+       spill->after  = determine_spill_point(phi);
+       spill->spill  = be_new_Phi(block, arity, ins, mode_M, NULL);
        spill->next   = NULL;
+       sched_add_after(block, spill->spill);
 
        spillinfo->spills = spill;
-#ifdef FIRM_STATISTICS
        env->spilled_phi_count++;
-#endif
 
        for (i = 0; i < arity; ++i) {
                ir_node      *arg      = get_irn_n(phi, i);
@@ -549,7 +526,7 @@ static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
 static int is_value_available(spill_env_t *env, const ir_node *arg,
                               const ir_node *reloader)
 {
-       if (is_Unknown(arg) || arg == new_NoMem())
+       if (is_Unknown(arg) || is_NoMem(arg))
                return 1;
 
        if (be_is_Spill(skip_Proj_const(arg)))
@@ -569,7 +546,7 @@ static int is_value_available(spill_env_t *env, const ir_node *arg,
        if (arch_irn_is_ignore(arg))
                return 1;
 
-       return 0;
+       return 0;
 }
 
 /**
@@ -603,7 +580,7 @@ static int check_remat_conditions_costs(spill_env_t *env,
                return REMAT_COST_INFINITE;
        }
        /* never rematerialize a node which modifies the flags.
-        * (would be better to test wether the flags are actually live at point
+        * (would be better to test whether the flags are actually live at point
         * reloader...)
         */
        if (arch_irn_is(insn, modify_flags)) {
@@ -637,7 +614,7 @@ static int check_remat_conditions_costs(spill_env_t *env,
 /**
  * Re-materialize a node.
  *
- * @param senv      the spill environment
+ * @param env       the spill environment
  * @param spilled   the node that was spilled
  * @param reloader  a irn that requires a reload
  */
@@ -662,10 +639,8 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
                        ins[i] = arg;
                } else {
                        ins[i] = do_remat(env, arg, reloader);
-#ifdef FIRM_STATISTICS
                        /* don't count the recursive call as remat */
                        env->remat_count--;
-#endif
                }
        }
 
@@ -682,9 +657,7 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
                /* insert in schedule */
                sched_reset(res);
                sched_add_before(reloader, res);
-#ifdef FIRM_STATISTICS
                env->remat_count++;
-#endif
        }
 
        return res;
@@ -773,11 +746,12 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
         * predecessor (of a PhiM) but this test might match other things too...
         */
        if (!sched_is_scheduled(insn)) {
+               ir_graph *irg = get_irn_irg(to_spill);
                /* override spillinfos or create a new one */
                spill_t *spill = OALLOC(&env->obst, spill_t);
                spill->after = NULL;
                spill->next  = NULL;
-               spill->spill = new_NoMem();
+               spill->spill = get_irg_no_mem(irg);
 
                spillinfo->spills      = spill;
                spillinfo->spill_costs = 0;
@@ -824,7 +798,7 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
 
        /* override spillinfos or create a new one */
        spill        = OALLOC(&env->obst, spill_t);
-       spill->after = skip_keeps_phis(to_spill);
+       spill->after = determine_spill_point(to_spill);
        spill->next  = NULL;
        spill->spill = NULL;
 
@@ -884,7 +858,7 @@ void be_insert_spills_reloads(spill_env_t *env)
        }
 
        /* process each spilled node */
-       for (si = set_first(env->spills); si; si = set_next(env->spills)) {
+       foreach_set(env->spills, spill_info_t*, si) {
                reloader_t *rld;
                ir_node  *to_spill        = si->to_spill;
                ir_mode  *mode            = get_irn_mode(to_spill);
@@ -973,9 +947,7 @@ void be_insert_spills_reloads(spill_env_t *env)
                                assert(si->spills != NULL);
                                copy = be_reload(si->reload_cls, rld->reloader, mode,
                                                 si->spills->spill);
-#ifdef FIRM_STATISTICS
                                env->reload_count++;
-#endif
                        }
 
                        DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
@@ -1048,7 +1020,7 @@ void be_insert_spills_reloads(spill_env_t *env)
        be_timer_pop(T_RA_SPILL_APPLY);
 }
 
-BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill)
 void be_init_spill(void)
 {
        FIRM_DBG_REGISTER(dbg, "firm.be.spill");