ConstantFolding.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. EvaluateUnaryI64(operation, (x) => (int)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.Subtract:
  188. if (type == OperandType.I32)
  189. {
  190. EvaluateBinaryI32(operation, (x, y) => x - y);
  191. }
  192. else if (type == OperandType.I64)
  193. {
  194. EvaluateBinaryI64(operation, (x, y) => x - y);
  195. }
  196. break;
  197. }
  198. }
  199. private static bool AreAllSourcesConstantAndCFEnabled(Operation operation)
  200. {
  201. for (int index = 0; index < operation.SourcesCount; index++)
  202. {
  203. Operand srcOp = operation.GetSource(index);
  204. if (srcOp.Kind != OperandKind.Constant || srcOp.Relocatable)
  205. {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. private static void EvaluateUnaryI32(Operation operation, Func<int, int> op)
  212. {
  213. int x = operation.GetSource(0).AsInt32();
  214. operation.TurnIntoCopy(Const(op(x)));
  215. }
  216. private static void EvaluateUnaryI64(Operation operation, Func<long, long> op)
  217. {
  218. long x = operation.GetSource(0).AsInt64();
  219. operation.TurnIntoCopy(Const(op(x)));
  220. }
  221. private static void EvaluateBinaryI32(Operation operation, Func<int, int, int> op)
  222. {
  223. int x = operation.GetSource(0).AsInt32();
  224. int y = operation.GetSource(1).AsInt32();
  225. operation.TurnIntoCopy(Const(op(x, y)));
  226. }
  227. private static void EvaluateBinaryI64(Operation operation, Func<long, long, long> op)
  228. {
  229. long x = operation.GetSource(0).AsInt64();
  230. long y = operation.GetSource(1).AsInt64();
  231. operation.TurnIntoCopy(Const(op(x, y)));
  232. }
  233. }
  234. }