Commit effe7267 authored by Taddeus Kroes's avatar Taddeus Kroes

Added common subexpression elimination (unfinished and untested).

parent c308e7b7
...@@ -3,6 +3,7 @@ from src.dataflow import find_basic_blocks ...@@ -3,6 +3,7 @@ from src.dataflow import find_basic_blocks
from standard import redundant_move_1, redundant_move_2, \ from standard import redundant_move_1, redundant_move_2, \
redundant_move_3, redundant_move_4, redundant_load, \ redundant_move_3, redundant_move_4, redundant_load, \
redundant_shift, redundant_add redundant_shift, redundant_add
from advanced import eliminate_common_subexpressions, fold_constants
def optimize_global(statements): def optimize_global(statements):
...@@ -30,41 +31,34 @@ def optimize_global(statements): ...@@ -30,41 +31,34 @@ def optimize_global(statements):
statements.replace(3, [s, label]) statements.replace(3, [s, label])
#def optimize_blocks(blocks): def optimize_block(block):
# """Call the optimizer for each basic block. Do this several times until
# no more optimizations are achieved."""
# for block in blocks:
# optimize_block(block)
#
# return blocks
def optimize_block(statements):
"""Optimize a basic block.""" """Optimize a basic block."""
standard = [redundant_move_1, redundant_move_2, redundant_move_3, \ standard = [redundant_move_1, redundant_move_2, redundant_move_3, \
redundant_move_4, redundant_load, redundant_shift, \ redundant_move_4, redundant_load, redundant_shift, \
redundant_add] redundant_add]
old_len = -1 old_len = -1
while old_len != len(statements): # Standard optimizations
old_len = len(statements) while old_len != len(block):
old_len = len(block)
while not statements.end():
s = statements.read()
# while not block.end():
cont = False s = block.read()
for callback in standard: for callback in standard:
if callback(s, statements): if callback(s, block):
cont = True
break break
if cont: # Advanced optimizations
continue #changed = True
# Other optimizations... #while changed:
# changed = eliminate_common_subexpressions(block) \
# or fold_constants(block)
while eliminate_common_subexpressions(block) \
| fold_constants(block):
pass
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
......
from src.statement import Statement as S
def eliminate_common_subexpressions(block):
"""
Common subexpression elimination:
- Traverse through the statements in reverse order.
- If the statement can be possibly be eliminated, walk further collecting
all other occurrences of the expression until one of the arguments is
assigned in a statement, or the start of the block has been reached.
- If one or more occurrences were found, insert the expression with a new
destination address before the last found occurrence and change all
occurrences to a move instruction from that address.
"""
found = False
block.reverse_statements()
while not block.end():
s = block.read()
if s.is_arith():
pointer = block.pointer
last = False
args = s[1:]
# Collect similar statements
while not block.end():
s2 = block.read()
# Stop if one of the arguments is assigned
if len(s2) and s2[0] in args:
break
# Replace a similar expression by a move instruction
if s2.name == s.name and s2[1:] == args:
block.replace(1, [S('command', 'move', s2[0], new_reg)])
last = block.pointer
# Insert an additional expression with a new destination address
if last:
block.insert(S('command', s.name, [new_reg] + args), last)
found = True
# Reset pointer to and continue from the original statement
block.pointer = pointer
block.reverse_statements()
return found
def fold_constants(block):
"""
Constant folding:
"""
return False
...@@ -139,7 +139,18 @@ class Block: ...@@ -139,7 +139,18 @@ class Block:
self.statements = before + replacement + after self.statements = before + replacement + after
self.pointer = start + len(replacement) self.pointer = start + len(replacement)
def insert(self, statement, index=None):
if index == None:
index = self.pointer
self.statements.insert(index, statement)
def apply_filter(self, callback): def apply_filter(self, callback):
"""Apply a filter to the statement list. If the callback returns True, """Apply a filter to the statement list. If the callback returns True,
the statement will remain in the list..""" the statement will remain in the list.."""
self.statements = filter(callback, self.statements) self.statements = filter(callback, self.statements)
def reverse_statements(self):
"""Reverse the statement list and reset the pointer."""
self.statements = self.statements[::-1]
self.pointer = 0
import unittest
from src.optimize.advanced import eliminate_common_subexpressions
from src.statement import Statement as S, Block as B
class TestOptimizeAdvanced(unittest.TestCase):
def setUp(self):
pass
def test_eliminate_common_subexpressions(self):
pass
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