ConstantFolding.cs 11 KB

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