Skip to content

Commit

Permalink
Fix constant optimization and fused instruction
Browse files Browse the repository at this point in the history
1. Fix tailcall updating in fused instructions
2. Fix errors in constant optimization

Close: #228
  • Loading branch information
qwe661234 committed Sep 26, 2023
1 parent 9f63962 commit 0746a40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
18 changes: 10 additions & 8 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ static void match_pattern(block_t *block)
ir->rs2 = next_ir->rd;
ir->rs1 = next_ir->rs1;
ir->impl = dispatch_table[ir->opcode];
ir->tailcall = next_ir->tailcall;
} else if (next_ir->opcode == rv_insn_add &&
ir->rd == next_ir->rs1) {
/* The destination register of the LUI instruction is the
Expand All @@ -935,21 +936,22 @@ static void match_pattern(block_t *block)
ir->rs2 = next_ir->rd;
ir->rs1 = next_ir->rs2;
ir->impl = dispatch_table[ir->opcode];
} else {
ir->tailcall = next_ir->tailcall;
} else if (next_ir->opcode == rv_insn_lui) {
count = 1;
next_ir = ir + 1;
while (1) {
if (next_ir->opcode != rv_insn_lui)
break;
next_ir->opcode = rv_insn_nop;
count++;
next_ir += 1;
}
if (count > 1) {
ir->imm2 = count;
ir->opcode = rv_insn_fuse1;
ir->impl = dispatch_table[ir->opcode];
if (next_ir->tailcall)
break;
next_ir++;
}
ir->imm2 = count;
ir->opcode = rv_insn_fuse1;
ir->impl = dispatch_table[ir->opcode];
ir->tailcall = next_ir->tailcall;
}
break;

Expand Down
22 changes: 12 additions & 10 deletions src/rv32_constopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ CONSTOPT(mulh, {
if (constopt_info->is_constant[ir->rs1] &&
constopt_info->is_constant[ir->rs2]) {
constopt_info->is_constant[ir->rd] = true;
const int64_t a = constopt_info->const_val[ir->rs1];
const int64_t b = constopt_info->const_val[ir->rs2];
const int64_t a = (int32_t) constopt_info->const_val[ir->rs1];
const int64_t b = (int32_t) constopt_info->const_val[ir->rs2];
ir->imm = ((uint64_t) (a * b)) >> 32;
constopt_info->const_val[ir->rd] = ir->imm;
ir->opcode = rv_insn_lui;
Expand All @@ -457,7 +457,6 @@ CONSTOPT(mulh, {
CONSTOPT(mulhsu, {
if (constopt_info->is_constant[ir->rs1] &&
constopt_info->is_constant[ir->rs2]) {
printf("OPT MUL\n");
constopt_info->is_constant[ir->rd] = true;
const int64_t a = (int32_t) constopt_info->const_val[ir->rs1];
const int64_t b = constopt_info->const_val[ir->rs2];
Expand Down Expand Up @@ -757,7 +756,7 @@ CONSTOPT(csw, {})
CONSTOPT(caddi, {
if (constopt_info->is_constant[ir->rd]) {
constopt_info->is_constant[ir->rd] = true;
ir->imm = constopt_info->const_val[ir->rd] + (uint16_t) ir->imm;
ir->imm = constopt_info->const_val[ir->rd] + (int16_t) ir->imm;
constopt_info->const_val[ir->rd] = ir->imm;
ir->opcode = rv_insn_clui;
ir->impl = dispatch_table[ir->opcode];
Expand All @@ -768,7 +767,7 @@ CONSTOPT(caddi, {
/* C.JAL */
CONSTOPT(cjal, {
constopt_info->is_constant[rv_reg_ra] = true;
constopt_info->const_val[ir->rd] = ir->pc + ir->insn_len;
constopt_info->const_val[rv_reg_ra] = ir->pc + ir->insn_len;
})

/* C.LI loads the sign-extended 6-bit immediate, imm, into register rd.
Expand Down Expand Up @@ -817,7 +816,8 @@ CONSTOPT(csrli, {
if (constopt_info->is_constant[ir->rs1]) {
constopt_info->is_constant[ir->rs1] = true;
ir->imm = constopt_info->const_val[ir->rs1] >> ir->shamt;
constopt_info->const_val[ir->rd] = ir->imm;
constopt_info->const_val[ir->rs1] = ir->imm;
ir->rd = ir->rs1;
ir->opcode = rv_insn_clui;
ir->impl = dispatch_table[ir->opcode];
} else
Expand All @@ -834,7 +834,8 @@ CONSTOPT(csrai, {
ir->imm = constopt_info->const_val[ir->rs1] >> ir->shamt;
for (unsigned int i = 0; i < ir->shamt; ++i)
ir->imm |= mask >> i;
constopt_info->const_val[ir->rd] = ir->imm;
constopt_info->const_val[ir->rs1] = ir->imm;
ir->rd = ir->rs1;
ir->opcode = rv_insn_clui;
ir->impl = dispatch_table[ir->opcode];
} else
Expand All @@ -848,8 +849,9 @@ CONSTOPT(csrai, {
CONSTOPT(candi, {
if (constopt_info->is_constant[ir->rs1]) {
constopt_info->is_constant[ir->rs1] = true;
ir->imm = constopt_info->const_val[ir->rs1] & ir->shamt;
constopt_info->const_val[ir->rd] = ir->imm;
ir->imm = constopt_info->const_val[ir->rs1] & ir->imm;
constopt_info->const_val[ir->rs1] = ir->imm;
ir->rd = ir->rs1;
ir->opcode = rv_insn_clui;
ir->impl = dispatch_table[ir->opcode];
} else
Expand Down Expand Up @@ -948,7 +950,7 @@ CONSTOPT(cbnez, {
CONSTOPT(cslli, {
if (constopt_info->is_constant[ir->rd]) {
constopt_info->is_constant[ir->rd] = true;
ir->imm = constopt_info->const_val[ir->rs2] << (uint8_t) ir->imm;
ir->imm = constopt_info->const_val[ir->rd] << (uint8_t) ir->imm;
constopt_info->const_val[ir->rd] = ir->imm;
ir->opcode = rv_insn_clui;
ir->impl = dispatch_table[ir->opcode];
Expand Down

1 comment on commit 0746a40

@jserv
Copy link
Contributor

@jserv jserv commented on 0746a40 Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Benchmark suite Current: 0746a40 Previous: 54f986f Ratio
Dhrystone 1064.77 Average DMIPS over 10 runs 1058.12 Average DMIPS over 10 runs 0.99
Coremark 933.034 Average iterations/sec over 10 runs 922.773 Average iterations/sec over 10 runs 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.