test_optimize_advanced.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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', '$t0', '$regA', '$regB'), \
  17. S('command', 'move', '$regC', '$t0'), \
  18. S('command', 'move', '$regD', '$t0')]
  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_other_arg(self):
  43. block = B([self.foo,
  44. S('command', 'move', '$1', '$2'),
  45. self.foo,
  46. S('command', 'addu', '$3', '$4', '$1'),
  47. self.bar])
  48. self.assertTrue(copy_propagation(block))
  49. self.assertEqual(block.statements, [self.foo,
  50. S('command', 'move', '$1', '$2'),
  51. self.foo,
  52. S('command', 'addu', '$3', '$4', '$2'),
  53. self.bar])
  54. def test_copy_propagation_overwrite(self):
  55. block = B([self.foo, \
  56. S('command', 'move', '$1', '$2'),
  57. S('command', 'move', '$1', '$5'),
  58. S('command', 'addu', '$3', '$1', '$4'),
  59. self.bar])
  60. self.assertTrue(copy_propagation(block))
  61. self.assertEqual(block.statements, [self.foo,
  62. S('command', 'move', '$1', '$2'),
  63. S('command', 'move', '$1', '$5'),
  64. S('command', 'addu', '$3', '$5', '$4'),
  65. self.bar])
  66. def test_copy_propagation_false(self):
  67. arguments = [self.foo,
  68. S('command', 'move', '$1', '$2'),
  69. S('command', 'move', '$10', '$20'),
  70. S('command', 'addu', '$1', '$5', 1),
  71. S('command', 'addu', '$3', '$1', '$4'),
  72. self.bar]
  73. block = B(arguments)
  74. self.assertFalse(copy_propagation(block))
  75. self.assertEqual(block.statements, arguments)
  76. def test_copy_propagation_false_severalmoves(self):
  77. arguments = [self.foo,
  78. S('command', 'move', '$1', '$2'),
  79. self.foo,
  80. S('command', 'addu', '$1', '$5', 1),
  81. S('command', 'addu', '$3', '$1', '$4'),
  82. self.bar]
  83. block = B(arguments)
  84. self.assertFalse(copy_propagation(block))
  85. self.assertEqual(block.statements, arguments)
  86. def test_algebraic_transforms_add0(self):
  87. block = B([self.foo,
  88. S('command', 'addu', '$1', '$2', 0),
  89. self.bar])
  90. self.assertTrue(algebraic_transformations(block))
  91. self.assertEqual(block.statements, [self.foo,
  92. S('command', 'move', '$1', '$2'),
  93. self.bar])
  94. def test_algebraic_transforms_add1(self):
  95. arguments = [self.foo,
  96. S('command', 'addu', '$1', '$2', 1),
  97. self.bar]
  98. block = B(arguments)
  99. self.assertFalse(algebraic_transformations(block))
  100. self.assertEqual(block.statements, arguments)
  101. def test_algebraic_transforms_sub0(self):
  102. block = B([self.foo,
  103. S('command', 'subu', '$1', '$2', 0),
  104. self.bar])
  105. self.assertTrue(algebraic_transformations(block))
  106. self.assertEqual(block.statements, [self.foo,
  107. S('command', 'move', '$1', '$2'),
  108. self.bar])
  109. def test_algebraic_transforms_sub1(self):
  110. arguments = [self.foo,
  111. S('command', 'subu', '$1', '$2', 1),
  112. self.bar]
  113. block = B(arguments)
  114. self.assertFalse(algebraic_transformations(block))
  115. self.assertEqual(block.statements, arguments)
  116. def test_algebraic_transforms_mult0(self):
  117. block = B([self.foo,
  118. S('command', 'mult', '$2', 0),
  119. S('command', 'mflo', '$1'),
  120. self.bar])
  121. self.assertTrue(algebraic_transformations(block))
  122. self.assertEqual(block.statements, [self.foo,
  123. S('command', 'li', '$1', '0x00000000'),
  124. self.bar])
  125. def test_algebraic_transforms_mult1(self):
  126. block = B([self.foo,
  127. S('command', 'mult', '$2', 1),
  128. S('command', 'mflo', '$1'),
  129. self.bar])
  130. self.assertTrue(algebraic_transformations(block))
  131. self.assertEqual(block.statements, [self.foo,
  132. S('command', 'move', '$1', '$2'),
  133. self.bar])
  134. def test_algebraic_transforms_mult2(self):
  135. block = B([self.foo,
  136. S('command', 'mult', '$2', 2),
  137. S('command', 'mflo', '$1'),
  138. self.bar])
  139. self.assertTrue(algebraic_transformations(block))
  140. self.assertEqual(block.statements, [self.foo,
  141. S('command', 'sll', '$1', '$2', 1),
  142. self.bar])
  143. def test_algebraic_transforms_mult16(self):
  144. block = B([self.foo,
  145. S('command', 'mult', '$2', 16),
  146. S('command', 'mflo', '$1'),
  147. self.bar])
  148. self.assertTrue(algebraic_transformations(block))
  149. self.assertEqual(block.statements, [self.foo,
  150. S('command', 'sll', '$1', '$2', 4),
  151. self.bar])
  152. def test_algebraic_transforms_mult3(self):
  153. arguments = [self.foo,
  154. S('command', 'mult', '$2', 3),
  155. S('command', 'mflo', '$1'),
  156. self.bar]
  157. block = B(arguments)
  158. self.assertFalse(algebraic_transformations(block))
  159. self.assertEqual(block.statements, arguments)