Skip to content

Commit db3c7b3

Browse files
committed
ZJIT: Add codegen for IsNil
1 parent f53ba90 commit db3c7b3

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

test/ruby/test_zjit.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,17 @@ def test = Foo
830830
}, call_threshold: 2
831831
end
832832

833+
def test_branchnil
834+
assert_compiles '1', %q{
835+
def test(x)
836+
x.to_i&.itself
837+
end
838+
839+
test(1)
840+
test(1)
841+
}, call_threshold: 2
842+
end
843+
833844
# tool/ruby_vm/views/*.erb relies on the zjit instructions a) being contiguous and
834845
# b) being reliably ordered after all the other instructions.
835846
def test_instruction_order

zjit/src/codegen.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
272272
Insn::FixnumLe { left, right } => gen_fixnum_le(asm, opnd!(left), opnd!(right))?,
273273
Insn::FixnumGt { left, right } => gen_fixnum_gt(asm, opnd!(left), opnd!(right))?,
274274
Insn::FixnumGe { left, right } => gen_fixnum_ge(asm, opnd!(left), opnd!(right))?,
275+
Insn::IsNil { val } => gen_isnil(asm, opnd!(val))?,
275276
Insn::Test { val } => gen_test(asm, opnd!(val))?,
276277
Insn::GuardType { val, guard_type, state } => gen_guard_type(jit, asm, opnd!(val), *guard_type, &function.frame_state(*state))?,
277278
Insn::GuardBitEquals { val, expected, state } => gen_guard_bit_equals(jit, asm, opnd!(val), *expected, &function.frame_state(*state))?,
@@ -890,6 +891,12 @@ fn gen_fixnum_ge(asm: &mut Assembler, left: lir::Opnd, right: lir::Opnd) -> Opti
890891
Some(asm.csel_ge(Qtrue.into(), Qfalse.into()))
891892
}
892893

894+
// Compile val == nil
895+
fn gen_isnil(asm: &mut Assembler, val: lir::Opnd) -> Option<lir::Opnd> {
896+
asm.cmp(val, Qnil.into());
897+
Some(asm.csel_e(Opnd::Imm(1),Opnd::Imm(0)))
898+
}
899+
893900
fn gen_anytostring(asm: &mut Assembler, val: lir::Opnd, str: lir::Opnd, state: &FrameState) -> Option<lir::Opnd> {
894901
// Save PC
895902
gen_save_pc(asm, state);

0 commit comments

Comments
 (0)