Commit df7b96c8 authored by Jayke Meijer's avatar Jayke Meijer

Merge branch 'master' of github.com:taddeus/peephole

parents e305a0d2 47e216c2
...@@ -17,6 +17,8 @@ def remove_redundancies(block): ...@@ -17,6 +17,8 @@ def remove_redundancies(block):
while old_len != len(block): while old_len != len(block):
old_len = len(block) old_len = len(block)
block.reset()
while not block.end(): while not block.end():
s = block.read() s = block.read()
...@@ -30,20 +32,34 @@ def remove_redundancies(block): ...@@ -30,20 +32,34 @@ def remove_redundancies(block):
def optimize_block(block): def optimize_block(block):
"""Optimize a basic block.""" """Optimize a basic block."""
#changed = True
#while changed:
# changed = False
# if remove_redundancies(block): changed = True
# if eliminate_common_subexpressions(block): changed = True
# if fold_constants(block): changed = True
# if copy_propagation(block): changed = True
# if eliminate_dead_code(block): changed = True
# print 'iteration'
while remove_redundancies(block) \ while remove_redundancies(block) \
| eliminate_common_subexpressions(block) \ | eliminate_common_subexpressions(block) \
| fold_constants(block) \ | fold_constants(block) \
| copy_propagation(block) \ | copy_propagation(block) \
| eliminate_dead_code(block): | eliminate_dead_code(block):
#| algebraic_transformations(block) \ #| algebraic_transformations(block) \
#print 'iteration'
pass pass
from copy import deepcopy
def optimize(statements, verbose=0): def optimize(statements, verbose=0):
"""Optimization wrapper function, calls global and basic-block level """Optimization wrapper function, calls global and basic-block level
optimization functions.""" optimization functions."""
# Optimize on a global level # Optimize on a global level
# TODO: only count instructions (no directives) # TODO: only count instructions (no directives)
statements = deepcopy(statements)
o = len(statements) o = len(statements)
remove_redundant_jumps(statements) remove_redundant_jumps(statements)
g = len(statements) g = len(statements)
...@@ -64,18 +80,12 @@ def optimize(statements, verbose=0): ...@@ -64,18 +80,12 @@ def optimize(statements, verbose=0):
opt_blocks = reduce(lambda a, b: a + b, block_statements) opt_blocks = reduce(lambda a, b: a + b, block_statements)
b = len(opt_blocks) b = len(opt_blocks)
# - Common subexpression elimination # Print results
# - Constant folding
# - Copy propagation
# - Dead-code elimination
# - Temporary variable renaming
# - Interchange of independent statements
if verbose: if verbose:
print 'Original statements: %d' % o print 'Original statements: %d' % o
print 'After global optimization: %d' % g print 'After global optimization: %d (%d removed)' % (g, o - g)
print 'After basic blocks optimization: %d' % b print 'After basic block optimization: %d (%d removed)' % (b, g - b)
print 'Optimization: %d (%d%%)' \ print 'Statements removed: %d (%d%%)' \
% (o - b, int((o - b) / float(b) * 100)) % (o - b, int((o - b) / float(b) * 100))
return opt_blocks return opt_blocks
...@@ -50,6 +50,8 @@ def eliminate_common_subexpressions(block): ...@@ -50,6 +50,8 @@ def eliminate_common_subexpressions(block):
""" """
changed = False changed = False
block.reset()
while not block.end(): while not block.end():
s = block.read() s = block.read()
...@@ -123,6 +125,8 @@ def fold_constants(block): ...@@ -123,6 +125,8 @@ def fold_constants(block):
# Current known values in register # Current known values in register
register = {} register = {}
block.reset()
while not block.end(): while not block.end():
s = block.read() s = block.read()
...@@ -147,10 +151,10 @@ def fold_constants(block): ...@@ -147,10 +151,10 @@ def fold_constants(block):
elif s.name == 'lw' and s[1] in constants: elif s.name == 'lw' and s[1] in constants:
# Usage of variable with constant value # Usage of variable with constant value
register[s[0]] = constants[s[1]] register[s[0]] = constants[s[1]]
elif s.name == 'mflo': elif s.name == 'mflo' and '$lo' in register:
# Move of `Lo' register to another register # Move of `Lo' register to another register
register[s[0]] = register['$lo'] register[s[0]] = register['$lo']
elif s.name == 'mfhi': elif s.name == 'mfhi' and '$hi' in register:
# Move of `Hi' register to another register # Move of `Hi' register to another register
register[s[0]] = register['$hi'] register[s[0]] = register['$hi']
elif s.name in ['mult', 'div'] \ elif s.name in ['mult', 'div'] \
...@@ -252,6 +256,8 @@ def copy_propagation(block): ...@@ -252,6 +256,8 @@ def copy_propagation(block):
moves_to = [] moves_to = []
changed = False changed = False
block.reset()
while not block.end(): while not block.end():
s = block.read() s = block.read()
...@@ -305,6 +311,8 @@ def algebraic_transformations(block): ...@@ -305,6 +311,8 @@ def algebraic_transformations(block):
""" """
changed = False changed = False
block.reset()
while not block.end(): while not block.end():
s = block.read() s = block.read()
......
...@@ -175,7 +175,7 @@ class Statement: ...@@ -175,7 +175,7 @@ class Statement:
if self.is_branch() or self.is_store() or self.is_compare() \ if self.is_branch() or self.is_store() or self.is_compare() \
or self.is_command(*['mult', 'div', 'dsz', 'mtc1']): or self.is_command(*['mult', 'div', 'dsz', 'mtc1']):
if self.name == 'dsz': if self.name == 'dsz':
m = re.match('^\d+\(([^)]+)\)$', self[0]) m = re.match('^[^(]+\(([^)]+)\)$', self[0])
if m: if m:
use.append(m.group(1)) use.append(m.group(1))
...@@ -190,7 +190,7 @@ class Statement: ...@@ -190,7 +190,7 @@ class Statement:
use.append(self[1]) use.append(self[1])
# Case arg1 relative adressing # Case arg1 relative adressing
if self.is_load_non_immediate() or self.is_store(): if self.is_load_non_immediate() or self.is_store():
m = re.match('^\d+\(([^)]+)\)$', self[1]) m = re.match('^[^(]+\(([^)]+)\)$', self[1])
if m: if m:
use.append(m.group(1)) use.append(m.group(1))
...@@ -290,4 +290,8 @@ class Block: ...@@ -290,4 +290,8 @@ class Block:
def reverse_statements(self): def reverse_statements(self):
"""Reverse the statement list and reset the pointer.""" """Reverse the statement list and reset the pointer."""
self.statements = self.statements[::-1] self.statements = self.statements[::-1]
self.reset()
def reset(self):
"""Reset the internal pointer."""
self.pointer = 0 self.pointer = 0
...@@ -33,7 +33,7 @@ def write_statements(statements): ...@@ -33,7 +33,7 @@ def write_statements(statements):
else: else:
line += ' ' line += ' '
line += ','.join(s.args) line += ','.join(map(str, s))
else: else:
raise Exception('Unsupported statement type "%s"' % s.stype) raise Exception('Unsupported statement type "%s"' % s.stype)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment