Mercurial > lcfOS
comparison python/transform.py @ 175:a51b3c956386
Added function call in expressions
author | Windel Bouwman |
---|---|
date | Fri, 19 Apr 2013 22:15:54 +0200 |
parents | 3eb06f5fb987 |
children | 5fd02aa38b42 |
comparison
equal
deleted
inserted
replaced
174:3eb06f5fb987 | 175:a51b3c956386 |
---|---|
29 # Usefull transforms: | 29 # Usefull transforms: |
30 class ConstantFolder(InstructionPass): | 30 class ConstantFolder(InstructionPass): |
31 def onInstruction(self, i): | 31 def onInstruction(self, i): |
32 if type(i) is ImmLoad: | 32 if type(i) is ImmLoad: |
33 i.target.constval = i.value | 33 i.target.constval = i.value |
34 print(type(i.value), i.value) | |
35 elif type(i) is BinaryOperator: | 34 elif type(i) is BinaryOperator: |
36 a = i.value1 | 35 a = i.value1 |
37 b = i.value2 | 36 b = i.value2 |
38 if hasattr(a, 'constval') and hasattr(b,'constval'): | 37 if hasattr(a, 'constval') and hasattr(b,'constval'): |
39 op = i.operation | 38 op = i.operation |
40 if op == '+': | 39 if op == '+': |
41 i2 = ImmLoad(i.result, a.constval + b.constval) | 40 i2 = ImmLoad(i.result, a.constval + b.constval) |
42 print(i2) | |
43 i.Parent.replaceInstruction(i, i2) | 41 i.Parent.replaceInstruction(i, i2) |
44 elif op == '*': | 42 elif op == '*': |
45 i2 = ImmLoad(i.result, a.constval * b.constval) | 43 i2 = ImmLoad(i.result, a.constval * b.constval) |
46 print(i2) | |
47 i.Parent.replaceInstruction(i, i2) | 44 i.Parent.replaceInstruction(i, i2) |
48 elif op == '-': | 45 elif op == '-': |
49 i2 = ImmLoad(i.result, a.constval - b.constval) | 46 i2 = ImmLoad(i.result, a.constval - b.constval) |
50 print(i2) | |
51 i.Parent.replaceInstruction(i, i2) | 47 i.Parent.replaceInstruction(i, i2) |
52 | 48 |
53 class DeadCodeDeleter(BasicBlockPass): | 49 class DeadCodeDeleter(BasicBlockPass): |
54 def onBasicBlock(self, bb): | 50 def onBasicBlock(self, bb): |
55 def instructionUsed(ins): | 51 def instructionUsed(ins): |
60 if d.IsUsed: | 56 if d.IsUsed: |
61 return True | 57 return True |
62 return False | 58 return False |
63 bb.Instructions = list(filter(instructionUsed, bb.Instructions)) | 59 bb.Instructions = list(filter(instructionUsed, bb.Instructions)) |
64 | 60 |
61 def isAllocPromotable(allocinst): | |
62 # Check if alloc value is only used by load and store operations. | |
63 assert type(allocinst) is Alloc | |
64 for use in ai.value.used_by: | |
65 print(use.user, use) | |
66 if not type(use.user) in [Load, Store]: | |
67 # TODO: check volatile | |
68 return False | |
69 otherUse = True | |
70 return True | |
71 | |
72 class Mem2RegPromotor(FunctionPass): | |
73 def onFunction(self, f): | |
74 print(f) | |
75 |