tcg: Change reg_to_temp to TCGTemp pointer

Backports commit f8b2f202344b362b1e676688f838d6b7c08f1975 from qemu
This commit is contained in:
Richard Henderson 2018-02-19 11:30:24 -05:00 committed by Lioncash
parent cf59e51811
commit daf837956c
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 57 additions and 62 deletions

View file

@ -891,29 +891,32 @@ static void tcg_reg_alloc_start(TCGContext *s)
ts->mem_allocated = 0; ts->mem_allocated = 0;
ts->fixed_reg = 0; ts->fixed_reg = 0;
} }
for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
s->reg_to_temp[i] = -1; memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
}
} }
static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size, static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
int idx) TCGTemp *ts)
{ {
TCGTemp *ts; int idx = temp_idx(s, ts);
assert(idx >= 0 && idx < s->nb_temps);
ts = &s->temps[idx];
if (idx < s->nb_globals) { if (idx < s->nb_globals) {
pstrcpy(buf, buf_size, ts->name); pstrcpy(buf, buf_size, ts->name);
} else if (ts->temp_local) {
snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
} else { } else {
if (ts->temp_local) snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
else
snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
} }
return buf; return buf;
} }
static char *tcg_get_arg_str_idx(TCGContext *s, char *buf,
int buf_size, int idx)
{
assert(idx >= 0 && idx < s->nb_temps);
return tcg_get_arg_str_ptr(s, buf, buf_size, &s->temps[idx]);
}
/* Find helper name. */ /* Find helper name. */
static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val) static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
{ {
@ -1657,10 +1660,10 @@ static void dump_regs(TCGContext *s)
} }
for(i = 0; i < TCG_TARGET_NB_REGS; i++) { for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
if (s->reg_to_temp[i] >= 0) { if (s->reg_to_temp[i] != NULL) {
printf("%s: %s\n", printf("%s: %s\n",
tcg_target_reg_names[i], tcg_target_reg_names[i],
tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i])); tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
} }
} }
} }
@ -1672,29 +1675,26 @@ static void check_regs(TCGContext *s)
TCGTemp *ts; TCGTemp *ts;
char buf[64]; char buf[64];
for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) { for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
k = s->reg_to_temp[reg]; ts = s->reg_to_temp[reg];
if (k >= 0) { if (ts != NULL) {
ts = &s->temps[k]; if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
if (ts->val_type != TEMP_VAL_REG ||
ts->reg != reg) {
printf("Inconsistency for register %s:\n", printf("Inconsistency for register %s:\n",
tcg_target_reg_names[reg]); tcg_target_reg_names[reg]);
goto fail; goto fail;
} }
} }
} }
for(k = 0; k < s->nb_temps; k++) { for (k = 0; k < s->nb_temps; k++) {
ts = &s->temps[k]; ts = &s->temps[k];
if (ts->val_type == TEMP_VAL_REG && if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
!ts->fixed_reg && && s->reg_to_temp[ts->reg] != ts) {
s->reg_to_temp[ts->reg] != k) { printf("Inconsistency for temp %s:\n",
printf("Inconsistency for temp %s:\n", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
fail: fail:
printf("reg state:\n"); printf("reg state:\n");
dump_regs(s); dump_regs(s);
tcg_abort(); tcg_abort();
} }
} }
} }
@ -1723,15 +1723,12 @@ static void temp_allocate_frame(TCGContext *s, int temp)
/* sync register 'reg' by saving it to the corresponding temporary */ /* sync register 'reg' by saving it to the corresponding temporary */
static inline void tcg_reg_sync(TCGContext *s, TCGReg reg) static inline void tcg_reg_sync(TCGContext *s, TCGReg reg)
{ {
TCGTemp *ts; TCGTemp *ts = s->reg_to_temp[reg];
int temp;
temp = s->reg_to_temp[reg];
ts = &s->temps[temp];
assert(ts->val_type == TEMP_VAL_REG); assert(ts->val_type == TEMP_VAL_REG);
if (!ts->mem_coherent && !ts->fixed_reg) { if (!ts->mem_coherent && !ts->fixed_reg) {
if (!ts->mem_allocated) { if (!ts->mem_allocated) {
temp_allocate_frame(s, temp); temp_allocate_frame(s, temp_idx(s, ts));
} }
tcg_out_st(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset); tcg_out_st(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
} }
@ -1741,13 +1738,12 @@ static inline void tcg_reg_sync(TCGContext *s, TCGReg reg)
/* free register 'reg' by spilling the corresponding temporary if necessary */ /* free register 'reg' by spilling the corresponding temporary if necessary */
static void tcg_reg_free(TCGContext *s, TCGReg reg) static void tcg_reg_free(TCGContext *s, TCGReg reg)
{ {
int temp; TCGTemp *ts = s->reg_to_temp[reg];
temp = s->reg_to_temp[reg]; if (ts != NULL) {
if (temp != -1) {
tcg_reg_sync(s, reg); tcg_reg_sync(s, reg);
s->temps[temp].val_type = TEMP_VAL_MEM; ts->val_type = TEMP_VAL_MEM;
s->reg_to_temp[reg] = -1; s->reg_to_temp[reg] = NULL;
} }
} }
@ -1763,7 +1759,7 @@ static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
/* first try free registers */ /* first try free registers */
for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) { for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
reg = tcg_target_reg_alloc_order[i]; reg = tcg_target_reg_alloc_order[i];
if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1) if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == NULL)
return reg; return reg;
} }
@ -1782,12 +1778,11 @@ static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
/* mark a temporary as dead. */ /* mark a temporary as dead. */
static inline void temp_dead(TCGContext *s, int temp) static inline void temp_dead(TCGContext *s, int temp)
{ {
TCGTemp *ts; TCGTemp *ts = &s->temps[temp];
ts = &s->temps[temp];
if (!ts->fixed_reg) { if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG) { if (ts->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ts->reg] = -1; s->reg_to_temp[ts->reg] = NULL;
} }
if (temp < s->nb_globals || ts->temp_local) { if (temp < s->nb_globals || ts->temp_local) {
ts->val_type = TEMP_VAL_MEM; ts->val_type = TEMP_VAL_MEM;
@ -1801,16 +1796,15 @@ static inline void temp_dead(TCGContext *s, int temp)
temporary registers needs to be allocated to store a constant. */ temporary registers needs to be allocated to store a constant. */
static inline void temp_sync(TCGContext *s, int temp, TCGRegSet allocated_regs) static inline void temp_sync(TCGContext *s, int temp, TCGRegSet allocated_regs)
{ {
TCGTemp *ts; TCGTemp *ts = &s->temps[temp];
ts = &s->temps[temp];
if (!ts->fixed_reg) { if (!ts->fixed_reg) {
switch(ts->val_type) { switch(ts->val_type) {
case TEMP_VAL_CONST: case TEMP_VAL_CONST:
ts->reg = tcg_reg_alloc(s, (TCGRegSet)s->tcg_target_available_regs[ts->type], ts->reg = tcg_reg_alloc(s, (TCGRegSet)s->tcg_target_available_regs[ts->type],
allocated_regs); allocated_regs);
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
s->reg_to_temp[ts->reg] = temp; s->reg_to_temp[ts->reg] = ts;
ts->mem_coherent = 0; ts->mem_coherent = 0;
tcg_out_movi(s, ts->type, ts->reg, ts->val); tcg_out_movi(s, ts->type, ts->reg, ts->val);
/* fallthrough*/ /* fallthrough*/
@ -1912,8 +1906,9 @@ static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
tcg_out_movi(s, ots->type, ots->reg, val); tcg_out_movi(s, ots->type, ots->reg, val);
} else { } else {
/* The movi is not explicitly generated here */ /* The movi is not explicitly generated here */
if (ots->val_type == TEMP_VAL_REG) if (ots->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ots->reg] = -1; s->reg_to_temp[ots->reg] = NULL;
}
ots->val_type = TEMP_VAL_CONST; ots->val_type = TEMP_VAL_CONST;
ots->val = val; ots->val = val;
} }
@ -1956,7 +1951,7 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
tcg_out_movi(s, itype, ts->reg, ts->val); tcg_out_movi(s, itype, ts->reg, ts->val);
ts->mem_coherent = 0; ts->mem_coherent = 0;
} }
s->reg_to_temp[ts->reg] = args[1]; s->reg_to_temp[ts->reg] = ts;
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
} }
@ -1977,7 +1972,7 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
} else if (ts->val_type == TEMP_VAL_CONST) { } else if (ts->val_type == TEMP_VAL_CONST) {
/* propagate constant */ /* propagate constant */
if (ots->val_type == TEMP_VAL_REG) { if (ots->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ots->reg] = -1; s->reg_to_temp[ots->reg] = NULL;
} }
ots->val_type = TEMP_VAL_CONST; ots->val_type = TEMP_VAL_CONST;
ots->val = ts->val; ots->val = ts->val;
@ -1988,7 +1983,7 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) { if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
/* the mov can be suppressed */ /* the mov can be suppressed */
if (ots->val_type == TEMP_VAL_REG) { if (ots->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ots->reg] = -1; s->reg_to_temp[ots->reg] = NULL;
} }
ots->reg = ts->reg; ots->reg = ts->reg;
temp_dead(s, args[1]); temp_dead(s, args[1]);
@ -2004,7 +1999,7 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
} }
ots->val_type = TEMP_VAL_REG; ots->val_type = TEMP_VAL_REG;
ots->mem_coherent = 0; ots->mem_coherent = 0;
s->reg_to_temp[ots->reg] = args[0]; s->reg_to_temp[ots->reg] = ots;
if (NEED_SYNC_ARG(0)) { if (NEED_SYNC_ARG(0)) {
tcg_reg_sync(s, ots->reg); tcg_reg_sync(s, ots->reg);
} }
@ -2046,7 +2041,7 @@ static void tcg_reg_alloc_op(TCGContext *s,
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
ts->reg = reg; ts->reg = reg;
ts->mem_coherent = 1; ts->mem_coherent = 1;
s->reg_to_temp[reg] = arg; s->reg_to_temp[reg] = ts;
} else if (ts->val_type == TEMP_VAL_CONST) { } else if (ts->val_type == TEMP_VAL_CONST) {
if (tcg_target_const_match(ts->val, ts->type, arg_ct)) { if (tcg_target_const_match(ts->val, ts->type, arg_ct)) {
/* constant is OK for instruction */ /* constant is OK for instruction */
@ -2060,7 +2055,7 @@ static void tcg_reg_alloc_op(TCGContext *s,
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
ts->reg = reg; ts->reg = reg;
ts->mem_coherent = 0; ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg; s->reg_to_temp[reg] = ts;
} }
} }
assert(ts->val_type == TEMP_VAL_REG); assert(ts->val_type == TEMP_VAL_REG);
@ -2141,14 +2136,14 @@ static void tcg_reg_alloc_op(TCGContext *s,
/* if a fixed register is used, then a move will be done afterwards */ /* if a fixed register is used, then a move will be done afterwards */
if (!ts->fixed_reg) { if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG) { if (ts->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ts->reg] = -1; s->reg_to_temp[ts->reg] = NULL;
} }
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
ts->reg = reg; ts->reg = reg;
/* temp value is modified, so the value kept in memory is /* temp value is modified, so the value kept in memory is
potentially not the same */ potentially not the same */
ts->mem_coherent = 0; ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg; s->reg_to_temp[reg] = ts;
} }
oarg_end: oarg_end:
new_args[i] = reg; new_args[i] = reg;
@ -2303,7 +2298,7 @@ static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
arg = args[i]; arg = args[i];
ts = &s->temps[arg]; ts = &s->temps[arg];
reg = tcg_target_call_oarg_regs[i]; reg = tcg_target_call_oarg_regs[i];
assert(s->reg_to_temp[reg] == -1); assert(s->reg_to_temp[reg] == NULL);
if (ts->fixed_reg) { if (ts->fixed_reg) {
if (ts->reg != reg) { if (ts->reg != reg) {
@ -2311,12 +2306,12 @@ static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
} }
} else { } else {
if (ts->val_type == TEMP_VAL_REG) { if (ts->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ts->reg] = -1; s->reg_to_temp[ts->reg] = NULL;
} }
ts->val_type = TEMP_VAL_REG; ts->val_type = TEMP_VAL_REG;
ts->reg = reg; ts->reg = reg;
ts->mem_coherent = 0; ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg; s->reg_to_temp[reg] = ts;
if (NEED_SYNC_ARG(i)) { if (NEED_SYNC_ARG(i)) {
tcg_reg_sync(s, reg); tcg_reg_sync(s, reg);
} }

View file

@ -700,9 +700,9 @@ struct TCGContext {
TCGTempSet free_temps[TCG_TYPE_COUNT * 2]; TCGTempSet free_temps[TCG_TYPE_COUNT * 2];
TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */ TCGTemp temps[TCG_MAX_TEMPS]; /* globals first, temps after */
/* tells in which temporary a given register is. It does not take /* Tells which temporary holds a given register.
into account fixed registers */ It does not take into account fixed registers */
int reg_to_temp[TCG_TARGET_NB_REGS]; TCGTemp *reg_to_temp[TCG_TARGET_NB_REGS];
TCGOp gen_op_buf[OPC_BUF_SIZE]; TCGOp gen_op_buf[OPC_BUF_SIZE];
TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE]; TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];