Skip to content

Commit

Permalink
Merge branch 'feature/venom_updates' of github.com:harkal/vyper into …
Browse files Browse the repository at this point in the history
…feature/venom_updates
  • Loading branch information
harkal committed Mar 19, 2024
2 parents 477643e + 41474df commit 5f4f548
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions vyper/venom/passes/simplify_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ def _merge_blocks(self, a: IRBasicBlock, b: IRBasicBlock):
# Update CFG
a.cfg_out = b.cfg_out
if len(b.cfg_out) > 0:
next = b.cfg_out.first()
next.remove_cfg_in(b)
next.add_cfg_in(a)
next_bb = b.cfg_out.first()
next_bb.remove_cfg_in(b)
next_bb.add_cfg_in(a)

self.ctx.basic_blocks.remove(b)

def _merge_jump(self, a: IRBasicBlock, b: IRBasicBlock):
next = b.cfg_out.first()
next_bb = 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
jump_inst.operands[jump_inst.operands.index(b.label)] = next_bb.label

# Update CFG
a.remove_cfg_out(b)
a.add_cfg_out(next)
next.remove_cfg_in(b)
next.add_cfg_in(a)
a.add_cfg_out(next_bb)
next_bb.remove_cfg_in(b)
next_bb.add_cfg_in(a)

self.ctx.basic_blocks.remove(b)

Expand All @@ -45,16 +45,20 @@ def _collapse_chained_blocks_r(self, bb: IRBasicBlock):
DFS into the cfg and collapse blocks with a single predecessor to the predecessor
"""
if len(bb.cfg_out) == 1:
next = bb.cfg_out.first()
if len(next.cfg_in) == 1:
self._merge_blocks(bb, next)
next_bb = bb.cfg_out.first()
if len(next_bb.cfg_in) == 1:
self._merge_blocks(bb, next_bb)
self._collapse_chained_blocks_r(bb)
return
elif len(bb.cfg_out) > 1:
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)
for next_bb in bb_out:
if (
len(next_bb.cfg_in) == 1
and len(next_bb.cfg_out) == 1
and len(next_bb.instructions) == 1
):
self._merge_jump(bb, next_bb)
self._collapse_chained_blocks_r(bb)
return

Expand Down

0 comments on commit 5f4f548

Please sign in to comment.