Skip to content

Commit

Permalink
refactor[venom]: move commutative instruction set (vyperlang#4307)
Browse files Browse the repository at this point in the history
this commit moves the commutative instruction set from
`venom_to_assembly.py` into `basicblock.py` and adds a convenience
method in `IRInstruction`.
  • Loading branch information
HodanPlodky authored Oct 15, 2024
1 parent 990a6fa commit 8f9a8ca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 6 additions & 0 deletions vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions vyper/venom/venom_to_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@
]
)

COMMUTATIVE_INSTRUCTIONS = frozenset(["add", "mul", "smul", "or", "xor", "and", "eq"])


_REVERT_POSTAMBLE = ["_sym___revert", "JUMPDEST", *PUSH(0), "DUP1", "REVERT"]


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8f9a8ca

Please sign in to comment.