Skip to content

Commit 56a3ccd

Browse files
awesomeklingkalenikaliaksandr
authored andcommitted
LibJS/Bytecode: Turn UnaryMinus(NumericLiteral) into a constant
1 parent e37feaa commit 56a3ccd

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> LogicalExpression::gene
269269
Bytecode::CodeGenerationErrorOr<Optional<ScopedOperand>> UnaryExpression::generate_bytecode(Bytecode::Generator& generator, Optional<ScopedOperand> preferred_dst) const
270270
{
271271
Bytecode::Generator::SourceLocationScope scope(generator, *this);
272+
273+
// OPTIMIZATION: Turn expressions like `-1` into a constant.
274+
if (m_op == UnaryOp::Minus && is<NumericLiteral>(*m_lhs)) {
275+
auto& numeric_literal = static_cast<NumericLiteral const&>(*m_lhs);
276+
auto value = numeric_literal.value();
277+
return generator.add_constant(Value(-value.as_double()));
278+
}
279+
272280
if (m_op == UnaryOp::Delete)
273281
return generator.emit_delete_reference(m_lhs);
274282

0 commit comments

Comments
 (0)