comparison python/transform.py @ 239:63bb40758066

added check
author Windel Bouwman
date Mon, 22 Jul 2013 17:57:25 +0200
parents 81752b0f85a5
children 6259856841a0
comparison
equal deleted inserted replaced
238:90637d1bbfad 239:63bb40758066
49 vr = va - vb 49 vr = va - vb
50 else: 50 else:
51 vr = None 51 vr = None
52 return 52 return
53 self.constMap[i.result] = vr 53 self.constMap[i.result] = vr
54 i.removeDef(i.result)
54 i2 = ImmLoad(i.result, vr) 55 i2 = ImmLoad(i.result, vr)
55 i.Parent.replaceInstruction(i, i2) 56 i.Parent.replaceInstruction(i, i2)
56 57
57 58
58 class ConstantMerge(InstructionPass): 59 class ConstantMerge(InstructionPass):
95 if d.IsUsed: 96 if d.IsUsed:
96 return True 97 return True
97 return False 98 return False
98 bb.Instructions = list(filter(instructionUsed, bb.Instructions)) 99 bb.Instructions = list(filter(instructionUsed, bb.Instructions))
99 100
101
102 class SameImmLoadDeletePass(BasicBlockPass):
103 def onBasicBlock(self, bb):
104 constMap = {}
105 imms = filter(lambda i: isinstance(i, ImmLoad), bb.Instructions)
106 for ins in list(imms):
107 if ins.value in constMap:
108 # remove this immload and update all references to the target
109 t_old = ins.target
110 if not t_old.onlyUsedInBlock(bb):
111 continue
112 # update all references:
113 t_new = constMap[ins.value]
114 print('Replace {} with {}'.format(t_old, t_new))
115 for use in t_old.used_by:
116 use.replaceValue(t_old, t_new)
117 bb.removeInstruction(ins)
118 else:
119 constMap[ins.value] = ins.target
120
121
100 def isAllocPromotable(allocinst): 122 def isAllocPromotable(allocinst):
101 # Check if alloc value is only used by load and store operations. 123 # Check if alloc value is only used by load and store operations.
102 assert type(allocinst) is Alloc 124 assert type(allocinst) is Alloc
103 for use in ai.value.used_by: 125 for use in ai.value.used_by:
104 if not type(use.user) in [Load, Store]: 126 if not type(use.user) in [Load, Store]:
107 otherUse = True 129 otherUse = True
108 return True 130 return True
109 131
110 class CleanPass(FunctionPass): 132 class CleanPass(FunctionPass):
111 def onFunction(self, f): 133 def onFunction(self, f):
112 bbs = list(f.BasicBlocks) 134 bbs = list(f.BasicBlocks)
113 for bb in bbs: 135 for bb in bbs:
114 # TODO: determine check for 'empty' 136 # TODO: determine check for 'empty'
115 137
116 # If a block only contains a branch, it can be removed: 138 # If a block only contains a branch, it can be removed:
117 if len(bb.Instructions) == 1: 139 if len(bb.Instructions) == 1 and type(bb.LastInstruction) is Branch:
118 # This block is empty. 140 # This block is empty.
119 # find predecessors of this block and replace this block reference with the jumped reference. 141 # find predecessors of this block and replace this block reference with the jumped reference.
120 ins = bb.LastInstruction 142 ins = bb.LastInstruction
121 if type(ins) is Branch: 143 preds = bb.Predecessors
122 preds = bb.Predecessors 144 if bb in preds:
123 if bb in preds:
124 # Do not remove if preceeded by itself 145 # Do not remove if preceeded by itself
125 pass 146 pass
126 else: 147 else:
127 for pred in bb.Predecessors: 148 for pred in bb.Predecessors:
128 pred.LastInstruction.changeTarget(bb, ins.target) 149 pred.LastInstruction.changeTarget(bb, ins.target)
129 f.removeBasicBlock(bb) 150 f.removeBasicBlock(bb)
130 151
131 class Mem2RegPromotor(FunctionPass): 152 class Mem2RegPromotor(FunctionPass):