Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor[venom]: move commutative instruction set #4307

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading