Simplification.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using ARMeilleure.IntermediateRepresentation;
  2. using System;
  3. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  4. namespace ARMeilleure.CodeGen.Optimizations
  5. {
  6. static class Simplification
  7. {
  8. public static void RunPass(Operation operation)
  9. {
  10. switch (operation.Instruction)
  11. {
  12. case Instruction.Add:
  13. case Instruction.BitwiseExclusiveOr:
  14. TryEliminateBinaryOpComutative(operation, 0);
  15. break;
  16. case Instruction.BitwiseAnd:
  17. TryEliminateBitwiseAnd(operation);
  18. break;
  19. case Instruction.BitwiseOr:
  20. TryEliminateBitwiseOr(operation);
  21. break;
  22. case Instruction.ConditionalSelect:
  23. TryEliminateConditionalSelect(operation);
  24. break;
  25. case Instruction.Divide:
  26. TryEliminateBinaryOpY(operation, 1);
  27. break;
  28. case Instruction.Multiply:
  29. TryEliminateBinaryOpComutative(operation, 1);
  30. break;
  31. case Instruction.ShiftLeft:
  32. case Instruction.ShiftRightSI:
  33. case Instruction.ShiftRightUI:
  34. case Instruction.Subtract:
  35. TryEliminateBinaryOpY(operation, 0);
  36. break;
  37. }
  38. }
  39. private static void TryEliminateBitwiseAnd(Operation operation)
  40. {
  41. // Try to recognize and optimize those 3 patterns (in order):
  42. // x & 0xFFFFFFFF == x, 0xFFFFFFFF & y == y,
  43. // x & 0x00000000 == 0x00000000, 0x00000000 & y == 0x00000000
  44. Operand x = operation.GetSource(0);
  45. Operand y = operation.GetSource(1);
  46. if (IsConstEqual(x, AllOnes(x.Type)))
  47. {
  48. operation.TurnIntoCopy(y);
  49. }
  50. else if (IsConstEqual(y, AllOnes(y.Type)))
  51. {
  52. operation.TurnIntoCopy(x);
  53. }
  54. else if (IsConstEqual(x, 0) || IsConstEqual(y, 0))
  55. {
  56. operation.TurnIntoCopy(Const(0));
  57. }
  58. }
  59. private static void TryEliminateBitwiseOr(Operation operation)
  60. {
  61. // Try to recognize and optimize those 3 patterns (in order):
  62. // x | 0x00000000 == x, 0x00000000 | y == y,
  63. // x | 0xFFFFFFFF == 0xFFFFFFFF, 0xFFFFFFFF | y == 0xFFFFFFFF
  64. Operand x = operation.GetSource(0);
  65. Operand y = operation.GetSource(1);
  66. if (IsConstEqual(x, 0))
  67. {
  68. operation.TurnIntoCopy(y);
  69. }
  70. else if (IsConstEqual(y, 0))
  71. {
  72. operation.TurnIntoCopy(x);
  73. }
  74. else if (IsConstEqual(x, AllOnes(x.Type)) || IsConstEqual(y, AllOnes(y.Type)))
  75. {
  76. operation.TurnIntoCopy(Const(AllOnes(x.Type)));
  77. }
  78. }
  79. private static void TryEliminateBinaryOpY(Operation operation, ulong comparand)
  80. {
  81. Operand x = operation.GetSource(0);
  82. Operand y = operation.GetSource(1);
  83. if (IsConstEqual(y, comparand))
  84. {
  85. operation.TurnIntoCopy(x);
  86. }
  87. }
  88. private static void TryEliminateBinaryOpComutative(Operation operation, ulong comparand)
  89. {
  90. Operand x = operation.GetSource(0);
  91. Operand y = operation.GetSource(1);
  92. if (IsConstEqual(x, comparand))
  93. {
  94. operation.TurnIntoCopy(y);
  95. }
  96. else if (IsConstEqual(y, comparand))
  97. {
  98. operation.TurnIntoCopy(x);
  99. }
  100. }
  101. private static void TryEliminateConditionalSelect(Operation operation)
  102. {
  103. Operand cond = operation.GetSource(0);
  104. if (cond.Kind != OperandKind.Constant)
  105. {
  106. return;
  107. }
  108. // The condition is constant, we can turn it into a copy, and select
  109. // the source based on the condition value.
  110. int srcIndex = cond.Value != 0 ? 1 : 2;
  111. Operand source = operation.GetSource(srcIndex);
  112. operation.TurnIntoCopy(source);
  113. }
  114. private static bool IsConstEqual(Operand operand, ulong comparand)
  115. {
  116. if (operand.Kind != OperandKind.Constant || !operand.Type.IsInteger())
  117. {
  118. return false;
  119. }
  120. return operand.Value == comparand;
  121. }
  122. private static ulong AllOnes(OperandType type)
  123. {
  124. switch (type)
  125. {
  126. case OperandType.I32: return ~0U;
  127. case OperandType.I64: return ~0UL;
  128. }
  129. throw new ArgumentException("Invalid operand type \"" + type + "\".");
  130. }
  131. }
  132. }