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

added ability to iterate over basic block's predecessors #776

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions llvmlite/ir/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,17 @@ def switch(self, value, default):
self._set_terminator(swt)
return swt

def add_pred(self, block):
if not self.basic_block in block.predecessors:
block.predecessors.append(self.basic_block)

def branch(self, target):
"""
Unconditional branch to *target*.
"""
br = instructions.Branch(self.block, "br", [target])
self._set_terminator(br)
self.add_pred(target)
return br

def cbranch(self, cond, truebr, falsebr):
Expand All @@ -837,6 +842,8 @@ def cbranch(self, cond, truebr, falsebr):
br = instructions.ConditionalBranch(self.block, "br",
[cond, truebr, falsebr])
self._set_terminator(br)
self.add_pred(truebr)
self.add_pred(falsebr)
return br

def branch_indirect(self, addr):
Expand Down
4 changes: 4 additions & 0 deletions llvmlite/ir/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,15 @@ def __init__(self, parent, name=''):
self.scope = parent.scope
self.instructions = []
self.terminator = None
self.predecessors = []

@property
def is_terminated(self):
return self.terminator is not None

def has_predecessors(self):
return len(self.predecessors) > 0

@property
def function(self):
return self.parent
Expand Down