comparison python/transform.py @ 240:6259856841a0

Remove project
author Windel Bouwman
date Mon, 22 Jul 2013 22:37:33 +0200
parents 63bb40758066
children c4370696ccc7
comparison
equal deleted inserted replaced
239:63bb40758066 240:6259856841a0
1 from ir import * 1 """
2 Transformation to optimize IR-code
3 """
4
5 from ir import *
2 # Standard passes: 6 # Standard passes:
3 7
4 class FunctionPass: 8 class FunctionPass:
5 def run(self, ir): 9 def run(self, ir):
6 """ Main entry point for the pass """ 10 """ Main entry point for the pass """
11 """ Override this virtual method """ 15 """ Override this virtual method """
12 raise NotImplementedError() 16 raise NotImplementedError()
13 def prepare(self): 17 def prepare(self):
14 pass 18 pass
15 19
20
16 class BasicBlockPass(FunctionPass): 21 class BasicBlockPass(FunctionPass):
17 def onFunction(self, f): 22 def onFunction(self, f):
18 for bb in f.BasicBlocks: 23 for bb in f.BasicBlocks:
19 self.onBasicBlock(bb) 24 self.onBasicBlock(bb)
20 def onBasicBlock(self, bb): 25
21 """ Override this virtual method """ 26 def onBasicBlock(self, bb):
22 raise NotImplementedError() 27 """ Override this virtual method """
23 28 raise NotImplementedError()
29
30
24 class InstructionPass(BasicBlockPass): 31 class InstructionPass(BasicBlockPass):
25 def onBasicBlock(self, bb): 32 def onBasicBlock(self, bb):
26 for ins in iter(bb.Instructions): 33 for ins in iter(bb.Instructions):
27 self.onInstruction(ins) 34 self.onInstruction(ins)
28 def onInstruction(self, ins): 35
29 """ Override this virtual method """ 36 def onInstruction(self, ins):
30 raise NotImplementedError() 37 """ Override this virtual method """
38 raise NotImplementedError()
31 39
32 # Usefull transforms: 40 # Usefull transforms:
33 class ConstantFolder(InstructionPass): 41 class ConstantFolder(InstructionPass):
34 def prepare(self): 42 def prepare(self):
35 self.constMap = {} 43 self.constMap = {}
88 96
89 class DeadCodeDeleter(BasicBlockPass): 97 class DeadCodeDeleter(BasicBlockPass):
90 def onBasicBlock(self, bb): 98 def onBasicBlock(self, bb):
91 def instructionUsed(ins): 99 def instructionUsed(ins):
92 if len(ins.defs) == 0: 100 if len(ins.defs) == 0:
93 # In case this instruction does not define any variables, assume it is usefull. 101 # In case this instruction does not define any
102 # variables, assume it is usefull.
94 return True 103 return True
95 for d in ins.defs: 104 for d in ins.defs:
96 if d.IsUsed: 105 if d.IsUsed:
97 return True 106 return True
98 return False 107 return False
109 t_old = ins.target 118 t_old = ins.target
110 if not t_old.onlyUsedInBlock(bb): 119 if not t_old.onlyUsedInBlock(bb):
111 continue 120 continue
112 # update all references: 121 # update all references:
113 t_new = constMap[ins.value] 122 t_new = constMap[ins.value]
114 print('Replace {} with {}'.format(t_old, t_new))
115 for use in t_old.used_by: 123 for use in t_old.used_by:
116 use.replaceValue(t_old, t_new) 124 use.replaceValue(t_old, t_new)
117 bb.removeInstruction(ins) 125 bb.removeInstruction(ins)
118 else: 126 else:
119 constMap[ins.value] = ins.target 127 constMap[ins.value] = ins.target
120 128
121 129
122 def isAllocPromotable(allocinst): 130 def isAllocPromotable(allocinst):
123 # Check if alloc value is only used by load and store operations. 131 # Check if alloc value is only used by load and store operations.
124 assert type(allocinst) is Alloc 132 assert type(allocinst) is Alloc
125 for use in ai.value.used_by: 133 for use in ai.value.used_by:
126 if not type(use.user) in [Load, Store]: 134 if not type(use.user) in [Load, Store]:
127 # TODO: check volatile 135 # TODO: check volatile
128 return False 136 return False
129 otherUse = True 137 otherUse = True
130 return True 138 return True
139
131 140
132 class CleanPass(FunctionPass): 141 class CleanPass(FunctionPass):
133 def onFunction(self, f): 142 def onFunction(self, f):
134 bbs = list(f.BasicBlocks) 143 bbs = list(f.BasicBlocks)
135 for bb in bbs: 144 for bb in bbs:
147 else: 156 else:
148 for pred in bb.Predecessors: 157 for pred in bb.Predecessors:
149 pred.LastInstruction.changeTarget(bb, ins.target) 158 pred.LastInstruction.changeTarget(bb, ins.target)
150 f.removeBasicBlock(bb) 159 f.removeBasicBlock(bb)
151 160
161
152 class Mem2RegPromotor(FunctionPass): 162 class Mem2RegPromotor(FunctionPass):
153 def onFunction(self, f): 163 def onFunction(self, f):
154 # TODO 164 # TODO
155 pass 165 pass
156 166