test_optimize_advanced.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. import src.liveness as liveness
  7. class TestOptimizeAdvanced(unittest.TestCase):
  8. def setUp(self):
  9. self.foo = S('command', 'foo')
  10. self.bar = S('command', 'bar')
  11. def tearDown(self):
  12. del self.foo
  13. del self.bar
  14. def test_eliminate_common_subexpressions_simple(self):
  15. b = B([S('command', 'addu', '$regC', '$regA', '$regB'),
  16. S('command', 'addu', '$regD', '$regA', '$regB')])
  17. e = [S('command', 'addu', '$8', '$regA', '$regB'), \
  18. S('command', 'move', '$regC', '$8'), \
  19. S('command', 'move', '$regD', '$8')]
  20. liveness.create_in_out([b])
  21. eliminate_common_subexpressions(b)
  22. self.assertEqual(b.statements, e)
  23. def test_eliminate_common_subexpressions_assigned(self):
  24. b = B([S('command', 'addu', '$regC', '$regA', '$regB'),
  25. S('command', 'li', '$regA', '0x00000001'),
  26. S('command', 'addu', '$regD', '$regA', '$regB')])
  27. e = copy(b.statements)
  28. liveness.create_in_out([b])
  29. eliminate_common_subexpressions(b)
  30. self.assertEqual(b.statements, e)
  31. def test_fold_constants(self):
  32. pass
  33. def test_copy_propagation_true(self):
  34. block = B([self.foo,
  35. S('command', 'move', '$1', '$2'),
  36. self.foo,
  37. S('command', 'addu', '$3', '$1', '$4'),
  38. self.bar])
  39. self.assertTrue(copy_propagation(block))
  40. self.assertEqual(block.statements, [self.foo,
  41. S('command', 'move', '$1', '$2'),
  42. self.foo,
  43. S('command', 'addu', '$3', '$2', '$4'),
  44. self.bar])
  45. def test_copy_propagation_other_arg(self):
  46. block = B([self.foo,
  47. S('command', 'move', '$1', '$2'),
  48. self.foo,
  49. S('command', 'addu', '$3', '$4', '$1'),
  50. self.bar])
  51. self.assertTrue(copy_propagation(block))
  52. self.assertEqual(block.statements, [self.foo,
  53. S('command', 'move', '$1', '$2'),
  54. self.foo,
  55. S('command', 'addu', '$3', '$4', '$2'),
  56. self.bar])
  57. def test_copy_propagation_overwrite(self):
  58. block = B([self.foo, \
  59. S('command', 'move', '$1', '$2'),
  60. S('command', 'move', '$1', '$5'),
  61. S('command', 'addu', '$3', '$1', '$4'),
  62. self.bar])
  63. self.assertTrue(copy_propagation(block))
  64. self.assertEqual(block.statements, [self.foo,
  65. S('command', 'move', '$1', '$2'),
  66. S('command', 'move', '$1', '$5'),
  67. S('command', 'addu', '$3', '$5', '$4'),
  68. self.bar])
  69. def test_copy_propagation_false(self):
  70. arguments = [self.foo,
  71. S('command', 'move', '$1', '$2'),
  72. S('command', 'move', '$10', '$20'),
  73. S('command', 'addu', '$1', '$5', 1),
  74. S('command', 'addu', '$3', '$1', '$4'),
  75. self.bar]
  76. block = B(arguments)
  77. self.assertFalse(copy_propagation(block))
  78. self.assertEqual(block.statements, arguments)
  79. def test_copy_propagation_false_severalmoves(self):
  80. arguments = [self.foo,
  81. S('command', 'move', '$1', '$2'),
  82. self.foo,
  83. S('command', 'addu', '$1', '$5', 1),
  84. S('command', 'addu', '$3', '$1', '$4'),
  85. self.bar]
  86. block = B(arguments)
  87. self.assertFalse(copy_propagation(block))
  88. self.assertEqual(block.statements, arguments)
  89. def test_algebraic_transforms_add0(self):
  90. block = B([self.foo,
  91. S('command', 'addu', '$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_add1(self):
  98. arguments = [self.foo,
  99. S('command', 'addu', '$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_sub0(self):
  105. block = B([self.foo,
  106. S('command', 'subu', '$1', '$2', 0),
  107. self.bar])
  108. self.assertTrue(algebraic_transformations(block))
  109. self.assertEqual(block.statements, [self.foo,
  110. S('command', 'move', '$1', '$2'),
  111. self.bar])
  112. def test_algebraic_transforms_sub1(self):
  113. arguments = [self.foo,
  114. S('command', 'subu', '$1', '$2', 1),
  115. self.bar]
  116. block = B(arguments)
  117. self.assertFalse(algebraic_transformations(block))
  118. self.assertEqual(block.statements, arguments)
  119. def test_algebraic_transforms_mult0(self):
  120. block = B([self.foo,
  121. S('command', 'mult', '$2', 0),
  122. S('command', 'mflo', '$1'),
  123. self.bar])
  124. self.assertTrue(algebraic_transformations(block))
  125. self.assertEqual(block.statements, [self.foo,
  126. S('command', 'li', '$1', '0x00000000'),
  127. self.bar])
  128. def test_algebraic_transforms_mult1(self):
  129. block = B([self.foo,
  130. S('command', 'mult', '$2', 1),
  131. S('command', 'mflo', '$1'),
  132. self.bar])
  133. self.assertTrue(algebraic_transformations(block))
  134. self.assertEqual(block.statements, [self.foo,
  135. S('command', 'move', '$1', '$2'),
  136. self.bar])
  137. def test_algebraic_transforms_mult2(self):
  138. block = B([self.foo,
  139. S('command', 'mult', '$2', 2),
  140. S('command', 'mflo', '$1'),
  141. self.bar])
  142. self.assertTrue(algebraic_transformations(block))
  143. self.assertEqual(block.statements, [self.foo,
  144. S('command', 'sll', '$1', '$2', 1),
  145. self.bar])
  146. def test_algebraic_transforms_mult16(self):
  147. block = B([self.foo,
  148. S('command', 'mult', '$2', 16),
  149. S('command', 'mflo', '$1'),
  150. self.bar])
  151. self.assertTrue(algebraic_transformations(block))
  152. self.assertEqual(block.statements, [self.foo,
  153. S('command', 'sll', '$1', '$2', 4),
  154. self.bar])
  155. def test_algebraic_transforms_mult3(self):
  156. arguments = [self.foo,
  157. S('command', 'mult', '$2', 3),
  158. S('command', 'mflo', '$1'),
  159. self.bar]
  160. block = B(arguments)
  161. self.assertFalse(algebraic_transformations(block))
  162. self.assertEqual(block.statements, arguments)