HybridAllocator.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Translation;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Numerics;
  6. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  7. using static ARMeilleure.IntermediateRepresentation.OperationHelper;
  8. namespace ARMeilleure.CodeGen.RegisterAllocators
  9. {
  10. class HybridAllocator : IRegisterAllocator
  11. {
  12. private const int RegistersCount = 16;
  13. private const int MaxIROperands = 4;
  14. private struct BlockInfo
  15. {
  16. public bool HasCall { get; }
  17. public int IntFixedRegisters { get; }
  18. public int VecFixedRegisters { get; }
  19. public BlockInfo(bool hasCall, int intFixedRegisters, int vecFixedRegisters)
  20. {
  21. HasCall = hasCall;
  22. IntFixedRegisters = intFixedRegisters;
  23. VecFixedRegisters = vecFixedRegisters;
  24. }
  25. }
  26. private class LocalInfo
  27. {
  28. public int Uses { get; set; }
  29. public int UseCount { get; set; }
  30. public bool PreAllocated { get; set; }
  31. public int Register { get; set; }
  32. public int SpillOffset { get; set; }
  33. public int Sequence { get; set; }
  34. public Operand Temp { get; set; }
  35. public OperandType Type { get; }
  36. private int _first;
  37. private int _last;
  38. public bool IsBlockLocal => _first == _last;
  39. public LocalInfo(OperandType type, int uses)
  40. {
  41. Uses = uses;
  42. Type = type;
  43. _first = -1;
  44. _last = -1;
  45. }
  46. public void SetBlockIndex(int blkIndex)
  47. {
  48. if (_first == -1 || blkIndex < _first)
  49. {
  50. _first = blkIndex;
  51. }
  52. if (_last == -1 || blkIndex > _last)
  53. {
  54. _last = blkIndex;
  55. }
  56. }
  57. }
  58. public AllocationResult RunPass(
  59. ControlFlowGraph cfg,
  60. StackAllocator stackAlloc,
  61. RegisterMasks regMasks)
  62. {
  63. int intUsedRegisters = 0;
  64. int vecUsedRegisters = 0;
  65. int intFreeRegisters = regMasks.IntAvailableRegisters;
  66. int vecFreeRegisters = regMasks.VecAvailableRegisters;
  67. BlockInfo[] blockInfo = new BlockInfo[cfg.Blocks.Count];
  68. List<LocalInfo> locInfo = new List<LocalInfo>();
  69. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  70. {
  71. BasicBlock block = cfg.PostOrderBlocks[index];
  72. int intFixedRegisters = 0;
  73. int vecFixedRegisters = 0;
  74. bool hasCall = false;
  75. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  76. {
  77. if (node is Operation operation && operation.Instruction == Instruction.Call)
  78. {
  79. hasCall = true;
  80. }
  81. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  82. {
  83. Operand source = node.GetSource(srcIndex);
  84. if (source.Kind == OperandKind.LocalVariable)
  85. {
  86. locInfo[source.AsInt32() - 1].SetBlockIndex(block.Index);
  87. }
  88. else if (source.Kind == OperandKind.Memory)
  89. {
  90. MemoryOperand memOp = (MemoryOperand)source;
  91. if (memOp.BaseAddress != null)
  92. {
  93. locInfo[memOp.BaseAddress.AsInt32() - 1].SetBlockIndex(block.Index);
  94. }
  95. if (memOp.Index != null)
  96. {
  97. locInfo[memOp.Index.AsInt32() - 1].SetBlockIndex(block.Index);
  98. }
  99. }
  100. }
  101. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  102. {
  103. Operand dest = node.GetDestination(dstIndex);
  104. if (dest.Kind == OperandKind.LocalVariable)
  105. {
  106. LocalInfo info;
  107. if (dest.Value != 0)
  108. {
  109. info = locInfo[dest.AsInt32() - 1];
  110. }
  111. else
  112. {
  113. dest.NumberLocal(locInfo.Count + 1);
  114. info = new LocalInfo(dest.Type, UsesCount(dest));
  115. locInfo.Add(info);
  116. }
  117. info.SetBlockIndex(block.Index);
  118. }
  119. else if (dest.Kind == OperandKind.Register)
  120. {
  121. if (dest.Type.IsInteger())
  122. {
  123. intFixedRegisters |= 1 << dest.GetRegister().Index;
  124. }
  125. else
  126. {
  127. vecFixedRegisters |= 1 << dest.GetRegister().Index;
  128. }
  129. }
  130. }
  131. }
  132. blockInfo[block.Index] = new BlockInfo(hasCall, intFixedRegisters, vecFixedRegisters);
  133. }
  134. int sequence = 0;
  135. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  136. {
  137. BasicBlock block = cfg.PostOrderBlocks[index];
  138. BlockInfo blkInfo = blockInfo[block.Index];
  139. int intLocalFreeRegisters = intFreeRegisters & ~blkInfo.IntFixedRegisters;
  140. int vecLocalFreeRegisters = vecFreeRegisters & ~blkInfo.VecFixedRegisters;
  141. int intCallerSavedRegisters = blkInfo.HasCall ? regMasks.IntCallerSavedRegisters : 0;
  142. int vecCallerSavedRegisters = blkInfo.HasCall ? regMasks.VecCallerSavedRegisters : 0;
  143. int intSpillTempRegisters = SelectSpillTemps(
  144. intCallerSavedRegisters & ~blkInfo.IntFixedRegisters,
  145. intLocalFreeRegisters);
  146. int vecSpillTempRegisters = SelectSpillTemps(
  147. vecCallerSavedRegisters & ~blkInfo.VecFixedRegisters,
  148. vecLocalFreeRegisters);
  149. intLocalFreeRegisters &= ~(intSpillTempRegisters | intCallerSavedRegisters);
  150. vecLocalFreeRegisters &= ~(vecSpillTempRegisters | vecCallerSavedRegisters);
  151. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  152. {
  153. int intLocalUse = 0;
  154. int vecLocalUse = 0;
  155. void AllocateRegister(Operand source, MemoryOperand memOp, int srcIndex)
  156. {
  157. LocalInfo info = locInfo[source.AsInt32() - 1];
  158. info.UseCount++;
  159. Debug.Assert(info.UseCount <= info.Uses);
  160. if (info.Register != -1)
  161. {
  162. Operand reg = Register(info.Register, source.Type.ToRegisterType(), source.Type);
  163. if (memOp != null)
  164. {
  165. if (srcIndex == 0)
  166. {
  167. memOp.BaseAddress = reg;
  168. }
  169. else /* if (srcIndex == 1) */
  170. {
  171. memOp.Index = reg;
  172. }
  173. }
  174. else
  175. {
  176. node.SetSource(srcIndex, reg);
  177. }
  178. if (info.UseCount == info.Uses && !info.PreAllocated)
  179. {
  180. if (source.Type.IsInteger())
  181. {
  182. intLocalFreeRegisters |= 1 << info.Register;
  183. }
  184. else
  185. {
  186. vecLocalFreeRegisters |= 1 << info.Register;
  187. }
  188. }
  189. }
  190. else if (node is Operation operation && operation.Instruction == Instruction.Copy)
  191. {
  192. Operation fillOp = Operation(Instruction.Fill, node.Destination, Const(info.SpillOffset));
  193. block.Operations.AddBefore(node, fillOp);
  194. block.Operations.Remove(node);
  195. node = fillOp;
  196. }
  197. else
  198. {
  199. Operand temp = info.Temp;
  200. if (temp == null || info.Sequence != sequence)
  201. {
  202. temp = source.Type.IsInteger()
  203. ? GetSpillTemp(source, intSpillTempRegisters, ref intLocalUse)
  204. : GetSpillTemp(source, vecSpillTempRegisters, ref vecLocalUse);
  205. info.Sequence = sequence;
  206. info.Temp = temp;
  207. }
  208. if (memOp != null)
  209. {
  210. if (srcIndex == 0)
  211. {
  212. memOp.BaseAddress = temp;
  213. }
  214. else /* if (srcIndex == 1) */
  215. {
  216. memOp.Index = temp;
  217. }
  218. }
  219. else
  220. {
  221. node.SetSource(srcIndex, temp);
  222. }
  223. Operation fillOp = Operation(Instruction.Fill, temp, Const(info.SpillOffset));
  224. block.Operations.AddBefore(node, fillOp);
  225. }
  226. }
  227. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  228. {
  229. Operand source = node.GetSource(srcIndex);
  230. if (source.Kind == OperandKind.LocalVariable)
  231. {
  232. AllocateRegister(source, null, srcIndex);
  233. }
  234. else if (source.Kind == OperandKind.Memory)
  235. {
  236. MemoryOperand memOp = (MemoryOperand)source;
  237. if (memOp.BaseAddress != null)
  238. {
  239. AllocateRegister(memOp.BaseAddress, memOp, 0);
  240. }
  241. if (memOp.Index != null)
  242. {
  243. AllocateRegister(memOp.Index, memOp, 1);
  244. }
  245. }
  246. }
  247. int intLocalAsg = 0;
  248. int vecLocalAsg = 0;
  249. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  250. {
  251. Operand dest = node.GetDestination(dstIndex);
  252. if (dest.Kind != OperandKind.LocalVariable)
  253. {
  254. continue;
  255. }
  256. LocalInfo info = locInfo[dest.AsInt32() - 1];
  257. if (info.UseCount == 0 && !info.PreAllocated)
  258. {
  259. int mask = dest.Type.IsInteger()
  260. ? intLocalFreeRegisters
  261. : vecLocalFreeRegisters;
  262. if (info.IsBlockLocal && mask != 0)
  263. {
  264. int selectedReg = BitOperations.TrailingZeroCount(mask);
  265. info.Register = selectedReg;
  266. if (dest.Type.IsInteger())
  267. {
  268. intLocalFreeRegisters &= ~(1 << selectedReg);
  269. intUsedRegisters |= 1 << selectedReg;
  270. }
  271. else
  272. {
  273. vecLocalFreeRegisters &= ~(1 << selectedReg);
  274. vecUsedRegisters |= 1 << selectedReg;
  275. }
  276. }
  277. else
  278. {
  279. info.Register = -1;
  280. info.SpillOffset = stackAlloc.Allocate(dest.Type.GetSizeInBytes());
  281. }
  282. }
  283. info.UseCount++;
  284. Debug.Assert(info.UseCount <= info.Uses);
  285. if (info.Register != -1)
  286. {
  287. node.SetDestination(dstIndex, Register(info.Register, dest.Type.ToRegisterType(), dest.Type));
  288. }
  289. else
  290. {
  291. Operand temp = info.Temp;
  292. if (temp == null || info.Sequence != sequence)
  293. {
  294. temp = dest.Type.IsInteger()
  295. ? GetSpillTemp(dest, intSpillTempRegisters, ref intLocalAsg)
  296. : GetSpillTemp(dest, vecSpillTempRegisters, ref vecLocalAsg);
  297. info.Sequence = sequence;
  298. info.Temp = temp;
  299. }
  300. node.SetDestination(dstIndex, temp);
  301. Operation spillOp = Operation(Instruction.Spill, null, Const(info.SpillOffset), temp);
  302. block.Operations.AddAfter(node, spillOp);
  303. node = spillOp;
  304. }
  305. }
  306. sequence++;
  307. intUsedRegisters |= intLocalAsg | intLocalUse;
  308. vecUsedRegisters |= vecLocalAsg | vecLocalUse;
  309. }
  310. }
  311. return new AllocationResult(intUsedRegisters, vecUsedRegisters, stackAlloc.TotalSize);
  312. }
  313. private static int SelectSpillTemps(int mask0, int mask1)
  314. {
  315. int selection = 0;
  316. int count = 0;
  317. while (count < MaxIROperands && mask0 != 0)
  318. {
  319. int mask = mask0 & -mask0;
  320. selection |= mask;
  321. mask0 &= ~mask;
  322. count++;
  323. }
  324. while (count < MaxIROperands && mask1 != 0)
  325. {
  326. int mask = mask1 & -mask1;
  327. selection |= mask;
  328. mask1 &= ~mask;
  329. count++;
  330. }
  331. Debug.Assert(count == MaxIROperands, "No enough registers for spill temps.");
  332. return selection;
  333. }
  334. private static Operand GetSpillTemp(Operand local, int freeMask, ref int useMask)
  335. {
  336. int selectedReg = BitOperations.TrailingZeroCount(freeMask & ~useMask);
  337. useMask |= 1 << selectedReg;
  338. return Register(selectedReg, local.Type.ToRegisterType(), local.Type);
  339. }
  340. private static int UsesCount(Operand local)
  341. {
  342. return local.Assignments.Count + local.Uses.Count;
  343. }
  344. }
  345. }