LibJS: Skip allocating locals for arguments that allowed to be local

This allows us to get rid of instructions that move arguments to locals
and allocate smaller JS::Value vector in ExecutionContext by reusing
slots that were already allocated for arguments.

With this change for following function:
```js
function f(x, y) {
    return x + y;
}
```

we now produce following bytecode:
```
[   0]    0: Add dst:reg6, lhs:arg0, rhs:arg1
[  10]       Return value:reg6
```

instead of:
```
[   0]    0: GetArgument 0, dst:x~1
[  10]       GetArgument 1, dst:y~0
[  20]       Add dst:reg6, lhs:x~1, rhs:y~0
[  30]       Return value:reg6
```
This commit is contained in:
Aliaksandr Kalenik 2025-04-25 17:10:47 +02:00 committed by Andreas Kling
commit 2d732b2251
Notes: github-actions[bot] 2025-04-26 09:03:23 +00:00
6 changed files with 102 additions and 39 deletions

View file

@ -44,14 +44,15 @@ public:
CodeGenerationErrorOr<void> emit_function_declaration_instantiation(ECMAScriptFunctionObject const& function);
[[nodiscard]] ScopedOperand allocate_register();
[[nodiscard]] ScopedOperand local(u32 local_index);
[[nodiscard]] ScopedOperand local(Identifier::Local const&);
[[nodiscard]] ScopedOperand accumulator();
[[nodiscard]] ScopedOperand this_value();
void free_register(Register);
void set_local_initialized(u32 local_index);
void set_local_initialized(Identifier::Local const&);
[[nodiscard]] bool is_local_initialized(u32 local_index) const;
[[nodiscard]] bool is_local_initialized(Identifier::Local const&) const;
class SourceLocationScope {
public:
@ -411,6 +412,7 @@ private:
Vector<ScopedOperand> m_home_objects;
HashTable<u32> m_initialized_locals;
HashTable<u32> m_initialized_arguments;
bool m_finished { false };
bool m_must_propagate_completion { true };