Commit 8c9efd40 authored by Jayke Meijer's avatar Jayke Meijer

Changed algebraic transformations to more generic form.

parent 5aba6a1e
......@@ -220,11 +220,11 @@ def copy_propagation(block):
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 * 0 -> x = 0
- x = x * 2 -> x = x << 1
- x = y + 0 -> x = y
- x = y - 0 -> x = y
- x = y * 1 -> x = y
- x = y * 0 -> x = 0
- x = y * 2 -> x = x << 1
"""
changed = False
......@@ -232,10 +232,10 @@ def algebraic_transformations(block):
s = block.read()
if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
block.replace(1, [])
block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True
elif s.is_command('mult') and s[2] == 1:
block.replace(1, [])
block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True
elif s.is_command('mult') and s[2] == 0:
block.replace(1, [S('command', 'li', '$1', to_hex(0))])
......
......@@ -78,6 +78,7 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo,
S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_add1(self):
......@@ -96,6 +97,7 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo,
S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_sub1(self):
......@@ -124,6 +126,7 @@ class TestOptimizeAdvanced(unittest.TestCase):
self.assertTrue(algebraic_transformations(block))
self.assertEqual(block.statements, [self.foo,
S('command', 'move', '$1', '$2'),
self.bar])
def test_algebraic_transforms_mult2(self):
......
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