glob.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import re
  2. def redundant_move_1(mov, statements):
  3. """
  4. mov $regA, $regA -> --- remove it
  5. """
  6. if mov.is_command('move') and mov[0] == mov[1]:
  7. statements.replace(1, [])
  8. return True
  9. def redundant_move_2(mov, statements):
  10. """
  11. mov $regA, $regB -> instr $regA, $regB, ...
  12. instr $regA, $regA, ...
  13. """
  14. if mov.is_command('move'):
  15. ins = statements.peek()
  16. if ins and len(ins) >= 2 and ins[0] == mov[0] and ins[1] == mov[0]:
  17. ins[1] = mov[1]
  18. statements.replace(2, [ins])
  19. return True
  20. def redundant_move_3(ins, statements):
  21. """
  22. instr $regA, ... -> instr $4, ...
  23. mov $4, $regA jal XX
  24. jal XX
  25. """
  26. if ins.is_command() and len(ins):
  27. following = statements.peek(2)
  28. if len(following) == 2:
  29. mov, jal = following
  30. if mov.is_command('move') and mov[1] == ins[0] \
  31. and re.match('^\$[4-7]$', mov[0]) \
  32. and jal.is_command('jal'):
  33. ins[0] = mov[0]
  34. statements.replace(2, [ins])
  35. return True
  36. def redundant_move_4(mov1, statements):
  37. """
  38. mov $RegA, $RegB -> move $RegA, $RegB
  39. mov $RegB, $RegA
  40. """
  41. if mov1.is_command('move'):
  42. mov2 = statements.peek()
  43. if mov2.is_command('move') and mov2[0] == mov1[1] and \
  44. mov2[1] == mov1[0]:
  45. statements.replace(2, [mov1])
  46. return True
  47. def redundant_load(sw, statements):
  48. """
  49. sw $regA, XX -> sw $regA, XX
  50. ld $regA, XX
  51. """
  52. if sw.is_command('sw'):
  53. ld = statements.peek()
  54. if ld.is_command('lw') and ld.args == sw.args:
  55. statements.replace(2, [sw])
  56. return True
  57. def redundant_shift(shift, statements):
  58. """
  59. shift $regA, $regA, 0 -> --- remove it
  60. """
  61. if shift.is_shift() and shift[0] == shift[1] and shift[2] == 0:
  62. statements.replace(1, [])
  63. return True
  64. def redundant_add(add, statements):
  65. """
  66. add $regA, $regA, X -> lw ..., X($regA)
  67. lw ..., 0($regA)
  68. """
  69. if add.is_command('addu') and add[0] == add[1] and isinstance(add[2], int):
  70. lw = statements.peek()
  71. if lw.is_load() and lw[-1] == '0(%s)' % add[0]:
  72. lw[-1] = '%s(%s)' % (add[2], add[0])
  73. statements.replace(2, [lw])
  74. return True