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
|
174
|
57 class DeadCodeDeleter(BasicBlockPass):
|
|
58 def onBasicBlock(self, bb):
|
|
59 def instructionUsed(ins):
|
|
60 if len(ins.defs) == 0:
|
|
61 # In case this instruction does not define any variables, assume it is usefull.
|
|
62 return True
|
|
63 for d in ins.defs:
|
|
64 if d.IsUsed:
|
|
65 return True
|
|
66 return False
|
|
67 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
|
173
|
68
|
175
|
69 def isAllocPromotable(allocinst):
|
|
70 # Check if alloc value is only used by load and store operations.
|
|
71 assert type(allocinst) is Alloc
|
|
72 for use in ai.value.used_by:
|
|
73 print(use.user, use)
|
|
74 if not type(use.user) in [Load, Store]:
|
|
75 # TODO: check volatile
|
|
76 return False
|
|
77 otherUse = True
|
|
78 return True
|
|
79
|
177
|
80 class CleanPass(FunctionPass):
|
219
|
81 def onFunction(self, f):
|
177
|
82 bbs = list(f.BasicBlocks)
|
|
83 for bb in bbs:
|
|
84 # TODO: determine check for 'empty'
|
219
|
85
|
|
86 # If a block only contains a branch, it can be removed:
|
177
|
87 if len(bb.Instructions) == 1:
|
|
88 # This block is empty.
|
|
89 # find predecessors of this block and replace this block reference with the jumped reference.
|
|
90 ins = bb.LastInstruction
|
|
91 if type(ins) is Branch:
|
219
|
92 print('Removing block {}'.format(bb))
|
|
93 #print(ins, bb.Predecessors)
|
|
94 preds = bb.Predecessors
|
|
95 if bb in preds:
|
|
96 # Do not remove if preceeded by itself
|
|
97 pass
|
|
98 else:
|
|
99 for pred in bb.Predecessors:
|
|
100 print('predecessor: {}'.format(pred))
|
|
101 pred.LastInstruction.changeTarget(bb, ins.target)
|
|
102 f.removeBasicBlock(bb)
|
177
|
103
|
175
|
104 class Mem2RegPromotor(FunctionPass):
|
|
105 def onFunction(self, f):
|
176
|
106 # TODO
|
175
|
107 print(f)
|
|
108
|