Simplification.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  3. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  4. {
  5. static class Simplification
  6. {
  7. private const int AllOnes = ~0;
  8. public static void RunPass(Operation operation)
  9. {
  10. switch (operation.Inst)
  11. {
  12. case Instruction.Add:
  13. case Instruction.BitwiseExclusiveOr:
  14. TryEliminateBinaryOpCommutative(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. TryEliminateBinaryOpCommutative(operation, 1);
  30. break;
  31. case Instruction.ShiftLeft:
  32. case Instruction.ShiftRightS32:
  33. case Instruction.ShiftRightU32:
  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))
  47. {
  48. operation.TurnIntoCopy(y);
  49. }
  50. else if (IsConstEqual(y, AllOnes))
  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) || IsConstEqual(y, AllOnes))
  75. {
  76. operation.TurnIntoCopy(Const(AllOnes));
  77. }
  78. }
  79. private static void TryEliminateBinaryOpY(Operation operation, int 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 TryEliminateBinaryOpCommutative(Operation operation, int 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.Type != OperandType.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, int comparand)
  115. {
  116. if (operand.Type != OperandType.Constant)
  117. {
  118. return false;
  119. }
  120. return operand.Value == comparand;
  121. }
  122. }
  123. }