From bb9230bbcd7b4f8152a4b2188285b4c1fdf3969f Mon Sep 17 00:00:00 2001 From: Todderod Date: Wed, 8 Nov 2023 20:21:48 +0100 Subject: [PATCH] LibJS: Remove redundant Store op If the property for GetByValue in Generator::load_from_reference is a calculated value this would be stored in an allocated register and returned from the function. Not all callers want this information however, so now only give it out when asked for. Reduced the instruction count for Kraken/ai-astar.js function "neighbours" from 214 to 192. --- .../Libraries/LibJS/Bytecode/ASTCodegen.cpp | 6 +++--- .../Libraries/LibJS/Bytecode/Generator.cpp | 21 ++++++++++++------- Userland/Libraries/LibJS/Bytecode/Generator.h | 6 +++++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index d4d97e52a4c..2e73b7303d8 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -525,7 +525,7 @@ Bytecode::CodeGenerationErrorOr AssignmentExpression::generate_bytecode(By VERIFY(m_lhs.has>()); auto& lhs = m_lhs.get>(); - auto reference_registers = TRY(generator.emit_load_from_reference(lhs)); + auto reference_registers = TRY(generator.emit_load_from_reference(lhs, Bytecode::Generator::CollectRegisters::Yes)); Bytecode::BasicBlock* rhs_block_ptr { nullptr }; Bytecode::BasicBlock* end_block_ptr { nullptr }; @@ -1057,7 +1057,7 @@ Bytecode::CodeGenerationErrorOr ArrayExpression::generate_bytecode(Bytecod Bytecode::CodeGenerationErrorOr MemberExpression::generate_bytecode(Bytecode::Generator& generator) const { Bytecode::Generator::SourceLocationScope scope(generator, *this); - (void)TRY(generator.emit_load_from_reference(*this)); + (void)TRY(generator.emit_load_from_reference(*this, Bytecode::Generator::CollectRegisters::No)); return {}; } @@ -2258,7 +2258,7 @@ Bytecode::CodeGenerationErrorOr TaggedTemplateLiteral::generate_bytecode(B Bytecode::CodeGenerationErrorOr UpdateExpression::generate_bytecode(Bytecode::Generator& generator) const { Bytecode::Generator::SourceLocationScope scope(generator, *this); - auto reference_registers = TRY(generator.emit_load_from_reference(*m_argument)); + auto reference_registers = TRY(generator.emit_load_from_reference(*m_argument, Bytecode::Generator::CollectRegisters::Yes)); Optional previous_value_for_postfix_reg; if (!m_prefixed) { diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.cpp b/Userland/Libraries/LibJS/Bytecode/Generator.cpp index 56735e7f15f..678904f690b 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Generator.cpp @@ -205,7 +205,7 @@ CodeGenerationErrorOr Generator::emit_super_refer }; } -CodeGenerationErrorOr> Generator::emit_load_from_reference(JS::ASTNode const& node) +CodeGenerationErrorOr> Generator::emit_load_from_reference(JS::ASTNode const& node, CollectRegisters collect_registers) { if (is(node)) { auto& identifier = static_cast(node); @@ -238,15 +238,20 @@ CodeGenerationErrorOr> Generator::emit_l emit(object_reg); TRY(expression.property().generate_bytecode(*this)); - auto property_reg = allocate_register(); - emit(property_reg); + Optional property_reg {}; + if (collect_registers == CollectRegisters::Yes) { + property_reg = allocate_register(); + emit(property_reg.value()); + } emit(object_reg); - return ReferenceRegisters { - .base = object_reg, - .referenced_name = property_reg, - .this_value = object_reg, - }; + if (collect_registers == CollectRegisters::Yes) + return ReferenceRegisters { + .base = object_reg, + .referenced_name = property_reg.value(), + .this_value = object_reg, + }; + return Optional {}; } else if (expression.property().is_identifier()) { auto identifier_table_ref = intern_identifier(verify_cast(expression.property()).string()); emit_get_by_id(identifier_table_ref); diff --git a/Userland/Libraries/LibJS/Bytecode/Generator.h b/Userland/Libraries/LibJS/Bytecode/Generator.h index d549da998d0..4945259277a 100644 --- a/Userland/Libraries/LibJS/Bytecode/Generator.h +++ b/Userland/Libraries/LibJS/Bytecode/Generator.h @@ -98,7 +98,11 @@ public: Register this_value; // [[ThisValue]] }; - CodeGenerationErrorOr> emit_load_from_reference(JS::ASTNode const&); + enum class CollectRegisters { + Yes, + No + }; + CodeGenerationErrorOr> emit_load_from_reference(JS::ASTNode const&, CollectRegisters); CodeGenerationErrorOr emit_store_to_reference(JS::ASTNode const&); CodeGenerationErrorOr emit_store_to_reference(ReferenceRegisters const&); CodeGenerationErrorOr emit_delete_reference(JS::ASTNode const&);