Commit e0b4e598 authored by Taddeus Kroes's avatar Taddeus Kroes

Worked on optimization loop.

parents e6324624 15bbae43
...@@ -14,6 +14,7 @@ def optimize(program, verbose=0): ...@@ -14,6 +14,7 @@ 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
......
...@@ -124,30 +124,29 @@ def add_lw(add, statements): ...@@ -124,30 +124,29 @@ def add_lw(add, statements):
return True return True
def remove_redundant_jumps(statements): def remove_redundant_jumps(statements):
"""Remove jump if label follows immediatly.""" """Remove jump if label follows immediately."""
old_len = -1 changed = False
while old_len != len(statements):
old_len = len(statements)
statements.reset()
while not statements.end(): while not statements.end():
s = statements.read() s = statements.read()
# j $Lx -> $Lx: # j $Lx -> $Lx:
# $Lx: # $Lx:
if s.is_command('j'): if s.is_command('j'):
following = statements.peek(2) following = statements.peek()
if following.is_label(s[0]): if following.is_label(s[0]):
statements.replace(1, []) statements.replace(1, [])
changed = True
return True
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."""
old_len = -1 changed = False
while old_len != len(statements):
old_len = len(statements)
statements.reset()
while not statements.end(): while not statements.end():
s = statements.read() s = statements.read()
...@@ -164,5 +163,6 @@ def remove_redundant_branch_jumps(statements): ...@@ -164,5 +163,6 @@ def remove_redundant_branch_jumps(statements):
s.name = 'bne' if s.is_command('beq') else 'beq' s.name = 'bne' if s.is_command('beq') else 'beq'
s[2] = j[0] s[2] = j[0]
statements.replace(3, [s, label]) statements.replace(3, [s, label])
changed = True
statements.reset() return changed
from statement import Statement as S, Block from statement import Statement as S, Block
from dataflow import find_basic_blocks, generate_flow_graph from dataflow import find_basic_blocks, generate_flow_graph
from optimize.redundancies import remove_redundant_jumps, remove_redundancies from optimize.redundancies import remove_redundant_jumps, remove_redundancies,\
remove_redundant_branch_jumps
from optimize.advanced import eliminate_common_subexpressions, \ from optimize.advanced import eliminate_common_subexpressions, \
fold_constants, copy_propagation, eliminate_dead_code fold_constants, copy_propagation, eliminate_dead_code
from writer import write_statements from writer import write_statements
...@@ -65,42 +66,24 @@ class Program(Block): ...@@ -65,42 +66,24 @@ 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)
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."""
self.program_iterations = self.block_iterations = 0 changed = False
program_changed = True
while program_changed:
self.program_iterations += 1
program_changed = False
for block in self.blocks: for block in self.blocks:
self.block_iterations += 1 print 'block iteration'
block_changed = True if remove_redundancies(block) \
| eliminate_common_subexpressions(block) \
while block_changed: | fold_constants(block) \
block_changed = False | copy_propagation(block) \
| eliminate_dead_code(block):
if remove_redundancies(block): changed = True
block_changed = True
return changed
if eliminate_common_subexpressions(block):
block_changed = True
if fold_constants(block):
block_changed = True
if copy_propagation(block):
block_changed = True
if eliminate_dead_code(block):
block_changed = True
if block_changed:
program_changed = True
def find_basic_blocks(self): def find_basic_blocks(self):
"""Divide the statement list into basic blocks.""" """Divide the statement list into basic blocks."""
......
...@@ -63,3 +63,49 @@ class TestLiveness(unittest.TestCase): ...@@ -63,3 +63,49 @@ class TestLiveness(unittest.TestCase):
self.assertEqual(b2.live_out, set(['b', 'd'])) self.assertEqual(b2.live_out, set(['b', 'd']))
self.assertEqual(b3.live_in, set(['b', 'd'])) self.assertEqual(b3.live_in, set(['b', 'd']))
self.assertEqual(b3.live_out, set()) self.assertEqual(b3.live_out, set())
# def test_create_in_out_two(self):
# s11 = S('command', 'subu', 'i', 'm', '0x00000001')
# s12 = S('command', 'move', 'j', 'n')
# s13 = S('command', 'move', 'a', 'u1')
# s14 = S('command', 'subu', 'i', 'm', '0x00000001')
# s15 = S('command', 'j', 'L1')
# s16 = S('')
#
# s21 = S('label', 'L1')
# s22 = S('command', 'addi', 'i', '0x00000001')
# s23 = S('command', 'subi', 'j', '0x00000001')
# s24 = S('command', 'j', 'L2')
#
# s31 = S('label', 'L2')
# s32 = S('command', 'move', 'a', 'u2')
# s33 = S('command', 'j', 'L1')
# s41 = S('label', 'L3')
# s42 = S('command', 'move', 'i', 'u3')
# s43 = S('command', 'beq', 'g', 'd', 'L4')
#
# s51 = S('label', 'L4')
# s52 = S('command', 'addu', 'b', 'i', 'a')
#
# b1, b2, b3, b4 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22,\
# s31, s32, s33, s41, s42, s43, s51])
# generate_flow_graph([b1, b2, b3, b4, b5])
# create_in_out([b1, b2, b3, b4, b5])
#
#
#self.assertEqual(b1.)
import unittest import unittest
from src.optimize.redundancies import remove_redundancies, remove_redundant_jumps from src.optimize.redundancies import remove_redundancies, \
remove_redundant_jumps, remove_redundant_branch_jumps
from src.program import Program from src.program import Program
from src.statement import Statement as S, Block as B from src.statement import Statement as S, Block as B
...@@ -200,11 +201,11 @@ class TestOptimize(unittest.TestCase): ...@@ -200,11 +201,11 @@ class TestOptimize(unittest.TestCase):
S('label', '$L1'), S('label', '$L1'),
self.bar]) self.bar])
remove_redundancies(block) remove_redundant_jumps(block)
self.assertEqual(block.statements, B([self.foo, self.assertEqual(block.statements, [self.foo,
S('command', 'j', '$L1'), S('label', '$L1'),
self.bar])) self.bar])
def test_remove_redundant_jumps_false(self): def test_remove_redundant_jumps_false(self):
arguments = [self.foo, arguments = [self.foo,
...@@ -213,7 +214,7 @@ class TestOptimize(unittest.TestCase): ...@@ -213,7 +214,7 @@ class TestOptimize(unittest.TestCase):
self.bar] self.bar]
block = B(arguments) block = B(arguments)
remove_redundancies(block) remove_redundant_jumps(block)
self.assertEqual(block.statements, arguments) self.assertEqual(block.statements, arguments)
......
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