diff --git a/src/jit/backend/x64/x64_backend.cc b/src/jit/backend/x64/x64_backend.cc index e8a204f4..a48c0e20 100644 --- a/src/jit/backend/x64/x64_backend.cc +++ b/src/jit/backend/x64/x64_backend.cc @@ -93,12 +93,10 @@ const struct jit_register x64_registers[] = { const int x64_num_registers = ARRAY_SIZE(x64_registers); /* clang-format on */ -const Xbyak::Reg x64_backend_reg(struct x64_backend *backend, - const struct ir_value *v) { - int i = v->reg; - CHECK_NE(i, NO_REGISTER); - - Xbyak::Reg reg = *(const Xbyak::Reg *)x64_registers[i].data; +Xbyak::Reg x64_backend_reg(struct x64_backend *backend, + const struct ir_value *v) { + CHECK(v->reg >= 0 && v->reg < x64_num_registers); + Xbyak::Reg reg = *(const Xbyak::Reg *)x64_registers[v->reg].data; CHECK(reg.isREG()); switch (v->type) { @@ -122,12 +120,10 @@ const Xbyak::Reg x64_backend_reg(struct x64_backend *backend, return reg; } -const Xbyak::Xmm x64_backend_xmm(struct x64_backend *backend, - const struct ir_value *v) { - int i = v->reg; - CHECK_NE(i, NO_REGISTER); - - const Xbyak::Xmm &xmm = *(const Xbyak::Xmm *)x64_registers[i].data; +Xbyak::Xmm x64_backend_xmm(struct x64_backend *backend, + const struct ir_value *v) { + CHECK(v->reg >= 0 && v->reg < x64_num_registers); + Xbyak::Xmm xmm = *(const Xbyak::Xmm *)x64_registers[v->reg].data; CHECK(xmm.isXMM()); return xmm; } diff --git a/src/jit/backend/x64/x64_local.h b/src/jit/backend/x64/x64_local.h index 8f6159e9..e5a7f9d2 100644 --- a/src/jit/backend/x64/x64_local.h +++ b/src/jit/backend/x64/x64_local.h @@ -73,10 +73,10 @@ extern const Xbyak::Reg64 tmp1; extern const Xbyak::Reg64 guestctx; extern const Xbyak::Reg64 guestmem; -const Xbyak::Reg x64_backend_reg(struct x64_backend *backend, - const struct ir_value *v); -const Xbyak::Xmm x64_backend_xmm(struct x64_backend *backend, - const struct ir_value *v); +Xbyak::Reg x64_backend_reg(struct x64_backend *backend, + const struct ir_value *v); +Xbyak::Xmm x64_backend_xmm(struct x64_backend *backend, + const struct ir_value *v); void x64_backend_load_mem(struct x64_backend *backend, const struct ir_value *dst, const Xbyak::RegExp &src_exp); diff --git a/src/jit/ir/ir.c b/src/jit/ir/ir.c index 3f72dc1d..800570b7 100644 --- a/src/jit/ir/ir.c +++ b/src/jit/ir/ir.c @@ -160,7 +160,6 @@ struct ir_instr *ir_append_instr(struct ir *ir, enum ir_op op, struct ir_value *result = ir_calloc(ir, sizeof(struct ir_value)); result->type = result_type; result->def = instr; - result->reg = NO_REGISTER; instr->result = result; } @@ -222,7 +221,6 @@ struct ir_value *ir_alloc_int(struct ir *ir, int64_t c, enum ir_type type) { LOG_FATAL("unexpected value type"); break; } - v->reg = NO_REGISTER; return v; } @@ -230,7 +228,6 @@ struct ir_value *ir_alloc_i8(struct ir *ir, int8_t c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_I8; v->i8 = c; - v->reg = NO_REGISTER; return v; } @@ -238,7 +235,6 @@ struct ir_value *ir_alloc_i16(struct ir *ir, int16_t c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_I16; v->i16 = c; - v->reg = NO_REGISTER; return v; } @@ -246,7 +242,6 @@ struct ir_value *ir_alloc_i32(struct ir *ir, int32_t c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_I32; v->i32 = c; - v->reg = NO_REGISTER; return v; } @@ -254,7 +249,6 @@ struct ir_value *ir_alloc_i64(struct ir *ir, int64_t c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_I64; v->i64 = c; - v->reg = NO_REGISTER; return v; } @@ -262,7 +256,6 @@ struct ir_value *ir_alloc_f32(struct ir *ir, float c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_F32; v->f32 = c; - v->reg = NO_REGISTER; return v; } @@ -270,7 +263,6 @@ struct ir_value *ir_alloc_f64(struct ir *ir, double c) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_F64; v->f64 = c; - v->reg = NO_REGISTER; return v; } @@ -282,7 +274,6 @@ struct ir_value *ir_alloc_block_ref(struct ir *ir, struct ir_block *block) { struct ir_value *v = ir_calloc(ir, sizeof(struct ir_value)); v->type = VALUE_BLOCK; v->blk = block; - v->reg = NO_REGISTER; return v; } diff --git a/src/jit/ir/ir.h b/src/jit/ir/ir.h index d692ef68..94c49353 100644 --- a/src/jit/ir/ir.h +++ b/src/jit/ir/ir.h @@ -7,7 +7,6 @@ #include "core/list.h" #define IR_MAX_ARGS 4 -#define NO_REGISTER -1 enum ir_op { #define IR_OP(name, flags) OP_##name, diff --git a/src/jit/passes/register_allocation_pass.c b/src/jit/passes/register_allocation_pass.c index f249149a..75e1abc8 100644 --- a/src/jit/passes/register_allocation_pass.c +++ b/src/jit/passes/register_allocation_pass.c @@ -73,6 +73,7 @@ struct ra { int max_uses; }; +#define NO_REGISTER -1 #define NO_TMP -1 #define NO_USE -1 @@ -646,7 +647,8 @@ static void ra_legalize_args(struct ra *ra, struct ir *ir, } } -static void ra_reset(struct ra *ra, struct ir *ir) { +static void ra_reset(struct ra *ra, struct ir *ir, struct ir_block *block) { + /* reset allocation state */ for (int i = 0; i < ra->num_registers; i++) { struct ra_bin *bin = &ra->bins[i]; bin->tmp_idx = NO_TMP; @@ -654,17 +656,22 @@ static void ra_reset(struct ra *ra, struct ir *ir) { ra->num_tmps = 0; ra->num_uses = 0; + + /* reset register state */ + list_for_each_entry(instr, &block->instrs, struct ir_instr, it) { + if (instr->result) { + instr->result->reg = NO_REGISTER; + } + } } void ra_run(struct ra *ra, struct ir *ir) { list_for_each_entry(block, &ir->blocks, struct ir_block, it) { - ra_reset(ra, ir); - + ra_reset(ra, ir, block); ra_legalize_args(ra, ir, block); ra_assign_ordinals(ra, ir, block); ra_create_tmps(ra, ir, block); ra_alloc_bins(ra, ir, block); - #if 1 ra_validate(ra, ir, block); #endif