annotate python/transform.py @ 267:e7c8f7eb3f59

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