From a5e1e66abc962eb89598462854ba9b24d450ebb3 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 4 Mar 2024 10:01:40 +0100 Subject: [PATCH] LibJS/Bytecode: Add fast path for LeftShift with Int32 operands --- Userland/Libraries/LibJS/Bytecode/Interpreter.cpp | 14 ++++++++++++++ Userland/Libraries/LibJS/Bytecode/Op.h | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index fbcb89d8fd..271ef250ba 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -864,6 +864,20 @@ ThrowCompletionOr RightShift::execute_impl(Bytecode::Interpreter& interpre return {}; } +ThrowCompletionOr LeftShift::execute_impl(Bytecode::Interpreter& interpreter) const +{ + auto& vm = interpreter.vm(); + auto const lhs = interpreter.get(m_lhs); + auto const rhs = interpreter.get(m_rhs); + if (lhs.is_int32() && rhs.is_int32() && rhs.as_i32() >= 0) { + auto const shift_count = static_cast(rhs.as_i32()) % 32; + interpreter.set(m_dst, Value(lhs.as_i32() << shift_count)); + return {}; + } + interpreter.set(m_dst, TRY(left_shift(vm, lhs, rhs))); + return {}; +} + ThrowCompletionOr LessThan::execute_impl(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); diff --git a/Userland/Libraries/LibJS/Bytecode/Op.h b/Userland/Libraries/LibJS/Bytecode/Op.h index 035010f14a..e63d440334 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.h +++ b/Userland/Libraries/LibJS/Bytecode/Op.h @@ -59,6 +59,7 @@ private: O(BitwiseXor, bitwise_xor) \ O(GreaterThan, greater_than) \ O(GreaterThanEquals, greater_than_equals) \ + O(LeftShift, left_shift) \ O(LessThan, less_than) \ O(LessThanEquals, less_than_equals) \ O(Mul, mul) \ @@ -75,8 +76,7 @@ private: O(LooselyInequals, loosely_inequals) \ O(LooselyEquals, loosely_equals) \ O(StrictlyInequals, strict_inequals) \ - O(StrictlyEquals, strict_equals) \ - O(LeftShift, left_shift) + O(StrictlyEquals, strict_equals) #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \ class OpTitleCase final : public Instruction { \