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

feat[venom]: strip superfluous instructions from basic blocks #4043

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion vyper/venom/basicblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from vyper.utils import OrderedSet

# instructions which can terminate a basic block
BB_TERMINATORS = frozenset(["jmp", "djmp", "jnz", "ret", "return", "stop", "exit"])
BB_TERMINATORS = frozenset(["jmp", "djmp", "jnz", "ret", "return", "stop", "revert", "exit"])

VOLATILE_INSTRUCTIONS = frozenset(
[
Expand Down
31 changes: 29 additions & 2 deletions vyper/venom/passes/simplify_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,39 @@ def _optimize_empty_basicblocks(self) -> int:

return count

def _strip_superfluous_instructions(self) -> int:
"""
Strip superfluous instructions from basic blocks.
"""
fn = self.function
worklist = list(fn.get_basic_blocks())
i = count = 0
while i < len(worklist):
bb = worklist[i]
i += 1

for inst in bb.instructions:
if inst.opcode == "nop":
bb.remove_instruction(inst)
count += 1
elif inst.opcode == "revert":
# Remove all instructions after a revert unless the next instruction is an exit
idx = bb.instructions.index(inst)
if idx + 1 < len(bb.instructions) and bb.instructions[idx + 1].opcode != "exit":
bb.instructions = bb.instructions[: idx + 1]
count += 1

return count

def run_pass(self):
fn = self.function
entry = fn.entry

for _ in range(fn.num_basic_blocks):
changes = self._optimize_empty_basicblocks()
max_iterations = sum([len(bb.instructions) for bb in fn.get_basic_blocks()])

for _ in range(max_iterations):
changes = self._strip_superfluous_instructions()
changes += self._optimize_empty_basicblocks()
changes += fn.remove_unreachable_blocks()
if changes == 0:
break
Expand Down
Loading