optimise inc/dec; jcc

This commit is contained in:
Fabian 2022-11-05 14:58:38 -06:00
parent 625ad2802c
commit da1c713252
3 changed files with 37 additions and 15 deletions

View file

@ -1698,7 +1698,13 @@ pub fn gen_getzf(ctx: &mut JitContext, negate: ConditionNegate) {
pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {
match &ctx.previous_instruction {
Instruction::Cmp { source, opsize, .. } | Instruction::Sub { source, opsize, .. } => {
Instruction::Cmp { source, opsize, .. }
| Instruction::Sub {
source,
opsize,
is_dec: false,
..
} => {
// Note: x < y and x < x - y can be used interchangeably (see getcf)
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
@ -1714,7 +1720,12 @@ pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.ltu_i32();
}
},
Instruction::Add { source, opsize, .. } => {
Instruction::Add {
source,
opsize,
is_inc: false,
..
} => {
gen_get_last_result(ctx.builder, &ctx.previous_instruction);
match (opsize, source) {
(&OPSIZE_32, InstructionOperand::WasmLocal(l)) => ctx.builder.get_local(l),
@ -1728,6 +1739,14 @@ pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.ltu_i32();
}
},
Instruction::Add { is_inc: true, .. } | Instruction::Sub { is_dec: true, .. } => {
gen_get_flags(ctx.builder);
ctx.builder.const_i32(FLAG_CARRY);
ctx.builder.and_i32();
if negate == ConditionNegate::True {
ctx.builder.eqz_i32();
}
},
_ => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_UNOPTIMISED);
gen_getcf_unoptimised(ctx);
@ -1983,8 +2002,9 @@ pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) {
},
Instruction::Sub {
opsize,
dest: _,
source,
is_dec: false,
..
} => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
@ -2084,11 +2104,7 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.lt_i32();
}
},
Instruction::Sub {
opsize,
dest: _,
source,
} => {
Instruction::Sub { opsize, source, .. } => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
@ -2202,11 +2218,7 @@ pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.le_i32();
}
},
Instruction::Sub {
opsize,
dest: _,
source,
} => {
Instruction::Sub { opsize, source, .. } => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
gen_get_last_op1(ctx.builder, &ctx.previous_instruction);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {

View file

@ -270,11 +270,13 @@ pub enum Instruction {
dest: InstructionOperandDest,
source: InstructionOperand,
opsize: i32,
is_dec: bool,
},
Add {
dest: InstructionOperandDest,
source: InstructionOperand,
opsize: i32,
is_inc: bool,
},
// Any instruction that sets last_result
Arithmetic {

View file

@ -963,6 +963,7 @@ fn gen_add8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc
else {
source_operand.to_instruction_operand(ctx)
},
is_inc: false,
};
ctx.builder.const_i32(global_pointers::last_op1 as i32);
@ -994,6 +995,7 @@ fn gen_add32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Lo
else {
source_operand.to_instruction_operand(ctx)
},
is_inc: false,
};
codegen::gen_set_last_op1(ctx.builder, &dest_operand);
@ -1017,6 +1019,7 @@ fn gen_sub8(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Loc
else {
source_operand.to_instruction_operand(ctx)
},
is_dec: false,
};
ctx.builder.const_i32(global_pointers::last_op1 as i32);
@ -1048,6 +1051,7 @@ fn gen_sub32(ctx: &mut JitContext, dest_operand: &WasmLocal, source_operand: &Lo
else {
source_operand.to_instruction_operand(ctx)
},
is_dec: false,
};
codegen::gen_set_last_op1(ctx.builder, &dest_operand);
@ -2330,9 +2334,11 @@ fn gen_inc(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) {
}
ctx.builder.store_aligned_i32(0);
codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, size, FLAGS_ALL & !1);
ctx.current_instruction = Instruction::Arithmetic {
ctx.current_instruction = Instruction::Add {
opsize: size,
dest: local_to_instruction_operand(ctx, dest_operand),
source: InstructionOperand::Immediate(1),
is_inc: true,
};
}
fn gen_inc16(ctx: &mut JitContext, dest_operand: &WasmLocal) {
@ -2378,9 +2384,11 @@ fn gen_dec(ctx: &mut JitContext, dest_operand: &WasmLocal, size: i32) {
}
ctx.builder.store_aligned_i32(0);
codegen::gen_set_last_op_size_and_flags_changed(ctx.builder, size, FLAGS_ALL & !1 | FLAG_SUB);
ctx.current_instruction = Instruction::Arithmetic {
ctx.current_instruction = Instruction::Sub {
opsize: size,
dest: local_to_instruction_operand(ctx, dest_operand),
source: InstructionOperand::Immediate(1),
is_dec: true,
};
}
fn gen_dec16(ctx: &mut JitContext, dest_operand: &WasmLocal) {