Commit 45947974 authored by Jayke Meijer's avatar Jayke Meijer

Added algebraic conversions and first unittest for those.

parent a2133db3
...@@ -208,3 +208,30 @@ def copy_propagation(block): ...@@ -208,3 +208,30 @@ def copy_propagation(block):
changed = True changed = True
return changed return changed
def algebraic_transformations(block):
"""
Change ineffective or useless algebraic transformations. Handled are:
- x = x + 0 -> remove
- x = x - 0 -> remove
- x = x * 1 -> remove
- x = x * 2 -> x = x << 1
"""
changed = False
while not block.end():
changed = True
s = block.read()
if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
block.replace(1, [])
elif s.is_command('mult') and s[2] == 1:
block.replace(1, [])
elif s.is_command('mult') and s[2] == 2:
new_command = S(['command', 'sll', s[0], s[1], 1])
block.replace(1, [new_command])
else:
changed = False
return changed
import unittest import unittest
from src.optimize.advanced import eliminate_common_subexpressions, \ from src.optimize.advanced import eliminate_common_subexpressions, \
fold_constants, copy_propagation fold_constants, copy_propagation, algebraic_transformations
from src.statement import Statement as S, Block as B from src.statement import Statement as S, Block as B
...@@ -63,3 +63,13 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -63,3 +63,13 @@ class TestOptimizeAdvanced(unittest.TestCase):
block = B(arguments) block = B(arguments)
self.assertFalse(copy_propagation(block)) self.assertFalse(copy_propagation(block))
self.assertEqual(block.statements, arguments) self.assertEqual(block.statements, arguments)
def test_algebraic_transforms_add0(self):
block = B([self.foo,
S('command', 'addu', '$1', '$2', 0),
self.bar])
# self.assertTrue(copy_propagation(block))
algebraic_transformations(block)
self.assertEqual(block.statements, [self.foo,
self.bar])
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