annotate python/transform.py @ 239:63bb40758066

added check
author Windel Bouwman
date Mon, 22 Jul 2013 17:57:25 +0200
parents 81752b0f85a5
children 6259856841a0
rev   line source
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
1 from ir import *
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
2 # Standard passes:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
3
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
4 class FunctionPass:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
5 def run(self, ir):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
6 """ Main entry point for the pass """
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
7 self.prepare()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
8 for f in ir.Functions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
9 self.onFunction(f)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
10 def onFunction(self, f):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
11 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
12 raise NotImplementedError()
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
13 def prepare(self):
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
14 pass
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
15
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
16 class BasicBlockPass(FunctionPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
17 def onFunction(self, f):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
18 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
19 self.onBasicBlock(bb)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
20 def onBasicBlock(self, bb):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
21 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
22 raise NotImplementedError()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
23
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
24 class InstructionPass(BasicBlockPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
25 def onBasicBlock(self, bb):
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
26 for ins in iter(bb.Instructions):
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
27 self.onInstruction(ins)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
28 def onInstruction(self, ins):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
29 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
30 raise NotImplementedError()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
31
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
32 # Usefull transforms:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
33 class ConstantFolder(InstructionPass):
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
34 def prepare(self):
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
35 self.constMap = {}
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
36 def onInstruction(self, i):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
37 if type(i) is ImmLoad:
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
38 self.constMap[i.target] = i.value
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
39 elif type(i) is BinaryOperator:
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
40 if i.value1 in self.constMap and i.value2 in self.constMap:
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
41 op = i.operation
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
42 va = self.constMap[i.value1]
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
43 vb = self.constMap[i.value2]
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
44 if op == '+':
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
45 vr = va + vb
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
46 elif op == '*':
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
47 vr = va * vb
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
48 elif op == '-':
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
49 vr = va - vb
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
50 else:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
51 vr = None
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
52 return
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
53 self.constMap[i.result] = vr
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
54 i.removeDef(i.result)
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
55 i2 = ImmLoad(i.result, vr)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
56 i.Parent.replaceInstruction(i, i2)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
57
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
58
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
59 class ConstantMerge(InstructionPass):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
60 def prepare(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
61 self.constMap = {}
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
62 def onInstruction(self, i):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
63 if type(i) is ImmLoad:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
64 v = i.value
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
65 if v in self.constMap:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
66 # v is already defined, re-use the imm-load from elsewhere
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
67 pass
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
68 else:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
69 self.constMap[v] = i
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
70 elif type(i) is BinaryOperator:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
71 if i.value1 in self.constMap and i.value2 in self.constMap:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
72 op = i.operation
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
73 va = self.constMap[i.value1]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
74 vb = self.constMap[i.value2]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
75 if op == '+':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
76 vr = va + vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
77 elif op == '*':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
78 vr = va * vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
79 elif op == '-':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
80 vr = va - vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
81 else:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
82 vr = None
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
83 return
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
84 self.constMap[i.result] = vr
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
85 i2 = ImmLoad(i.result, vr)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
86 i.Parent.replaceInstruction(i, i2)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
87
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
88
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
89 class DeadCodeDeleter(BasicBlockPass):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
90 def onBasicBlock(self, bb):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
91 def instructionUsed(ins):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
92 if len(ins.defs) == 0:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
93 # In case this instruction does not define any variables, assume it is usefull.
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
94 return True
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
95 for d in ins.defs:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
96 if d.IsUsed:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
97 return True
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
98 return False
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
99 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
100
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
101
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
102 class SameImmLoadDeletePass(BasicBlockPass):
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
103 def onBasicBlock(self, bb):
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
104 constMap = {}
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
105 imms = filter(lambda i: isinstance(i, ImmLoad), bb.Instructions)
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
106 for ins in list(imms):
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
107 if ins.value in constMap:
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
108 # remove this immload and update all references to the target
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
109 t_old = ins.target
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
110 if not t_old.onlyUsedInBlock(bb):
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
111 continue
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
112 # update all references:
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
113 t_new = constMap[ins.value]
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
114 print('Replace {} with {}'.format(t_old, t_new))
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
115 for use in t_old.used_by:
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
116 use.replaceValue(t_old, t_new)
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
117 bb.removeInstruction(ins)
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
118 else:
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
119 constMap[ins.value] = ins.target
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
120
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
121
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
122 def isAllocPromotable(allocinst):
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
123 # Check if alloc value is only used by load and store operations.
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
124 assert type(allocinst) is Alloc
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
125 for use in ai.value.used_by:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
126 if not type(use.user) in [Load, Store]:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
127 # TODO: check volatile
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
128 return False
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
129 otherUse = True
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
130 return True
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
131
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
132 class CleanPass(FunctionPass):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
133 def onFunction(self, f):
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
134 bbs = list(f.BasicBlocks)
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
135 for bb in bbs:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
136 # TODO: determine check for 'empty'
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
137
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
138 # If a block only contains a branch, it can be removed:
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
139 if len(bb.Instructions) == 1 and type(bb.LastInstruction) is Branch:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
140 # This block is empty.
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
141 # find predecessors of this block and replace this block reference with the jumped reference.
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
142 ins = bb.LastInstruction
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
143 preds = bb.Predecessors
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
144 if bb in preds:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
145 # Do not remove if preceeded by itself
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
146 pass
239
63bb40758066 added check
Windel Bouwman
parents: 237
diff changeset
147 else:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
148 for pred in bb.Predecessors:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
149 pred.LastInstruction.changeTarget(bb, ins.target)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
150 f.removeBasicBlock(bb)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
151
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
152 class Mem2RegPromotor(FunctionPass):
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
153 def onFunction(self, f):
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
154 # TODO
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 225
diff changeset
155 pass
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
156