C99 feature removed.
[libfirm] / ir / be / bestate.c
index c83bc6c..32d0c14 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -47,6 +47,7 @@
 #include "belive_t.h"
 #include "bemodule.h"
 #include "benode.h"
+#include "beirgmod.h"
 #include "bessaconstr.h"
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
@@ -134,7 +135,8 @@ static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int for
                do {
                        after = next;
                        next = sched_next(after);
-               } while (is_Proj(next) || is_Phi(next) || be_is_Keep(next));
+               } while (is_Proj(next) || is_Phi(next) || be_is_Keep(next)
+                       || (arch_irn_get_flags(next) & arch_irn_flags_prolog));
        } else {
                after = state;
        }
@@ -161,6 +163,7 @@ static void spill_phi(minibelady_env_t *env, ir_node *phi)
        ir_node      *block         = get_nodes_block(phi);
        int           arity         = get_irn_arity(phi);
        ir_node     **in            = ALLOCAN(ir_node*, arity);
+       ir_node      *dummy         = new_r_Dummy(irg, mode_M);
        ir_node      *spill_to_kill = NULL;
        spill_info_t *spill_info;
        int           i;
@@ -175,13 +178,14 @@ static void spill_phi(minibelady_env_t *env, ir_node *phi)
 
        /* create a new phi-M with bad preds */
        for (i = 0; i < arity; ++i) {
-               in[i] = new_r_Unknown(irg, mode_M);
+               in[i] = dummy;
        }
 
        DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
 
        /* create a Phi-M */
-       spill_info->spill = new_r_Phi(block, arity, in, mode_M);
+       spill_info->spill = be_new_Phi(block, arity, in, mode_M, NULL);
+       sched_add_after(block, spill_info->spill);
 
        if (spill_to_kill != NULL) {
                exchange(spill_to_kill, spill_info->spill);
@@ -202,7 +206,7 @@ static void belady(minibelady_env_t *env, ir_node *block);
  * Collects all values live-in at block @p block and all phi results in this
  * block.
  * Then it adds the best values (at most n_regs) to the blocks start_workset.
- * The phis among the remaining values get spilled: Introduce psudo-copies of
+ * The phis among the remaining values get spilled: Introduce pseudo-copies of
  * their args to break interference and make it possible to spill them to the
  * same spill slot.
  */
@@ -259,7 +263,7 @@ static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *b
                        continue;
 
                DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
-               next_use = be_get_next_use(env->uses, first, 0, node, 0);
+               next_use = be_get_next_use(env->uses, first, node, 0);
 
                if (USES_IS_INFINITE(next_use.time)) {
                        DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
@@ -309,7 +313,7 @@ static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *b
                        continue;
 
                DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
-               next_use = be_get_next_use(env->uses, first, 0, node, 0);
+               next_use = be_get_next_use(env->uses, first, node, 0);
 
                if (USES_IS_INFINITE(next_use.time)) {
                        DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
@@ -348,6 +352,17 @@ static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *b
        return block_info;
 }
 
+static ir_node *get_reload_point(ir_node *before)
+{
+       while (true) {
+               ir_node *prev = sched_prev(before);
+               if (! (arch_irn_get_flags(prev) & arch_irn_flags_epilog))
+                       break;
+               before = prev;
+       }
+       return before;
+}
+
 /**
  * For the given block @p block, decide for each values
  * whether it is used from a register or is reloaded
@@ -404,8 +419,9 @@ static void belady(minibelady_env_t *env, ir_node *block)
                }
                /* create a reload to match state if necessary */
                if (need_val != NULL && need_val != current_state) {
+                       ir_node *before = get_reload_point(node);
                        DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
-                       create_reload(env, need_val, node, current_state);
+                       create_reload(env, need_val, before, current_state);
                        current_state = need_val;
                }
 
@@ -461,7 +477,7 @@ static ir_node *get_end_of_block_insertion_point(ir_node *block)
        if (!is_cfop(last)) {
                last = sched_next(last);
                /* last node must be a cfop, only exception is the start block */
-               assert(last     == get_irg_start_block(get_irn_irg(block)));
+               assert(last == get_irg_start_block(get_irn_irg(block)));
        }
 
        return last;
@@ -472,7 +488,7 @@ static ir_node *get_end_of_block_insertion_point(ir_node *block)
  */
 static void fix_block_borders(ir_node *block, void *data)
 {
-       minibelady_env_t *env = data;
+       minibelady_env_t *env = (minibelady_env_t*)data;
        ir_graph *irg = get_irn_irg(block);
        ir_node *startblock = get_irg_start_block(irg);
        int i;
@@ -507,7 +523,7 @@ static void fix_block_borders(ir_node *block, void *data)
                     pred_info->end_state, need_state));
 
                if (pred_info->end_state != need_state) {
-                       ir_node *insert_point = get_end_of_block_insertion_point(pred);
+                       ir_node *insert_point = get_end_of_block_insertion_point(pred);
 
 
                        DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
@@ -516,14 +532,13 @@ static void fix_block_borders(ir_node *block, void *data)
        }
 }
 
-void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
+void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
                      create_spill_func create_spill,
                      create_reload_func create_reload)
 {
        minibelady_env_t env;
-       ir_graph *irg = be_get_birg_irg(birg);
        spill_info_t *info;
-       be_lv_t *lv = be_assure_liveness(birg);
+       be_lv_t *lv = be_assure_liveness(irg);
 
        be_liveness_assure_sets(lv);
        /* construct control flow loop tree */
@@ -536,7 +551,7 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
        env.func_env      = func_env;
        env.create_spill  = create_spill;
        env.create_reload = create_reload;
-       env.lv            = be_get_birg_liveness(birg);
+       env.lv            = be_get_irg_liveness(irg);
        env.uses          = be_begin_uses(irg, env.lv);
        env.spills        = NULL;
        ir_nodemap_init(&env.spill_infos);
@@ -557,10 +572,10 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
        info = env.spills;
        while (info != NULL) {
                be_ssa_construction_env_t senv;
-               int i, len;
+               size_t i, len;
                ir_node **phis;
 
-               be_ssa_construction_init(&senv, birg);
+               be_ssa_construction_init(&senv, irg);
                if (sched_is_scheduled(info->value))
                        be_ssa_construction_add_copy(&senv, info->value);
                be_ssa_construction_add_copies(&senv,
@@ -592,7 +607,7 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
        }
 
        /* some nodes might be dead now. */
-       be_remove_dead_nodes_from_schedule(birg);
+       be_remove_dead_nodes_from_schedule(irg);
 
        ir_nodemap_destroy(&env.spill_infos);
        be_end_uses(env.uses);