LibJS/JIT: Add fast path for LessThan Int32 < Int32

This uses a new branch_if_both_int32() helper.

It's interesting to note that we can compare encoded Int32 values
without stripping the INT32_TAG, since it doesn't affect signedness
of values.
This commit is contained in:
Andreas Kling 2023-10-23 10:52:57 +02:00
parent 895c613400
commit 1fb95c7df9
Notes: sideshowbarker 2024-07-16 18:03:21 +09:00
3 changed files with 120 additions and 3 deletions

View file

@ -298,6 +298,17 @@ struct Assembler {
label.offset_in_instruction_stream = m_output.size();
}
void jump_if_less_than(Operand lhs, Operand rhs, Label& label)
{
cmp(lhs, rhs);
// jl label (RIP-relative 32-bit offset)
emit8(0x0f);
emit8(0x8c);
emit32(0xdeadbeef);
label.offset_in_instruction_stream = m_output.size();
}
void bitwise_and(Operand dst, Operand src)
{
// and dst,src

View file

@ -187,6 +187,35 @@ void Compiler::branch_if_int32(Assembler::Reg reg, Codegen codegen)
not_int32_case.link(m_assembler);
}
template<typename Codegen>
void Compiler::branch_if_both_int32(Assembler::Reg lhs, Assembler::Reg rhs, Codegen codegen)
{
// GPR0 = lhs >> 48;
m_assembler.mov(Assembler::Operand::Register(GPR0), Assembler::Operand::Register(lhs));
m_assembler.shift_right(Assembler::Operand::Register(GPR0), Assembler::Operand::Imm8(48));
// GPR1 = rhs >> 48;
m_assembler.mov(Assembler::Operand::Register(GPR1), Assembler::Operand::Register(rhs));
m_assembler.shift_right(Assembler::Operand::Register(GPR1), Assembler::Operand::Imm8(48));
// FIXME: Use one label once Assembler::Label supports multiple jumps to it.
auto not_int32_case1 = m_assembler.make_label();
auto not_int32_case2 = m_assembler.make_label();
m_assembler.jump_if_not_equal(
Assembler::Operand::Register(GPR0),
Assembler::Operand::Imm32(INT32_TAG),
not_int32_case1);
m_assembler.jump_if_not_equal(
Assembler::Operand::Register(GPR1),
Assembler::Operand::Imm32(INT32_TAG),
not_int32_case2);
codegen();
not_int32_case1.link(m_assembler);
not_int32_case2.link(m_assembler);
}
void Compiler::compile_increment(Bytecode::Op::Increment const&)
{
load_vm_register(ARG1, Bytecode::Register::accumulator());
@ -422,9 +451,55 @@ static ThrowCompletionOr<Value> typed_equals(VM&, Value src1, Value src2)
check_exception(); \
}
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
#undef DO_COMPILE_COMMON_BINARY_OP
static Value cxx_less_than(VM& vm, Value lhs, Value rhs)
{
return TRY_OR_SET_EXCEPTION(less_than(vm, lhs, rhs));
}
void Compiler::compile_less_than(Bytecode::Op::LessThan const& op)
{
load_vm_register(ARG1, op.lhs());
load_vm_register(ARG2, Bytecode::Register::accumulator());
// FIXME: Unify when we have multi-jump labels.
auto end1 = m_assembler.make_label();
auto end2 = m_assembler.make_label();
branch_if_both_int32(ARG1, ARG2, [&] {
// if (ARG1 < ARG2) return true;
// else return false;
auto true_case = m_assembler.make_label();
m_assembler.jump_if_less_than(
Assembler::Operand::Register(ARG1),
Assembler::Operand::Register(ARG2),
true_case);
m_assembler.mov(
Assembler::Operand::Register(GPR0),
Assembler::Operand::Imm64(Value(false).encoded()));
store_vm_register(Bytecode::Register::accumulator(), GPR0);
m_assembler.jump(end1);
true_case.link(m_assembler);
m_assembler.mov(
Assembler::Operand::Register(GPR0),
Assembler::Operand::Imm64(Value(true).encoded()));
store_vm_register(Bytecode::Register::accumulator(), GPR0);
m_assembler.jump(end2);
});
m_assembler.native_call((void*)cxx_less_than);
store_vm_register(Bytecode::Register::accumulator(), RET);
check_exception();
end1.link(m_assembler);
end2.link(m_assembler);
}
static ThrowCompletionOr<Value> not_(VM&, Value value)
{
return Value(!value.to_boolean());
@ -850,12 +925,15 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
case Bytecode::Instruction::Type::SetVariable:
compiler.compile_set_variable(static_cast<Bytecode::Op::SetVariable const&>(op));
break;
case Bytecode::Instruction::Type::LessThan:
compiler.compile_less_than(static_cast<Bytecode::Op::LessThan const&>(op));
break;
#define DO_COMPILE_COMMON_BINARY_OP(TitleCaseName, snake_case_name) \
case Bytecode::Instruction::Type::TitleCaseName: \
compiler.compile_##snake_case_name(static_cast<Bytecode::Op::TitleCaseName const&>(op)); \
break;
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
#undef DO_COMPILE_COMMON_BINARY_OP
#define DO_COMPILE_COMMON_UNARY_OP(TitleCaseName, snake_case_name) \

View file

@ -47,10 +47,33 @@ private:
void compile_to_numeric(Bytecode::Op::ToNumeric const&);
void compile_resolve_this_binding(Bytecode::Op::ResolveThisBinding const&);
#define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
O(Add, add) \
O(Sub, sub) \
O(Mul, mul) \
O(Div, div) \
O(Exp, exp) \
O(Mod, mod) \
O(In, in) \
O(InstanceOf, instance_of) \
O(GreaterThan, greater_than) \
O(GreaterThanEquals, greater_than_equals) \
O(LessThanEquals, less_than_equals) \
O(LooselyInequals, abstract_inequals) \
O(LooselyEquals, abstract_equals) \
O(StrictlyInequals, typed_inequals) \
O(StrictlyEquals, typed_equals) \
O(BitwiseAnd, bitwise_and) \
O(BitwiseOr, bitwise_or) \
O(BitwiseXor, bitwise_xor) \
O(LeftShift, left_shift) \
O(RightShift, right_shift) \
O(UnsignedRightShift, unsigned_right_shift)
#define DO_COMPILE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
void compile_##op_snake_case(Bytecode::Op::OpTitleCase const&);
JS_ENUMERATE_COMMON_BINARY_OPS(DO_COMPILE_COMMON_BINARY_OP)
JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(DO_COMPILE_COMMON_BINARY_OP)
#undef DO_COMPILE_COMMON_BINARY_OP
#define DO_COMPILE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
@ -59,6 +82,8 @@ private:
JS_ENUMERATE_COMMON_UNARY_OPS(DO_COMPILE_COMMON_UNARY_OP)
#undef DO_COMPILE_COMMON_UNARY_OP
void compile_less_than(Bytecode::Op::LessThan const&);
void compile_return(Bytecode::Op::Return const&);
void compile_new_string(Bytecode::Op::NewString const&);
void compile_new_object(Bytecode::Op::NewObject const&);
@ -92,6 +117,9 @@ private:
template<typename Codegen>
void branch_if_int32(Assembler::Reg, Codegen);
template<typename Codegen>
void branch_if_both_int32(Assembler::Reg, Assembler::Reg, Codegen);
explicit Compiler(Bytecode::Executable& bytecode_executable)
: m_bytecode_executable(bytecode_executable)
{