ConstantFolding.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using ARMeilleure.IntermediateRepresentation;
  2. using System;
  3. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  4. namespace ARMeilleure.CodeGen.Optimizations
  5. {
  6. static class ConstantFolding
  7. {
  8. public static void RunPass(Operation operation)
  9. {
  10. if (operation.Destination == null || operation.SourcesCount == 0)
  11. {
  12. return;
  13. }
  14. if (!AreAllSourcesConstant(operation))
  15. {
  16. return;
  17. }
  18. OperandType type = operation.Destination.Type;
  19. switch (operation.Instruction)
  20. {
  21. case Instruction.Add:
  22. if (type == OperandType.I32)
  23. {
  24. EvaluateBinaryI32(operation, (x, y) => x + y);
  25. }
  26. else if (type == OperandType.I64)
  27. {
  28. EvaluateBinaryI64(operation, (x, y) => x + y);
  29. }
  30. break;
  31. case Instruction.BitwiseAnd:
  32. if (type == OperandType.I32)
  33. {
  34. EvaluateBinaryI32(operation, (x, y) => x & y);
  35. }
  36. else if (type == OperandType.I64)
  37. {
  38. EvaluateBinaryI64(operation, (x, y) => x & y);
  39. }
  40. break;
  41. case Instruction.BitwiseExclusiveOr:
  42. if (type == OperandType.I32)
  43. {
  44. EvaluateBinaryI32(operation, (x, y) => x ^ y);
  45. }
  46. else if (type == OperandType.I64)
  47. {
  48. EvaluateBinaryI64(operation, (x, y) => x ^ y);
  49. }
  50. break;
  51. case Instruction.BitwiseNot:
  52. if (type == OperandType.I32)
  53. {
  54. EvaluateUnaryI32(operation, (x) => ~x);
  55. }
  56. else if (type == OperandType.I64)
  57. {
  58. EvaluateUnaryI64(operation, (x) => ~x);
  59. }
  60. break;
  61. case Instruction.BitwiseOr:
  62. if (type == OperandType.I32)
  63. {
  64. EvaluateBinaryI32(operation, (x, y) => x | y);
  65. }
  66. else if (type == OperandType.I64)
  67. {
  68. EvaluateBinaryI64(operation, (x, y) => x | y);
  69. }
  70. break;
  71. case Instruction.Copy:
  72. if (type == OperandType.I32)
  73. {
  74. EvaluateUnaryI32(operation, (x) => x);
  75. }
  76. else if (type == OperandType.I64)
  77. {
  78. EvaluateUnaryI64(operation, (x) => x);
  79. }
  80. break;
  81. case Instruction.Divide:
  82. if (type == OperandType.I32)
  83. {
  84. EvaluateBinaryI32(operation, (x, y) => y != 0 ? x / y : 0);
  85. }
  86. else if (type == OperandType.I64)
  87. {
  88. EvaluateBinaryI64(operation, (x, y) => y != 0 ? x / y : 0);
  89. }
  90. break;
  91. case Instruction.DivideUI:
  92. if (type == OperandType.I32)
  93. {
  94. EvaluateBinaryI32(operation, (x, y) => y != 0 ? (int)((uint)x / (uint)y) : 0);
  95. }
  96. else if (type == OperandType.I64)
  97. {
  98. EvaluateBinaryI64(operation, (x, y) => y != 0 ? (long)((ulong)x / (ulong)y) : 0);
  99. }
  100. break;
  101. case Instruction.Multiply:
  102. if (type == OperandType.I32)
  103. {
  104. EvaluateBinaryI32(operation, (x, y) => x * y);
  105. }
  106. else if (type == OperandType.I64)
  107. {
  108. EvaluateBinaryI64(operation, (x, y) => x * y);
  109. }
  110. break;
  111. case Instruction.Negate:
  112. if (type == OperandType.I32)
  113. {
  114. EvaluateUnaryI32(operation, (x) => -x);
  115. }
  116. else if (type == OperandType.I64)
  117. {
  118. EvaluateUnaryI64(operation, (x) => -x);
  119. }
  120. break;
  121. case Instruction.ShiftLeft:
  122. if (type == OperandType.I32)
  123. {
  124. EvaluateBinaryI32(operation, (x, y) => x << y);
  125. }
  126. else if (type == OperandType.I64)
  127. {
  128. EvaluateBinaryI64(operation, (x, y) => x << (int)y);
  129. }
  130. break;
  131. case Instruction.ShiftRightSI:
  132. if (type == OperandType.I32)
  133. {
  134. EvaluateBinaryI32(operation, (x, y) => x >> y);
  135. }
  136. else if (type == OperandType.I64)
  137. {
  138. EvaluateBinaryI64(operation, (x, y) => x >> (int)y);
  139. }
  140. break;
  141. case Instruction.ShiftRightUI:
  142. if (type == OperandType.I32)
  143. {
  144. EvaluateBinaryI32(operation, (x, y) => (int)((uint)x >> y));
  145. }
  146. else if (type == OperandType.I64)
  147. {
  148. EvaluateBinaryI64(operation, (x, y) => (long)((ulong)x >> (int)y));
  149. }
  150. break;
  151. case Instruction.SignExtend16:
  152. if (type == OperandType.I32)
  153. {
  154. EvaluateUnaryI32(operation, (x) => (short)x);
  155. }
  156. else if (type == OperandType.I64)
  157. {
  158. EvaluateUnaryI64(operation, (x) => (short)x);
  159. }
  160. break;
  161. case Instruction.SignExtend32:
  162. if (type == OperandType.I32)
  163. {
  164. EvaluateUnaryI32(operation, (x) => x);
  165. }
  166. else if (type == OperandType.I64)
  167. {
  168. EvaluateUnaryI64(operation, (x) => (int)x);
  169. }
  170. break;
  171. case Instruction.SignExtend8:
  172. if (type == OperandType.I32)
  173. {
  174. EvaluateUnaryI32(operation, (x) => (sbyte)x);
  175. }
  176. else if (type == OperandType.I64)
  177. {
  178. EvaluateUnaryI64(operation, (x) => (sbyte)x);
  179. }
  180. break;
  181. case Instruction.Subtract:
  182. if (type == OperandType.I32)
  183. {
  184. EvaluateBinaryI32(operation, (x, y) => x - y);
  185. }
  186. else if (type == OperandType.I64)
  187. {
  188. EvaluateBinaryI64(operation, (x, y) => x - y);
  189. }
  190. break;
  191. }
  192. }
  193. private static bool AreAllSourcesConstant(Operation operation)
  194. {
  195. for (int index = 0; index < operation.SourcesCount; index++)
  196. {
  197. if (operation.GetSource(index).Kind != OperandKind.Constant)
  198. {
  199. return false;
  200. }
  201. }
  202. return true;
  203. }
  204. private static void EvaluateUnaryI32(Operation operation, Func<int, int> op)
  205. {
  206. int x = operation.GetSource(0).AsInt32();
  207. operation.TurnIntoCopy(Const(op(x)));
  208. }
  209. private static void EvaluateUnaryI64(Operation operation, Func<long, long> op)
  210. {
  211. long x = operation.GetSource(0).AsInt64();
  212. operation.TurnIntoCopy(Const(op(x)));
  213. }
  214. private static void EvaluateBinaryI32(Operation operation, Func<int, int, int> op)
  215. {
  216. int x = operation.GetSource(0).AsInt32();
  217. int y = operation.GetSource(1).AsInt32();
  218. operation.TurnIntoCopy(Const(op(x, y)));
  219. }
  220. private static void EvaluateBinaryI64(Operation operation, Func<long, long, long> op)
  221. {
  222. long x = operation.GetSource(0).AsInt64();
  223. long y = operation.GetSource(1).AsInt64();
  224. operation.TurnIntoCopy(Const(op(x, y)));
  225. }
  226. }
  227. }