240
|
1 """
|
|
2 Transformation to optimize IR-code
|
|
3 """
|
|
4
|
253
|
5 import logging
|
240
|
6 from ir import *
|
173
|
7 # Standard passes:
|
|
8
|
|
9 class FunctionPass:
|
255
|
10 def __init__(self):
|
|
11 self.logger = logging.getLogger('optimize')
|
|
12
|
253
|
13 def run(self, ir):
|
|
14 """ Main entry point for the pass """
|
255
|
15 self.logger.info('Running pass {}'.format(type(self)))
|
|
16 ir.check()
|
253
|
17 self.prepare()
|
|
18 for f in ir.Functions:
|
|
19 self.onFunction(f)
|
255
|
20 ir.check()
|
253
|
21
|
|
22 def onFunction(self, f):
|
|
23 """ Override this virtual method """
|
|
24 raise NotImplementedError()
|
|
25
|
|
26 def prepare(self):
|
|
27 pass
|
173
|
28
|
240
|
29
|
173
|
30 class BasicBlockPass(FunctionPass):
|
240
|
31 def onFunction(self, f):
|
|
32 for bb in f.BasicBlocks:
|
|
33 self.onBasicBlock(bb)
|
|
34
|
|
35 def onBasicBlock(self, bb):
|
|
36 """ Override this virtual method """
|
|
37 raise NotImplementedError()
|
|
38
|
|
39
|
173
|
40 class InstructionPass(BasicBlockPass):
|
240
|
41 def onBasicBlock(self, bb):
|
|
42 for ins in iter(bb.Instructions):
|
|
43 self.onInstruction(ins)
|
|
44
|
|
45 def onInstruction(self, ins):
|
|
46 """ Override this virtual method """
|
|
47 raise NotImplementedError()
|
173
|
48
|
|
49 # Usefull transforms:
|
255
|
50 class ConstantFolder(BasicBlockPass):
|
|
51 def onBasicBlock(self, bb):
|
|
52 constMap = {}
|
|
53 ins = [i for i in bb.Instructions if type(i) in [ImmLoad, BinaryOperator]]
|
|
54 for i in ins:
|
|
55 if type(i) is ImmLoad:
|
|
56 constMap[i.target] = i.value
|
|
57 elif type(i) is BinaryOperator:
|
|
58 if i.value1 in constMap and i.value2 in constMap and i.operation in ['+', '-', '*', '<<']:
|
|
59 op = i.operation
|
|
60 va = constMap[i.value1]
|
|
61 vb = constMap[i.value2]
|
|
62 if op == '+':
|
|
63 vr = va + vb
|
|
64 elif op == '*':
|
|
65 vr = va * vb
|
|
66 elif op == '-':
|
|
67 vr = va - vb
|
|
68 elif op == '<<':
|
|
69 vr = va << vb
|
|
70 else:
|
|
71 raise NotImplementedError()
|
|
72 constMap[i.result] = vr
|
|
73 i.removeDef(i.result)
|
|
74 i2 = ImmLoad(i.result, vr)
|
|
75 logging.debug('Replacing {} with {}'.format(i, i2))
|
|
76 i.Parent.replaceInstruction(i, i2)
|
237
|
77
|
|
78
|
174
|
79 class DeadCodeDeleter(BasicBlockPass):
|
252
|
80 def onBasicBlock(self, bb):
|
|
81 def instructionUsed(ins):
|
253
|
82 if not type(ins) in [ImmLoad, BinaryOperator]:
|
|
83 return True
|
252
|
84 if len(ins.defs) == 0:
|
|
85 # In case this instruction does not define any
|
|
86 # variables, assume it is usefull.
|
|
87 return True
|
|
88 return any(d.Used for d in ins.defs)
|
|
89
|
|
90 change = True
|
|
91 while change:
|
|
92 change = False
|
|
93 for i in bb.Instructions:
|
|
94 if instructionUsed(i):
|
|
95 continue
|
|
96 bb.removeInstruction(i)
|
|
97 change = True
|
173
|
98
|
239
|
99
|
252
|
100 class CommonSubexpressionElimination(BasicBlockPass):
|
239
|
101 def onBasicBlock(self, bb):
|
|
102 constMap = {}
|
252
|
103 to_remove = []
|
|
104 for i in bb.Instructions:
|
|
105 if isinstance(i, ImmLoad):
|
|
106 if i.value in constMap:
|
|
107 t_new = constMap[i.value]
|
|
108 t_old = i.target
|
253
|
109 logging.debug('Replacing {} with {}'.format(t_old, t_new))
|
261
|
110 t_old.replaceby(t_new)
|
252
|
111 to_remove.append(i)
|
|
112 else:
|
|
113 constMap[i.value] = i.target
|
|
114 elif isinstance(i, BinaryOperator):
|
|
115 k = (i.value1, i.operation, i.value2)
|
|
116 if k in constMap:
|
|
117 t_old = i.result
|
|
118 t_new = constMap[k]
|
253
|
119 logging.debug('Replacing {} with {}'.format(t_old, t_new))
|
261
|
120 t_old.replaceby(t_new)
|
252
|
121 to_remove.append(i)
|
|
122 else:
|
|
123 constMap[k] = i.result
|
|
124 for i in to_remove:
|
253
|
125 logging.debug('removing {}'.format(i))
|
252
|
126 bb.removeInstruction(i)
|
240
|
127
|
|
128
|
177
|
129 class CleanPass(FunctionPass):
|
219
|
130 def onFunction(self, f):
|
239
|
131 bbs = list(f.BasicBlocks)
|
|
132 for bb in bbs:
|
253
|
133 # If a block only contains a branch, it can be removed:
|
|
134 if len(bb.Instructions) == 1 and type(bb.LastInstruction) is Branch:
|
|
135 # This block is empty.
|
|
136 # find predecessors of this block and replace this block reference with the jumped reference.
|
|
137 ins = bb.LastInstruction
|
|
138 preds = bb.Predecessors
|
|
139 if bb in preds:
|
219
|
140 # Do not remove if preceeded by itself
|
|
141 pass
|
253
|
142 else:
|
219
|
143 for pred in bb.Predecessors:
|
|
144 pred.LastInstruction.changeTarget(bb, ins.target)
|
|
145 f.removeBasicBlock(bb)
|
177
|
146
|
240
|
147
|
252
|
148
|
|
149
|