annotate python/transform.py @ 173:c1d2b6b9f9a7

Rework into passes
author Windel Bouwman
date Fri, 19 Apr 2013 12:42:21 +0200
parents
children 3eb06f5fb987
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 """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
7 for f in ir.Functions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
8 self.onFunction(f)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
9 def onFunction(self, f):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
10 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
11 raise NotImplementedError()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
12
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
13 class BasicBlockPass(FunctionPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
14 def onFunction(self, f):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
15 for bb in f.BasicBlocks:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
16 self.onBasicBlock(bb)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
17 def onBasicBlock(self, bb):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
18 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
19 raise NotImplementedError()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
20
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
21 class InstructionPass(BasicBlockPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
22 def onBasicBlock(self, bb):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
23 for ins in bb.Instructions:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
24 self.onInstruction(ins)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
25 def onInstruction(self, ins):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
26 """ Override this virtual method """
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
27 raise NotImplementedError()
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
28
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
29 # Usefull transforms:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
30 class ConstantFolder(InstructionPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
31 def onInstruction(self, i):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
32 if type(i) is ImmLoad:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
33 i.target.constval = i.value
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
34 print(type(i.value), i.value)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
35 elif type(i) is BinaryOperator:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
36 a = i.value1
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
37 b = i.value2
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
38 if hasattr(a, 'constval') and hasattr(b,'constval'):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
39 op = i.operation
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
40 if op == '+':
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
41 i2 = ImmLoad(i.result, a.constval + b.constval)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
42 print(i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
43 i.Parent.replaceInstruction(i, i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
44 elif op == '*':
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
45 i2 = ImmLoad(i.result, a.constval * b.constval)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
46 print(i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
47 i.Parent.replaceInstruction(i, i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
48 elif op == '-':
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
49 i2 = ImmLoad(i.result, a.constval - b.constval)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
50 print(i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
51 i.Parent.replaceInstruction(i, i2)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
52
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
53 class DeadCodeDeleter(InstructionPass):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
54 def onInstruction(self, ins):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
55 print(ins, ins.Users)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
56 if not ins.defs in ins.live_out:
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
57 ins.Parent.removeInstruction(ins)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents:
diff changeset
58