From 60b555107f30b739c55ed43eaaf83bc0f19dde63 Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 4 Nov 2022 18:36:30 -0600 Subject: [PATCH] identify add for the purpose of optimising conditions --- src/rust/codegen.rs | 12 +++++++++--- src/rust/jit.rs | 5 +++++ src/rust/jit_instructions.rs | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/rust/codegen.rs b/src/rust/codegen.rs index 0ff037a9..ff245e86 100644 --- a/src/rust/codegen.rs +++ b/src/rust/codegen.rs @@ -340,7 +340,12 @@ fn gen_get_flags_changed(builder: &mut WasmBuilder) { } fn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruction) { match previous_instruction { - Instruction::Sub { + Instruction::Add { + dest: InstructionOperandDest::WasmLocal(l), + opsize: OPSIZE_32, + .. + } + | Instruction::Sub { dest: InstructionOperandDest::WasmLocal(l), opsize: OPSIZE_32, .. @@ -1630,7 +1635,7 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { } } }, - Instruction::Cmp { .. } | Instruction::Sub { .. } => { + Instruction::Cmp { .. } | Instruction::Sub { .. } | Instruction::Add { .. } => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); gen_get_last_result(ctx.builder, &ctx.previous_instruction); if negate == ConditionNegate::False { @@ -1757,6 +1762,7 @@ pub fn gen_getsf(ctx: &mut JitContext, negate: ConditionNegate) { match &ctx.previous_instruction { Instruction::Cmp { opsize, .. } | Instruction::Sub { opsize, .. } + | Instruction::Add { opsize, .. } | Instruction::Arithmetic { opsize, .. } => { let &opsize = opsize; gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); @@ -1834,7 +1840,7 @@ pub fn gen_getof(ctx: &mut JitContext) { }); ctx.builder.and_i32(); }, - &Instruction::Other | Instruction::Arithmetic { .. } => { + Instruction::Add { .. } | Instruction::Arithmetic { .. } | Instruction::Other => { // TODO: add gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_get_flags_changed(ctx.builder); diff --git a/src/rust/jit.rs b/src/rust/jit.rs index a7ec551b..8e4a8b7e 100644 --- a/src/rust/jit.rs +++ b/src/rust/jit.rs @@ -271,6 +271,11 @@ pub enum Instruction { source: InstructionOperand, opsize: i32, }, + Add { + dest: InstructionOperandDest, + source: InstructionOperand, + opsize: i32, + }, // Any instruction that sets last_result Arithmetic { dest: InstructionOperandDest, diff --git a/src/rust/jit_instructions.rs b/src/rust/jit_instructions.rs index 4663a2e4..d179f48b 100644 --- a/src/rust/jit_instructions.rs +++ b/src/rust/jit_instructions.rs @@ -954,9 +954,15 @@ macro_rules! define_instruction_read_write_mem32( ); fn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Add { opsize: OPSIZE_8, dest: local_to_instruction_operand(ctx, dest_operand), + source: if source_operand.eq_local(dest_operand) { + InstructionOperand::Other // aliasing + } + else { + source_operand.to_instruction_operand(ctx) + }, }; ctx.builder.const_i32(global_pointers::last_op1 as i32); @@ -979,9 +985,15 @@ fn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_add32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Add { opsize: OPSIZE_32, dest: local_to_instruction_operand(ctx, dest_operand), + source: if source_operand.eq_local(dest_operand) { + InstructionOperand::Other // aliasing + } + else { + source_operand.to_instruction_operand(ctx) + }, }; codegen::gen_set_last_op1(ctx.builder, &dest_operand);