diff python/transform.py @ 237:81752b0f85a5

Added burn led test program
author Windel Bouwman
date Wed, 17 Jul 2013 22:31:54 +0200
parents ff40407c0240
children 63bb40758066
line wrap: on
line diff
--- a/python/transform.py	Mon Jul 15 20:15:31 2013 +0200
+++ b/python/transform.py	Wed Jul 17 22:31:54 2013 +0200
@@ -54,6 +54,37 @@
             i2 = ImmLoad(i.result, vr)
             i.Parent.replaceInstruction(i, i2)
 
+
+class ConstantMerge(InstructionPass):
+    def prepare(self):
+        self.constMap = {}
+    def onInstruction(self, i):
+        if type(i) is ImmLoad:
+            v = i.value
+            if v in self.constMap:
+                # v is already defined, re-use the imm-load from elsewhere
+                pass
+            else:
+                self.constMap[v] = i
+        elif type(i) is BinaryOperator:
+         if i.value1 in self.constMap and i.value2 in self.constMap:
+            op = i.operation
+            va = self.constMap[i.value1]
+            vb = self.constMap[i.value2]
+            if op == '+':
+               vr = va + vb
+            elif op == '*':
+               vr = va * vb
+            elif op == '-':
+               vr = va - vb
+            else:
+               vr = None
+               return
+            self.constMap[i.result] = vr
+            i2 = ImmLoad(i.result, vr)
+            i.Parent.replaceInstruction(i, i2)
+
+
 class DeadCodeDeleter(BasicBlockPass):
    def onBasicBlock(self, bb):
       def instructionUsed(ins):