ConstantFolding.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 (!AreAllSourcesConstantAndCFEnabled(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.ConvertI64ToI32:
  72. if (type == OperandType.I32)
  73. {
  74. EvaluateUnaryI32(operation, (x) => x);
  75. }
  76. break;
  77. case Instruction.Copy:
  78. if (type == OperandType.I32)
  79. {
  80. EvaluateUnaryI32(operation, (x) => x);
  81. }
  82. else if (type == OperandType.I64)
  83. {
  84. EvaluateUnaryI64(operation, (x) => x);
  85. }
  86. break;
  87. case Instruction.Divide:
  88. if (type == OperandType.I32)
  89. {
  90. EvaluateBinaryI32(operation, (x, y) => y != 0 ? x / y : 0);
  91. }
  92. else if (type == OperandType.I64)
  93. {
  94. EvaluateBinaryI64(operation, (x, y) => y != 0 ? x / y : 0);
  95. }
  96. break;
  97. case Instruction.DivideUI:
  98. if (type == OperandType.I32)
  99. {
  100. EvaluateBinaryI32(operation, (x, y) => y != 0 ? (int)((uint)x / (uint)y) : 0);
  101. }
  102. else if (type == OperandType.I64)
  103. {
  104. EvaluateBinaryI64(operation, (x, y) => y != 0 ? (long)((ulong)x / (ulong)y) : 0);
  105. }
  106. break;
  107. case Instruction.Multiply:
  108. if (type == OperandType.I32)
  109. {
  110. EvaluateBinaryI32(operation, (x, y) => x * y);
  111. }
  112. else if (type == OperandType.I64)
  113. {
  114. EvaluateBinaryI64(operation, (x, y) => x * y);
  115. }
  116. break;
  117. case Instruction.Negate:
  118. if (type == OperandType.I32)
  119. {
  120. EvaluateUnaryI32(operation, (x) => -x);
  121. }
  122. else if (type == OperandType.I64)
  123. {
  124. EvaluateUnaryI64(operation, (x) => -x);
  125. }
  126. break;
  127. case Instruction.ShiftLeft:
  128. if (type == OperandType.I32)
  129. {
  130. EvaluateBinaryI32(operation, (x, y) => x << y);
  131. }
  132. else if (type == OperandType.I64)
  133. {
  134. EvaluateBinaryI64(operation, (x, y) => x << (int)y);
  135. }
  136. break;
  137. case Instruction.ShiftRightSI:
  138. if (type == OperandType.I32)
  139. {
  140. EvaluateBinaryI32(operation, (x, y) => x >> y);
  141. }
  142. else if (type == OperandType.I64)
  143. {
  144. EvaluateBinaryI64(operation, (x, y) => x >> (int)y);
  145. }
  146. break;
  147. case Instruction.ShiftRightUI:
  148. if (type == OperandType.I32)
  149. {
  150. EvaluateBinaryI32(operation, (x, y) => (int)((uint)x >> y));
  151. }
  152. else if (type == OperandType.I64)
  153. {
  154. EvaluateBinaryI64(operation, (x, y) => (long)((ulong)x >> (int)y));
  155. }
  156. break;
  157. case Instruction.SignExtend16:
  158. if (type == OperandType.I32)
  159. {
  160. EvaluateUnaryI32(operation, (x) => (short)x);
  161. }
  162. else if (type == OperandType.I64)
  163. {
  164. EvaluateUnaryI64(operation, (x) => (short)x);
  165. }
  166. break;
  167. case Instruction.SignExtend32:
  168. if (type == OperandType.I32)
  169. {
  170. EvaluateUnaryI32(operation, (x) => x);
  171. }
  172. else if (type == OperandType.I64)
  173. {
  174. EvaluateUnaryI64(operation, (x) => (int)x);
  175. }
  176. break;
  177. case Instruction.SignExtend8:
  178. if (type == OperandType.I32)
  179. {
  180. EvaluateUnaryI32(operation, (x) => (sbyte)x);
  181. }
  182. else if (type == OperandType.I64)
  183. {
  184. EvaluateUnaryI64(operation, (x) => (sbyte)x);
  185. }
  186. break;
  187. case Instruction.ZeroExtend16:
  188. if (type == OperandType.I32)
  189. {
  190. EvaluateUnaryI32(operation, (x) => (ushort)x);
  191. }
  192. else if (type == OperandType.I64)
  193. {
  194. EvaluateUnaryI64(operation, (x) => (ushort)x);
  195. }
  196. break;
  197. case Instruction.ZeroExtend32:
  198. if (type == OperandType.I32)
  199. {
  200. EvaluateUnaryI32(operation, (x) => x);
  201. }
  202. else if (type == OperandType.I64)
  203. {
  204. EvaluateUnaryI64(operation, (x) => (uint)x);
  205. }
  206. break;
  207. case Instruction.ZeroExtend8:
  208. if (type == OperandType.I32)
  209. {
  210. EvaluateUnaryI32(operation, (x) => (byte)x);
  211. }
  212. else if (type == OperandType.I64)
  213. {
  214. EvaluateUnaryI64(operation, (x) => (byte)x);
  215. }
  216. break;
  217. case Instruction.Subtract:
  218. if (type == OperandType.I32)
  219. {
  220. EvaluateBinaryI32(operation, (x, y) => x - y);
  221. }
  222. else if (type == OperandType.I64)
  223. {
  224. EvaluateBinaryI64(operation, (x, y) => x - y);
  225. }
  226. break;
  227. }
  228. }
  229. private static bool AreAllSourcesConstantAndCFEnabled(Operation operation)
  230. {
  231. for (int index = 0; index < operation.SourcesCount; index++)
  232. {
  233. Operand srcOp = operation.GetSource(index);
  234. if (srcOp.Kind != OperandKind.Constant || srcOp.Relocatable)
  235. {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. private static void EvaluateUnaryI32(Operation operation, Func<int, int> op)
  242. {
  243. int x = operation.GetSource(0).AsInt32();
  244. operation.TurnIntoCopy(Const(op(x)));
  245. }
  246. private static void EvaluateUnaryI64(Operation operation, Func<long, long> op)
  247. {
  248. long x = operation.GetSource(0).AsInt64();
  249. operation.TurnIntoCopy(Const(op(x)));
  250. }
  251. private static void EvaluateBinaryI32(Operation operation, Func<int, int, int> op)
  252. {
  253. int x = operation.GetSource(0).AsInt32();
  254. int y = operation.GetSource(1).AsInt32();
  255. operation.TurnIntoCopy(Const(op(x, y)));
  256. }
  257. private static void EvaluateBinaryI64(Operation operation, Func<long, long, long> op)
  258. {
  259. long x = operation.GetSource(0).AsInt64();
  260. long y = operation.GetSource(1).AsInt64();
  261. operation.TurnIntoCopy(Const(op(x, y)));
  262. }
  263. }
  264. }