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):
|
|
81 def onFunction(self, f):
|
|
82 bbs = list(f.BasicBlocks)
|
|
83 for bb in bbs:
|
|
84 # TODO: determine check for 'empty'
|
|
85 if len(bb.Instructions) == 1:
|
|
86 # This block is empty.
|
|
87 # find predecessors of this block and replace this block reference with the jumped reference.
|
|
88 ins = bb.LastInstruction
|
|
89 if type(ins) is Branch:
|
|
90 print(ins, bb.Predecessors)
|
|
91 for pred in bb.Predecessors:
|
|
92 pred.LastInstruction.changeTarget(bb, ins.target)
|
|
93 f.removeBasicBlock(bb)
|
|
94
|
175
|
95 class Mem2RegPromotor(FunctionPass):
|
|
96 def onFunction(self, f):
|
176
|
97 # TODO
|
175
|
98 print(f)
|
|
99
|