PreAllocatorWindows.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using ARMeilleure.CodeGen.RegisterAllocators;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  7. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  8. namespace ARMeilleure.CodeGen.X86
  9. {
  10. class PreAllocatorWindows : PreAllocator
  11. {
  12. public static void InsertCallCopies(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  13. {
  14. Operand dest = node.Destination;
  15. // Handle struct arguments.
  16. int retArgs = 0;
  17. int stackAllocOffset = 0;
  18. int AllocateOnStack(int size)
  19. {
  20. // We assume that the stack allocator is initially empty (TotalSize = 0).
  21. // Taking that into account, we can reuse the space allocated for other
  22. // calls by keeping track of our own allocated size (stackAllocOffset).
  23. // If the space allocated is not big enough, then we just expand it.
  24. int offset = stackAllocOffset;
  25. if (stackAllocOffset + size > stackAlloc.TotalSize)
  26. {
  27. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  28. }
  29. stackAllocOffset += size;
  30. return offset;
  31. }
  32. Operand arg0Reg = default;
  33. if (dest != default && dest.Type == OperandType.V128)
  34. {
  35. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  36. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  37. Operation allocOp = Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  38. nodes.AddBefore(node, allocOp);
  39. retArgs = 1;
  40. }
  41. int argsCount = node.SourcesCount - 1;
  42. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  43. if (argsCount > maxArgs)
  44. {
  45. argsCount = maxArgs;
  46. }
  47. Operand[] sources = new Operand[1 + retArgs + argsCount];
  48. sources[0] = node.GetSource(0);
  49. if (arg0Reg != default)
  50. {
  51. sources[1] = arg0Reg;
  52. }
  53. for (int index = 1; index < node.SourcesCount; index++)
  54. {
  55. Operand source = node.GetSource(index);
  56. if (source.Type == OperandType.V128)
  57. {
  58. Operand stackAddr = Local(OperandType.I64);
  59. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  60. nodes.AddBefore(node, Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  61. Operation storeOp = Operation(Instruction.Store, default, stackAddr, source);
  62. InsertConstantRegCopies(nodes, nodes.AddBefore(node, storeOp));
  63. node.SetSource(index, stackAddr);
  64. }
  65. }
  66. // Handle arguments passed on registers.
  67. for (int index = 0; index < argsCount; index++)
  68. {
  69. Operand source = node.GetSource(index + 1);
  70. Operand argReg;
  71. int argIndex = index + retArgs;
  72. if (source.Type.IsInteger())
  73. {
  74. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  75. }
  76. else
  77. {
  78. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  79. }
  80. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  81. InsertConstantRegCopies(nodes, nodes.AddBefore(node, copyOp));
  82. sources[1 + retArgs + index] = argReg;
  83. }
  84. // The remaining arguments (those that are not passed on registers)
  85. // should be passed on the stack, we write them to the stack with "SpillArg".
  86. for (int index = argsCount; index < node.SourcesCount - 1; index++)
  87. {
  88. Operand source = node.GetSource(index + 1);
  89. Operand offset = Const((index + retArgs) * 8);
  90. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  91. InsertConstantRegCopies(nodes, nodes.AddBefore(node, spillOp));
  92. }
  93. if (dest != default)
  94. {
  95. if (dest.Type == OperandType.V128)
  96. {
  97. Operand retValueAddr = Local(OperandType.I64);
  98. nodes.AddBefore(node, Operation(Instruction.Copy, retValueAddr, arg0Reg));
  99. Operation loadOp = Operation(Instruction.Load, dest, retValueAddr);
  100. nodes.AddAfter(node, loadOp);
  101. node.Destination = default;
  102. }
  103. else
  104. {
  105. Operand retReg = dest.Type.IsInteger()
  106. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  107. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  108. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  109. nodes.AddAfter(node, copyOp);
  110. node.Destination = retReg;
  111. }
  112. }
  113. node.SetSources(sources);
  114. }
  115. public static void InsertTailcallCopies(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  116. {
  117. int argsCount = node.SourcesCount - 1;
  118. int maxArgs = CallingConvention.GetArgumentsOnRegsCount();
  119. if (argsCount > maxArgs)
  120. {
  121. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  122. }
  123. Operand[] sources = new Operand[1 + argsCount];
  124. // Handle arguments passed on registers.
  125. for (int index = 0; index < argsCount; index++)
  126. {
  127. Operand source = node.GetSource(1 + index);
  128. Operand argReg = source.Type.IsInteger()
  129. ? Gpr(CallingConvention.GetIntArgumentRegister(index), source.Type)
  130. : Xmm(CallingConvention.GetVecArgumentRegister(index), source.Type);
  131. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  132. InsertConstantRegCopies(nodes, nodes.AddBefore(node, copyOp));
  133. sources[1 + index] = argReg;
  134. }
  135. // The target address must be on the return registers, since we
  136. // don't return anything and it is guaranteed to not be a
  137. // callee saved register (which would be trashed on the epilogue).
  138. Operand retReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  139. Operation addrCopyOp = Operation(Instruction.Copy, retReg, node.GetSource(0));
  140. nodes.AddBefore(node, addrCopyOp);
  141. sources[0] = retReg;
  142. node.SetSources(sources);
  143. }
  144. public static Operation InsertLoadArgumentCopy(
  145. CompilerContext cctx,
  146. ref Span<Operation> buffer,
  147. IntrusiveList<Operation> nodes,
  148. Operand[] preservedArgs,
  149. Operation node)
  150. {
  151. Operand source = node.GetSource(0);
  152. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  153. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  154. int index = source.AsInt32() + retArgs;
  155. if (index < CallingConvention.GetArgumentsOnRegsCount())
  156. {
  157. Operand dest = node.Destination;
  158. if (preservedArgs[index] == default)
  159. {
  160. Operand argReg, pArg;
  161. if (dest.Type.IsInteger())
  162. {
  163. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  164. pArg = Local(dest.Type);
  165. }
  166. else if (dest.Type == OperandType.V128)
  167. {
  168. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  169. pArg = Local(OperandType.I64);
  170. }
  171. else
  172. {
  173. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  174. pArg = Local(dest.Type);
  175. }
  176. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  177. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  178. preservedArgs[index] = pArg;
  179. }
  180. Operation nextNode;
  181. if (dest.Type != OperandType.V128 && dest.AssignmentsCount == 1)
  182. {
  183. // Let's propagate the argument if we can to avoid copies.
  184. PreAllocatorCommon.Propagate(ref buffer, dest, preservedArgs[index]);
  185. nextNode = node.ListNext;
  186. }
  187. else
  188. {
  189. Operation argCopyOp = Operation(dest.Type == OperandType.V128
  190. ? Instruction.Load
  191. : Instruction.Copy, dest, preservedArgs[index]);
  192. nextNode = nodes.AddBefore(node, argCopyOp);
  193. }
  194. Delete(nodes, node);
  195. return nextNode;
  196. }
  197. else
  198. {
  199. // TODO: Pass on stack.
  200. return node;
  201. }
  202. }
  203. public static void InsertReturnCopy(
  204. CompilerContext cctx,
  205. IntrusiveList<Operation> nodes,
  206. Operand[] preservedArgs,
  207. Operation node)
  208. {
  209. if (node.SourcesCount == 0)
  210. {
  211. return;
  212. }
  213. Operand source = node.GetSource(0);
  214. Operand retReg;
  215. if (source.Type.IsInteger())
  216. {
  217. retReg = Gpr(CallingConvention.GetIntReturnRegister(), source.Type);
  218. }
  219. else if (source.Type == OperandType.V128)
  220. {
  221. if (preservedArgs[0] == default)
  222. {
  223. Operand preservedArg = Local(OperandType.I64);
  224. Operand arg0 = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  225. Operation copyOp = Operation(Instruction.Copy, preservedArg, arg0);
  226. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  227. preservedArgs[0] = preservedArg;
  228. }
  229. retReg = preservedArgs[0];
  230. }
  231. else
  232. {
  233. retReg = Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  234. }
  235. if (source.Type == OperandType.V128)
  236. {
  237. Operation retStoreOp = Operation(Instruction.Store, default, retReg, source);
  238. nodes.AddBefore(node, retStoreOp);
  239. }
  240. else
  241. {
  242. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  243. nodes.AddBefore(node, retCopyOp);
  244. }
  245. node.SetSources(Array.Empty<Operand>());
  246. }
  247. }
  248. }