Skip to content

Commit be3c350

Browse files
chore: improve interpreter loop performance, add more super instructions
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 70c5bb2 commit be3c350

4 files changed

Lines changed: 172 additions & 67 deletions

File tree

‎crates/parser/src/optimize.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ fn rewrite(
131131
rewrite!(instrs, i, [BinOpLocalConst64(BinOp::IShl, local, 32), Const64(32)] => [LocalGet64(local), I64Extend32S]);
132132
}
133133
}
134+
instr @ (I32Eq | I32Ne | I32LtS | I32LtU | I32GtS | I32GtU | I32LeS | I32LeU | I32GeS | I32GeU) => {
135+
let Some(op) = cmp_op(instr) else { unreachable!() };
136+
rewrite!(instrs, i, [LocalGet32(left), LocalGet32(right)] => CmpLocalLocal32(op, left, right));
137+
}
138+
instr @ (I64Eq | I64Ne | I64LtS | I64LtU | I64GtS | I64GtU | I64LeS | I64LeU | I64GeS | I64GeU) => {
139+
let Some(op) = cmp_op(instr) else { unreachable!() };
140+
rewrite!(instrs, i, [LocalGet64(left), LocalGet64(right)] => CmpLocalLocal64(op, left, right));
141+
}
134142
instr @ (F32Add | F32Mul | F32Min | F32Max) => {
135143
let Some(op) = float_bin_op(instr) else { unreachable!() };
136144
rewrite!(instrs, i, [LocalGet32(a), LocalGet32(b)] => BinOpLocalLocal32(op, a, b));
@@ -543,6 +551,31 @@ fn rewrite(
543551
replace!(instrs, i, 1 => JumpIfNonZero64(target));
544552
continue;
545553
});
554+
rewrite!(instrs, i,
555+
[BinOpLocalConstTee32(BinOp::IAdd, local, delta, dst)] if (local == dst) =>
556+
IncLocalJump32 { target_ip: target, local, delta, on_zero: true }
557+
);
558+
rewrite!(instrs, i,
559+
[CmpLocalLocal32(op, left, right)] =>
560+
JumpCmpLocalLocal32 { target_ip: target, left, right, op: inverse_cmp_op(op) }
561+
);
562+
rewrite!(instrs, i,
563+
[CmpLocalLocal64(op, left, right)] =>
564+
JumpCmpLocalLocal64 { target_ip: target, left, right, op: inverse_cmp_op(op) }
565+
);
566+
rewrite!(instrs, i,
567+
[BinOpLocalConstTee32(BinOp::IAdd, local, delta, dst), LocalGet32(right), cmp] if
568+
(local == dst && let Some(op) = cmp_op(cmp)) =>
569+
IncLocalJumpCmpLocal32 { target_ip: target, local, delta, right, op: inverse_cmp_op(op) }
570+
);
571+
rewrite!(instrs, i,
572+
[LocalGet32(local), cmp] if (let Some(op) = cmp_op(cmp)) =>
573+
JumpCmpStackLocal32 { target_ip: target, local, op: inverse_cmp_op(op) }
574+
);
575+
rewrite!(instrs, i,
576+
[LocalGet64(local), cmp] if (let Some(op) = cmp_op(cmp)) =>
577+
JumpCmpStackLocal64 { target_ip: target, local, op: inverse_cmp_op(op) }
578+
);
546579
rewrite!(instrs, i,
547580
[LocalGet32(local), Const32(imm), cmp] if (let Some(op) = cmp_op(cmp)) =>
548581
match (imm, inverse_cmp_op(op)) {
@@ -599,6 +632,31 @@ fn rewrite(
599632
replace!(instrs, i, 1 => JumpIfZero64(target));
600633
continue;
601634
});
635+
rewrite!(instrs, i,
636+
[BinOpLocalConstTee32(BinOp::IAdd, local, delta, dst)] if (local == dst) =>
637+
IncLocalJump32 { target_ip: target, local, delta, on_zero: false }
638+
);
639+
rewrite!(instrs, i,
640+
[CmpLocalLocal32(op, left, right)] =>
641+
JumpCmpLocalLocal32 { target_ip: target, left, right, op }
642+
);
643+
rewrite!(instrs, i,
644+
[CmpLocalLocal64(op, left, right)] =>
645+
JumpCmpLocalLocal64 { target_ip: target, left, right, op }
646+
);
647+
rewrite!(instrs, i,
648+
[BinOpLocalConstTee32(BinOp::IAdd, local, delta, dst), LocalGet32(right), cmp] if
649+
(local == dst && let Some(op) = cmp_op(cmp)) =>
650+
IncLocalJumpCmpLocal32 { target_ip: target, local, delta, right, op }
651+
);
652+
rewrite!(instrs, i,
653+
[LocalGet32(local), cmp] if (let Some(op) = cmp_op(cmp)) =>
654+
JumpCmpStackLocal32 { target_ip: target, local, op }
655+
);
656+
rewrite!(instrs, i,
657+
[LocalGet64(local), cmp] if (let Some(op) = cmp_op(cmp)) =>
658+
JumpCmpStackLocal64 { target_ip: target, local, op }
659+
);
602660
rewrite!(instrs, i,
603661
[LocalGet32(local), Const32(imm), cmp] if (let Some(op) = cmp_op(cmp)) =>
604662
match (imm, op) {
@@ -861,6 +919,10 @@ fn instruction_target_mut(instr: &mut Instruction) -> Option<&mut u32> {
861919
| Instruction::BrOnCast(ip, _, _)
862920
| Instruction::JumpCmpStackConst32 { target_ip: ip, .. }
863921
| Instruction::JumpCmpStackConst64 { target_ip: ip, .. }
922+
| Instruction::JumpCmpStackLocal32 { target_ip: ip, .. }
923+
| Instruction::JumpCmpStackLocal64 { target_ip: ip, .. }
924+
| Instruction::IncLocalJump32 { target_ip: ip, .. }
925+
| Instruction::IncLocalJumpCmpLocal32 { target_ip: ip, .. }
864926
| Instruction::JumpIfLocalZero32 { target_ip: ip, .. }
865927
| Instruction::JumpIfLocalNonZero32 { target_ip: ip, .. }
866928
| Instruction::JumpIfLocalZero64 { target_ip: ip, .. }

0 commit comments

Comments
 (0)