From 96511a7d19a82a84abfc3630155b4fc67220b082 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 9 May 2024 19:52:38 +0200 Subject: [PATCH] LibJS/Bytecode: Avoid unnecessary copy of call expression callee If the callee is already a temporary register, we don't need to copy it to *another* temporary before evaluating arguments. None of the arguments will clobber the existing temporary anyway. --- Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 8a6af4cf0d..e15c5a879a 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1652,9 +1652,16 @@ Bytecode::CodeGenerationErrorOr> CallExpression::generat original_callee = TRY(m_callee->generate_bytecode(generator)).value(); } - // NOTE: We copy the callee to a new register to avoid overwriting it while evaluating arguments. - auto callee = generator.allocate_register(); - generator.emit(callee, *original_callee); + // NOTE: If the callee isn't already a temporary, we copy it to a new register + // to avoid overwriting it while evaluating arguments. + auto callee = [&]() -> ScopedOperand { + if (!original_callee->operand().is_register()) { + auto callee = generator.allocate_register(); + generator.emit(callee, *original_callee); + return callee; + } + return *original_callee; + }(); Bytecode::Op::CallType call_type; if (is(*this)) {