Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
251:6ed3d3a82a63 | 252:c4370696ccc7 |
---|---|
12 | 12 |
13 def __repr__(self): | 13 def __repr__(self): |
14 return '{0}'.format(self.name) # + str(self.IsUsed) | 14 return '{0}'.format(self.name) # + str(self.IsUsed) |
15 | 15 |
16 @property | 16 @property |
17 def UsedInBlocks(self): | |
18 bbs = [i.parent for i in self.used_by + [self.Setter]] | |
19 assert all(isinstance(bb, BasicBlock) for bb in bbs) | |
20 return set(bbs) | |
21 | |
22 @property | |
17 def IsUsed(self): | 23 def IsUsed(self): |
18 return len(self.used_by) > 0 | 24 return len(self.used_by) > 0 |
25 | |
26 Used = IsUsed | |
19 | 27 |
20 def onlyUsedInBlock(self, bb): | 28 def onlyUsedInBlock(self, bb): |
21 for use in self.used_by: | 29 for use in self.used_by: |
22 ins = use | 30 ins = use |
23 if ins.parent != bb: | 31 if ins.parent != bb: |
83 def setParent(self, p): | 91 def setParent(self, p): |
84 self.parent = p | 92 self.parent = p |
85 Parent = property(getParent, setParent) | 93 Parent = property(getParent, setParent) |
86 | 94 |
87 def replaceValue(self, old, new): | 95 def replaceValue(self, old, new): |
88 raise NotImplementedError() | 96 raise NotImplementedError('{}'.format(type(self))) |
89 | 97 |
90 @property | 98 @property |
91 def Position(self): | 99 def Position(self): |
92 return self.parent.Instructions.index(self) | 100 return self.parent.Instructions.index(self) |
101 | |
102 @property | |
103 def Function(self): | |
104 return self.Block.parent | |
105 | |
106 @property | |
107 def Block(self): | |
108 return self.Parent | |
93 | 109 |
94 def check(self): | 110 def check(self): |
95 # Check that the variables defined by this instruction | 111 # Check that the variables defined by this instruction |
96 # are only used in the same block | 112 # are only used in the same block |
97 for v in self.defs: | 113 for v in self.defs: |
98 assert v.Setter is self | 114 assert v.Setter is self |
99 for ub in v.used_by: | 115 for ub in v.used_by: |
100 assert ub.parent == self.parent | 116 assert ub.Function == self.Function |
101 | 117 |
102 # Check that variables used are defined earlier: | 118 # Check that variables used are defined earlier: |
103 for u in self.uses: | 119 for u in self.uses: |
104 v = u.val | 120 v = u.val |
105 assert self.Position > v.Setter.Position | 121 #assert self.Position > v.Setter.Position |
106 | 122 |
107 | 123 |
108 | 124 |
109 | 125 |
110 class Terminator(Instruction): | 126 class Terminator(Instruction): |
204 self.removeUse(old) | 220 self.removeUse(old) |
205 self.addUse(new) | 221 self.addUse(new) |
206 | 222 |
207 # Memory functions: | 223 # Memory functions: |
208 class Load(Instruction): | 224 class Load(Instruction): |
209 def __init__(self, location, value): | 225 def __init__(self, location, value): |
210 super().__init__() | 226 super().__init__() |
211 assert type(value) is Value | 227 assert type(value) is Value |
212 assert isinstance(location, Value), "Location must be a value" | 228 assert isinstance(location, Value), "Location must be a value" |
213 self.value = value | 229 self.value = value |
214 self.addDef(value) | 230 self.addDef(value) |
215 self.location = location | 231 self.location = location |
216 self.addUse(self.location) | 232 self.addUse(self.location) |
217 def __repr__(self): | 233 |
218 return '{} = [{}]'.format(self.value, self.location) | 234 def __repr__(self): |
235 return '{} = [{}]'.format(self.value, self.location) | |
219 | 236 |
220 class Store(Instruction): | 237 class Store(Instruction): |
221 def __init__(self, location, value): | 238 def __init__(self, location, value): |
222 super().__init__() | 239 super().__init__() |
223 assert type(value) is Value, value | 240 assert type(value) is Value, value |
224 assert isinstance(location, Value), "Location must be a value" | 241 assert isinstance(location, Value), "Location must be a value" |
225 self.location = location | 242 self.location = location |
226 self.value = value | 243 self.value = value |
227 self.addUse(value) | 244 self.addUse(value) |
228 self.addUse(location) | 245 self.addUse(location) |
229 def __repr__(self): | 246 |
230 return '[{}] = {}'.format(self.location, self.value) | 247 def __repr__(self): |
248 return '[{}] = {}'.format(self.location, self.value) | |
249 | |
250 def replaceValue(self, old, new): | |
251 if old is self.value: | |
252 self.value = new | |
253 elif old is self.location: | |
254 self.location = new | |
255 else: | |
256 raise Exception() | |
257 self.removeUse(old) | |
258 self.addUse(new) | |
231 | 259 |
232 # Branching: | 260 # Branching: |
233 class Branch(Terminator): | 261 class Branch(Terminator): |
234 def __init__(self, target): | 262 def __init__(self, target): |
235 super().__init__() | 263 super().__init__() |
268 self.lab1 = tto | 296 self.lab1 = tto |
269 elif tfrom is self.lab2: | 297 elif tfrom is self.lab2: |
270 self.lab2 = tto | 298 self.lab2 = tto |
271 | 299 |
272 class PhiNode(Instruction): | 300 class PhiNode(Instruction): |
273 def __init__(self): | 301 def __init__(self): |
274 super().__init__() | 302 super().__init__() |
275 self.incBB = [] | 303 self.incBB = [] |
276 def addIncoming(self, bb): | 304 |
277 self.incBB.append(bb) | 305 def addIncoming(self, bb): |
278 | 306 self.incBB.append(bb) |
307 |