From 8f9a8cac49aafb3fbc9dde78f0f6125c390c32f0 Mon Sep 17 00:00:00 2001 From: HodanPlodky <36966616+HodanPlodky@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:40:11 +0200 Subject: [PATCH] refactor[venom]: move commutative instruction set (#4307) this commit moves the commutative instruction set from `venom_to_assembly.py` into `basicblock.py` and adds a convenience method in `IRInstruction`. --- vyper/venom/basicblock.py | 6 ++++++ vyper/venom/venom_to_assembly.py | 5 +---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/vyper/venom/basicblock.py b/vyper/venom/basicblock.py index 799dcfb33b..eb4f7e67ba 100644 --- a/vyper/venom/basicblock.py +++ b/vyper/venom/basicblock.py @@ -82,6 +82,8 @@ CFG_ALTERING_INSTRUCTIONS = frozenset(["jmp", "djmp", "jnz"]) +COMMUTATIVE_INSTRUCTIONS = frozenset(["add", "mul", "smul", "or", "xor", "and", "eq"]) + if TYPE_CHECKING: from vyper.venom.function import IRFunction @@ -235,6 +237,10 @@ def __init__( def is_volatile(self) -> bool: return self.opcode in VOLATILE_INSTRUCTIONS + @property + def is_commutative(self) -> bool: + return self.opcode in COMMUTATIVE_INSTRUCTIONS + @property def is_bb_terminator(self) -> bool: return self.opcode in BB_TERMINATORS diff --git a/vyper/venom/venom_to_assembly.py b/vyper/venom/venom_to_assembly.py index ee4b83201d..264ec35eee 100644 --- a/vyper/venom/venom_to_assembly.py +++ b/vyper/venom/venom_to_assembly.py @@ -103,9 +103,6 @@ ] ) -COMMUTATIVE_INSTRUCTIONS = frozenset(["add", "mul", "smul", "or", "xor", "and", "eq"]) - - _REVERT_POSTAMBLE = ["_sym___revert", "JUMPDEST", *PUSH(0), "DUP1", "REVERT"] @@ -431,7 +428,7 @@ def _generate_evm_for_instruction( # the same variable, however, before a jump that is not possible self._stack_reorder(assembly, stack, list(target_stack)) - if opcode in COMMUTATIVE_INSTRUCTIONS: + if inst.is_commutative: cost_no_swap = self._stack_reorder([], stack, operands, dry_run=True) operands[-1], operands[-2] = operands[-2], operands[-1] cost_with_swap = self._stack_reorder([], stack, operands, dry_run=True)