annotate python/transform.py @ 237:81752b0f85a5

Added burn led test program
author Windel Bouwman
date Wed, 17 Jul 2013 22:31:54 +0200
parents ff40407c0240
children 63bb40758066
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
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
54 i2 = ImmLoad(i.result, vr)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
55 i.Parent.replaceInstruction(i, i2)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
56
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
57
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
58 class ConstantMerge(InstructionPass):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
59 def prepare(self):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
60 self.constMap = {}
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
61 def onInstruction(self, i):
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
62 if type(i) is ImmLoad:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
63 v = i.value
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
64 if v in self.constMap:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
65 # v is already defined, re-use the imm-load from elsewhere
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
66 pass
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
67 else:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
68 self.constMap[v] = i
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
69 elif type(i) is BinaryOperator:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
70 if i.value1 in self.constMap and i.value2 in self.constMap:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
71 op = i.operation
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
72 va = self.constMap[i.value1]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
73 vb = self.constMap[i.value2]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
74 if op == '+':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
75 vr = va + vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
76 elif op == '*':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
77 vr = va * vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
78 elif op == '-':
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
79 vr = va - vb
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
80 else:
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
81 vr = None
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
82 return
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
83 self.constMap[i.result] = vr
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
84 i2 = ImmLoad(i.result, vr)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
85 i.Parent.replaceInstruction(i, i2)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
86
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 235
diff changeset
87
174
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
88 class DeadCodeDeleter(BasicBlockPass):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
89 def onBasicBlock(self, bb):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
90 def instructionUsed(ins):
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
91 if len(ins.defs) == 0:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
92 # 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
93 return True
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
94 for d in ins.defs:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
95 if d.IsUsed:
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
96 return True
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
97 return False
3eb06f5fb987 Added memory alloc for locals
Windel Bouwman
parents: 173
diff changeset
98 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
99
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
100 def isAllocPromotable(allocinst):
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
101 # Check if alloc value is only used by load and store operations.
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
102 assert type(allocinst) is Alloc
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
103 for use in ai.value.used_by:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
104 if not type(use.user) in [Load, Store]:
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
105 # TODO: check volatile
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
106 return False
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
107 otherUse = True
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
108 return True
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
109
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
110 class CleanPass(FunctionPass):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
111 def onFunction(self, f):
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
112 bbs = list(f.BasicBlocks)
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
113 for bb in bbs:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
114 # TODO: determine check for 'empty'
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
115
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
116 # If a block only contains a branch, it can be removed:
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
117 if len(bb.Instructions) == 1:
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
118 # This block is empty.
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
119 # 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
120 ins = bb.LastInstruction
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
121 if type(ins) is Branch:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
122 preds = bb.Predecessors
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
123 if bb in preds:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
124 # Do not remove if preceeded by itself
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
125 pass
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
126 else:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
127 for pred in bb.Predecessors:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
128 pred.LastInstruction.changeTarget(bb, ins.target)
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 177
diff changeset
129 f.removeBasicBlock(bb)
177
460db5669efa Added clean pass for IR
Windel Bouwman
parents: 176
diff changeset
130
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
131 class Mem2RegPromotor(FunctionPass):
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
132 def onFunction(self, f):
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 175
diff changeset
133 # TODO
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 225
diff changeset
134 pass
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 174
diff changeset
135