Skip to content

Commit

Permalink
simplify jumps
Browse files Browse the repository at this point in the history
  • Loading branch information
harkal committed Mar 11, 2024
1 parent f2e3871 commit 926eeb3
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion vyper/venom/passes/simplify_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ def _merge_blocks(self, a: IRBasicBlock, b: IRBasicBlock):
n.remove_cfg_in(b)
n.add_cfg_in(a)

self.ctx.basic_blocks.remove(b)

def _merge_jump(self, a: IRBasicBlock, b: IRBasicBlock):
next = b.cfg_out.first()
jump_inst = a.instructions[-1]
assert b.label in jump_inst.operands, f"{b.label} {jump_inst.operands}"
jump_inst.operands[jump_inst.operands.index(b.label)] = next.label
a.remove_cfg_out(b)
a.add_cfg_out(next)
next.remove_cfg_in(b)
next.add_cfg_in(a)
self.ctx.basic_blocks.remove(b)

def _collapse_chained_blocks_r(self, bb: IRBasicBlock):
if len(bb.cfg_out) == 1:
next = bb.cfg_out.first()
if len(next.cfg_in) == 1:
self._merge_blocks(bb, next)
self.ctx.basic_blocks.remove(next)
self._collapse_chained_blocks_r(bb)
return
elif len(bb.cfg_out) == 2:
bb_out = bb.cfg_out.copy()
for next in bb_out:
if len(next.cfg_in) == 1 and len(next.cfg_out) == 1 and len(next.instructions) == 1:
self._merge_jump(bb, next)
self._collapse_chained_blocks_r(bb)
return

if bb in self.visited:
return
Expand Down

0 comments on commit 926eeb3

Please sign in to comment.