comparison python/ppci/transform.py @ 316:56e6ff84f646

Fixed burn led demo
author Windel Bouwman
date Sat, 21 Dec 2013 13:13:26 +0100
parents 6753763d3bec
children e30a77ae359b
comparison
equal deleted inserted replaced
315:084cccaa5deb 316:56e6ff84f646
6 from . import ir 6 from . import ir
7 # Standard passes: 7 # Standard passes:
8 8
9 class FunctionPass: 9 class FunctionPass:
10 def __init__(self): 10 def __init__(self):
11 self.logger = logging.getLogger('optimize') 11 self.logger = logging.getLogger(str(self.__class__.__name__))
12 12
13 def run(self, ir): 13 def run(self, ir):
14 """ Main entry point for the pass """ 14 """ Main entry point for the pass """
15 self.logger.info('Running pass {}'.format(type(self))) 15 self.logger.info('Running pass {}'.format(type(self)))
16 ir.check() 16 ir.check()
114 t_old.replaceby(t_new) 114 t_old.replaceby(t_new)
115 to_remove.append(i) 115 to_remove.append(i)
116 else: 116 else:
117 constMap[k] = i.result 117 constMap[k] = i.result
118 for i in to_remove: 118 for i in to_remove:
119 logging.debug('removing {}'.format(i)) 119 self.logger.debug('removing {}'.format(i))
120 bb.removeInstruction(i) 120 bb.removeInstruction(i)
121 121
122 122
123 class CleanPass(FunctionPass): 123 class CleanPass(FunctionPass):
124 def onFunction(self, f): 124 def onFunction(self, f):
125 removeEmptyBasicBlocks(f) 125 self.remove_empty_blocks(f)
126 126
127 127 def remove_empty_blocks(self, f):
128 def removeEmptyBlocks(f): 128 """ Remove empty basic blocks from function. """
129 """ Remove empty basic blocks from function. """ 129 # If a block only contains a branch, it can be removed:
130 # If a block only contains a branch, it can be removed: 130 empty = lambda b: type(b.FirstInstruction) is ir.Jump
131 empty = lambda b: type(b.FirstInstruction) is ir.Jump 131 empty_blocks = list(filter(empty, f.Blocks))
132 empty_blocks = list(filter(empty, f.Blocks)) 132 for b in empty_blocks:
133 for b in empty_blocks: 133 # Update predecessors
134 # Update predecessors 134 preds = b.Predecessors
135 preds = b.Predecessors 135 if b not in preds + [f.entry]:
136 if b not in preds + [f.entry]: 136 # Do not remove if preceeded by itself
137 # Do not remove if preceeded by itself 137 tgt = b.LastInstruction.target
138 tgt = b.LastInstruction.target 138 for pred in preds:
139 for pred in preds: 139 pred.LastInstruction.changeTarget(b, tgt)
140 pred.LastInstruction.changeTarget(b, tgt) 140 self.logger.debug('Removing empty block: {}'.format(b))
141 logging.debug('Removing empty block: {}'.format(b)) 141 f.removeBlock(b)
142 f.removeBlock(b)