test_optimize_advanced.py 7.2 KB

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