173
|
1 from ir import *
|
|
2 # Standard passes:
|
|
3
|
|
4 class FunctionPass:
|
|
5 def run(self, ir):
|
|
6 """ Main entry point for the pass """
|
176
|
7 self.prepare()
|
173
|
8 for f in ir.Functions:
|
|
9 self.onFunction(f)
|
|
10 def onFunction(self, f):
|
|
11 """ Override this virtual method """
|
|
12 raise NotImplementedError()
|
176
|
13 def prepare(self):
|
|
14 pass
|
173
|
15
|
|
16 class BasicBlockPass(FunctionPass):
|
|
17 def onFunction(self, f):
|
|
18 for bb in f.BasicBlocks:
|
|
19 self.onBasicBlock(bb)
|
|
20 def onBasicBlock(self, bb):
|
|
21 """ Override this virtual method """
|
|
22 raise NotImplementedError()
|
|
23
|
|
24 class InstructionPass(BasicBlockPass):
|
|
25 def onBasicBlock(self, bb):
|
174
|
26 for ins in iter(bb.Instructions):
|
173
|
27 self.onInstruction(ins)
|
|
28 def onInstruction(self, ins):
|
|
29 """ Override this virtual method """
|
|
30 raise NotImplementedError()
|
|
31
|
|
32 # Usefull transforms:
|
|
33 class ConstantFolder(InstructionPass):
|
176
|
34 def prepare(self):
|
|
35 self.constMap = {}
|
173
|
36 def onInstruction(self, i):
|
|
37 if type(i) is ImmLoad:
|
176
|
38 self.constMap[i.target] = i.value
|
173
|
39 elif type(i) is BinaryOperator:
|
176
|
40 if i.value1 in self.constMap and i.value2 in self.constMap:
|
173
|
41 op = i.operation
|
176
|
42 va = self.constMap[i.value1]
|
|
43 vb = self.constMap[i.value2]
|
173
|
44 if op == '+':
|
176
|
45 vr = va + vb
|
173
|
46 elif op == '*':
|
176
|
47 vr = va * vb
|
173
|
48 elif op == '-':
|
176
|
49 vr = va - vb
|
|
50 else:
|
|
51 vr = None
|
|
52 return
|
|
53 self.constMap[i.result] = vr
|
|
54 i2 = ImmLoad(i.result, vr)
|
|
55 i.Parent.replaceInstruction(i, i2)
|
173
|
56
|
237
|
57
|
|
58 class ConstantMerge(InstructionPass):
|
|
59 def prepare(self):
|
|
60 self.constMap = {}
|
|
61 def onInstruction(self, i):
|
|
62 if type(i) is ImmLoad:
|
|
63 v = i.value
|
|
64 if v in self.constMap:
|
|
65 # v is already defined, re-use the imm-load from elsewhere
|
|
66 pass
|
|
67 else:
|
|
68 self.constMap[v] = i
|
|
69 elif type(i) is BinaryOperator:
|
|
70 if i.value1 in self.constMap and i.value2 in self.constMap:
|
|
71 op = i.operation
|
|
72 va = self.constMap[i.value1]
|
|
73 vb = self.constMap[i.value2]
|
|
74 if op == '+':
|
|
75 vr = va + vb
|
|
76 elif op == '*':
|
|
77 vr = va * vb
|
|
78 elif op == '-':
|
|
79 vr = va - vb
|
|
80 else:
|
|
81 vr = None
|
|
82 return
|
|
83 self.constMap[i.result] = vr
|
|
84 i2 = ImmLoad(i.result, vr)
|
|
85 i.Parent.replaceInstruction(i, i2)
|
|
86
|
|
87
|
174
|
88 class DeadCodeDeleter(BasicBlockPass):
|
|
89 def onBasicBlock(self, bb):
|
|
90 def instructionUsed(ins):
|
|
91 if len(ins.defs) == 0:
|
|
92 # In case this instruction does not define any variables, assume it is usefull.
|
|
93 return True
|
|
94 for d in ins.defs:
|
|
95 if d.IsUsed:
|
|
96 return True
|
|
97 return False
|
|
98 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
|
173
|
99
|
175
|
100 def isAllocPromotable(allocinst):
|
|
101 # Check if alloc value is only used by load and store operations.
|
|
102 assert type(allocinst) is Alloc
|
|
103 for use in ai.value.used_by:
|
|
104 if not type(use.user) in [Load, Store]:
|
|
105 # TODO: check volatile
|
|
106 return False
|
|
107 otherUse = True
|
|
108 return True
|
|
109
|
177
|
110 class CleanPass(FunctionPass):
|
219
|
111 def onFunction(self, f):
|
177
|
112 bbs = list(f.BasicBlocks)
|
|
113 for bb in bbs:
|
|
114 # TODO: determine check for 'empty'
|
219
|
115
|
|
116 # If a block only contains a branch, it can be removed:
|
177
|
117 if len(bb.Instructions) == 1:
|
|
118 # This block is empty.
|
|
119 # find predecessors of this block and replace this block reference with the jumped reference.
|
|
120 ins = bb.LastInstruction
|
|
121 if type(ins) is Branch:
|
219
|
122 preds = bb.Predecessors
|
|
123 if bb in preds:
|
|
124 # Do not remove if preceeded by itself
|
|
125 pass
|
|
126 else:
|
|
127 for pred in bb.Predecessors:
|
|
128 pred.LastInstruction.changeTarget(bb, ins.target)
|
|
129 f.removeBasicBlock(bb)
|
177
|
130
|
175
|
131 class Mem2RegPromotor(FunctionPass):
|
|
132 def onFunction(self, f):
|
176
|
133 # TODO
|
235
|
134 pass
|
175
|
135
|