Mercurial > lcfOS
diff python/ir/instruction.py @ 252:c4370696ccc7
added optimize function
author | Windel Bouwman |
---|---|
date | Tue, 30 Jul 2013 17:57:46 +0200 |
parents | 63bb40758066 |
children | 04c19282a5aa |
line wrap: on
line diff
--- a/python/ir/instruction.py Mon Jul 29 20:23:13 2013 +0200 +++ b/python/ir/instruction.py Tue Jul 30 17:57:46 2013 +0200 @@ -14,9 +14,17 @@ return '{0}'.format(self.name) # + str(self.IsUsed) @property + def UsedInBlocks(self): + bbs = [i.parent for i in self.used_by + [self.Setter]] + assert all(isinstance(bb, BasicBlock) for bb in bbs) + return set(bbs) + + @property def IsUsed(self): return len(self.used_by) > 0 + Used = IsUsed + def onlyUsedInBlock(self, bb): for use in self.used_by: ins = use @@ -85,24 +93,32 @@ Parent = property(getParent, setParent) def replaceValue(self, old, new): - raise NotImplementedError() + raise NotImplementedError('{}'.format(type(self))) @property def Position(self): return self.parent.Instructions.index(self) + @property + def Function(self): + return self.Block.parent + + @property + def Block(self): + return self.Parent + def check(self): # Check that the variables defined by this instruction # are only used in the same block for v in self.defs: assert v.Setter is self for ub in v.used_by: - assert ub.parent == self.parent + assert ub.Function == self.Function # Check that variables used are defined earlier: for u in self.uses: v = u.val - assert self.Position > v.Setter.Position + #assert self.Position > v.Setter.Position @@ -206,19 +222,20 @@ # Memory functions: class Load(Instruction): - def __init__(self, location, value): - super().__init__() - assert type(value) is Value - assert isinstance(location, Value), "Location must be a value" - self.value = value - self.addDef(value) - self.location = location - self.addUse(self.location) - def __repr__(self): - return '{} = [{}]'.format(self.value, self.location) + def __init__(self, location, value): + super().__init__() + assert type(value) is Value + assert isinstance(location, Value), "Location must be a value" + self.value = value + self.addDef(value) + self.location = location + self.addUse(self.location) + + def __repr__(self): + return '{} = [{}]'.format(self.value, self.location) class Store(Instruction): - def __init__(self, location, value): + def __init__(self, location, value): super().__init__() assert type(value) is Value, value assert isinstance(location, Value), "Location must be a value" @@ -226,8 +243,19 @@ self.value = value self.addUse(value) self.addUse(location) - def __repr__(self): - return '[{}] = {}'.format(self.location, self.value) + + def __repr__(self): + return '[{}] = {}'.format(self.location, self.value) + + def replaceValue(self, old, new): + if old is self.value: + self.value = new + elif old is self.location: + self.location = new + else: + raise Exception() + self.removeUse(old) + self.addUse(new) # Branching: class Branch(Terminator): @@ -270,9 +298,10 @@ self.lab2 = tto class PhiNode(Instruction): - def __init__(self): - super().__init__() - self.incBB = [] - def addIncoming(self, bb): - self.incBB.append(bb) + def __init__(self): + super().__init__() + self.incBB = [] + def addIncoming(self, bb): + self.incBB.append(bb) +