ConstantFolding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System;
  4. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  5. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  6. {
  7. static class ConstantFolding
  8. {
  9. public static void Fold(Operation operation)
  10. {
  11. if (!AreAllSourcesConstant(operation))
  12. {
  13. return;
  14. }
  15. switch (operation.Inst)
  16. {
  17. case Instruction.Add:
  18. EvaluateBinary(operation, (x, y) => x + y);
  19. break;
  20. case Instruction.BitwiseAnd:
  21. EvaluateBinary(operation, (x, y) => x & y);
  22. break;
  23. case Instruction.BitwiseExclusiveOr:
  24. EvaluateBinary(operation, (x, y) => x ^ y);
  25. break;
  26. case Instruction.BitwiseNot:
  27. EvaluateUnary(operation, (x) => ~x);
  28. break;
  29. case Instruction.BitwiseOr:
  30. EvaluateBinary(operation, (x, y) => x | y);
  31. break;
  32. case Instruction.BitfieldExtractS32:
  33. BitfieldExtractS32(operation);
  34. break;
  35. case Instruction.BitfieldExtractU32:
  36. BitfieldExtractU32(operation);
  37. break;
  38. case Instruction.Clamp:
  39. EvaluateTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
  40. break;
  41. case Instruction.ClampU32:
  42. EvaluateTernary(operation, (x, y, z) => (int)Math.Clamp((uint)x, (uint)y, (uint)z));
  43. break;
  44. case Instruction.CompareEqual:
  45. EvaluateBinary(operation, (x, y) => x == y);
  46. break;
  47. case Instruction.CompareGreater:
  48. EvaluateBinary(operation, (x, y) => x > y);
  49. break;
  50. case Instruction.CompareGreaterOrEqual:
  51. EvaluateBinary(operation, (x, y) => x >= y);
  52. break;
  53. case Instruction.CompareGreaterOrEqualU32:
  54. EvaluateBinary(operation, (x, y) => (uint)x >= (uint)y);
  55. break;
  56. case Instruction.CompareGreaterU32:
  57. EvaluateBinary(operation, (x, y) => (uint)x > (uint)y);
  58. break;
  59. case Instruction.CompareLess:
  60. EvaluateBinary(operation, (x, y) => x < y);
  61. break;
  62. case Instruction.CompareLessOrEqual:
  63. EvaluateBinary(operation, (x, y) => x <= y);
  64. break;
  65. case Instruction.CompareLessOrEqualU32:
  66. EvaluateBinary(operation, (x, y) => (uint)x <= (uint)y);
  67. break;
  68. case Instruction.CompareLessU32:
  69. EvaluateBinary(operation, (x, y) => (uint)x < (uint)y);
  70. break;
  71. case Instruction.CompareNotEqual:
  72. EvaluateBinary(operation, (x, y) => x != y);
  73. break;
  74. case Instruction.Divide:
  75. EvaluateBinary(operation, (x, y) => y != 0 ? x / y : 0);
  76. break;
  77. case Instruction.FP | Instruction.Add:
  78. EvaluateFPBinary(operation, (x, y) => x + y);
  79. break;
  80. case Instruction.FP | Instruction.Clamp:
  81. EvaluateFPTernary(operation, (x, y, z) => Math.Clamp(x, y, z));
  82. break;
  83. case Instruction.FP | Instruction.CompareEqual:
  84. EvaluateFPBinary(operation, (x, y) => x == y);
  85. break;
  86. case Instruction.FP | Instruction.CompareGreater:
  87. EvaluateFPBinary(operation, (x, y) => x > y);
  88. break;
  89. case Instruction.FP | Instruction.CompareGreaterOrEqual:
  90. EvaluateFPBinary(operation, (x, y) => x >= y);
  91. break;
  92. case Instruction.FP | Instruction.CompareLess:
  93. EvaluateFPBinary(operation, (x, y) => x < y);
  94. break;
  95. case Instruction.FP | Instruction.CompareLessOrEqual:
  96. EvaluateFPBinary(operation, (x, y) => x <= y);
  97. break;
  98. case Instruction.FP | Instruction.CompareNotEqual:
  99. EvaluateFPBinary(operation, (x, y) => x != y);
  100. break;
  101. case Instruction.FP | Instruction.Divide:
  102. EvaluateFPBinary(operation, (x, y) => x / y);
  103. break;
  104. case Instruction.FP | Instruction.Multiply:
  105. EvaluateFPBinary(operation, (x, y) => x * y);
  106. break;
  107. case Instruction.FP | Instruction.Negate:
  108. EvaluateFPUnary(operation, (x) => -x);
  109. break;
  110. case Instruction.FP | Instruction.Subtract:
  111. EvaluateFPBinary(operation, (x, y) => x - y);
  112. break;
  113. case Instruction.IsNan:
  114. EvaluateFPUnary(operation, (x) => float.IsNaN(x));
  115. break;
  116. case Instruction.Maximum:
  117. EvaluateBinary(operation, (x, y) => Math.Max(x, y));
  118. break;
  119. case Instruction.MaximumU32:
  120. EvaluateBinary(operation, (x, y) => (int)Math.Max((uint)x, (uint)y));
  121. break;
  122. case Instruction.Minimum:
  123. EvaluateBinary(operation, (x, y) => Math.Min(x, y));
  124. break;
  125. case Instruction.MinimumU32:
  126. EvaluateBinary(operation, (x, y) => (int)Math.Min((uint)x, (uint)y));
  127. break;
  128. case Instruction.Multiply:
  129. EvaluateBinary(operation, (x, y) => x * y);
  130. break;
  131. case Instruction.Negate:
  132. EvaluateUnary(operation, (x) => -x);
  133. break;
  134. case Instruction.ShiftLeft:
  135. EvaluateBinary(operation, (x, y) => x << y);
  136. break;
  137. case Instruction.ShiftRightS32:
  138. EvaluateBinary(operation, (x, y) => x >> y);
  139. break;
  140. case Instruction.ShiftRightU32:
  141. EvaluateBinary(operation, (x, y) => (int)((uint)x >> y));
  142. break;
  143. case Instruction.Subtract:
  144. EvaluateBinary(operation, (x, y) => x - y);
  145. break;
  146. case Instruction.UnpackHalf2x16:
  147. UnpackHalf2x16(operation);
  148. break;
  149. }
  150. }
  151. private static bool AreAllSourcesConstant(Operation operation)
  152. {
  153. for (int index = 0; index < operation.SourcesCount; index++)
  154. {
  155. if (operation.GetSource(index).Type != OperandType.Constant)
  156. {
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. private static void BitfieldExtractS32(Operation operation)
  163. {
  164. int value = GetBitfieldExtractValue(operation);
  165. int shift = 32 - operation.GetSource(2).Value;
  166. value = (value << shift) >> shift;
  167. operation.TurnIntoCopy(Const(value));
  168. }
  169. private static void BitfieldExtractU32(Operation operation)
  170. {
  171. operation.TurnIntoCopy(Const(GetBitfieldExtractValue(operation)));
  172. }
  173. private static int GetBitfieldExtractValue(Operation operation)
  174. {
  175. int value = operation.GetSource(0).Value;
  176. int lsb = operation.GetSource(1).Value;
  177. int length = operation.GetSource(2).Value;
  178. return value.Extract(lsb, length);
  179. }
  180. private static void UnpackHalf2x16(Operation operation)
  181. {
  182. int value = operation.GetSource(0).Value;
  183. value = (value >> operation.ComponentIndex * 16) & 0xffff;
  184. operation.TurnIntoCopy(ConstF(HalfConversion.HalfToSingle(value)));
  185. }
  186. private static void FPNegate(Operation operation)
  187. {
  188. float value = operation.GetSource(0).AsFloat();
  189. operation.TurnIntoCopy(ConstF(-value));
  190. }
  191. private static void EvaluateUnary(Operation operation, Func<int, int> op)
  192. {
  193. int x = operation.GetSource(0).Value;
  194. operation.TurnIntoCopy(Const(op(x)));
  195. }
  196. private static void EvaluateFPUnary(Operation operation, Func<float, float> op)
  197. {
  198. float x = operation.GetSource(0).AsFloat();
  199. operation.TurnIntoCopy(ConstF(op(x)));
  200. }
  201. private static void EvaluateFPUnary(Operation operation, Func<float, bool> op)
  202. {
  203. float x = operation.GetSource(0).AsFloat();
  204. operation.TurnIntoCopy(Const(op(x) ? IrConsts.True : IrConsts.False));
  205. }
  206. private static void EvaluateBinary(Operation operation, Func<int, int, int> op)
  207. {
  208. int x = operation.GetSource(0).Value;
  209. int y = operation.GetSource(1).Value;
  210. operation.TurnIntoCopy(Const(op(x, y)));
  211. }
  212. private static void EvaluateBinary(Operation operation, Func<int, int, bool> op)
  213. {
  214. int x = operation.GetSource(0).Value;
  215. int y = operation.GetSource(1).Value;
  216. operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
  217. }
  218. private static void EvaluateFPBinary(Operation operation, Func<float, float, float> op)
  219. {
  220. float x = operation.GetSource(0).AsFloat();
  221. float y = operation.GetSource(1).AsFloat();
  222. operation.TurnIntoCopy(ConstF(op(x, y)));
  223. }
  224. private static void EvaluateFPBinary(Operation operation, Func<float, float, bool> op)
  225. {
  226. float x = operation.GetSource(0).AsFloat();
  227. float y = operation.GetSource(1).AsFloat();
  228. operation.TurnIntoCopy(Const(op(x, y) ? IrConsts.True : IrConsts.False));
  229. }
  230. private static void EvaluateTernary(Operation operation, Func<int, int, int, int> op)
  231. {
  232. int x = operation.GetSource(0).Value;
  233. int y = operation.GetSource(1).Value;
  234. int z = operation.GetSource(2).Value;
  235. operation.TurnIntoCopy(Const(op(x, y, z)));
  236. }
  237. private static void EvaluateFPTernary(Operation operation, Func<float, float, float, float> op)
  238. {
  239. float x = operation.GetSource(0).AsFloat();
  240. float y = operation.GetSource(1).AsFloat();
  241. float z = operation.GetSource(2).AsFloat();
  242. operation.TurnIntoCopy(ConstF(op(x, y, z)));
  243. }
  244. }
  245. }