Commit d6e71f46 authored by Richard Torenvliet's avatar Richard Torenvliet

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

parents ebef48ed 122ce7a0
...@@ -14,7 +14,6 @@ def optimize(program, verbose=0): ...@@ -14,7 +14,6 @@ def optimize(program, verbose=0):
changed = True changed = True
while changed: while changed:
print 'main iteration'
changed = False changed = False
# Optimize on a global level # Optimize on a global level
......
...@@ -95,7 +95,6 @@ def eliminate_common_subexpressions(block): ...@@ -95,7 +95,6 @@ def eliminate_common_subexpressions(block):
% (s.name, ', '.join(map(str, s))) % (s.name, ', '.join(map(str, s)))
block.insert(S('command', s.name, *([new_reg] + args)), \ block.insert(S('command', s.name, *([new_reg] + args)), \
index=occurrences[0], message=message) index=occurrences[0], message=message)
changed = True changed = True
# Reset pointer to continue from the original statement # Reset pointer to continue from the original statement
...@@ -267,6 +266,7 @@ def fold_constants(block): ...@@ -267,6 +266,7 @@ def fold_constants(block):
else 'Substraction', s[1]) else 'Substraction', s[1])
block.replace(1, [S('command', 'move', rd, s[1])], \ block.replace(1, [S('command', 'move', rd, s[1])], \
message=message) message=message)
changed = True
else: else:
for reg in s.get_def(): for reg in s.get_def():
if reg in register: if reg in register:
......
...@@ -5,21 +5,16 @@ def remove_redundancies(block): ...@@ -5,21 +5,16 @@ def remove_redundancies(block):
"""Execute all functions that remove redundant statements.""" """Execute all functions that remove redundant statements."""
callbacks = [move_aa, move_inst, instr_move_jal, move_move, sw_ld, shift, callbacks = [move_aa, move_inst, instr_move_jal, move_move, sw_ld, shift,
add_lw] add_lw]
old_len = -1
changed = False changed = False
while old_len != len(block): block.reset()
old_len = len(block)
block.reset() while not block.end():
s = block.read()
while not block.end(): for callback in callbacks:
s = block.read() if callback(s, block):
changed = True
for callback in callbacks:
if callback(s, block):
changed = True
break
return changed return changed
...@@ -140,7 +135,7 @@ def remove_redundant_jumps(statements): ...@@ -140,7 +135,7 @@ def remove_redundant_jumps(statements):
statements.replace(1, []) statements.replace(1, [])
changed = True changed = True
return True return changed
def remove_redundant_branch_jumps(statements): def remove_redundant_branch_jumps(statements):
"""Optimize statement sequences on a global level.""" """Optimize statement sequences on a global level."""
......
...@@ -66,22 +66,21 @@ class Program(Block): ...@@ -66,22 +66,21 @@ class Program(Block):
if not hasattr(self, 'statements'): if not hasattr(self, 'statements'):
self.statements = self.get_statements() self.statements = self.get_statements()
return remove_redundant_jumps(self) \ return remove_redundant_jumps(self)
| remove_redundant_branch_jumps(self) # | remove_redundant_branch_jumps(self)
def optimize_blocks(self): def optimize_blocks(self):
"""Optimize on block level. Keep executing all optimizations until no """Optimize on block level. Keep executing all optimizations until no
more changes occur.""" more changes occur."""
changed = False changed = False
for block in self.blocks: # for block in self.blocks:
print 'block iteration' # if remove_redundancies(block) \
if 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): # changed = True
changed = True
return changed return changed
......
...@@ -41,12 +41,12 @@ class TestLiveness(unittest.TestCase): ...@@ -41,12 +41,12 @@ class TestLiveness(unittest.TestCase):
s33 = S('command', 'mult', 'b', 'd') s33 = S('command', 'mult', 'b', 'd')
s34 = S('command', 'mflo', 'temp') s34 = S('command', 'mflo', 'temp')
s35 = S('command', 'addu', 'return', 'temp', 'c') s35 = S('command', 'addu', 'return', 'temp', 'c')
b1, b2, b3 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22, \ b1, b2, b3, b4 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22, \
s31, s32, s33, s34, s35]) s31, s32, s33, s34, s35])
generate_flow_graph([b1, b2, b3]) generate_flow_graph([b1, b2, b3, b4])
create_in_out([b1, b2, b3]) create_in_out([b1, b2, b3, b4])
self.assertEqual(b1.use_set, set()) self.assertEqual(b1.use_set, set())
self.assertEqual(b1.def_set, set(['a', 'b', 'd', 'x'])) self.assertEqual(b1.def_set, set(['a', 'b', 'd', 'x']))
...@@ -57,12 +57,12 @@ class TestLiveness(unittest.TestCase): ...@@ -57,12 +57,12 @@ class TestLiveness(unittest.TestCase):
self.assertEqual(b3.use_set, set(['b', 'd'])) self.assertEqual(b3.use_set, set(['b', 'd']))
self.assertEqual(b3.def_set, set(['c', 'temp', 'return'])) self.assertEqual(b3.def_set, set(['c', 'temp', 'return']))
self.assertEqual(b1.live_in, set()) self.assertEqual(b1.live_in, set(['$4', '$5', '$6', '$7']))
self.assertEqual(b1.live_out, set(['a', 'b', 'd'])) self.assertEqual(b1.live_out, set(['a', 'b', 'd', '$4', '$5', '$6', '$7']))
self.assertEqual(b2.live_in, set(['a', 'b'])) self.assertEqual(b2.live_in, set(['a', 'b', '$4', '$5', '$6', '$7']))
self.assertEqual(b2.live_out, set(['b', 'd'])) self.assertEqual(b2.live_out, set(['b', 'd', '$4', '$5', '$6', '$7']))
self.assertEqual(b3.live_in, set(['b', 'd'])) self.assertEqual(b3.live_in, set(['b', 'd', '$4', '$5', '$6', '$7']))
self.assertEqual(b3.live_out, set()) self.assertEqual(b3.live_out, set(['$4', '$5', '$6', '$7']))
def test_create_in_out_two(self): def test_create_in_out_two(self):
s11 = S('command', 'subu', 'i', 'm', '0x00000001') s11 = S('command', 'subu', 'i', 'm', '0x00000001')
......
...@@ -55,11 +55,11 @@ class TestReachingDefinitions(unittest.TestCase): ...@@ -55,11 +55,11 @@ class TestReachingDefinitions(unittest.TestCase):
s34 = S('command', 'mflo', 'temp') s34 = S('command', 'mflo', 'temp')
s35 = S('command', 'addu', 'return', 'temp', 'c') s35 = S('command', 'addu', 'return', 'temp', 'c')
b1, b2, b3 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22, \ b1, b2, b3, b4 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22, \
s31, s32, s33, s34, s35]) s31, s32, s33, s34, s35])
generate_flow_graph([b1, b2, b3]) generate_flow_graph([b1, b2, b3, b4])
create_in_out([b1, b2, b3]) create_in_out([b1, b2, b3, b4])
self.assertEqual(b1.gen_set, set([s11.sid, s12.sid, s13.sid, self.assertEqual(b1.gen_set, set([s11.sid, s12.sid, s13.sid,
s14.sid])) s14.sid]))
......
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