Simplify x87_dump_stack().
[libfirm] / ir / be / ia32 / ia32_x87.c
index 917d575..4019e5c 100644 (file)
@@ -152,6 +152,12 @@ static int x87_get_depth(const x87_state *state)
        return state->depth;
 }
 
+static st_entry *x87_get_entry(x87_state *const state, int const pos)
+{
+       assert(0 <= pos && pos < state->depth);
+       return &state->st[MASK_TOS(state->tos + pos)];
+}
+
 /**
  * Return the virtual register index at st(pos).
  *
@@ -162,25 +168,10 @@ static int x87_get_depth(const x87_state *state)
  */
 static int x87_get_st_reg(const x87_state *state, int pos)
 {
-       assert(pos < state->depth);
-       return state->st[MASK_TOS(state->tos + pos)].reg_idx;
+       return x87_get_entry((x87_state*)state, pos)->reg_idx;
 }
 
 #ifdef DEBUG_libfirm
-/**
- * Return the node at st(pos).
- *
- * @param state  the x87 state
- * @param pos    a stack position
- *
- * @return the IR node that produced the value at st(pos)
- */
-static ir_node *x87_get_st_node(const x87_state *state, int pos)
-{
-       assert(pos < state->depth);
-       return state->st[MASK_TOS(state->tos + pos)].node;
-}
-
 /**
  * Dump the stack for debugging.
  *
@@ -188,11 +179,9 @@ static ir_node *x87_get_st_node(const x87_state *state, int pos)
  */
 static void x87_dump_stack(const x87_state *state)
 {
-       int i;
-
-       for (i = state->depth - 1; i >= 0; --i) {
-               DB((dbg, LEVEL_2, "vf%d(%+F) ", x87_get_st_reg(state, i),
-                   x87_get_st_node(state, i)));
+       for (int i = state->depth; i-- != 0;) {
+               st_entry const *const entry = x87_get_entry((x87_state*)state, i);
+               DB((dbg, LEVEL_2, "vf%d(%+F) ", entry->reg_idx, entry->node));
        }
        DB((dbg, LEVEL_2, "<-- TOS\n"));
 }
@@ -208,9 +197,9 @@ static void x87_dump_stack(const x87_state *state)
  */
 static void x87_set_st(x87_state *state, int reg_idx, ir_node *node, int pos)
 {
-       assert(0 < state->depth);
-       state->st[MASK_TOS(state->tos + pos)].reg_idx = reg_idx;
-       state->st[MASK_TOS(state->tos + pos)].node    = node;
+       st_entry *const entry = x87_get_entry(state, pos);
+       entry->reg_idx = reg_idx;
+       entry->node    = node;
 
        DB((dbg, LEVEL_2, "After SET_REG: "));
        DEBUG_ONLY(x87_dump_stack(state);)
@@ -236,12 +225,11 @@ static void x87_set_tos(x87_state *state, int reg_idx, ir_node *node)
  */
 static void x87_fxch(x87_state *state, int pos)
 {
-       st_entry entry;
-       assert(pos < state->depth);
-
-       entry = state->st[MASK_TOS(state->tos + pos)];
-       state->st[MASK_TOS(state->tos + pos)] = state->st[MASK_TOS(state->tos)];
-       state->st[MASK_TOS(state->tos)] = entry;
+       st_entry *const a = x87_get_entry(state, pos);
+       st_entry *const b = x87_get_entry(state, 0);
+       st_entry  const t = *a;
+       *a = *b;
+       *b = t;
 
        DB((dbg, LEVEL_2, "After FXCH: "));
        DEBUG_ONLY(x87_dump_stack(state);)
@@ -258,11 +246,10 @@ static void x87_fxch(x87_state *state, int pos)
  */
 static int x87_on_stack(const x87_state *state, int reg_idx)
 {
-       int i, tos = state->tos;
-
-       for (i = 0; i < state->depth; ++i)
-               if (state->st[MASK_TOS(tos + i)].reg_idx == reg_idx)
+       for (int i = 0; i < state->depth; ++i) {
+               if (x87_get_st_reg(state, i) == reg_idx)
                        return i;
+       }
        return -1;
 }
 
@@ -279,8 +266,9 @@ static void x87_push_dbl(x87_state *state, int reg_idx, ir_node *node)
 
        ++state->depth;
        state->tos = MASK_TOS(state->tos - 1);
-       state->st[state->tos].reg_idx = reg_idx;
-       state->st[state->tos].node    = node;
+       st_entry *const entry = x87_get_entry(state, 0);
+       entry->reg_idx = reg_idx;
+       entry->node    = node;
 
        DB((dbg, LEVEL_2, "After PUSH: ")); DEBUG_ONLY(x87_dump_stack(state);)
 }
@@ -1990,15 +1978,12 @@ static int sim_Perm(x87_state *state, ir_node *irn)
 /**
  * Kill any dead registers at block start by popping them from the stack.
  *
- * @param sim          the simulator handle
- * @param block        the current block
- * @param start_state  the x87 state at the begin of the block
- *
- * @return the x87 state after dead register killed
+ * @param sim    the simulator handle
+ * @param block  the current block
+ * @param state  the x87 state at the begin of the block
  */
-static x87_state *x87_kill_deads(x87_simulator *sim, ir_node *block, x87_state *start_state)
+static void x87_kill_deads(x87_simulator *const sim, ir_node *const block, x87_state *const state)
 {
-       x87_state *state = start_state;
        ir_node *first_insn = sched_first(block);
        ir_node *keep = NULL;
        unsigned live = vfp_live_args_after(sim, block, 0);
@@ -2015,9 +2000,6 @@ static x87_state *x87_kill_deads(x87_simulator *sim, ir_node *block, x87_state *
        }
 
        if (kill_mask) {
-               /* create a new state, will be changed */
-               state = x87_clone_state(sim, state);
-
                DB((dbg, LEVEL_1, "Killing deads:\n"));
                DEBUG_ONLY(vfp_dump_live(live);)
                DEBUG_ONLY(x87_dump_stack(state);)
@@ -2035,7 +2017,7 @@ static x87_state *x87_kill_deads(x87_simulator *sim, ir_node *block, x87_state *
                                sched_add_before(first_insn, keep);
                                keep_alive(keep);
                                x87_emms(state);
-                               return state;
+                               return;
                        }
                }
                /* now kill registers */
@@ -2071,7 +2053,6 @@ static x87_state *x87_kill_deads(x87_simulator *sim, ir_node *block, x87_state *
                }
                keep_alive(keep);
        }
-       return state;
 }
 
 /**
@@ -2096,10 +2077,10 @@ static void x87_simulate_block(x87_simulator *sim, ir_node *block)
        DB((dbg, LEVEL_2, "State at Block begin:\n "));
        DEBUG_ONLY(x87_dump_stack(state);)
 
-       /* at block begin, kill all dead registers */
-       state = x87_kill_deads(sim, block, state);
        /* create a new state, will be changed */
        state = x87_clone_state(sim, state);
+       /* at block begin, kill all dead registers */
+       x87_kill_deads(sim, block, state);
 
        /* beware, n might change */
        for (n = sched_first(block); !sched_is_end(n); n = next) {