From 6fcbe7b3d00ca103d1224e52c246c399dcce4ab8 Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 4 Nov 2022 12:12:04 -0600 Subject: [PATCH] optimise cmp x, 0; jz --- src/rust/codegen.rs | 16 ++++++++++++++-- src/rust/jit.rs | 8 ++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rust/codegen.rs b/src/rust/codegen.rs index 865e325a..d1ec3066 100644 --- a/src/rust/codegen.rs +++ b/src/rust/codegen.rs @@ -348,6 +348,18 @@ fn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruc dest: InstructionOperandDest::WasmLocal(l), opsize: OPSIZE_32, } => builder.get_local(&l), + Instruction::Cmp { + dest: InstructionOperandDest::WasmLocal(l), + source, + opsize: OPSIZE_32, + } => { + if source.is_zero() { + builder.get_local(&l) + } + else { + builder.load_fixed_i32(global_pointers::last_result as u32) + } + }, _ => builder.load_fixed_i32(global_pointers::last_result as u32), } } @@ -1582,7 +1594,6 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { match &ctx.previous_instruction { Instruction::Cmp { .. } | Instruction::Sub { .. } => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); - // TODO: Could use local for cmp x, 0 // TODO: Could use eq(local, local) for cmp x, y gen_get_last_result(ctx.builder, &ctx.previous_instruction); if negate == ConditionNegate::False { @@ -1734,7 +1745,8 @@ pub fn gen_getsf(ctx: &mut JitContext) { let &opsize = opsize; gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); // TODO: - // Could use local for cmp x, 0; test x, x + // use local for test x, x + // x >= 0 for ConditionNegate::True gen_get_last_result(ctx.builder, &ctx.previous_instruction); ctx.builder.const_i32(if opsize == OPSIZE_32 { 0x8000_0000u32 as i32 diff --git a/src/rust/jit.rs b/src/rust/jit.rs index 924d513b..18eec8f5 100644 --- a/src/rust/jit.rs +++ b/src/rust/jit.rs @@ -244,6 +244,14 @@ pub enum InstructionOperand { Immediate(i32), Other, } +impl InstructionOperand { + pub fn is_zero(&self) -> bool { + match self { + InstructionOperand::Immediate(0) => true, + _ => false, + } + } +} impl Into for InstructionOperandDest { fn into(self: InstructionOperandDest) -> InstructionOperand { match self {