From 37841eb0c9557e6e5a37500ceca790c05110ac6a Mon Sep 17 00:00:00 2001 From: Fabian Date: Sat, 5 Nov 2022 15:04:21 -0600 Subject: [PATCH] optimise and/or/xor/test; jc/jo/jbe/jl/jle --- src/rust/codegen.rs | 41 ++++++++++++++++++++++++++++++------ src/rust/jit.rs | 3 +-- src/rust/jit_instructions.rs | 14 ++++++------ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/rust/codegen.rs b/src/rust/codegen.rs index 58819029..321c8bce 100644 --- a/src/rust/codegen.rs +++ b/src/rust/codegen.rs @@ -350,7 +350,7 @@ fn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruc opsize: OPSIZE_32, .. } - | Instruction::Arithmetic { + | Instruction::Bitwise { dest: InstructionOperandDest::WasmLocal(l), opsize: OPSIZE_32, } => builder.get_local(&l), @@ -1642,7 +1642,7 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.eqz_i32(); } }, - Instruction::Arithmetic { opsize, .. } => { + Instruction::Bitwise { opsize, .. } => { let &opsize = opsize; gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); // Note: Necessary because test{8,16} don't mask their neither last_result nor any of their operands @@ -1662,7 +1662,7 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.eqz_i32(); } }, - _ => { + &Instruction::Other => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_get_flags_changed(ctx.builder); ctx.builder.const_i32(FLAG_ZERO); @@ -1747,7 +1747,12 @@ pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.eqz_i32(); } }, - _ => { + Instruction::Bitwise { .. } => { + gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); + ctx.builder + .const_i32(if negate == ConditionNegate::True { 1 } else { 0 }); + }, + &Instruction::Other => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_getcf_unoptimised(ctx); @@ -1795,7 +1800,7 @@ pub fn gen_getsf(ctx: &mut JitContext, negate: ConditionNegate) { Instruction::Cmp { opsize, .. } | Instruction::Sub { opsize, .. } | Instruction::Add { opsize, .. } - | Instruction::Arithmetic { opsize, .. } => { + | Instruction::Bitwise { opsize, .. } => { let &opsize = opsize; gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); gen_get_last_result(ctx.builder, &ctx.previous_instruction); @@ -1818,7 +1823,7 @@ pub fn gen_getsf(ctx: &mut JitContext, negate: ConditionNegate) { } } }, - Instruction::Other => { + &Instruction::Other => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_get_flags_changed(ctx.builder); ctx.builder.const_i32(FLAG_SIGN); @@ -1899,7 +1904,11 @@ pub fn gen_getof(ctx: &mut JitContext) { }); ctx.builder.and_i32(); }, - Instruction::Arithmetic { .. } | Instruction::Other => { + Instruction::Bitwise { .. } => { + gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); + ctx.builder.const_i32(0); + }, + &Instruction::Other => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_get_flags_changed(ctx.builder); let flags_changed = ctx.builder.tee_new_local(); @@ -2031,6 +2040,10 @@ pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.leu_i32(); } }, + &Instruction::Bitwise { .. } => { + gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); + gen_getzf(ctx, negate); + }, _ => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_getcf(ctx, ConditionNegate::False); @@ -2143,6 +2156,10 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.lt_i32(); } }, + &Instruction::Bitwise { .. } => { + gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); + gen_getsf(ctx, negate); + }, _ => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_getsf(ctx, ConditionNegate::False); @@ -2257,6 +2274,16 @@ pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) { ctx.builder.le_i32(); } }, + &Instruction::Bitwise { .. } => { + gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED); + // TODO: Could probably be improved (<= 0) + gen_test_l(ctx, ConditionNegate::False); + gen_getzf(ctx, ConditionNegate::False); + ctx.builder.or_i32(); + if negate == ConditionNegate::True { + ctx.builder.eqz_i32(); + } + }, _ => { gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED); gen_test_l(ctx, ConditionNegate::False); diff --git a/src/rust/jit.rs b/src/rust/jit.rs index c01d1f4a..bfdb5e60 100644 --- a/src/rust/jit.rs +++ b/src/rust/jit.rs @@ -278,8 +278,7 @@ pub enum Instruction { opsize: i32, is_inc: bool, }, - // Any instruction that sets last_result - Arithmetic { + Bitwise { dest: InstructionOperandDest, opsize: i32, }, diff --git a/src/rust/jit_instructions.rs b/src/rust/jit_instructions.rs index 50ebcba8..fbd31616 100644 --- a/src/rust/jit_instructions.rs +++ b/src/rust/jit_instructions.rs @@ -1268,7 +1268,7 @@ fn gen_sbb32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Lo } fn gen_and8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_8, dest: local_to_instruction_operand(ctx, dest_operand), }; @@ -1290,7 +1290,7 @@ fn gen_and8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_and32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_32, dest: local_to_instruction_operand(ctx, dest_operand), }; @@ -1316,7 +1316,7 @@ fn gen_test( size: i32, ) { let is_self_test = source_operand.eq_local(dest_operand); - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: size, dest: if is_self_test { local_to_instruction_operand(ctx, dest_operand) @@ -1355,7 +1355,7 @@ fn gen_test32(ctx: &mut JitContext, dest: &WasmLocal, source: &LocalOrImmediate) } fn gen_or8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_8, dest: local_to_instruction_operand(ctx, dest_operand), }; @@ -1377,7 +1377,7 @@ fn gen_or8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loca .load_fixed_u8(global_pointers::last_result as u32); } fn gen_or32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_32, dest: local_to_instruction_operand(ctx, dest_operand), }; @@ -1397,7 +1397,7 @@ fn gen_or32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc } fn gen_xor8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_8, dest: local_to_instruction_operand(ctx, dest_operand), }; @@ -1419,7 +1419,7 @@ fn gen_xor8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc .load_fixed_u8(global_pointers::last_result as u32); } fn gen_xor32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &LocalOrImmediate) { - ctx.current_instruction = Instruction::Arithmetic { + ctx.current_instruction = Instruction::Bitwise { opsize: OPSIZE_32, dest: local_to_instruction_operand(ctx, dest_operand), };