test_optimize_advanced.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_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', '$1', '$2', 0),
  107. self.bar])
  108. self.assertTrue(algebraic_transformations(block))
  109. self.assertEqual(block.statements, [self.foo,
  110. S('command', 'li', '$1', '0x00000000'),
  111. self.bar])
  112. def test_algebraic_transforms_mult1(self):
  113. block = B([self.foo,
  114. S('command', 'mult', '$1', '$2', 1),
  115. self.bar])
  116. self.assertTrue(algebraic_transformations(block))
  117. self.assertEqual(block.statements, [self.foo,
  118. S('command', 'move', '$1', '$2'),
  119. self.bar])
  120. def test_algebraic_transforms_mult2(self):
  121. block = B([self.foo,
  122. S('command', 'mult', '$1', '$2', 2),
  123. self.bar])
  124. self.assertTrue(algebraic_transformations(block))
  125. self.assertEqual(block.statements, [self.foo,
  126. S('command', 'sll', '$1', '$2', 1),
  127. self.bar])
  128. def test_algebraic_transforms_mult16(self):
  129. block = B([self.foo,
  130. S('command', 'mult', '$1', '$2', 16),
  131. self.bar])
  132. self.assertTrue(algebraic_transformations(block))
  133. self.assertEqual(block.statements, [self.foo,
  134. S('command', 'sll', '$1', '$2', 4),
  135. self.bar])
  136. def test_algebraic_transforms_mult3(self):
  137. arguments = [self.foo,
  138. S('command', 'mult', '$1', '$2', 3),
  139. self.bar]
  140. block = B(arguments)
  141. self.assertFalse(algebraic_transformations(block))
  142. self.assertEqual(block.statements, arguments)