test_optimize_advanced.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import unittest
  2. from copy import copy
  3. from src.optimize.advanced import eliminate_common_subexpressions, \
  4. fold_constants, copy_propagation, algebraic_transformations
  5. from src.statement import Statement as S, Block as B
  6. class TestOptimizeAdvanced(unittest.TestCase):
  7. def setUp(self):
  8. self.foo = S('command', 'foo')
  9. self.bar = S('command', 'bar')
  10. def tearDown(self):
  11. del self.foo
  12. del self.bar
  13. def test_eliminate_common_subexpressions_simple(self):
  14. b = B([S('command', 'addu', '$regC', '$regA', '$regB'),
  15. S('command', 'addu', '$regD', '$regA', '$regB')])
  16. e = [S('command', 'addu', '$8', '$regA', '$regB'), \
  17. S('command', 'move', '$regC', '$8'), \
  18. S('command', 'move', '$regD', '$8')]
  19. eliminate_common_subexpressions(b)
  20. self.assertEqual(b.statements, e)
  21. def test_eliminate_common_subexpressions_assigned(self):
  22. b = B([S('command', 'addu', '$regC', '$regA', '$regB'),
  23. S('command', 'li', '$regA', '0x00000001'),
  24. S('command', 'addu', '$regD', '$regA', '$regB')])
  25. e = copy(b.statements)
  26. eliminate_common_subexpressions(b)
  27. self.assertEqual(b.statements, e)
  28. def test_fold_constants(self):
  29. pass
  30. def test_copy_propagation_true(self):
  31. block = B([self.foo,
  32. S('command', 'move', '$1', '$2'),
  33. self.foo,
  34. S('command', 'addu', '$3', '$1', '$4'),
  35. self.bar])
  36. self.assertTrue(copy_propagation(block))
  37. self.assertEqual(block.statements, [self.foo,
  38. S('command', 'move', '$1', '$2'),
  39. self.foo,
  40. S('command', 'addu', '$3', '$2', '$4'),
  41. self.bar])
  42. def test_copy_propagation_overwrite(self):
  43. block = B([self.foo, \
  44. S('command', 'move', '$1', '$2'),
  45. S('command', 'move', '$1', '$5'),
  46. S('command', 'addu', '$3', '$1', '$4'),
  47. self.bar])
  48. self.assertTrue(copy_propagation(block))
  49. self.assertEqual(block.statements, [self.foo,
  50. S('command', 'move', '$1', '$2'),
  51. S('command', 'move', '$1', '$5'),
  52. S('command', 'addu', '$3', '$5', '$4'),
  53. self.bar])
  54. def test_copy_propagation_false(self):
  55. arguments = [self.foo,
  56. S('command', 'move', '$1', '$2'),
  57. S('command', 'move', '$10', '$20'),
  58. S('command', 'addu', '$1', '$5', 1),
  59. S('command', 'addu', '$3', '$1', '$4'),
  60. self.bar]
  61. block = B(arguments)
  62. self.assertFalse(copy_propagation(block))
  63. self.assertEqual(block.statements, arguments)
  64. def test_copy_propagation_false_severalmoves(self):
  65. arguments = [self.foo,
  66. S('command', 'move', '$1', '$2'),
  67. self.foo,
  68. S('command', 'addu', '$1', '$5', 1),
  69. S('command', 'addu', '$3', '$1', '$4'),
  70. self.bar]
  71. block = B(arguments)
  72. self.assertFalse(copy_propagation(block))
  73. self.assertEqual(block.statements, arguments)
  74. def test_algebraic_transforms_add0(self):
  75. block = B([self.foo,
  76. S('command', 'addu', '$1', '$2', 0),
  77. self.bar])
  78. self.assertTrue(algebraic_transformations(block))
  79. self.assertEqual(block.statements, [self.foo,
  80. S('command', 'move', '$1', '$2'),
  81. self.bar])
  82. def test_algebraic_transforms_add1(self):
  83. arguments = [self.foo,
  84. S('command', 'addu', '$1', '$2', 1),
  85. self.bar]
  86. block = B(arguments)
  87. self.assertFalse(algebraic_transformations(block))
  88. self.assertEqual(block.statements, arguments)
  89. def test_algebraic_transforms_sub0(self):
  90. block = B([self.foo,
  91. S('command', 'subu', '$1', '$2', 0),
  92. self.bar])
  93. self.assertTrue(algebraic_transformations(block))
  94. self.assertEqual(block.statements, [self.foo,
  95. S('command', 'move', '$1', '$2'),
  96. self.bar])
  97. def test_algebraic_transforms_sub1(self):
  98. arguments = [self.foo,
  99. S('command', 'subu', '$1', '$2', 1),
  100. self.bar]
  101. block = B(arguments)
  102. self.assertFalse(algebraic_transformations(block))
  103. self.assertEqual(block.statements, arguments)
  104. def test_algebraic_transforms_mult0(self):
  105. block = B([self.foo,
  106. S('command', 'mult', '$2', 0),
  107. S('command', 'mflo', '$1'),
  108. self.bar])
  109. self.assertTrue(algebraic_transformations(block))
  110. self.assertEqual(block.statements, [self.foo,
  111. S('command', 'li', '$1', '0x00000000'),
  112. self.bar])
  113. def test_algebraic_transforms_mult1(self):
  114. block = B([self.foo,
  115. S('command', 'mult', '$2', 1),
  116. S('command', 'mflo', '$1'),
  117. self.bar])
  118. self.assertTrue(algebraic_transformations(block))
  119. self.assertEqual(block.statements, [self.foo,
  120. S('command', 'move', '$1', '$2'),
  121. self.bar])
  122. def test_algebraic_transforms_mult2(self):
  123. block = B([self.foo,
  124. S('command', 'mult', '$2', 2),
  125. S('command', 'mflo', '$1'),
  126. self.bar])
  127. self.assertTrue(algebraic_transformations(block))
  128. self.assertEqual(block.statements, [self.foo,
  129. S('command', 'sll', '$1', '$2', 1),
  130. self.bar])
  131. def test_algebraic_transforms_mult16(self):
  132. block = B([self.foo,
  133. S('command', 'mult', '$2', 16),
  134. S('command', 'mflo', '$1'),
  135. self.bar])
  136. self.assertTrue(algebraic_transformations(block))
  137. self.assertEqual(block.statements, [self.foo,
  138. S('command', 'sll', '$1', '$2', 4),
  139. self.bar])
  140. def test_algebraic_transforms_mult3(self):
  141. arguments = [self.foo,
  142. S('command', 'mult', '$1', '$2', 3),
  143. self.bar]
  144. block = B(arguments)
  145. self.assertFalse(algebraic_transformations(block))
  146. self.assertEqual(block.statements, arguments)