Skip to content
Snippets Groups Projects
Commit 45947974 authored by Jayke Meijer's avatar Jayke Meijer
Browse files

Added algebraic conversions and first unittest for those.

parent a2133db3
No related branches found
No related tags found
No related merge requests found
......@@ -208,3 +208,30 @@ def copy_propagation(block):
changed = True
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
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
......@@ -63,3 +63,13 @@ class TestOptimizeAdvanced(unittest.TestCase):
block = B(arguments)
self.assertFalse(copy_propagation(block))
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])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment