diff python/transform.py @ 239:63bb40758066

added check
author Windel Bouwman
date Mon, 22 Jul 2013 17:57:25 +0200
parents 81752b0f85a5
children 6259856841a0
line wrap: on
line diff
--- a/python/transform.py	Sat Jul 20 13:18:04 2013 +0200
+++ b/python/transform.py	Mon Jul 22 17:57:25 2013 +0200
@@ -51,6 +51,7 @@
                vr = None
                return
             self.constMap[i.result] = vr
+            i.removeDef(i.result)
             i2 = ImmLoad(i.result, vr)
             i.Parent.replaceInstruction(i, i2)
 
@@ -97,6 +98,27 @@
          return False
       bb.Instructions = list(filter(instructionUsed, bb.Instructions))
 
+
+class SameImmLoadDeletePass(BasicBlockPass):
+    def onBasicBlock(self, bb):
+        constMap = {}
+        imms = filter(lambda i: isinstance(i, ImmLoad), bb.Instructions)
+        for ins in list(imms):
+            if ins.value in constMap:
+                # remove this immload and update all references to the target
+                t_old = ins.target
+                if not t_old.onlyUsedInBlock(bb):
+                    continue
+                # update all references:
+                t_new = constMap[ins.value]
+                print('Replace {} with {}'.format(t_old, t_new))
+                for use in t_old.used_by:
+                    use.replaceValue(t_old, t_new)
+                bb.removeInstruction(ins)
+            else:
+                constMap[ins.value] = ins.target
+            
+
 def isAllocPromotable(allocinst):
    # Check if alloc value is only used by load and store operations.
    assert type(allocinst) is Alloc
@@ -109,21 +131,20 @@
 
 class CleanPass(FunctionPass):
     def onFunction(self, f):
-      bbs = list(f.BasicBlocks)
-      for bb in bbs:
+        bbs = list(f.BasicBlocks)
+        for bb in bbs:
          # TODO: determine check for 'empty'
 
          # If a block only contains a branch, it can be removed:
-         if len(bb.Instructions) == 1:
+         if len(bb.Instructions) == 1 and type(bb.LastInstruction) is Branch:
             # This block is empty.
             # find predecessors of this block and replace this block reference with the jumped reference.
             ins = bb.LastInstruction
-            if type(ins) is Branch:
-                preds = bb.Predecessors
-                if bb in preds:
+            preds = bb.Predecessors
+            if bb in preds:
                     # Do not remove if preceeded by itself
                     pass
-                else:
+            else:
                     for pred in bb.Predecessors:
                           pred.LastInstruction.changeTarget(bb, ins.target)
                     f.removeBasicBlock(bb)